Capítulo 263 de 859

Chapter 263: Renderer

Core Idea

Base class for renderers.

Key Concepts

  • alpha : boolean: Whether the default framebuffer should be transparent or opaque.
  • autoClear : boolean: Whether the renderer should automatically clear the current rendering target before execute a render() call. The target can be the canvas…
  • autoClearColor : boolean: When autoClear is set to true, this property defines whether the renderer should clear the color buffer.
  • autoClearDepth : boolean: When autoClear is set to true, this property defines whether the renderer should clear the depth buffer.
  • autoClearStencil : boolean: When autoClear is set to true, this property defines whether the renderer should clear the stencil buffer.
  • backend : Backend: A reference to the current backend.
  • contextNode : ContextNode: A global context node that stores override nodes for specific transformations or calculations. These nodes can be used to replace default b…
  • coordinateSystem : number (readonly): The coordinate system of the renderer. The value of this property depends on the selected backend. Either THREE.WebGLCoordinateSystem or …
  • currentColorSpace : string: The current color space of the renderer. When not producing screen output, the color space is always the working color space.
  • currentSamples : number: The current number of samples used for multi-sample anti-aliasing (MSAA).

Reference Tables

Constructor

Signature
new Renderer( backend : Backend, parameters : Renderer~Options )

Properties

PropertyDescription
.alpha : booleanWhether the default framebuffer should be transparent or opaque.
.autoClear : booleanWhether the renderer should automatically clear the current rendering target before execute a render() call. The target can be the canvas (default framebuffer) or the current bound render target (c…
.autoClearColor : booleanWhen autoClear is set to true, this property defines whether the renderer should clear the color buffer.
.autoClearDepth : booleanWhen autoClear is set to true, this property defines whether the renderer should clear the depth buffer.
.autoClearStencil : booleanWhen autoClear is set to true, this property defines whether the renderer should clear the stencil buffer.
.backend : BackendA reference to the current backend.
.contextNode : ContextNodeA global context node that stores override nodes for specific transformations or calculations. These nodes can be used to replace default behavior in the rendering pipeline.
.coordinateSystem : number (readonly)The coordinate system of the renderer. The value of this property depends on the selected backend. Either THREE.WebGLCoordinateSystem or THREE.WebGPUCoordinateSystem.
.currentColorSpace : stringThe current color space of the renderer. When not producing screen output, the color space is always the working color space.
.currentSamples : numberThe current number of samples used for multi-sample anti-aliasing (MSAA).
.currentToneMapping : numberThe current tone mapping of the renderer. When not producing screen output, the tone mapping is always NoToneMapping.
.debug : DebugConfigThe renderer's debug configuration.
.depth : booleanWhether the default framebuffer should have a depth buffer or not.
`.domElement : HTMLCanvasElementOffscreenCanvas`
.highPrecision : booleanEnables or disables high precision for model-view and normal-view matrices. When enabled, will use CPU 64-bit precision for higher precision instead of GPU 32-bit for higher performance.
.highPrecision : booleanReturns whether high precision is enabled or not.
.info : InfoHolds a series of statistical information about the GPU memory and the rendering process. Useful for debugging and monitoring.
.initialized (readonly)Returns whether the renderer has been initialized or not.
.inspector : InspectorBaseThe inspector instance. The inspector can be any class that extends from InspectorBase.
.isOutputTargetReturns true if the rendering settings are set to screen output.
.isRenderer : boolean (readonly)This flag can be used for type testing.
.library : NodeLibraryThe node library defines how certain library objects like materials, lights or tone mapping functions are mapped to node types. This is required since although instances of classes like `MeshBasicMat…
.lighting : LightingA map-like data structure for managing lights.
.logarithmicDepthBuffer : boolean (readonly)Whether logarithmic depth buffer is enabled or not.
.needsFrameBufferTargetReturns true if a framebuffer target is needed to perform tone mapping or color space conversion. If this is the case, the renderer allocates an internal render target for that purpose.
.onDeviceLost : functionA callback function that defines what should happen when a device/context lost occurs.
.onError : functionA callback function that defines what should happen when an uncaptured backend error is reported (e.g. a WebGPU validation/out-of-memory/internal error raised outside an error scope). Applications ca…
.opaque : booleanWhether the renderer should render opaque render objects or not.
.outputColorSpace : stringDefines the output color space of the renderer.
.reversedDepthBuffer : boolean (readonly)Whether reversed depth buffer is enabled or not.
.samples : numberThe number of samples used for multi-sample anti-aliasing (MSAA).
.shadowMap : ShadowMapConfigThe renderer's shadow configuration.
.sortObjects : booleanWhether the renderer should sort its render lists or not.
.stencil : booleanWhether the default framebuffer should have a stencil buffer or not.
.toneMapping : numberDefines the tone mapping of the renderer.
.toneMappingExposure : numberDefines the tone mapping exposure.
.transparent : booleanWhether the renderer should render transparent render objects or not.
.xr : XRManagerThe renderer's XR manager.

Methods

MethodDescription
.clear( color : boolean, depth : boolean, stencil : boolean )Performs a manual clear operation. This method ignores autoClear properties.
.clearAsync( color : boolean, depth : boolean, stencil : boolean ) : Promise (async)Async version of Renderer#clear.
.clearColor()Performs a manual clear operation of the color buffer. This method ignores autoClear properties.
.clearColorAsync() : Promise (async)Async version of Renderer#clearColor.
.clearDepth()Performs a manual clear operation of the depth buffer. This method ignores autoClear properties.
.clearDepthAsync() : Promise (async)Async version of Renderer#clearDepth.
.clearStencil()Performs a manual clear operation of the stencil buffer. This method ignores autoClear properties.
.clearStencilAsync() : Promise (async)Async version of Renderer#clearStencil.
.compile( scene : Object3D, camera : Camera, targetScene : Scene ) : functionAlias for compileAsync().
.compileAsync( scene : Object3D, camera : Camera, targetScene : Scene ) : Promise (async)Compiles all materials in the given scene. This can be useful to avoid a phenomenon which is called "shader compilation stutter", which occurs when rendering an object with a new shader for the first…
`.compute( computeNodes : NodeArray.<Node>, dispatchSize : number
`.computeAsync( computeNodes : NodeArray.<Node>, dispatchSize : number
`.copyFramebufferToTexture( framebufferTexture : FramebufferTexture, rectangle : Vector2Vector4 )`
`.copyTextureToTexture( srcTexture : Texture, dstTexture : Texture, srcRegion : Box2Box3, dstPosition : Vector2
.dispose()Frees all internal resources of the renderer. Call this method if the renderer is no longer in use by your app.
.getActiveCubeFace() : numberReturns the active cube face.
.getActiveMipmapLevel() : numberReturns the active mipmap level.
.getAnimationLoop() : functionReturns the current animation loop callback.
`.getArrayBufferAsync( attribute : BufferAttribute, target : ReadbackBufferArrayBuffer, offset : number, count : number ) : Promise.<(ArrayBuffer
.getCanvasTarget() : CanvasTargetReturns the current canvas target.
.getClearAlpha() : numberReturns the clear alpha.
.getClearColor( target : Color ) : ColorReturns the clear color.
.getClearDepth() : numberReturns the clear depth.
.getClearStencil() : numberReturns the clear stencil.
.getColorBufferType() : numberReturns the output buffer type.
`.getContext() : GPUCanvasContextWebGL2RenderingContext`
.getDrawingBufferSize( target : Vector2 ) : Vector2Returns the drawing buffer size in physical pixels. This method honors the pixel ratio.
.getMRT() : MRTNodeReturns the MRT configuration.
.getMaxAnisotropy() : numberReturns the maximum available anisotropy for texture filtering.
.getOutputBufferType() : numberReturns the output buffer type.
.getOutputRenderTarget() : RenderTargetReturns the current output target.
.getPixelRatio() : numberReturns the pixel ratio.
.getRenderObjectFunction() : functionReturns the current render object function.
.getRenderTarget() : RenderTargetReturns the current render target.
.getScissor( target : Vector4 ) : Vector4Returns the scissor rectangle.
.getScissorTest() : booleanReturns the scissor test value.
.getSize( target : Vector2 ) : Vector2Returns the renderer's size in logical pixels. This method does not honor the pixel ratio.
.getViewport( target : Vector4 ) : Vector4Returns the viewport definition.
.hasCompatibility( name : string ) : booleanChecks if the given compatibility is supported by the selected backend.
.hasFeature( name : string ) : booleanChecks if the given feature is supported by the selected backend. If the renderer has not been initialized, this method always returns false.
.hasFeatureAsync( name : string ) : Promise.<boolean> (async)Checks if the given feature is supported by the selected backend.
.hasInitialized() : booleanReturns true when the renderer has been initialized.
.init() : Promise.<this> (async)Initializes the renderer so it is ready for usage.
.initRenderTarget( renderTarget : RenderTarget )Initializes the given render target.
.initTexture( texture : Texture )Initializes the given texture. Useful for preloading a texture rather than waiting until first render (which can cause noticeable lags due to decode and GPU upload overhead).
.initTextureAsync( texture : Texture ) : Promise (async)Initializes the given textures. Useful for preloading a texture rather than waiting until first render (which can cause noticeable lags due to decode and GPU upload overhead).
.isOccluded( object : Object3D ) : booleanThis method performs an occlusion query for the given 3D object. It returns true if the given 3D object is fully occluded by other 3D objects in the scene.
.readRenderTargetPixelsAsync( renderTarget : RenderTarget, x : number, y : number, width : number, height : number, textureIndex : number, faceIndex : number ) : Promise.<TypedArray> (async)Reads pixel data from the given render target.
.render( scene : Object3D, camera : Camera )Renders the scene or 3D object with the given camera. This method can only be called if the renderer has been initialized. When using render() inside an animation loop, it's guaranteed the renderer…
.renderAsync( scene : Object3D, camera : Camera ) : Promise (async)Renders the scene in an async fashion.
.renderObject( object : Object3D, scene : Scene, camera : Camera, geometry : BufferGeometry, material : Material, group : Object, lightsNode : LightsNode, clippingContext : ClippingContext, passId : string )This method represents the default render object function that manages the render lifecycle of the object.
.setAnimationLoop( callback : onAnimationCallback ) : Promise (async)Applications are advised to always define the animation loop with this method and not manually with requestAnimationFrame() for best compatibility.
.setCanvasTarget( canvasTarget : CanvasTarget )Sets the canvas target. The canvas target manages the HTML canvas or the offscreen canvas the renderer draws into.
.setClearAlpha( alpha : number )Defines the clear alpha.
.setClearColor( color : Color, alpha : number )Defines the clear color and optionally the clear alpha.
.setClearDepth( depth : number )Defines the clear depth.
.setClearStencil( stencil : number )Defines the clear stencil.
.setDrawingBufferSize( width : number, height : number, pixelRatio : number )This method allows to define the drawing buffer size by specifying width, height and pixel ratio all at once. The size of the drawing buffer is computed with this formula:
.setMRT( mrt : MRTNode ) : RendererSets the given MRT configuration.
.setOpaqueSort( method : function )Defines a manual sort function for the opaque render list. Pass null to use the default sort.
.setOutputRenderTarget( renderTarget : RenderTarget )Sets the output render target for the renderer.
.setPixelRatio( value : number )Sets the given pixel ratio and resizes the canvas if necessary.
.setRenderObjectFunction( renderObjectFunction : renderObjectFunction )Sets the given render object function. Calling this method overwrites the default implementation which is Renderer#renderObject. Defining a custom function can be useful…
.setRenderTarget( renderTarget : RenderTarget, activeCubeFace : number, activeMipmapLevel : number )Sets the given render target. Calling this method means the renderer does not target the default framebuffer (meaning the canvas) anymore but a custom framebuffer. Use null as the first argument to…
`.setScissor( x : numberVector4, y : number, width : number, height : number )`
.setScissorTest( boolean : boolean )Defines the scissor test.
.setSize( width : number, height : number, updateStyle : boolean )Sets the size of the renderer.
.setTransparentSort( method : function )Defines a manual sort function for the transparent render list. Pass null to use the default sort.
`.setViewport( x : numberVector4, y : number, width : number, height : number, minDepth : number, maxDepth : number )`
.waitForGPU() : Promise (async)Can be used to synchronize CPU operations with GPU tasks. So when this method is called, the CPU waits for the GPU to complete its operation (e.g. a compute task).

Key Takeaways

  1. Most relevant properties: alpha, autoClear, autoClearColor, autoClearDepth.
  2. Key methods: clear, clearAsync, clearColor, clearColorAsync.

Connects To