【问题标题】:Directional light shadow in mapboxmapbox中的定向光影
【发布时间】:2021-09-07 04:14:26
【问题描述】:

我在 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);

【问题讨论】:

    标签: three.js camera mapbox shadow directional-light


    【解决方案1】:

    “移动光源,使其位于平面上方” - 看起来您已经知道如何操作了,只需更改 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 );
    

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

    【讨论】:

    • 我试过这个,但我仍然没有在球体上得到阴影。在不太了解三个 js 的情况下,似乎我需要更改灯光位置以外的其他内容。光源似乎是正确的,但不是阴影。我可以以某种方式改变截头的位置吗?据我了解,这是两件不同的事情?
    猜你喜欢
    • 2014-05-26
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2018-06-20
    • 2013-01-03
    相关资源
    最近更新 更多