Search Unity

Please help! Issue with light intensity of overlapping lights with LWRP in 2D

Discussion in 'Shaders' started by unity_yTqO2qPOnNjq_w, Feb 12, 2020.

  1. unity_yTqO2qPOnNjq_w

    unity_yTqO2qPOnNjq_w

    Joined:
    Aug 14, 2019
    Posts:
    1
    Hi Everyone! I am working with shaders for the first time and have run into an issue I cant figure out how to resolve. I am hoping somebody here with some expertise would be willing to bless me with their guidance. I am working on a scene with point light 2Ds projected onto a background image in a 2D game. I am using the new LWRP 2D light features in 2019.3.

    When the lights overlap the intensity value of the light doubles! It looks like this example image (In the image, left is the issue I am having, and center is the desired result):


    The current default shader on my background sprite renderers material is "Sprite-Lit-Default", which is required by LWRP to render lights and shadows properly.

    I found some guidance online stating that I need to add "BlendOp Max" to the "ForwardAddPass" of the shader. This makes sense, as it would make the strongest light set the maximum intensity and would prevent it from increasing further (Unity documentation on BlendOp Max: https://docs.unity3d.com/Manual/SL-...1.1069406989.1581530830-1123899672.1569303480 )

    They suggested adding the following code to the shader, but I have a feeling this is not for the same type of shader as mine, as it did not work.

    Code (CSharp):
    1.  Pass {
    2.              Name "FORWARD_DELTA"
    3.              Tags {
    4.                  "LightMode"="ForwardAdd"
    5.              }
    6.              BlendOp Max
    7.            
    8.            
    9.              CGPROGRAM
    10.              #pragma vertex vert
    11.              #pragma fragment frag
    12.              #define UNITY_PASS_FORWARDADD

    Heres what the code of the "Sprite-Lit-Default" shader currently uses looks like, that my background sprite material:

    Code (CSharp):
    1. Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Diffuse", 2D) = "white" {}
    6.         _MaskTex("Mask", 2D) = "white" {}
    7.         _NormalMap("Normal Map", 2D) = "bump" {}
    8.  
    9.         // Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
    10.         [HideInInspector] _Color("Tint", Color) = (1,1,1,1)
    11.         [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
    12.         [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
    13.         [HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
    14.         [HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
    15.     }
    16.  
    17.     HLSLINCLUDE
    18.     #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    19.     ENDHLSL
    20.  
    21.     SubShader
    22.     {
    23.         Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
    24.  
    25.         Blend SrcAlpha OneMinusSrcAlpha
    26.         Cull Off
    27.         ZWrite Off
    28.  
    29.         Pass
    30.         {
    31.             Tags { "LightMode" = "Universal2D" }
    32.             HLSLPROGRAM
    33.             #pragma prefer_hlslcc gles
    34.             #pragma vertex CombinedShapeLightVertex
    35.             #pragma fragment CombinedShapeLightFragment
    36.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
    37.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
    38.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
    39.             #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
    40.  
    41.             struct Attributes
    42.             {
    43.                 float3 positionOS   : POSITION;
    44.                 float4 color        : COLOR;
    45.                 float2  uv           : TEXCOORD0;
    46.             };
    47.  
    48.             struct Varyings
    49.             {
    50.                 float4  positionCS  : SV_POSITION;
    51.                 float4  color       : COLOR;
    52.                 float2    uv          : TEXCOORD0;
    53.                 float2    lightingUV  : TEXCOORD1;
    54.             };
    55.  
    56.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
    57.  
    58.             TEXTURE2D(_MainTex);
    59.             SAMPLER(sampler_MainTex);
    60.             TEXTURE2D(_MaskTex);
    61.             SAMPLER(sampler_MaskTex);
    62.             TEXTURE2D(_NormalMap);
    63.             SAMPLER(sampler_NormalMap);
    64.             half4 _MainTex_ST;
    65.             half4 _NormalMap_ST;
    66.  
    67.             #if USE_SHAPE_LIGHT_TYPE_0
    68.             SHAPE_LIGHT(0)
    69.             #endif
    70.  
    71.             #if USE_SHAPE_LIGHT_TYPE_1
    72.             SHAPE_LIGHT(1)
    73.             #endif
    74.  
    75.             #if USE_SHAPE_LIGHT_TYPE_2
    76.             SHAPE_LIGHT(2)
    77.             #endif
    78.  
    79.             #if USE_SHAPE_LIGHT_TYPE_3
    80.             SHAPE_LIGHT(3)
    81.             #endif
    82.  
    83.             Varyings CombinedShapeLightVertex(Attributes v)
    84.             {
    85.                 Varyings o = (Varyings)0;
    86.  
    87.                 o.positionCS = TransformObjectToHClip(v.positionOS);
    88.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    89.                 float4 clipVertex = o.positionCS / o.positionCS.w;
    90.                 o.lightingUV = ComputeScreenPos(clipVertex).xy;
    91.                 o.color = v.color;
    92.                 return o;
    93.             }
    94.  
    95.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
    96.  
    97.             half4 CombinedShapeLightFragment(Varyings i) : SV_Target
    98.             {
    99.                 half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    100.                 half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);
    101.  
    102.                 return CombinedShapeLightShared(main, mask, i.lightingUV);
    103.             }
    104.             ENDHLSL
    105.         }
    106.  
    107.         Pass
    108.         {
    109.             Tags { "LightMode" = "NormalsRendering"}
    110.             HLSLPROGRAM
    111.             #pragma prefer_hlslcc gles
    112.             #pragma vertex NormalsRenderingVertex
    113.             #pragma fragment NormalsRenderingFragment
    114.  
    115.             struct Attributes
    116.             {
    117.                 float3 positionOS   : POSITION;
    118.                 float4 color        : COLOR;
    119.                 float2 uv            : TEXCOORD0;
    120.                 float4 tangent      : TANGENT;
    121.             };
    122.  
    123.             struct Varyings
    124.             {
    125.                 float4  positionCS        : SV_POSITION;
    126.                 float4  color            : COLOR;
    127.                 float2    uv                : TEXCOORD0;
    128.                 float3  normalWS        : TEXCOORD1;
    129.                 float3  tangentWS        : TEXCOORD2;
    130.                 float3  bitangentWS        : TEXCOORD3;
    131.             };
    132.  
    133.             TEXTURE2D(_MainTex);
    134.             SAMPLER(sampler_MainTex);
    135.             TEXTURE2D(_NormalMap);
    136.             SAMPLER(sampler_NormalMap);
    137.             float4 _NormalMap_ST;  // Is this the right way to do this?
    138.  
    139.             Varyings NormalsRenderingVertex(Attributes attributes)
    140.             {
    141.                 Varyings o = (Varyings)0;
    142.  
    143.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    144.                 o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
    145.                 o.uv = attributes.uv;
    146.                 o.color = attributes.color;
    147.                 o.normalWS = TransformObjectToWorldDir(float3(0, 0, -1));
    148.                 o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
    149.                 o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
    150.                 return o;
    151.             }
    152.  
    153.             #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"
    154.  
    155.             float4 NormalsRenderingFragment(Varyings i) : SV_Target
    156.             {
    157.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    158.                 float3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
    159.                 return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
    160.             }
    161.             ENDHLSL
    162.         }
    163.         Pass
    164.         {
    165.             Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}
    166.  
    167.             HLSLPROGRAM
    168.             #pragma prefer_hlslcc gles
    169.             #pragma vertex UnlitVertex
    170.             #pragma fragment UnlitFragment
    171.  
    172.             struct Attributes
    173.             {
    174.                 float3 positionOS   : POSITION;
    175.                 float4 color        : COLOR;
    176.                 float2 uv            : TEXCOORD0;
    177.             };
    178.  
    179.             struct Varyings
    180.             {
    181.                 float4  positionCS        : SV_POSITION;
    182.                 float4  color            : COLOR;
    183.                 float2    uv                : TEXCOORD0;
    184.             };
    185.  
    186.             TEXTURE2D(_MainTex);
    187.             SAMPLER(sampler_MainTex);
    188.             float4 _MainTex_ST;
    189.  
    190.             Varyings UnlitVertex(Attributes attributes)
    191.             {
    192.                 Varyings o = (Varyings)0;
    193.  
    194.                 o.positionCS = TransformObjectToHClip(attributes.positionOS);
    195.                 o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
    196.                 o.uv = attributes.uv;
    197.                 o.color = attributes.color;
    198.                 return o;
    199.             }
    200.  
    201.             float4 UnlitFragment(Varyings i) : SV_Target
    202.             {
    203.                 float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    204.                 return mainTex;
    205.             }
    206.             ENDHLSL
    207.         }
    208.     }
    209.  
    210.     Fallback "Sprites/Default"
    211. }
    I would appreciate if anyone could provide me with some guidance on how to get the "BlendOp Max" working in my shader to stop the overlapping lights intensity from increasing!

    Thanks!
     
    Last edited: Feb 12, 2020
  2. Davidjsap

    Davidjsap

    Joined:
    Aug 17, 2017
    Posts:
    11
    Bumping this, as I know I'm also having this issue and can't find any solid resolution online
     
  3. sk7725

    sk7725

    Joined:
    Dec 11, 2021
    Posts:
    4
    Sorry to necro the post, but I have found the answer - add BlendOP Max to the shader
    Packages/com.unity.render-pipelines.universal/Shaders/2D/Light2D.shader
    .

    It should look like this:

    Code (CSharp):
    1. Shader "Hidden/Light2D"
    2. {
    3.     Properties
    4.     {
    5.         [HideInInspector] _SrcBlend("__src", Float) = 1.0
    6.         [HideInInspector] _DstBlend("__dst", Float) = 0.0
    7.         [Enum(UnityEngine.Rendering.CompareFunction)] _HandleZTest("_HandleZTest", Int) = 4
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags { "Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
    13.  
    14.         Pass
    15.         {
    16.             Blend[_SrcBlend][_DstBlend]
    17.             ZWrite Off
    18.             Cull Off
    19.             BlendOp Max //ADDED CODE HERE
    20.  
    Do note that the URP package is immutable and thus you will need to store the edit somewhere and make URP reference the modified Light2D shader. I will update the post once I figure this out.

    BEFORE:
    upload_2024-3-12_14-54-52.png

    AFTER:
    upload_2024-3-12_14-55-50.png
     
  4. sk7725

    sk7725

    Joined:
    Dec 11, 2021
    Posts:
    4
    Update - You can follow https://support.unity.com/hc/en-us/...uilt-in-packages#h_01GC8VVVEMSWBZ3G3QKF80K4YH to force the URP package to be mutable.