Search Unity

Feature Request Read lights texture in ShaderGraph

Discussion in '2D Experimental Preview' started by ThundThund, Dec 25, 2020.

  1. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    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.
     
    GliderGuy and Lo-renzo like this.
  2. yuanxing_cai

    yuanxing_cai

    Unity Technologies

    Joined:
    Sep 26, 2014
    Posts:
    335
    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.
     
    GliderGuy likes this.
  3. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    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 :D
     
  4. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    Thanks to this I can emulate emissive color, finally.
     
  5. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    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.

    Capture.PNG

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

    Capture2.PNG

    EDIT2: With Bloom:

    Capture3.PNG
     
    Last edited: Jan 6, 2021
    GliderGuy likes this.
  6. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    With this, I can even implement sprite masks that use gradients using the light shapes when rendering some FX.
     
  7. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,111
    Nice one but it still doesnt give us ability to do partly emissive sprite?
     
  8. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    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.
     
  9. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,111
    So I have to change entire game's rendering shader to this "unlit" shader that manually "relight" them? :D
     
  10. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    Yes! :rolleyes: 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.
     
  11. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,111
    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..
     
    ThundThund likes this.
  12. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    741
    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
     
    Last edited: Jan 11, 2021
    GliderGuy likes this.
  13. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    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.
     
    Last edited: Mar 18, 2021
  14. castor76

    castor76

    Joined:
    Dec 5, 2011
    Posts:
    2,111
    ThundThund likes this.
  15. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
  16. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    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.
     
  17. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    Yes, that was it. False alarm. Just a expected behaviour that was due to a bug that is now fixed, I deduce.
     
  18. lorenzoteslermabe

    lorenzoteslermabe

    Joined:
    Dec 23, 2020
    Posts:
    11
    Hey! Have you had any success with emissive and sprite masks using 2D point lights, or does it only work with shape lights? Thanks :)
     
  19. ThundThund

    ThundThund

    Joined:
    Feb 7, 2017
    Posts:
    175
    Yes I made it. It works with point lights. Although I don't use sprite masks.
     
  20. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    741
    2021.2.0a16
     
    GliderGuy and ThundThund like this.
unityunity