shadowmap
directional light
by Millieyan
HTML
<div id="info">Directional Light AND Shadows
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/107/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js">
</script>
CSS
#info {
position: absolute;
top: 0px;
width: 100%;
padding: 10px;
text-align: center;
color: #ffff00
}
body {
// margin: 0;
overflow: hidden;
}
JavaScript
var camera, scene, renderer, light2;
var mesh2, angle = 0;
var dlhelper, dlshelper;
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 5000);
camera.position.z = 500;
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x333333);
document.body.appendChild(renderer.domElement);
let controls = new THREE.OrbitControls(camera, renderer.domElement);
///////////////////////////////////////////////////////////
mesh2 = new THREE.Mesh(new THREE.TorusKnotGeometry(20, 6, 100, 16),
new THREE.MeshLambertMaterial({color:0xffef12}));
scene.add(mesh2);
// floor
var floor = new THREE.Mesh(new THREE.PlaneGeometry(400, 400),
new THREE.MeshPhongMaterial());
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
// directional light
light2 = new THREE.DirectionalLight(0xffffff);
light2.position.set(-80, 160, 80);
light2.castShadow = true;
light2.shadow.camera.left = -180;
light2.shadow.camera.top = -180;
light2.shadow.camera.right = 180;
light2.shadow.camera.bottom = 180;
light2.shadow.camera.near = 1;
light2.shadow.camera.far = 300;
light2.target = mesh2;
light2.shadow.mapSize.width = light2.shadow.mapSize.height = 1024;
scene.add(light2);
light2.shadow.bias = -.01
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
floor.receiveShadow = true;
mesh2.castShadow = true;
mesh2.receiveShadow = true; // self shadow
dlhelper = new THREE.DirectionalLightHelper( light2, 5 );
scene.add( dlhelper );
dlshelper = new THREE.CameraHelper (light2.shadow.camera)
scene.add ( dlshelper );
}
function animate() {
// angle += 0.01;
mesh2.position.set (40*Math.cos(angle), 50, 40*Math.sin(angle));
dlhelper.update();
dlshelper.update();
requestAnimationFrame(animate);
render();
}
function render() {
...