WebGLRenderer

The WebGL renderer displays your beautifully crafted scenes using WebGL.

Constructor

WebGLRenderer(parameters : Object)

parameters - (optional) object with properties defining the renderer's behaviour. The constructor also accepts no parameters at all. In all cases, it will assume sane defaults when parameters are missing. The following are valid parameters:

canvas - A canvas where the renderer draws its output. This corresponds to the domElement property below. If not passed in here, a new canvas element will be created.
context - This can be used to attach the renderer to an existing RenderingContext. Default is null.
precision - Shader precision. Can be "highp", "mediump" or "lowp". Defaults to "highp" if supported by the device. See the note in "Things to Avoid" here.
alpha - whether the canvas contains an alpha (transparency) buffer or not. Default is false.
premultipliedAlpha - whether the renderer will assume that colors have premultiplied alpha. Default is true.
antialias - whether to perform antialiasing. Default is false.
stencil - whether the drawing buffer has a stencil buffer of at least 8 bits. Default is true.
preserveDrawingBuffer - whether to preserve the buffers until manually cleared or overwritten. Default is false.
powerPreference - Provides a hint to the user agent indicating what configuration of GPU is suitable for this WebGL context. Can be "high-performance", "low-power" or "default". Default is "default". See the WebGL spec for more information.
depth - whether the drawing buffer has a depth buffer of at least 16 bits. Default is true.
logarithmicDepthBuffer - whether to use a logarithmic depth buffer. It may be neccesary to use this if dealing with huge differences in scale in a single scene. Default is false. See the camera / logarithmicdepthbuffer example.

Properties

.autoClear : Boolean

Defines whether the renderer should automatically clear its output before rendering a frame.

.autoClearColor : Boolean

If autoClear is true, defines whether the renderer should clear the color buffer. Default is true.

.autoClearDepth : Boolean

If autoClear is true, defines whether the renderer should clear the depth buffer. Default is true.

.autoClearStencil : Boolean

If autoClear is true, defines whether the renderer should clear the stencil buffer. Default is true.

.capabilities : Object

An object containing details about the capabilities of the current RenderingContext.
- floatFragmentTextures: whether the context supports the OES_texture_float extension. According to WebGLStats, as of February 2016 over 95% of WebGL enabled devices support this.
- floatVertexTextures: true if floatFragmentTextures and vertexTextures are both true.
- getMaxAnisotropy(): Returns the maximum available anisotropy.
- getMaxPrecision(): Returns the maximum available precision for vertex and fragment shaders.
- logarithmicDepthBuffer: true if the logarithmicDepthBuffer was set to true in the constructor and the context supports the EXT_frag_depth extension. According to WebGLStats, as of February 2016 around 66% of WebGL enabled devices support this.
- maxAttributes: The value of gl.MAX_VERTEX_ATTRIBS.
- maxCubemapSize: The value of gl.MAX_CUBE_MAP_TEXTURE_SIZE. Maximum height * width of cube map textures that a shader can use.
- maxFragmentUniforms: The value of gl.MAX_FRAGMENT_UNIFORM_VECTORS. The number of uniforms that can be used by a fragment shader.
- maxTextureSize: The value of gl.MAX_TEXTURE_SIZE. Maximum height * width of a texture that a shader use.
- maxTextures: The value of gl.MAX_TEXTURE_IMAGE_UNITS. The maximum number of textures that can be used by a shader.
- maxVaryings: The value of gl.MAX_VARYING_VECTORS. The number of varying vectors that can used by shaders.
- maxVertexTextures: The value of gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS. The number of textures that can be used in a vertex shader.
- maxVertexUniforms: The value of gl.MAX_VERTEX_UNIFORM_VECTORS. The maximum number of uniforms that can be used in a vertex shader.
- precision: The shader precision currently being used by the renderer.
- vertexTextures: true if .maxVertexTextures : Integeris greater than 0 (i.e. vertext textures can be used).

.clippingPlanes : Array

User-defined clipping planes specified as v3d.Plane objects in world space. These planes apply globally. Points in space whose dot product with the plane is negative are cut away. Default is [].

.context : WebGLRenderingContext

The renderer obtains a RenderingContext context from its domElement by default, using HTMLCanvasElement.getContext().

You can create this manually, however it must correspond to the domElement in order to render to the screen.

.domElement : DOMElement

A canvas where the renderer draws its output.
This is automatically created by the renderer in the constructor (if not provided already); you just need to add it to your page like so:
document.body.appendChild(renderer.domElement);

.extensions : Object

A wrapper for the .extensions.get method, used to check whether various WebGL extensions are supported.

.gammaFactor : Float

Default is 2.

.gammaInput : Boolean

If set, then it expects that all textures and colors are premultiplied gamma. Default is false.

.gammaOutput : Boolean

If set, then it expects that all textures and colors need to be outputted in premultiplied gamma. Default is false.

.info : Object

An object with a series of statistical information about the graphics board memory and the rendering process. Useful for debugging or just for the sake of curiosity. The object contains the following fields:

By default these data are reset at each render calls, but when using the composer or mirrors it can be preferred to reset them with a custom pattern : renderer.info.autoReset = false; renderer.info.reset();

.localClippingEnabled : Boolean

Defines whether the renderer respects object-level clipping planes. Default is false.

.maxMorphTargets : Integer

Default is 8. The maximum number of MorphTargets allowed in a shader. Keep in mind that the standard materials only allow 8 MorphTargets.

.maxMorphNormals : Integer

Default is 4. The maximum number of MorphNormals allowed in a shader. Keep in mind that the standard materials only allow 4 MorphNormals.

.physicallyCorrectLights : Boolean

Whether to use physically correct lighting mode. Default is false. See the lights / physical example.

.properties : Object

Used internally by the renderer to keep track of various sub object properties.

.renderLists : WebGLRenderLists

Used internally to handle ordering of scene object rendering.

.shadowMap : WebGLShadowMap

This contains the reference to the shadow map, if used.

.shadowMap.enabled : Boolean

If set, use shadow maps in the scene. Default is false.

.shadowMap.autoUpdate : Boolean

Enables automatic updates to the shadows in the scene. Default is true.

If you do not require dynamic lighting / shadows, you may set this to false when the renderer is instantiated.

.shadowMap.needsUpdate : Boolean

When set to true, shadow maps in the scene will be updated in the next render call. Default is false.

If you have disabled automatic updates to shadow maps (shadowMap.autoUpdate = false), you will need to set this to true and then make a render call to update the shadows in your scene.

.shadowMap.type : Integer

Defines shadow map type (unfiltered, percentage close filtering, percentage close filtering with bilinear filtering in shader)

Options are v3d.BasicShadowMap, v3d.PCFShadowMap (default), v3d.PCFSoftShadowMap. See Renderer constants for details.

.sortObjects : Boolean

Defines whether the renderer should sort objects. Default is true.

Note: Sorting is used to attempt to properly render objects that have some degree of transparency. By definition, sorting objects may not work in all cases. Depending on the needs of application, it may be neccessary to turn off sorting and use other methods to deal with transparency rendering e.g. manually determining each object's rendering order.

.state : Object

Contains functions for setting various properties of the WebGLRenderer.context state.

.toneMapping : Constant

Default is LinearToneMapping. See the Renderer constants for other choices.

.toneMappingExposure : Number

Exposure level of tone mapping. Default is 1.

.toneMappingWhitePoint : Number

Tone mapping white point. Default is 1.

Methods

.allocTextureUnit : Integer

Attempt to allocate a texture unit for use by a shader. Will warn if trying to allocate more texture units than the GPU supports. This is mainly used internally. See capabilities.maxTextures.

.clear (color : Boolean, depth : Boolean, stencil : Boolean) : null

Tells the renderer to clear its color, depth or stencil drawing buffer(s). This method initializes the color buffer to the current clear color value.
Arguments default to true.

.clearColor () : null

Clear the color buffer. Equivalent to calling .clear(true, false, false).

.clearDepth () : null

Clear the depth buffer. Equivalent to calling .clear(false, true, false).

.clearStencil () : null

Clear the stencil buffers. Equivalent to calling .clear(false, false, true).

.compile (scene : Scene, camera : Camera) : null

Compiles all materials in the scene with the camera. This is useful to precompile shaders before the first rendering.

.copyFramebufferToTexture (position : Vector2, texture : Texture, level : Number) : null

Copies pixels from the current WebGLFramebuffer into a 2D texture. Enables access to WebGLRenderingContext.copyTexImage2D.

.copyTextureToTexture (position : Vector2, srcTexture : Texture, dstTexture : Texture, level : Number) : null

Copies all pixels of a texture to an existing texture starting from the given position. Enables access to WebGLRenderingContext.texSubImage2D.

.dispose () : null

Dispose of the current rendering context.

.extensions.get (extensionName : String) : Object

Used to check whether various extensions are supported and returns an object with details of the extension if available. This method can check for the following extensions:

- WEBGL_depth_texture
- EXT_texture_filter_anisotropic
- WEBGL_compressed_texture_s3tc
- WEBGL_compressed_texture_pvrtc
- WEBGL_compressed_texture_etc1

.forceContextLoss () : null

Simulate loss of the WebGL context. This requires support for the WEBGL_lose_context extensions. According to WebGLStats, as of February 2016 90% of WebGL enabled devices support this.

.getClearAlpha () : Float

Returns a float with the current clear alpha. Ranges from 0 to 1.

.getClearColor () : Color

Returns a v3d.Color instance with the current clear color.

.getContext () : WebGLRenderingContext

Return the current WebGL context.

.getContextAttributes () : WebGLContextAttributes

Returns an object that describes the attributes set on the WebGL context when it was created.

.getRenderTarget () : RenderTarget

Returns the current RenderTarget, if any.

.getCurrentViewport () : RenderTarget

Returns the current viewport.

.getDrawingBufferSize () : Object

Returns an object containing the width and height of the renderer's drawing buffer, in pixels.

.getPixelRatio () : number

Returns current device pixel ratio used.

.getSize () : Object

Returns an object containing the width and height of the renderer's output canvas, in pixels.

.resetGLState () : null

Reset the GL state to default. Called internally if the WebGL context is lost.

.readRenderTargetPixels (renderTarget : WebGLRenderTarget, x : Float, y : Float, width : Float, height : Float, buffer) : null

Reads the pixel data from the renderTarget into the buffer you pass in. This is a wrapper around WebGLRenderingContext.readPixels().
See the interactive / cubes / gpu example.

.render (scene : Scene, camera : Camera, renderTarget : WebGLRenderTarget, forceClear : Boolean) : null

Render a scene using a camera.
The render is done to the renderTarget (if specified) or to the canvas as usual.
If forceClear is true, the depth, stencil and color buffers will be cleared before rendering even if the renderer's autoClear property is false.
Even with forceClear set to true you can prevent certain buffers being cleared by setting either the autoClearColor, autoClearStencil or autoClearDepth properties to false.

.renderBufferDirect (camera : Camera, fog : Fog, geometry : Geometry, material : Material, object : Object3D, group : Object) : null

Render a buffer geometry group using the camera and with the specified material.

.renderBufferImmediate (object : Object3D, program : shaderprogram, shading : Material) : null

object - an instance of Object3D
program - an instance of shaderProgram
shading - an instance of Material

Render an immediate buffer. Gets called by renderImmediateObject.

.setAnimationLoop (callback : Function) : null

callback — The function will be called every available frame. If `null` is passed it will stop any already ongoing animation.

A build in function that can be used instead of requestAnimationFrame. For WebVR projects this function must be used.

.setClearAlpha (alpha : Float) : null

Sets the clear alpha. Valid input is a float between 0.0 and 1.0.

.setClearColor (color : Color, alpha : Float) : null

Sets the clear color and opacity.

.setPixelRatio (value : number) : null

Sets device pixel ratio. This is usually used for HiDPI device to prevent bluring output canvas.

.setRenderTarget (renderTarget : WebGLRenderTarget) : null

renderTarget -- The renderTarget that needs to be activated (optional).

This method sets the active rendertarget. If the parameter is omitted the canvas is set as the active rendertarget.

.setScissor (x : Integer, y : Integer, width : Integer, height : Integer) : null

Sets the scissor area from (x, y) to (x + width, y + height)

.setScissorTest (boolean : Boolean) : null

Enable or disable the scissor test. When this is enabled, only the pixels within the defined scissor area will be affected by further renderer actions.

.setSize (width : Integer, height : Integer, updateStyle : Boolean) : null

Resizes the output canvas to (width, height) with device pixel ratio taken into account, and also sets the viewport to fit that size, starting in (0, 0). Setting updateStyle to false prevents any style changes to the output canvas.

.setTexture2D (texture : Texture, slot : number) : null

texture -- The texture that needs to be set.
slot -- The number indicating which slot should be used by the texture.

This method sets the correct texture to the correct slot for the WebGL shader. The slot number can be found as a value of the uniform of the sampler.

Note: This method replaces the older .setTexture : nullmethod.

.setTextureCube (cubeTexture : CubeTexture, slot : Number) : null

texture -- The cubeTexture that needs to be set.
slot -- The number indicating which slot should be used by the texture.

This method sets the correct texture to the correct slot for the WebGL shader. The slot number can be found as a value of the uniform of the sampler.

.setViewport (x : Integer, y : Integer, width : Integer, height : Integer) : null

Sets the viewport to render from (x, y) to (x + width, y + height).

Source

For more info on how to obtain the source code of this module see this page.