Three Js Intro
Creating a context and rendering a mesh in THREE.js
What follows is slightly modified from the introduction to creating scenes on the Three.js website itself.
Three.js is a popular framework for making 3D graphics for the Web. We include it in our html
document with a <script>
tag:
We initialize a WebGL context by creating a new WebGLRenderer, setting its width and height, and adding it to the html document.
We now have an empty context. We’ll also need a scene and a camera.
Let’s create an object and add it to the scene.
We can define an update()
function that will rotate our cube every time it is called:
And a render()
function that calls update()
, renders scene, and also calls itself again and again in an
endless loop (that recursion is controlled by the requestAnimationFrame
part at the beginning). Note also that the renderer needs to know about the scene
and the camera
.
Finally, we call render()
render();