three.js ~ example ~ Mr.doob
2012-01-05 ~
JavaScript
var camera, scene, renderer, geometry, material, mesh1, mesh2;
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.z = 500;
scene.add(camera);
geometry = new THREE.CubeGeometry(200, 200, 200);
material = new THREE.MeshNormalMaterial();
mesh1 = new THREE.Mesh(geometry, material);
mesh1.position.x = 150;
scene.add(mesh1);
mesh2 = new THREE.Mesh(geometry, material);
mesh2.position.x = -150;
scene.add(mesh2);
renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
document.addEventListener('wheel', zoom);
}
function zoom(e) {
camera.position.z *= Math.pow(1.001, e.deltaY);
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
mesh1.rotation.x += 0.01;
mesh1.rotation.y += 0.02;
mesh2.rotation.x += 0.01;
mesh2.rotation.y += 0.02;
let scaleConstant = camera.position.z / 500;
mesh1.scale.x = scaleConstant;
mesh1.scale.y = scaleConstant;
mesh1.scale.z = scaleConstant;
let offsetConstant = camera.position.z * 150 / 500;
mesh1.position.x = offsetConstant;
renderer.render(scene, camera);
}