How to reconstruct JME3 lighting behavior in a custom shader

Hi,

i’m trying to create a custom material with some experimental procedural bump-mapping. Therefore i need to reimplement jme3 lighting in the new material. I am no expert, and this is primary a learning project, but i have some basic knowledge about lightning and shading equations, and some good books for reference.



The following questions arise while experimenting with the shader:


  • How can i get access to light direction of DirectionalLight?
  • How can i distinguish between DirectionalLight, PointLight, or other light types in my shader?
  • Can i use the ShaderLibs somehow to reuse existing light functions?
  • How would i access multiple light sources?



    I tried to look into Lighting matdef and its vert and frag shaders for better understanding. There i discovered the following global uniform parameters:



    uniform vec4 g_LightColor;

    uniform vec4 g_LightPosition;

    uniform vec4 g_AmbientLightColor;



    which are not mentioned in the matdef itself. is this all i need to get the data for lighting computation, are there more such global lighting uniforms that i could use? I also don’t see any array for multiple light-sources there, so i wonder how this works for many lights.



    I would love to digg deeper into this stuff and learn to make some nice shaders effects with the engine, so i would be very greatfull for some hints if there are any special things to know about how the jme3 lighting pipeline works, or where to look at to find out more. I could do it with some own custom uniforms, but using the jme3 infra structure would be awsome… Thanks

I’ve been trying to investigate further…

Could it be that the 4th component of the g_LightColor vector indicates wether the light is a Directional or a Point one?

trival said:
- How can i get access to light direction of DirectionalLight?

Look at the lighting.vert, there is a lightComputeDir method that does it.

trival said:
- How can i distinguish between DirectionalLight, PointLight, or other light types in my shader?

the type of the light is stored in the w component of the light color. see the Type enum in the light class for values (spot light is not implemented yet)

trival said:
- Can i use the ShaderLibs somehow to reuse existing light functions?

That's what it's done for, but i don't know if it's very up to date, i don't think the lighting shader use it.
to use lib use #import "Common/ShaderLib/thelib.glsllib" at the beginning of the shader

trival said:
- How would i access multiple light sources?

You don't. There is one shader pass per light source so only one light is sent to the shader. Look at the renderMultipassLighting method of the Material class for explanations.
The Ambient lighting pass is rendered only once with the first light (or alone if there is no light)

That's pretty much it.

Thanks a lot nehon for your explanations. it gives a good overview and background to continue playing :slight_smile: