JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://rawgit.com/mrdoob/three.js/dev/build/three.min.js"></script>
<script src="https://rawgit.com/mrdoob/three.js/dev/examples/js/controls/OrbitControls.js"></script>
CSS
body {
background-color: #000;
margin: 0px;
overflow: hidden;
}
JavaScript
// Simple three.js example
var lightY = 30;
var logDepthBuffer = false; // <=== change to true to see bug
var mesh, renderer, scene, camera, controls, plane, light;
init();
animate();
function init() {
// renderer
renderer = new THREE.WebGLRenderer( { antialias: true, logarithmicDepthBuffer: logDepthBuffer } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
document.body.appendChild( renderer.domElement );
// scene
scene = new THREE.Scene();
// camera
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set( 70, 70, 70 );
// controls
controls = new THREE.OrbitControls( camera );
// ambient
scene.add( new THREE.AmbientLight( 0x222222 ) );
// light
light = new THREE.DirectionalLight( 0xffffff, 1 );
light.position.set( 0, lightY, 0 );
light.castShadow = true;
light.shadowCameraNear = 30;
light.shadowCameraFar = 75;
console.log( light );
scene.add( light );
scene.add( new THREE.CameraHelper( light.shadow.camera ) );
// axes
scene.add( new THREE.AxisHelper( 20 ) );
// geometry
var geometry = new THREE.SphereGeometry( 5, 12, 8 );
// material
var material = new THREE.MeshPhongMaterial( {
color: 0x00ffff,
shading: THREE.SmoothShading,
} );
// mesh
mesh = new THREE.Mesh( geometry, material );
mesh.castShadow = true;
scene.add( mesh );
var planeMaterial = new THREE.MeshPhongMaterial( {
color: 0xffff00,
shading: THREE.FlatShading,
} );
plane = new THREE.Mesh( new THREE.PlaneGeometry( 20, 20 ), planeMaterial );
plane.position.y = -20;
plane.rotation.x = -Math.PI / 2;
plane.receiveShadow = true;
scene.add( plane );
}
function animate() {
var delta = Date.now() * 0.001;
...