I have a basic spot light shadow program that create two boxes on the screen and calculates the shadows.
import { OrbitControls } from "https://unpkg.com/three@v0.126.1/examples/jsm/controls/OrbitControls.js"
import * as three from "https://unpkg.com/three@0.141.0/build/three.module.js"
/* Scene set up */
var scene = new three.Scene();
var camera = new three.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
var renderer = new three.WebGLRenderer({
shadowMap: {
enabled: true,
type: three.PCFSoftShadowMap
}
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
camera.position.y = 20
camera.position.x = 20
camera.position.z = 20
const controls = new OrbitControls(camera, renderer.domElement);
let light = new three.SpotLight()
light.castShadow = true
light.shadow.mapSize.width = 2056;
light.shadow.mapSize.height = 2056;
light.shadow.camera.near = 0.5;
light.shadow.camera.far = 1000;
light.shadow.bias = -0.00001;
light.shadow.penumbra = 0.5
light.shadow.focus = 1;
light.position.set(11,11,11)
const spotLightHelper = new three.SpotLightHelper( light );
scene.add( spotLightHelper );
const helper = new three.CameraHelper( light.shadow.camera );
scene.add( helper );
scene.add(light)
light.position.set(0,0,30)
let box = new three.Mesh(new three.BoxGeometry(10,10,10), new three.MeshPhongMaterial())
box.receiveShadow = true;
box.castShadow = true;
scene.add(box)
light.target = box
let box2 = new three.Mesh(new three.BoxGeometry(100,100,0.1), new three.MeshPhongMaterial())
box2.receiveShadow = true;
box2.castShadow = true;
scene.add(box2)
box2.position.set(0,0,-10)
let delta;
let clock = new three.Clock()
const animate = () => {
delta = clock.getDelta()
requestAnimationFrame(animate);
controls.update()
helper.update()
box.rotation.x += Math.sin(0.01)
box.rotation.y += Math.sin(0.01*5)
spotLightHelper.update();
renderer.render(scene, camera);
};
animate();
Despite this, I do not see any shadow:
The plane behind the cube should have a shadow?