Global Light Info in Custom Shader, similar to the Lights Node

What would be the best way to get all the lights in the scenes contributions in a custom shader?

I have sneaking suspicion its a couple steps.

You can do it like that:

	// Lighting
	vec3 diffuseBase = vec3(0., 0., 0.);
	lightingInfo info;
	vec3 specularBase = vec3(0., 0., 0.);
	float shadow = 1.;

	vec3 lightmapColor = texture2D(lightmapSampler, vLightmapUV + uvOffset).rgb * vLightmapInfos.y;


It’s a copy/paste from the fragment shader of the standard material.

After this block of code, you get the light contributions in diffuseBase and specularBase and the shadow level in shadow (1 meaning not in shadow).

You will need some includes too (lightFragmentDeclaration, lightsFragmentFunctions, …).

Note also that the code in those includes (including lightFragment) is expecting some variables to be defined (like viewDirectionW, normalW, …) so you need to setup them first before this code will work. You can also rename a variable when including a file in your shader with this syntax:


Which will replace all occurrences of the string vPositionW by myWorldPos when including the content of the file lightsFragmentFunctions. You can give more parameters if you need more replacing to take place:


All in all you should look at the code of default.fragment.fx for a better view of the process. Or use the node material editor and export the shader code :wink:

1 Like

Awesome! Now I can really do volumetric particles, my limitation was having to worry about all the lights and only really having directional light as an option.