1

我在 mapbocx 中有一个自定义网格几何体(三个 js)。我正在尝试创建用于投射定向阴影的灯光,但我总是在基本平面中使用光源(这导致平面上方的对象上没有投射阴影)。有谁知道我如何移动光源使其位于飞机上方?我添加了一个助手来查看范围框,我想将它沿着下图中的 z 向量向上移动。

在此处输入图像描述

//Create a WebGLRenderer and turn on shadows in the renderer
        const renderer = new THREE.WebGLRenderer();
        renderer.shadowMap.enabled = true;
        renderer.shadowMap.type = THREE.PCFSoftShadowMap; // default THREE.PCFShadowMap

        //Add Ambient light
        const amblight = new THREE.AmbientLight(0xffffff, 0.8);
        amblight.position.set(8, 10, 5); //default; light shining from top
        scene.add(amblight);

        //Create a DirectionalLight and turn on shadows for the light
        const light = new THREE.DirectionalLight(0xffffff, 0.5);
        //light.position.set(8, 10, 5); //default; light shining from top
        light.position.y = 2000;
        light.position.x = 10;
        light.position.z = 5;
        light.castShadow = true; // default false
        scene.add(light);
        //scene.add(light.target);

        //Set up shadow properties for the light
        light.shadow.mapSize.width = 512;
        light.shadow.mapSize.height = 512;
        light.shadow.camera.left = -100;
        light.shadow.camera.right = 100;
        light.shadow.camera.top = 100;
        light.shadow.camera.bottom = -100;
        light.shadow.camera.near = 0.5;
        light.shadow.camera.far = 100; //Scope box depth

        //Create a plane that receives shadows (but does not cast them)
        const planeGeometry = new THREE.PlaneGeometry(1000, 1000, 10, 10);
        const planeMaterial = new THREE.MeshPhongMaterial({
          color: 0x808080,
          opacity: 0.8,
          transparent: true,
        });
        const plane = new THREE.Mesh(planeGeometry, planeMaterial);
        plane.receiveShadow = true;
        scene.add(plane);

        const meshString = result.mesh.meshString;
        const mesh = meshToThreejs(rhino, meshString, THREE);
        //scene.add(mesh);

        //Add shadows
        mesh.castShadow = true; //default is false
        mesh.receiveShadow = true; //default
        scene.add(mesh);

        //ENd shadows

        //Create a helper for the shadow camera (optional)
        const helper = new THREE.CameraHelper(light.shadow.camera);
        scene.add(helper);
4

1 回答 1

0

“移动光源,使其位于平面上方” - 看起来您已经知道如何执行此操作,只需更改 z 数即可。

light.position.z = 20;
// or
light.position.set(0, 0, 20);

// Check note below - If y is up
light.position.y = 20;
// or
light.position.set(0, 20, 0);

请注意,默认情况下 Y 在 Three.js 中是向上的,除非您已经在此处未显示的代码中处理了它。如果您需要检查此项,请将axesHelper 添加到您的场景中。X 轴为红色。Y 轴为绿色。Z 轴为蓝色。确保相机朝正确的方向移动。

const axesHelper = new THREE.AxesHelper( 100 );
scene.add( axesHelper );

如果您仍然没有得到阴影,您可以尝试像 Three.js 文档(https://threejs.org/docs/#api/en/lights/shadows/DirectionalLightShadow)那样添加一个球体

//Create a sphere that cast shadows (but does not receive them)
const sphereGeometry = new THREE.SphereGeometry( 5, 32, 32 );
const sphereMaterial = new THREE.MeshStandardMaterial( { color: 0xff0000 } );
const sphere = new THREE.Mesh( sphereGeometry, sphereMaterial );
sphere.castShadow = true; //default is false
sphere.receiveShadow = false; //default
scene.add( sphere );

如果正确投射阴影,则可能是您的网格存在问题,或者这些建筑物的高度太小以至于阴影非常小

于 2021-06-23T23:53:29.007 回答