Song: Joe Hertz - Stay Lost ft. Amber-Simone (Cabu Remix)
<canvas>
element<html>
<head>
<title>My first Three.js app</title>
<style>
body { margin: 0; }
canvas { width: 100%; height: 100% }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r82/three.min.js"></script>
<script>
// OUR CODE WILL GO HERE
</script>
</body>
</html>
Geometry = Shape
var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
Mesh = Thing
var cube = new THREE.Mesh(geometry, material);
cube.position.x = 0;
cube.position.y = 0;
cube.position.z = 0;
cube.rotation.x = 0;
cube.rotation.y = 0;
cube.rotation.z = 0;
Scene = The World = A list of meshes
var scene = new THREE.Scene();
scene.add(cube);
Camera = Your Eyes
var aspectRatio = window.innerWidth/window.innerHeight;
var camera = new THREE.PerspectiveCamera(75, aspectRatio, 0.1, 1000);
camera.position.z = 2;
Renderer = How you draw on the canvas
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
var canvas = renderer.domElement;
document.body.appendChild(canvas);
renderer.render(scene, camera);
function render() {
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
var texture = new THREE.TextureLoader().load('textures/crate.gif');
var material = new THREE.MeshBasicMaterial({ map: texture });
Let's learn two last things:
These aren't included in Three.js, they are plugins contained in separate JS files
<script src="OrbitControls.js"></script>
var controls = new THREE.OrbitControls(camera, canvas);
function render() {
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
controls.update();
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
These are also plugins.
<script src="StereoEffect.js"></script>
var renderer = new THREE.WebGLRenderer();
var canvas = renderer.domElement;
document.body.appendChild(canvas);
var effect = new THREE.StereoEffect( renderer );
effect.setSize(window.innerWidth, window.innerHeight);
function render() {
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
controls.update();
effect.render(scene, camera);
requestAnimationFrame(render);
}
render();
Some great resource for learning more: