Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                animate = ->
  updateBoxes()
  render()
  requestAnimationFrame animate
  #stats.update()

render = =>

  this_time = Date.now()
  if ((this_time - @start_time) >= 1500) && boxes.length <= @box_limit
    addBox()
    @start_time = Date.now()

  @renderer.render @scene, @camera

initScene = =>
    @renderer = new THREE.WebGLRenderer()
    @renderer.shadowMapEnabled = true
    @renderer.shadowMapSoft = true
    
    ###
    @renderer.shadowCameraNear = 3
    @renderer.shadowCameraFar = @camera.far
    @renderer.shadowCameraFov = 50

    @renderer.shadowMapBias = 0.0039
    @renderer.shadowMapDarkness = 0.5
    @renderer.shadowMapWidth = 512
    @renderer.shadowMapHeight = 512
    ###
    #@renderer = new THREE.CanvasRenderer()
    @camera = new THREE.PerspectiveCamera(VIEW_ANGLE,ASPECT,NEAR,FAR)
    @scene = new THREE.Scene()
    @scene.add(@camera)

    @camera.position.x = 50
    @camera.position.y = 30
    @camera.position.z = 65
    @camera.lookAt(new THREE.Vector3(0, 0, 0))

    @renderer.setSize(WIDTH, HEIGHT)
    container = document.createElement( 'div' )
    document.body.appendChild( container )
    container.appendChild(@renderer.domElement)
      
    pointLight = new THREE.SpotLight(0xFFFFFF)
    pointLight.position.x = 10
    pointLight.position.y = 50                                                         
    pointLight.position.z = 130
    pointLight.castShadow = true
    scene.add(pointLight)

    #cube = new THREE.Mesh(new THREE.CubeGeometry(3, 3, 3), new THREE.MeshNormalMaterial())
    #cube.position.y = 150
    #@scene.add(cube)

initPhysicsWorld = =>
    collisionConfiguration = new Ammo.btDefaultCollisionConfiguration()
    dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration)
    overlappingPairCache = new Ammo.btDbvtBroadphase()
    solver = new Ammo.btSequentialImpulseConstraintSolver()
    @scene.world = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration)
    @scene.world.setGravity new Ammo.btVector3(0, @gravity, 0)

addFloor = =>
    phong = new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ) 
    floor = new THREE.Mesh(new THREE.CubeGeometry(50, 2, 50), phong)
    floor.position.y = 150
    floor.castShadow = true
    floor.receiveShadow = true
    groundShape = new Ammo.btBoxShape(new Ammo.btVector3(25, 1, 25)) # Create block 50x2x50
    groundTransform = new Ammo.btTransform()
    groundTransform.setIdentity()
    groundTransform.setOrigin new Ammo.btVector3(0, 0, 0) # Set initial position
    groundMass = 0 # Mass of 0 means ground won't move from gravity or collisions
    localInertia = new Ammo.btVector3(0, 0, 0)
    motionState = new Ammo.btDefaultMotionState(groundTransform)
    rbInfo = new Ammo.btRigidBodyConstructionInfo(groundMass, motionState, groundShape, localInertia)
    groundAmmo = new Ammo.btRigidBody(rbInfo)
    groundAmmo.mesh = floor
    boxes.push groundAmmo # Keep track of this box
    @scene.world.addRigidBody groundAmmo
    @scene.add(groundAmmo.mesh)


addBox = ->

    mass = @box_width * @box_height * @box_depth # Matches box dimensions for simplicity
    startTransform = new Ammo.btTransform()
    startTransform.setIdentity()

    pos_x = Math.random() * 10
    pos_z = Math.random() * 10

    startTransform.setOrigin new Ammo.btVector3(pos_x, 20, pos_z) # Set initial position
    localInertia = new Ammo.btVector3(0, 0, 0)
    boxShape = new Ammo.btBoxShape(new Ammo.btVector3(@box_width/2, @box_height/2, @box_depth/2)) # Box is 3x3x3
    boxShape.calculateLocalInertia mass, localInertia
    motionState = new Ammo.btDefaultMotionState(startTransform)
    rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, motionState, boxShape, localInertia)
    boxAmmo = new Ammo.btRigidBody(rbInfo)

    @scene.world.addRigidBody boxAmmo

    wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x00ee00, wireframe: true, transparent: true } )
    darkMaterial = new THREE.MeshBasicMaterial( { color: 0x000088, transparent: true })
    multiMaterial = [ darkMaterial, wireframeMaterial ]
    phongMaterial = new THREE.MeshPhongMaterial( { ambient: 0x030303, color: 0x0000dd, specular: 0x009900, shininess: 60 } ) 
     



    new_cube = new THREE.Mesh(new THREE.CubeGeometry(@box_width, @box_height, @box_depth), phongMaterial)
    new_cube.position.y = 150
    new_cube.castShadow = true
    new_cube.receiveShadow = true
    boxAmmo.mesh = new_cube # Assign the Three.js mesh in `box`, this is used to update the model position later

    boxes.push boxAmmo # Keep track of this box

    @scene.add(boxAmmo.mesh)
    

updateBoxes = ->
    @scene.world.stepSimulation 1 / 60, 5 # Tells Ammo.js to apply physics for 1/60th of a second with a maximum of 5 steps
    i = undefined
    transform = new Ammo.btTransform()
    origin = undefined
    rotation = undefined
    i = 0
    while i < boxes.length
        boxes[i].getMotionState().getWorldTransform transform # Retrieve box position & rotation from Ammo
        origin = transform.getOrigin()
        boxes[i].mesh.position.x = origin.x()
        boxes[i].mesh.position.y = origin.y()
        boxes[i].mesh.position.z = origin.z()
        
        # Update rotation
        rotation = transform.getRotation()
        boxes[i].mesh.rotation.x = rotation.x()
        boxes[i].mesh.rotation.y = rotation.y()
        boxes[i].mesh.rotation.z = rotation.z()
        #boxes[i].mesh.quaternion.w = rotation.w()
        i++

    @camera.lookAt(new THREE.Vector3(boxes[0].mesh.position.x,boxes[0].mesh.position.y,boxes[0].mesh.position.z))

@start_time = Date.now()

WIDTH = window.innerWidth
HEIGHT = window.innerHeight

VIEW_ANGLE = 45
ASPECT = WIDTH / HEIGHT
NEAR = 0.1
FAR = 10000
cube = undefined

initScene()

@gravity = -20

@box_limit = 100
@box_width = 3
@box_height = 3
@box_depth = 3

boxes = []

initPhysicsWorld()
addFloor()
animate()

              
            
!
999px

Console