Skip to content

Instantly share code, notes, and snippets.

@charliegerard
Last active February 10, 2017 16:37
Show Gist options
  • Save charliegerard/72d7a9cce283feaa46c1ba889e2e17a5 to your computer and use it in GitHub Desktop.
Save charliegerard/72d7a9cce283feaa46c1ba889e2e17a5 to your computer and use it in GitHub Desktop.
basic three.js scene
window.onload = function(){
var camera, controls, scene, renderer, light, material;
init();
animate();
function init(){
var container = document.getElementById('container');
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 2000);
camera.position.z = 100;
scene = new THREE.Scene();
controls = new THREE.OrbitControls(camera);
controls.minDistance = 30;
controls.maxDistance = 100;
controls.addEventListener('change', render);
light = new THREE.PointLight(0xffffff);
light.position.copy(camera.position);
scene.add(camera);
camera.add(light);
renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
renderer.setClearColor(0x000000, 0);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(container.offsetWidth, container.offsetHeight);
container.appendChild(renderer.domElement);
window.addEventListener('resize', onWindowResize, false);
}
function onWindowResize(){
camera.aspect = window.innerwidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.offsetWidth, container.offsetHeight);
}
function animate(){
requestAnimationFrame(animate);
controls.update();
}
function render(){
camera.lookAt(scene.position);
renderer.render(scene, camera);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment