How does Unity use lighting in the shaders? Where do I pass my own light data?

Hi - I would like to know how exactly is the light from the scene used in the shaders.
I have a voxel world, where I am calculating my own light data and what I need is:

  • light to be the max value between my own light data and the lights in the scene.

I cannot see a way to do this in ShaderGraph. I could pass the custom light data to the albedo, but then the custom light will add to the scene lights.
I could damp the normals, but then the game won’t respond to scene light at all.
I could pass the custom light data to the ambient occlusion, but it will also not have the desired effect.

Ideally I would have the following setup:

  • global light level which changes with day/night cycle
  • no directional light whatsoever - I need my caves dark.
  • custom light data, which spreads voxel light sources inside caves etc - this allows me to have thousands of light sources for 1 iteration / voxel chunk only.
  • if played used a point light (say a torch) inside the caves, they should also respond to that light.
  • if the point light is brighter, the brightness should be from the point light, but if there is some alien light voxel source, which is brighter, it should not be boosted further by the point light.

Hope I can get some advice.

ps. I am planning to pass my own data to the shaders via UV2,UV3 etc.
pps. I am not planning to use many “unity” lights - for now I am thinking of 1 for portable torches. But, there could be some more usage for interesting effects - say a space ship lands, and light flicker, rotate, change colors etc.

Kind regards,

You did not mention which pipeline you’re using, but if you’re on URP you can implement custom lighting calculation using custom functions, check out this project - GitHub - Cyanilux/URP_ShaderGraphCustomLighting: Some custom lighting functions/sub-graphs for Shader Graph, Universal Render Pipeline
i believe you can also find some relevant stuff if you search for “custom lighting shader graph”

outside of shader graph, you can make an hlsl shader that does whatever you want. you can reference the default shaders, for instance, here is the default urp forward pass - Graphics/Packages/com.unity.render-pipelines.universal/Shaders/LitForwardPass.hlsl at master · Unity-Technologies/Graphics · GitHub, which uses UniversalFragmentPBR() for lighting calculation.

Thank you for replying. Yes, it happens that I use URP, so the resources that you have provided are perfect. I definitely look forward on checking them out.
The question is - are these resources maintained? Especially first one, it seems that last update is from last year. The second one is from Unity, but HLSL would be a new challenge for me.
I will also check out github & such for custom lighting.

Thanks again - these will definitely keep me busy :slight_smile:

ps. I also found this, is it still up to date?

Kind regards,
Ciprian B.

Follow-up problem. I got custom lighting using an unlit graph, based on:
https://gist.githubusercontent.com/NedMakesGames/13e64bed3a84e81826b05e5fb0214e70/raw/ca9105c8a308181860f1a9926d0ce13a394bf5e8/CustomLighting.hlsl

The problem that I have now, and I see reported over the internet, is that there are no shadows.
Any fix for this? the code is for an older version of Unity, 2019.1

Kind regards,