|
| 1 | +document.addEventListener('DOMContentLoaded', function () { |
| 2 | + // Baselines for cube rotation |
| 3 | + var rotations = { |
| 4 | + x : 0.01, |
| 5 | + y : 0.01, |
| 6 | + z : 0.01, |
| 7 | + cubes : 0.1 |
| 8 | + }; |
| 9 | + |
| 10 | + ['x', 'y', 'z', 'cubes'].forEach(function (axis) { |
| 11 | + var input = document.getElementById('rotate-' + axis); |
| 12 | + input.value = rotations[axis]; |
| 13 | + |
| 14 | + // Debounce, only changing after a short time |
| 15 | + var timer; |
| 16 | + input.addEventListener('change', function (e) { |
| 17 | + // If we're already pending, reset |
| 18 | + if (timer) { clearTimeout(timer); } |
| 19 | + |
| 20 | + // Set our new timer to update axis rotation |
| 21 | + // in roughly a quarter-second, clearing out |
| 22 | + // the timer value when it happens. |
| 23 | + timer = setTimeout(function () { |
| 24 | + rotations[axis] = parseFloat(input.value); |
| 25 | + timer = null; |
| 26 | + }, 250); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + // ThreeJS stuff |
| 31 | + var scene = new THREE.Scene(); |
| 32 | + var camera = new THREE.PerspectiveCamera(100, window.innerWidth / window.innerHeight, 0.1, 1000); |
| 33 | + |
| 34 | + var renderer = new THREE.WebGLRenderer(); |
| 35 | + renderer.setSize(window.innerWidth, window.innerHeight); |
| 36 | + document.getElementById('canvas').appendChild(renderer.domElement); |
| 37 | + |
| 38 | + var x, y, z; |
| 39 | + var count = 5; |
| 40 | + var cubes = []; |
| 41 | + |
| 42 | + // Magical ratios determined by dorking around in console |
| 43 | + camera.position.x = count * 0.8; |
| 44 | + camera.position.y = count * 0.8; |
| 45 | + camera.position.z = 4 * count; |
| 46 | + |
| 47 | + var geometry, material, color, cube; |
| 48 | + |
| 49 | + /* |
| 50 | + var axisHelper = new THREE.AxisHelper(10 * count); |
| 51 | + scene.add(axisHelper); |
| 52 | + */ |
| 53 | + |
| 54 | + // Cube of Cubes! |
| 55 | + var cubeOfCubes = new THREE.Object3D(); |
| 56 | + |
| 57 | + // Eww |
| 58 | + for (x = 0; x < count; x++) { |
| 59 | + for (y = 0; y < count; y++) { |
| 60 | + for (z = 0; z < count; z++) { |
| 61 | + geometry = new THREE.BoxGeometry(1, 1, 1); |
| 62 | + color = new THREE.Color(x/count, y/count, z/count); |
| 63 | + material = new THREE.MeshLambertMaterial({ |
| 64 | + color: color |
| 65 | + }); |
| 66 | + |
| 67 | + cube = new THREE.Mesh(geometry, material); |
| 68 | + cube.position.x = x * 2; |
| 69 | + cube.position.y = y * 2; |
| 70 | + cube.position.z = z * 2; |
| 71 | + |
| 72 | + cubes.push(cube); |
| 73 | + |
| 74 | + //scene.add(cube); |
| 75 | + cubeOfCubes.add(cube); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + scene.add(cubeOfCubes); |
| 80 | + |
| 81 | + // Light |
| 82 | + // All light are things I've seen used in other demos, |
| 83 | + // and I have no idea what I'm doing here. |
| 84 | + var ambientLight = new THREE.AmbientLight(0x404040); |
| 85 | + scene.add(ambientLight); |
| 86 | + |
| 87 | + // directional lighting |
| 88 | + var directionalLight = new THREE.DirectionalLight(0xffffff); |
| 89 | + directionalLight.position.set(10, 10, 10).normalize(); |
| 90 | + scene.add(directionalLight); |
| 91 | + |
| 92 | + var directionalLight2 = new THREE.DirectionalLight(0xffffff); |
| 93 | + directionalLight2.position.set(-10, -10, -10).normalize(); |
| 94 | + scene.add(directionalLight2); |
| 95 | + |
| 96 | + /* |
| 97 | + var pointLight = new THREE.PointLight(0xffffff, 1, 100); |
| 98 | + scene.add(pointLight); |
| 99 | +
|
| 100 | + var spotLight = new THREE.SpotLight( 0xffffff ); |
| 101 | + spotLight.position.set( -40, 60, -10 ); |
| 102 | + scene.add( spotLight ); |
| 103 | + */ |
| 104 | + |
| 105 | + window.renderer = renderer; |
| 106 | + window.scene = scene; |
| 107 | + window.camera = camera; |
| 108 | + window.cubes = cubes; |
| 109 | + window.cubeOfCubes = cubeOfCubes; |
| 110 | + |
| 111 | + var render = function () { |
| 112 | + requestAnimationFrame(render); |
| 113 | + |
| 114 | + // Rotate the individuals |
| 115 | + var cube; |
| 116 | + for (var i = 0, l = cubes.length; i < l; i++) { |
| 117 | + cube = cubes[i]; |
| 118 | + cube.rotation.x += rotations.cubes; |
| 119 | + cube.rotation.y += rotations.cubes; |
| 120 | + cube.rotation.z += rotations.cubes; |
| 121 | + } |
| 122 | + |
| 123 | + cubeOfCubes.rotation.x += rotations.x; |
| 124 | + cubeOfCubes.rotation.y += rotations.y; |
| 125 | + cubeOfCubes.rotation.z += rotations.z; |
| 126 | + |
| 127 | + renderer.render(scene, camera); |
| 128 | + }; |
| 129 | + |
| 130 | + render(); |
| 131 | +}); |
0 commit comments