Shader for 2.5D Lighting

Hi everyone,

I am currently working on a custom shader for my 2.5D game using the Sprite Renderer. The aim is to interact with the URP 2D Light texture (_ShapeLightTexture0) and make ALL pixels of the sprite brighter if the sprite intersects the bright part of the light texture within a given area(position and radius in material properties, let’s call it the light trigger area).

I have tried using UNITY_SAMPLE_TEX2D_LOD in the vertex function to create a “is in light flag,” but it didn’t work as expected.

As a result, I believe I need to use multiple passes for this shader:

  • First pass: in the fragment function, sample the light texture, detect if there is enough light in the light trigger area and set some global flag (if possible) or share this information with the next pass.

  • Second pass: get the flag indicating whether there is enough light in the light trigger area and apply sampling for the sprite texture with the appropriate brightness adjustments.

  • Is it possible to use multiple passes with URP (e.g., LightMode tag)?

  • How can I share this flag between passes if possible?

  • Or perhaps there is a better way to achieve the desired behavior?

I’m currently using Unity 2021.3.23 with URP and 2D renderer.
Any help would be much appreciated!
Thank you! \(゚ー゚\)

(Sprite used in image created by Yamilian, CC-License: LPC Heroine 2 | OpenGameArt.org)

would you consider simply to create the area you believe the light is touching, and if the sprite is within the area you activate the brightness property on the shader? It might be easier than trying to activate it directly via light sources when you could do it by intersecting with an elipse

Right now, it’s implemented almost exactly as you described. I have sprite light sources with generated physical colliders, and on each sprite, I have a trigger collider. When they intersect, I switch the material to Unlit, which contains full brightness.

However, I’m trying to find a cleaner way to do this because I have a large number of objects, and the number of colliders will increase drastically. It also requires ugly calculations if the light trigger area is on the crossing of multiple light sources with different colors.

ah i see, well I have been trying shader graph, dont know if that would help you whatsoever, but somehow I still have a hard time pulling off shader magic even with the aid

@venediklee had a similar problem:

Solved by offset sampling from the object.position, but not without some complexity.

If sampling within an area matters to you, then you might be able to average several sample points.

1 Like

I have read one post of Venediklee about this problem and he solved it himself. You can read his post to refer the solution.