Possible to use BABYLON.ShadowGenerator with shader material?

I have the following playground with a ground mesh using a material that receives shadows: https://www.babylonjs-playground.com/#ZH0IX6#6

I wanted to smooth the edges of the different colours and blend them into each other based on height. Perhaps there’s an easier way of doing this but I used a shader: https://www.babylonjs-playground.com/#ZH0IX6#7

Is there an easy way to have the shadows (and the specular highlights) work with this shader?
Would this be easier to accomplish in the node material editor?
Or perhaps a different solution altogether like modelling the vertices on contour lines instead of a regular grid?
After this I’m aiming to get a shimmering watery surface at the water level height of ~0.25 in case that influences any choice of solution.

Thanks!

I’ve just seen this post: Get shadows working with custom shader that links to this announcement: Custom shadows with ShadowDepthWrapper.

Using a shadow depth wrapper won’t work because your fragment code doesn’t have light support. You would have to include all the required shader code to support lights in your shader.

It would be a lot easier to create a material plugin instead. That way, your code will be embedded in a standard / PBR material, and you won’t have anything to do to get shadow support.

2 Likes

Thanks @Evgeni_Popov! I’ll check it out :slight_smile:

Material Plugin looks really powerful! I don’t know if there’s an easier / better way of using the info.diffuse and shadow but this looks better: https://www.babylonjs-playground.com/#ZH0IX6#15

1 Like

You could simply set the diffuse color of the material (called baseColor in the standard material):

If you want to support PBR materials, the injection point is named “CUSTOM_FRAGMENT_UPDATE_ALBEDO” and the shader variable is surfaceAlbedo.

3 Likes

That’s great to know. Thank you. I can see both CUSTOM_FRAGMENT_UPDATE_ALBEDO and surfaceAlbedo in the code now: https://github.com/BabylonJS/Babylon.js/blob/29934a73c9077fbed3c0b69577801cf02fc60f8a/packages/dev/core/src/Shaders/ShadersInclude/pbrBlockAlbedoOpacity.fx#L64 (though I have no idea when or how to use them yet… that’ll wait for another time! :slight_smile: )