Code structure
Initialise renderer
Initialise scene and camera
Set render loop
throw it in a pot, add some broth, a potato.
Baby, you've got a stew going.
<script src="three.min.js" ></script>
threejs.org | GitHub
var renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
var scene = new THREE.Scene();
var aspectRatio = window.innerWidth / window.innerHeight;
var camera = new THREE.PerspectiveCamera( 75, aspectRatio, 0.1, 1000 );
function render() {
requestAnimationFrame( render );
renderer.render( scene, camera );
}
render();
Meshes
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
Three.js primitives:
Sphere, Box, Cylinder and many more!
Meshes
var material = new THREE.MeshNormalMaterial( { wireframe: true } );
var cube = new THREE.Mesh( new THREE.BoxGeometry( 20, 20, 20 ), material );
scene.add( cube );
var sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 10, 2 ), material );
sphere.position.x = 30;
scene.add( sphere );
var torus = new THREE.Mesh( new THREE.TorusKnotGeometry( 10, 3, 50, 20 ), material );
torus.position.x = -30;
scene.add( torus );
models (OBJ, STL, etc.)
Models
<script src="js/OBJLoader.js" ></script>
var snowden;
var loader = new THREE.OBJLoader();
loader.load( 'assets/snowden.obj', function( geometry ) {
snowden = new THREE.Mesh(
geometry,
new THREE.MeshNormalMaterial() } )
);
scene.add( snowden );
}
Flat, basic, Lambert, Phong...
Lights
// Soft white light
var light = new THREE.AmbientLight( 0x404040 );
scene.add( light );
// White directional light at half intensity shining from the top.
var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
directionalLight.position.set( 0, 1, 0 );
scene.add( directionalLight );
var mesh = new THREE.Mesh(
new THREE.BoxGeometry( 10, 10, 10 ),
new THREE.MeshBasicMaterial( { color: 0xff00ff } )
);
scene.add( mesh );
Lights
// white spotlight shining from the side, casting shadow
var spotLight = new THREE.SpotLight( 0xffffff );
spotLight.position.set( 100, 1000, 100 );
spotLight.castShadow = true;
scene.add( spotLight );
var mesh = new THREE.Mesh(
new THREE.BoxGeometry( 10, 10, 10 ),
new THREE.MeshPhongMaterial()
);
scene.add( mesh );
Spherical environment mapping (SEM)
Image-based lighting (IBL)
Baked
Everything is a THREE.Object3D
Can be translated, rotated, scaled
cube.position.set( 10, 20 + t, 30 );
cylinder.position.y = 100 * Math.sin( t );
cylinder.rotation.z = t;
sphere.rotation.set( t, 2 * t, 3 * t );
camera.position.set( 0, 10, 10 + t );
var pos = new THREE.Vector3( 0, 10 * Math.sin( t ), 0 );
camera.lookAt( pos );
camera.lookAt( cube.position );
// Standard controls
var controls = new THREE.OrbitControls( camera );
// WebVR (HMD) controls
var controls = new THREE.VRControls( camera );
// DeviceOrientation (Cardboard) controls
var controls = new THREE.DeviceOrientationControls( camera );
function render() {
controls.update();
renderer.render( scene, camera );
}
Parallax-corrected reflection mapping
Surfacing and PBR materials
Camera scripting
Wagner
GitHub page
<script src="Wagner.js" ></script>
<script src="Wagner.base.js" ></script>
var composer = new WAGNER.Composer( renderer, {} );
composer.setSize( renderer.domElement.clientWidth, renderer.domElement.clientHeight );
function render() {
requestAnimationFrame( render );
//renderer.render( scene, camera );
composer.reset();
composer.render( scene, camera );
composer.toScreen();
}
passes
var CGAPass = new WAGNER.CGAPass();
function render() {
requestAnimationFrame( render );
composer.reset();
composer.render( scene, camera );
composer.pass( CGAPass );
composer.toScreen();
}
Passes you can never go wrong with:
Depth-of-field
Bloom
Chromatic aberration
Tone mapping
Vignette
Noise
The secret is to not over do it!
Different versions and hosting considerations
Mobile
ANGLE
Retina displays
Might have to play with:
simplify models and textures
devicePixelRatio / CSS scaling
reduce or remove postprocessing
Use a CDN, like CloudFlare
Congratulations!
Release early, release often
Don't obsess, let it rest,
come back at a later time
Remember: creation through procrastination
or yak shaving. That works, too
10 Iterate
20 Goto 10
Blocking as soon as possible
Keep music for last, you'll end up hating it
Performance driven development
As long as it works, it's fine
Questions?
Hit me up on twitter!
@thespite