var scene, width, height, camera, cubeCamera, renderer, toruses, box;
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();
boxGeometry = new THREE.BoxGeometry(2, 2, 2);
boxMaterial = new THREE.MeshBasicMaterial({
envMap: cubeCamera.renderTarget,
box = new THREE.Mesh(boxGeometry, boxMaterial);
box.position.set(0, 0, 0);
renderer.setSize(width, height);
renderer.setClearColor(0x17293a);
cubeCamera.position.set(0, 0, 0);
camera.position.set(0, -50, 0);
camera.lookAt(scene.position);
generateToruses(200, 10);
document.body.appendChild(renderer.domElement);
function generateToruses(num, step) {
var torusGeometry, torusMaterial, torus,
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);
window.requestAnimationFrame(drawFrame);
renderer.render(scene, camera);
toruses.forEach(moveTorus);
cubeCamera.visible = false;
cubeCamera.updateCubeMap( renderer, scene );
cubeCamera.visible = true;
function moveTorus(torus) {
if (torus.position.z >= 200) torus.position.z = -200;