spotlight & shadow
by Millieyan
HTML
<div id="info">Shadowing a Group
</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;
var light;
var slhelper, lschelper;
var angle = Math.PI/3, sign = 1;
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.z = 500;
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x333333);
document.body.appendChild(renderer.domElement);
let controls = new THREE.OrbitControls(camera, renderer.domElement);
var amblight = new THREE.AmbientLight(0x444444); // soft white light
scene.add(amblight);
////////////////////////////////////////////////////
// occluder
boxes = new THREE.Group()
let geometry = new THREE.BoxGeometry(10, 10, 10);
let material = new THREE.MeshLambertMaterial({
color: 0x888888
});
let box = new THREE.Mesh(geometry, material);
boxes.add(box);
box2 = box.clone()
box2.position.set (30,30,0)
boxes.add (box2)
box.position.set (-30,30,0);
scene.add (boxes)
// spotlight
light = new THREE.SpotLight(0xffffff, 1.5);
light.position.set(0, 100, 0);
light.angle = Math.PI/3;
light.penumbra = 0.5
scene.add(light);
light.target = boxes;
// ocludee: floor
var floor = new THREE.Mesh(new THREE.PlaneGeometry(400, 400),
new THREE.MeshPhongMaterial());
floor.rotation.x = -Math.PI / 2;
scene.add(floor);
//////////////////////////////////////////////////
// shadow map settings
light.castShadow = true;
light.shadow.mapSize.width = 1024;
light.shadow.mapSize.height = 1024;
light.shadow.camera.near = 10;
light.shadow.camera.far = 200;
light.shadow.camera.fov = light.angle / Math.PI * 180 * 2;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
floor.receiveShadow = true;
// these WONT work for a Group/Object3D
// boxes.castShadow = true
// boxes.receiveShadow = true
boxes.traverse (
function...