Directional light shadow in mapbox

653 views Asked by At

I have a custom mesh geometry (three js) in mapbocx. I am trying to create a light for casting directional shadows but I always end up woth the light source in the base plane (which results in no casted shadows on my objects above the plane). Does anyone know how I can move the light source so it is above the plane? I added a helper to see the scope box and I would like to move it upwards along the z-vector in the image below.

enter image description here

//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);
1

There are 1 answers

1
JP4 On

"move the light source so it is above the plane" - It looks like you already know how to do this, just change the z number.

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

Just a note, by default Y is up in Three.js unless you have already handled that in code not shown here. If you need to check this add the axesHelper to your scene. The X axis is red. The Y axis is green. The Z axis is blue. Make sure the camera is moved in the correct direction.

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

If you are still not getting shadows you could try to add a sphere like in the Three.js docs (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 );

If that is casting a shadow correctly then perhaps there is an issue with your mesh, or the height of those buildings is so small that the shadows are really small