Renderer
Video Lecture
Description
The most common Renderer used in Three.js is the WebGLRenderer
.
It paints the scene and camera information onto a HTML Canvas Element.
The WebGLRenderer
will use WebGL.
WebGL allows GPU-accelerated image processing and effects as the renderer creates the 2D image for the Canvas.
To start, replace your ./src/main.ts
with this script below.
Lesson Script
./src/main.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
When creating a renderer for your Three.js app, it is useful to consider if it should use up 100% of the browsers window space, or you want to set the size explicitly.
When using the whole browser window for the renderer, you can instantiate, setSize
and add the renderer.domElement
to the HTML body as shown below.
const renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
The above code will dynamically add a new HTMLCanvasElement
to the HMTL document for use when rendering.
If you want to render to an existing HTMLCanvasElement
, then you could use something like this below.
Hard code a <canvas>
element into your HTML.
<canvas id="canvas"></canvas>
And reference its id
when you instantiate a new WebGLRenderer
.
const canvas = document.getElementById('canvas') as HTMLCanvasElement
const renderer = new THREE.WebGLRenderer({ canvas: canvas })
renderer.setSize(200, 200)
Note in the above sample, the width and height were set using the renderer.setSize
method. This will also automatically set the CSS width and height of the canvas element as well.
Another useful option when instantiating a renderer is to also set the antialias
attribute to true.
const renderer = new THREE.WebGLRenderer({ antialias: true })
Useful Links
WebGLRenderer setScissor Example (sbedit.net)
Multiple Renderers Example (sbedit.net)