Skip to content

Instantly share code, notes, and snippets.

@KariTrace
Created November 6, 2012 22:20
Show Gist options
  • Save KariTrace/4028041 to your computer and use it in GitHub Desktop.
Save KariTrace/4028041 to your computer and use it in GitHub Desktop.
three.js - basic setup
<!DOCTYPE html>
<html lang="en">
<head>
<title>wtf - three.js test 1 - basic controls</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0px;
overflow: hidden;
}
</style>
</head>
<body>
<script src="../build/three.min.js"></script>
<script src="js/controls/OrbitControls.js"></script>
<script src="js/renderers/SoftwareRenderer.js"></script>
<script src="js/renderers/SoftwareRenderer2.js"></script>
<script src="js/renderers/SoftwareRenderer3.js"></script>
<script src="js/libs/stats.min.js"></script>
<script>
var container, stats, camera, controls, scene, renderer, sphere, plane, start = Date.now();
init();
animate();
// render();
function init() {
//create DIV to contain the animation
container = document.createElement( 'div' );
//add div called 'container' to body content
document.body.appendChild( container );
//camera setup
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = 0;
camera.position.y = 0;
camera.position.z = 600;
//controls type
controls = new THREE.OrbitControls( camera );
//scene instantiation
scene = new THREE.Scene();
//Sphere mesh
sphere = new THREE.Mesh( new THREE.IcosahedronGeometry( 150, 2 ), new THREE.MeshBasicMaterial() );
scene.add( sphere );
// Plane mesh
plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
plane.position.y = - 150;
plane.rotation.x = - Math.PI / 2;
scene.add( plane );
//Render type
renderer = new THREE.SoftwareRenderer3();
renderer.setSize( window.innerWidth || 2, window.innerHeight || 2 );
//Render HTML container
container.appendChild( renderer.domElement );
//stats box
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '5px';
stats.domElement.style.left = '5px';
container.appendChild( stats.domElement );
//Window listener for resizing
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
var timer = Date.now() - start;
sphere.position.y = Math.abs( Math.sin( timer * 0.001 ) ) * 150;
sphere.rotation.x = timer * 0.0003;
sphere.rotation.z = timer * 0.0002;
controls.update();
renderer.render( scene, camera );
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment