var scene, width, height, camera, cubeCamera, renderer, toruses, box; init(); function init() { var boxGeometry, boxMaterial, light; scene = new THREE.Scene(); width = window.innerWidth; height = window.innerHeight; camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 1000); cubeCamera = new THREE.CubeCamera(1, 1000, 20); renderer = new THREE.WebGLRenderer(); toruses = []; boxGeometry = new THREE.BoxGeometry(2, 2, 2); boxMaterial = new THREE.MeshBasicMaterial({ envMap: cubeCamera.renderTarget, reflectivity: 0.8, color: 0xFFFFFF }); box = new THREE.Mesh(boxGeometry, boxMaterial); box.position.set(0, 0, 0); scene.add(box); renderer.setSize(width, height); renderer.setClearColor(0x17293a); cubeCamera.position.set(0, 0, 0); scene.add(cubeCamera); camera.position.set(0, -50, 0); camera.lookAt(scene.position); generateToruses(200, 10); document.body.appendChild(renderer.domElement); drawFrame(); } function generateToruses(num, step) { var torusGeometry, torusMaterial, torus, radius, colors; colors = ['#f1c40f', '#f39c12', '#e67e22', '#d35400', '#e74c3c', '#c0392b']; for (var i = -num; i < num; i += step) { radius = 4 + Math.random() * 8; torusGeometry = new THREE.TorusGeometry(radius, 1, 8, 30, Math.PI * 2); torusMaterial = new THREE.MeshBasicMaterial({color: colors[Math.floor(Math.random() * colors.length)]}); torus = new THREE.Mesh(torusGeometry, torusMaterial); torus.position.set(0, 0, i); torus.rotation.set(-0.1 + Math.random() * 0.2, -0.3 + Math.random() * 0.6, 0); scene.add(torus); toruses.push(torus); } } function drawFrame() { var speed = 0.05; window.requestAnimationFrame(drawFrame); renderer.render(scene, camera); box.rotation.z += speed; box.rotation.y += speed; toruses.forEach(moveTorus); cubeCamera.visible = false; cubeCamera.updateCubeMap( renderer, scene ); cubeCamera.visible = true; } function moveTorus(torus) { torus.position.z += 0.6; if (torus.position.z >= 200) torus.position.z = -200; }