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 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

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

              
                html, body
  margin: 0
              
            
!

JS

              
                let renderer, scene, camera, cameraControl
let group
let cube, plane, door
let raycaster
let mouse = new THREE.Vector2(), INTERSECTED
let isOpen = false

function init(){
  scene = new THREE.Scene()
  // scene.fog = new THREE.Fog(0x090b33, 5, 50)
  
  renderer = new THREE.WebGLRenderer()
  renderer.setSize(window.innerWidth, window.innerHeight)
  renderer.shadowMap.enable = true
  
  document.body.appendChild(renderer.domElement)
  
  camera = new THREE.PerspectiveCamera(
    45, window.innerWidth/window.innerHeight, 1, 1000
  )
  camera.position.set(0, 50, -50)
  camera.lookAt(scene.position)
  cameraControl = new THREE.OrbitControls(camera)
  
  raycaster = new THREE.Raycaster()
  
  group = new THREE.Object3D()
  scene.add(group)
  
  let ambientLight = new THREE.AmbientLight("#333")
  scene.add(ambientLight)
  
  let directionalLight = new THREE.DirectionalLight(0xffffff, 0.5)
  scene.add(directionalLight)
  
  let spotLight = new THREE.SpotLight({color: "#fff"})
  spotLight.position.set(0, 50, -50)
  spotLight.CastShadow = true
  scene.add(spotLight)
  
  let spotLight2 = new THREE.SpotLight({color: "#fff"})
  spotLight2.position.set(0, 10, 10)
  spotLight2.CastShadow = true
  // scene.add(spotLight2)
  
  let spotLight3 = new THREE.SpotLight({color: "#fff"})
  spotLight3.position.set(10, 10, 30)
  spotLight3.CastShadow = true
  scene.add(spotLight3)

  let planeGeo = new THREE.PlaneGeometry(20, 20)
  let planeMaterial = new THREE.MeshLambertMaterial( {color: 0x898282, side: THREE.DoubleSide} )
  let doorGeo = new THREE.BoxGeometry( 1, 15, 8 )
  let doorMaterial = new THREE.MeshLambertMaterial({color:0x630810})
  
  plane = new THREE.Mesh( planeGeo, planeMaterial )
  plane.position.set(0, -5, 0)
  plane.rotation.x = -0.5* Math.PI
  plane.name = "floor"
  scene.add( plane )
  
  plane2 = new THREE.Mesh( planeGeo, planeMaterial )
  plane2.position.set(-10, 5, 0)
  plane2.rotation.y = 0.5* Math.PI
  plane2.name = "rightWall"
  scene.add( plane2 )
  
  plane3_up = new THREE.Mesh( new THREE.PlaneGeometry(5, 8), planeMaterial )
  plane3_up.position.set(0, 12.5, 10)
  plane3_up.rotation.z = 1.5* Math.PI
  plane3_up.name = "backWallUp"
  scene.add( plane3_up )
  plane3_right = new THREE.Mesh( new THREE.PlaneGeometry(20, 6), planeMaterial )
  plane3_right.position.set(-7, 5, 10)
  plane3_right.rotation.z = 0.5* Math.PI
  plane3_right.name = "backWallRight"
  scene.add( plane3_right )
  plane3_left = new THREE.Mesh( new THREE.PlaneGeometry(20, 6), planeMaterial )
  plane3_left.position.set(7, 5, 10)
  plane3_left.rotation.z = 0.5* Math.PI
  plane3_left.name = "backWallLeft"
  scene.add( plane3_left )
  
  plane4 = new THREE.Mesh( planeGeo, planeMaterial )
  plane4.position.set(10, 5, 0)
  plane4.rotation.y = -0.5* Math.PI
  plane4.name = "leftWall"
  scene.add( plane4 )
  
  door = new THREE.Mesh(doorGeo, doorMaterial)
  door.position.set(0, 2.5, 10)
  door.rotation.y = 0.5* Math.PI
  door.name = "door"
  scene.add(door)
  

  
  document.addEventListener( 'click', onMouseClick, false )
}

// ray caster
function onMouseClick( event ) {
  event.preventDefault()
  mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1
  mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1
  raycaster.setFromCamera( mouse, camera )
  let intersects = raycaster.intersectObjects( scene.children )
  console.log(intersects)
  if(intersects && intersects[0] && intersects[0].object && intersects[0].object.name === "door"){
    if(isOpen){
      // close door
      door.rotation.y = 0.5 * Math.PI
      door.position.set(0, 2.5, 10)
    }else {
      // open door
      door.rotation.y = 1 * Math.PI
      door.position.set(4, 2.5, 6)
    }
    isOpen = !isOpen
  }
}

function render(){
  renderer.render(scene, camera)
  cameraControl.update()
  requestAnimationFrame(render)
}

window.addEventListener('resize', function(){
  camera.aspect = window.innerWidth/window.innerHeight
  camera.updateProjectionMatrix()
  renderer.setSize(window.innerWidth, window.innerHeight)
})

init()
render()




              
            
!
999px

Console