Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
	<head>
	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
	<meta charset="UTF-8">

		<title>[]</title>
		<style>
			body {
				margin: 0px;
    			overflow: hidden; 
    			background-color: red;
    			font-family: 'Arial';
    		 	cursor: crosshair;
    		}
			
			#help{
				position: absolute;
			    top: 1%;
			    left: 1%;
			    font-size: 30px;
			    color: white;
			}

		</style>
	</head>
	<body>
		<div id="help">[forces] - use arrows</div>
	</body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                //Created by XORXOR, 2016
var renderer,scene,camera;
var mirrorCamera;
var texture, floortex;
var obj;
var count = 0;

var dirCode = [38,39,40,37];
var keys = []; 
var radius = 2;
var cubesPhy = [];
var cubes=[];
var mirrorCameras=[];
var light;
var cornellBox;

document.addEventListener('keydown', function(e) {
   keys[e.keyCode] = true;
}, false);

document.addEventListener('keyup', function(e) {
   keys[e.keyCode] = false;
}, false);



var textureLoader = new THREE.TextureLoader();
var loader = new THREE.ColladaLoader();

loader.options.convertUpAxis = true;


  textureLoader.load( 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/896175/checkerboard.png', function ( t2 ) {
    floortex = t2;

  loader.load('https://s3-us-west-2.amazonaws.com/s.cdpn.io/896175/cornell.dae', function(collada) {
      var c = collada.scene.getObjectByName( "back_wall", true );
      console.log(collada)
      var colladaObject=c.children[0];
      cornellBox = new THREE.Mesh(colladaObject.geometry,colladaObject.material)
      cornellBox.scale.set(3,3,3);

      initCannon();
      init();
      animate();
    
  });
})

function init(){
  renderer = new THREE.WebGLRenderer({ antialias: true });
  renderer.setClearColor(0xffffff);
  renderer.setSize(window.innerWidth, window.innerHeight)
  renderer.setPixelRatio( window.devicePixelRatio );
  
  // renderer.shadowMap.enabled = true;
  // renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  
  document.body.appendChild(renderer.domElement);

  scene = new THREE.Scene();
  scene.fog = new THREE.FogExp2(0xffffff, .02);

  //env
  scene.add(cornellBox);
  cornellBox.recieveShadow = true;
  cornellBox.castShadow = true;
  var size = 1.5;
  var halfExtents = new CANNON.Vec3(size,size,size);
  var sphereShape = new CANNON.Sphere(size); 
  var sphereGeometry = new THREE.SphereGeometry(halfExtents.x,32,32);
  
  for(var i=0; i<6; i++){
      var x = (Math.random())*10;
      var y = halfExtents.y;
      var z = (Math.random()-0.5)*15;
      
      var mirrorcam = new THREE.CubeCamera( 0.1, 512, 128 );
      mirrorcam.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;
      
      var matt = new THREE.MeshBasicMaterial( { envMap: mirrorcam.renderTarget.texture,fog:true});

      var sphereBody = new CANNON.Body({ mass: .1 });
      sphereBody.addShape(sphereShape);
      // sphereBody.linearDamping =0.9;
      // sphereBody.angularDamping = 0.9;

      var spheremesh = new THREE.Mesh( sphereGeometry, matt );
      world.addBody(sphereBody);
      scene.add(spheremesh);
      // spheremesh.castShadow = true;
      // cornellBox.recieveShadow = true;

      sphereBody.position.set(x,y,z);
      spheremesh.position.set(x,y,z);

      scene.add( mirrorcam );

      cubesPhy.push(sphereBody);
      cubes.push(spheremesh);
      mirrorCameras.push(mirrorcam);
  }

  camera = new THREE.PerspectiveCamera( 90, window.innerWidth/window.innerHeight, .1, 2000 );

  camera.position.set(-10,10,0 )
  camera.rotation.set(0,-Math.PI/2,0)

  mirrorCamera = new THREE.CubeCamera( 0.1, 5000, 128 );
  mirrorCamera.renderTarget.texture.minFilter = THREE.LinearMipMapLinearFilter;

  scene.add( mirrorCamera );
 
  var geometry = new THREE.SphereGeometry( radius, 32,32 );

  obj = THREE.SceneUtils.createMultiMaterialObject( geometry, [

  new THREE.MeshBasicMaterial( { envMap: mirrorCamera.renderTarget.texture,fog:true}),
  new THREE.MeshBasicMaterial( { color: 0xcccccc,opacity:.2, transparent:true, wireframe: true} )
  ]);

  scene.add(obj);
  // obj.castShadow = true;
  // obj.recieveShadow = true;
  obj.position.y = radius;
 
  var ambient = new THREE.AmbientLight( 0xffffff );
  scene.add( ambient );

//Light todo
  // light = new THREE.SpotLight( 0xffffff,.1 );
  // light.angle = Math.PI/2
  // light.target.position.set( 0, 0, 0);
  // light.castShadow = true;
  // light.position.set(7.5,15,0)

  // light.shadow = new THREE.LightShadow(new THREE.PerspectiveCamera( 70, window.innerWidth/window.innerHeight, .1,10 ));
  // light.shadow.bias = 0.01;
  
  // light.shadow.camera.near = 20;
  // light.shadow.camera.far = 50;//camera.far;
  // light.shadow.camera.fov = 40;
  
  // light.shadow.mapSize.width = 1024;
  // light.shadow.mapSize.height = 1024;

  // scene.add( light );
  
  // var spotLightHelper = new THREE.SpotLightHelper( light );
  // scene.add( spotLightHelper );

  mirrorCamera.position.y = obj.position.y;

  //PLANE
  var floorMaterial = new THREE.MeshBasicMaterial( { map: floortex, side:THREE.DoubleSide } );
  var floorGeometry = new THREE.PlaneGeometry(50, 50, 1, 1);
  var floor = new THREE.Mesh(floorGeometry, floorMaterial);
  floor.rotation.set(Math.PI/2,0,0)
  floor.position.x = -25;
  scene.add( floor );

  window.addEventListener('resize', onWindowResize, false);
}
function initCannon(){
    // Setup our world
    world = new CANNON.World();

    world.quatNormalizeSkip = 0; 
    world.quatNormalizeFast = false; 
    var solver = new CANNON.GSSolver();

    solver.iterations = 7;
    solver.tolerance = 0.1;

    world.solver = new CANNON.SplitSolver(solver);
    world.gravity.set(0,-10,0);
    world.broadphase = new CANNON.NaiveBroadphase();

    // Create a sphere
    var mass = 1;
    sphereShape = new CANNON.Sphere(radius);
    sphereBody = new CANNON.Body({ mass: mass });
    sphereBody.addShape(sphereShape);
    sphereBody.position.set(radius,radius,0);
    sphereBody.linearDamping =0.7;
    sphereBody.angularDamping = 0.9;

    world.addBody(sphereBody);

    // Create a plane
    var groundShape = new CANNON.Plane();
    var groundBody = new CANNON.Body({ mass: 0 });
    groundBody.addShape(groundShape);
    groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),-Math.PI/2);
    world.addBody(groundBody);

  //wall1
    var redwall = new CANNON.Plane();
    var redwallBody = new CANNON.Body({ mass: 0 });
    redwallBody.addShape(redwall);
    redwallBody.quaternion.setFromAxisAngle(new CANNON.Vec3(0,0,1),-Math.PI/2);
    redwallBody.position.set(0,0,-7.5)
    world.addBody(redwallBody);
 //wall2
    var greenwall = new CANNON.Box(new CANNON.Vec3(15,10,1));
    var greenwallBody = new CANNON.Body({ mass: 0 });
    greenwallBody.addShape(greenwall);
    greenwallBody.position.set(0,0,8.5)
    world.addBody(greenwallBody);
 //wall3
    var backwall = new CANNON.Box(new CANNON.Vec3(1,10,15));
    var backwallBody = new CANNON.Body({ mass: 0 });
    backwallBody.addShape(backwall);
    backwallBody.position.set(16,0,0)
    world.addBody(backwallBody);
//wall4
    var frontWall = new CANNON.Box(new CANNON.Vec3(1,10,15));
    var frontWallBody = new CANNON.Body({ mass: 0 });
    frontWallBody.addShape(frontWall);
    frontWallBody.position.set(-1,0,0)
    world.addBody(frontWallBody);
}



function addForce(){
    var f = 30;
    var worldPoint = new CANNON.Vec3(obj.position.x,obj.position.y+radius*2,obj.position.z);
    if ( keys[dirCode[0]] || keys[dirCode[1]] || keys[dirCode[2]] || keys[dirCode[3]]) {
      if (keys[dirCode[0]] && keys[dirCode[1]]) {
        //right top
        var force = new CANNON.Vec3(f,0,f);
      } 
      if (keys[dirCode[1]] && keys[dirCode[2]]) {
        //right down
        var force = new CANNON.Vec3(-f,0,f);
      };
      if (keys[dirCode[2]] && keys[dirCode[3]]) {
        //left down
        var force = new CANNON.Vec3(-f,0,-f);
      };
      if (keys[dirCode[3]] && keys[dirCode[0]]) {
        //left top
        var force = new CANNON.Vec3(f,0,-f);
      };
      if (keys[dirCode[0]]) { //top
        var force = new CANNON.Vec3(f,0,0);
      }
      if (keys[dirCode[1]]) { //right
        var force = new CANNON.Vec3(0,0,f);
      }
      if (keys[dirCode[2]]) { //down
        var force = new CANNON.Vec3(-f,0,0);
      }
      if (keys[dirCode[3]]) { //left
        var force = new CANNON.Vec3(0,0,-f);
      }
    sphereBody.applyForce(force,worldPoint);
  }
}


var dt = 1/60;

function animate() {
    var time = performance.now() * 0.0001;
    addForce();
    world.step(dt);

    obj.visible = false; 
    // pingpong
    mirrorCamera.updateCubeMap( renderer, scene );
    obj.visible = true; 

      var r = scene.position.distanceTo( sphereBody.position );
      var alpha = Math.atan2(sphereBody.position.x,sphereBody.position.z)

      sphereBody.position.y = radius;
      obj.position.copy(sphereBody.position)
     
      obj.quaternion.x = sphereBody.quaternion.x;
      obj.quaternion.y = sphereBody.quaternion.y;
      obj.quaternion.z = sphereBody.quaternion.z;
      obj.quaternion.w = sphereBody.quaternion.w;

    for(var i=0; i<cubes.length; i++){
        cubes[i].visible = false;

        cubes[i].position.copy(cubesPhy[i].position);
        cubes[i].quaternion.copy(cubesPhy[i].quaternion);
        mirrorCameras[i].position.copy(cubes[i].position);

        mirrorCameras[i].updateCubeMap( renderer, scene );
        cubes[i].visible = true;
    }
    
    camera.position.set(obj.position.x-5*radius,obj.position.y+5*radius,0 )
    // camera.position.set(0,0,0 )

    camera.lookAt(obj.position);
    mirrorCamera.position.copy( obj.position)
    requestAnimationFrame(animate);
    renderer.render(scene, camera);


}

function onWindowResize() {
    camera.left = window.innerWidth / - 2;
    camera.right = window.innerWidth / 2;
    camera.top = window.innerHeight / 2;
    camera.bottom = window.innerHeight / - 2;
    camera.updateProjectionMatrix();
    renderer.setSize( window.innerWidth, window.innerHeight );
}

              
            
!
999px

Console