Shadows on a ShaderMaterial?

In my scene, I have a custom ShaderMaterial with GLSL code. How do I get the shadows from the ShadowGenerator rendered onto my material?

Having shadows is not that simple, as you need also the lighting info. You could have a look at our implementation of ShadowOnlyMaterial and derived your work from it.

Else you could also maybe rely on the brand new NodeMaterial from @Deltakosh and its editor (https://nme.babylonjs.com)

I have had a look at ShadowOnlyMaterial and at SimpleMaterial. It seems as if the shaders there are assembled via a number of #include statements. I’m totally new to this and I’d like to understand what is being assembled there and what the end product (the whole shader source) looks like.

Is there any kind of documentation on this? Or else, can you point me to the places in the source tree from where the shader snippets are included? Then I could try to understand what happens and write a proper shadow-enabled and light-source-enabled shader for my terrain.

Thanks a lot, @sebavan

The first part is in the vertex (shadowOnly.vertex):
#include<__decl__lightFragment>[0..maxSimultaneousLights]

then you can use the include the vertex part:
#include<shadowsVertex>[0..maxSimultaneousLights]

The fragment needs:


#include<helperFunctions>

// Lights

#include<__decl__lightFragment>[0..maxSimultaneousLights]

#include<lightsFragmentFunctions>

#include<shadowsFragmentFunctions>

and the following code in the main par:

// Lighting
	vec3 diffuseBase = vec3(0., 0., 0.);
    lightingInfo info;
	float shadow = 1.;
    float glossiness = 0.;
    
#include<lightFragment>[0..1]

	// Composition
	vec4 color = vec4(shadowColor, (1.0 - clamp(shadow, 0., 1.)) * alpha);

All the #include are replacing the include by the code of the corresponding file in the shaderIncludes directory.

In the code side, the materialHelpers can help you bind and prepare accordingly.

It is not a simple thing as you need the entire light setup which I bet could be greatly simplified by using the node material. @PirateJC if he knows how we could set this up in NME :slight_smile:

1 Like

Ah, OK. I’ll have a look at that tomorrow. Thanks!