Cannot replicate MeshPhongMaterial's new displacementMap-based self shadowing using custom ShaderMaterial (with ShaderLib.phong)

I’ve spent almost days on this one already … so I kindly ask for your help!

Since r132, it seems to be possible to have self-shadowing in geometry that has a displacementMap applied (see DisplacementMap with Shadow - Three.js Tutorials).

Using debug textures in r132, I get the following (correct) result using MeshPhongMaterial (the shadow casts from the grid structures – please ignore the block structures in the front):

Up to r131, I had a custom depth material that enabled the same functionality (customized to a RGBA elevation texture that I’m decoding in the shader – which is why I can’t work with out-of-the-box MeshPhongMaterial), which now stopped working since r132. In one of many attempts to fix this, I tried to get a custom ShaderMaterial up and running, based on ShaderLib.phong. While the displacement mapping happens (displaced geometry can be seen in the following screenshot’s grid lines), there is no shadowing.

Up to now, I tried out various defines, I’m setting the textures on “material level”, etc. – but I can’t get self-shadowing to work. It seems that there’s some define missing or some other flag off. I feel like I’m almost there but I’m not.

Here’s the current code for the screenshot above (mind some random defines I tried out unsuccessfully):

this.mesh.receiveShadow = true;
this.mesh.castShadow = true;

const terrainMaterial = new THREE.ShaderMaterial(THREE.ShaderLib.phong);
terrainMaterial.defines = {
    USE_UV: "",
    USE_MAP: "",
    USE_BUMPMAP: "",
    USE_DISPLACEMENTMAP: "",
    VERTEX_TEXTURES: "",
};
terrainMaterial.lights = true;
terrainMaterial.fog = true;
terrainMaterial.extensions.derivatives = true;
terrainMaterial.extensions.fragDepth = true;

terrainMaterial.map = TerrainTile.TextureLoader.load("img/grid512.png");
terrainMaterial.uniforms.map.value = TerrainTile.TextureLoader.load("img/grid512.png");
terrainMaterial.displacementMap = TerrainTile.TextureLoader.load("img/grid512.png");
terrainMaterial.uniforms.displacementMap.value = TerrainTile.TextureLoader.load("img/grid512.png");

this.mesh.material = terrainMaterial;
this.mesh.material.needsUpdate = true;

Why are there no shadows? :thinking:

Sounds like a regression maybe. Do you mind creating an issue on GitHub about it?

1 Like

Will do! Is it ok to just copy the post above into the issue? Or should I add sth?

Should be okay yes :+1:

1 Like

I’ve created the issue: Since r132, I can't replicate MeshPhongMaterial’s new displacementMap-based self-shadowing using ShaderMaterial (with ShaderLib.phong) · Issue #22881 · mrdoob/three.js · GitHub

Alright, @Mugen87 fixed it! See Since r132, I can't replicate MeshPhongMaterial’s new displacementMap-based self-shadowing using ShaderMaterial (with ShaderLib.phong) · Issue #22881 · mrdoob/three.js · GitHub

I need to post a follow up since my core problem wasn’t solved yet: