css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <script src="https://ajax.googleapis.com/ajax/libs/threejs/r84/three.min.js"></script>
            
          
!
            
              canvas {
  width: 100%;
  height: 100%;
}

body {
  margin: 0;
}
            
          
!
            
              let width = window.innerWidth;
let height = window.innerHeight;

// Create scene and camera.
let scene = new THREE.Scene();
let camera = new THREE.PerspectiveCamera(
  30,  // degrees FOV
  width/height,
  1, 1000
);

camera.position.z = 200;
camera.position.y += 300;
camera.rotation.x = -70;

// Create renderer and canvas.
let renderer = new THREE.WebGLRenderer({antialias: true});
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);

// Create floor surface.
let baseGeometry = new THREE.BoxGeometry(200, 100, 2);
let baseMaterial = new THREE.MeshStandardMaterial({color: 0xffffff});
let baseMesh = new THREE.Mesh(baseGeometry, baseMaterial);
baseMesh.receiveShadow = true;
baseMesh.castShadow = true;
scene.add(baseMesh);

// Create main track line.
let lineGeometry = new THREE.BoxGeometry(180, 20, 5);
let lineMaterial = new THREE.MeshStandardMaterial({color: "#0098D4"});
let lineMesh = new THREE.Mesh(lineGeometry, lineMaterial);
lineMesh.receiveShadow = true;
lineMesh.position.z += 2;
lineMesh.castShadow = true;
baseMesh.add(lineMesh);

// Create station 1.

let stationSideGeometry = new THREE.BoxGeometry(15, 10, 10);
let station1NearMesh = new THREE.Mesh(stationSideGeometry, lineMaterial);
station1NearMesh.receiveShadow = true;
station1NearMesh.castShadow = true;
station1NearMesh.position.x -= 82.5;
station1NearMesh.position.y -= 15;
lineMesh.add(station1NearMesh);

let station1FarMesh = new THREE.Mesh(stationSideGeometry, lineMaterial);
station1FarMesh.receiveShadow = true;
station1FarMesh.castShadow = true;
station1FarMesh.position.x -= 82.5;
station1FarMesh.position.y += 15;
lineMesh.add(station1FarMesh);

let stationBackGeometry = new THREE.BoxGeometry(5, 40, 10);
let station1BackMesh = new THREE.Mesh(stationBackGeometry, lineMaterial);
station1BackMesh.receiveShadow = true;
station1BackMesh.castShadow = true;
station1BackMesh.position.x -= 92.5;
lineMesh.add(station1BackMesh);

// Create station 2.

let station2NearMesh = new THREE.Mesh(stationSideGeometry, lineMaterial);
station2NearMesh.receiveShadow = true;
station2NearMesh.castShadow = true;
station2NearMesh.position.x += 82.5;
station2NearMesh.position.y -= 15;
lineMesh.add(station2NearMesh);

let station2FarMesh = new THREE.Mesh(stationSideGeometry, lineMaterial);
station2FarMesh.receiveShadow = true;
station2FarMesh.castShadow = true;
station2FarMesh.position.x += 82.5;
station2FarMesh.position.y += 15;
lineMesh.add(station2FarMesh);

let station2BackMesh = new THREE.Mesh(stationBackGeometry, lineMaterial);
station2BackMesh.receiveShadow = true;
station2BackMesh.castShadow = true;
station2BackMesh.position.x += 92.5;
lineMesh.add(station2BackMesh);


// Create a cuboid.
let carGeometry = new THREE.BoxGeometry(30, 20, 15);
let carMesh = new THREE.Mesh(carGeometry, lineMaterial);
carMesh.castShadow = true;
carMesh.receiveShadow = true;
carMesh.position.z += 5;
lineMesh.add(carMesh);

// Put everything on the floor.
baseMesh.rotation.x -= 90;
baseMesh.receiveShadow = true;

let ambientLight = new THREE.AmbientLight( "#fff", 1.2 ); // White light.
scene.add(ambientLight);

var leftLight = new THREE.SpotLight( 0xffffff, 0.8);
leftLight.position.set(-130, 30, -30 );
leftLight.castShadow = true;
scene.add(leftLight);

var rightLight = new THREE.SpotLight("#fff", 0.8);
rightLight.position.set(130, 30, -30);
rightLight.castShadow = true;
scene.add(rightLight);

var backLight = new THREE.DirectionalLight("#fff", 0.1);
backLight.position.set(0, 0.4, -1);
backLight.castShadow = true;
scene.add(backLight);

backLight.shadow.camera.left = -100;
backLight.shadow.camera.right = 100;
backLight.shadow.camera.top = 50;
backLight.shadow.camera.bottom = -10;

// Animate the car moving along.
let angle = 0;
function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
  carMesh.position.x = Math.sin(angle) * 75;
  angle += 0.01;
}

animate();


window.addEventListener( 'resize', onWindowResize, false );

function onWindowResize(){

    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();

    renderer.setSize( window.innerWidth, window.innerHeight );

}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console