可以从DirectionalLight
创建阴影吗?
如果使用SpotLight
,则会看到阴影,但如果使用DirectionalLight
,它不起作用。
可以从DirectionalLight
创建阴影吗?
如果使用SpotLight
,则会看到阴影,但如果使用DirectionalLight
,它不起作用。
一定要通过 CameraHelper
启用每个灯光的调试渲染:
scene.add(new THREE.CameraHelper(camera))
或者在较旧版本的Three.js中:
light.shadowCameraVisible = true;
以下是一些可能有用的代码片段。
var light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, 2, 2);
light.target.position.set(0, 0, 0);
light.castShadow = true;
light.shadowDarkness = 0.5;
light.shadowCameraVisible = true; // only for debugging
// these six values define the boundaries of the yellow box seen above
light.shadowCameraNear = 2;
light.shadowCameraFar = 5;
light.shadowCameraLeft = -0.5;
light.shadowCameraRight = 0.5;
light.shadowCameraTop = 0.5;
light.shadowCameraBottom = -0.5;
scene.add(light);
确保某些对象投射阴影:
object.castShadow = true;
确保某些对象接收阴影:
object.receiveShadow = true;
WebGLRenderer
上配置一些值:renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(canvasWidth, canvasHeight);
renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;
light.shadowCameraVisible = true
生效吗?在这个分支示例中似乎没有任何作用(来自上面接受的答案):http://jsfiddle.net/Q4uqE/566/ - Ates Gorallight.shadowCameraVisible = true
:scene.add(new THREE.CameraHelper(camera))
。 - ezzescene.add(new THREE.CameraHelper(light.shadow.camera));
- Thoronwen是的,你绝对可以使用定向光来投射阴影。你需要确保没有使用不支持阴影的MeshBasicMaterial材质。应该使用MeshLambertMaterial 或者 MeshPhongMaterial 材质代替。
在渲染器中启用阴影需要类似以下的设置:
renderer.shadowMapEnabled = true;
renderer.shadowMapSoft = true;
renderer.shadowCameraNear = 3;
renderer.shadowCameraFar = camera.far;
renderer.shadowCameraFov = 50;
renderer.shadowMapBias = 0.0039;
renderer.shadowMapDarkness = 0.5;
renderer.shadowMapWidth = 1024;
renderer.shadowMapHeight = 1024;
然后你必须针对每个对象和每个光源启用阴影投射和接收,这样你就会拥有
dirLight.castShadow = true;
object.castShadow = true;
otherObject.receiveShadow = true;
然后,如果光源和物体被放置在合适的位置,dirLight
将使得 object
的阴影被投射在 otherObject
上。
[编辑]: 这里有一个可工作的演示,供任何想要做类似事情的人参考。
light.shadowCameraVisible
没有作用吗?http://jsfiddle.net/Q4uqE/566/ - Ates Goralrenderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap; //THREE.BasicShadowMap | THREE.PCFShadowMap | THREE.VSMShadowMap | THREE.PCFSoftShadowMap
renderer.shadowMap.soft = true;
renderer.shadowMap.bias = -0.0001;
renderer.shadowMap.darkness = 1;
renderer.shadowMap.width = 2048;
renderer.shadowMap.height = 2048;
示例:
const spotLight = new THREE.DirectionalLight();
spotLight.castShadow = true;
scene.add(spotLight);
spotLight.position.set(0, 8, 0);
//Object cast shadow
const cube = new THREE.Mesh( new THREE.BoxGeometry(1,1,1), new THREE.MeshBasicMaterial({color: 0x00ff00}) );
cube.castShadow = true;
scene.add(cube);
//Plane floor receive shadows
const planeShadow = new THREE.Mesh(new THREE.PlaneGeometry(3, 3), new THREE.MeshPhongMaterial()); //Material they have to be MeshPhongMaterial
planeShadow.name = "FloorShadow";
planeShadow.renderOrder = -2;
planeShadow.lookAt(new THREE.Vector3(0, 1, 0));
planeShadow.receiveShadow = true;
planeShadow.position.set(0, 0, 0);
scene.add(planeShadow);
如果您需要更多信息或其他示例,可以查看此链接[https://sbcode.net/threejs/directional-light-shadow/]