Read lights texture in ShaderGraph

Hi everyone, I’ve seen that the 2D renderer uses some screen-wide intermediary textures to render the point lights and the shape lights, before they are blended with the rendered objects that are affected by such lights. For example, one of them is called _ShapeLightTexture0. It would be amazing to be able to access such textures in our shaders, it would allow us to achieve a lot of great VFX.

2 Likes

You can actually do that today. A few things to pay attention to:

  • You must use the Sprite Unlit material (master node), otherwise symbol redefinition error in shader.
  • You need a light texture property that
    a) references _ShapeLightTexture0-3
    b) doesn’t have a default value
    c) isn’t Exposed
    d) is sampled in screen space by using the Screen Position node.
1 Like

Wow thanks! In the past I tried with a Sprite Lit material and got errors (redefinition of _ShapeLightTexture0), I assumed the light textures were not defined in the Unlit material so I didn’t try :smile:

Thanks to this I can emulate emissive color, finally.

Example of emissive:
-No postprocessing.
-No Sprite Lit materials.
-Using custom Unlit shader, reading the light texture, the sprite main texture and a secondary texture as emissive texture.

EDIT: You can see here how the red global light does not mix with the emissive colors, which is what I wanted.

EDIT2: With Bloom:

1 Like

With this, I can even implement sprite masks that use gradients using the light shapes when rendering some FX.

Nice one but it still doesnt give us ability to do partly emissive sprite?

Yes it does. You have the main sprite texture and the secondary emissive texture. Then you just multiply the main color by the light texture when the emissive texture is transparent and use the main color without multiplying it is not.

So I have to change entire game’s rendering shader to this “unlit” shader that manually “relight” them? :smile:

Yes! :roll_eyes: In my case it was not that bad since 95% of sprites used the same base shader. It is not a solution, it’s a temporary workaround.

Yeah, it beats me why this was not implemented the first time. It is the first thing that I would think of if I ever come up with any rendering pipeline…

1 Like

Is it possible to write directly to _ShapeLightTexture? I think I could eliminate 90% of my Light2Ds and do some really funky stuff.

edit: looks possible… requires digging into the 2d renderer… not tried yet

1 Like

Is this texture gone in 2021.1.0b12 ? Everything is black now.

EDIT: I correct myself, the texture is there. It must be another thing that has changed in this version.

I am not sure it could possibly to do with :

I reported this…

1 Like

It seems to be a different case, I disable postprocessing and get the same, only for materials that use the shader that read the light texture.

I correct myself, the texture is there. It must be another thing that has changed in this version. It seems to be something related to the Global Light, in the previous version 2020.3.0f1 it was affecting sprites that it should not, since they have sorting layers not included in the target sorting layers of the light. Now all the sprites that are not affected appear in black, which is the appropriate behavior. Let me research a bit more.

Yes, that was it. False alarm. Just a expected behaviour that was due to a bug that is now fixed, I deduce.

Hey! Have you had any success with emissive and sprite masks using 2D point lights, or does it only work with shape lights? Thanks :slight_smile:

Yes I made it. It works with point lights. Although I don’t use sprite masks.

2021.2.0a16

2 Likes