Search Unity

how to sort a light in 2d lwrp render? like Graveyard Keeper

Discussion in '2D' started by luoweihong, Apr 12, 2020.

  1. luoweihong

    luoweihong

    Joined:
    May 27, 2019
    Posts:
    56



    i use the orthgraphic camera to create my 2d game, i am struggle in this effect.
    in my view, i want to get the pointLight pos in shader (sprite-lit-default), but i am noob in shader, how to get the point light pos in this bulit - in shader? wait for your help
     
  2. luoweihong

    luoweihong

    Joined:
    May 27, 2019
    Posts:
    56

    Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
    {
    Properties
    {
    _MainTex("Diffuse", 2D) = "white" {}
    _MaskTex("Mask", 2D) = "white" {}
    _NormalMap("Normal Map", 2D) = "bump" {}
    _test("_test", Float) = 2
    // Legacy properties. They're here so that materials using this shader can gracefully fallback to the legacy sprite shader.
    [HideInInspector] _Color("Tint", Color) = (1,1,1,1)
    [HideInInspector] _RendererColor("RendererColor", Color) = (1,1,1,1)
    [HideInInspector] _Flip("Flip", Vector) = (1,1,1,1)
    [HideInInspector] _AlphaTex("External Alpha", 2D) = "white" {}
    [HideInInspector] _EnableExternalAlpha("Enable External Alpha", Float) = 0
    }

    HLSLINCLUDE
    #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    ENDHLSL

    SubShader
    {
    Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }

    Blend SrcAlpha OneMinusSrcAlpha
    Cull Off
    ZWrite Off

    Pass
    {
    Tags { "LightMode" = "Universal2D" }
    HLSLPROGRAM
    #pragma prefer_hlslcc gles
    #pragma vertex CombinedShapeLightVertex
    #pragma fragment CombinedShapeLightFragment
    #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
    #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
    #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
    #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __

    struct Attributes
    {
    float3 positionOS : POSITION;
    float4 color : COLOR;
    float2 uv : TEXCOORD0;
    float2 uvLM : TEXCOORD1;
    };

    struct Varyings
    {
    float4 positionCS : SV_POSITION;
    float4 color : COLOR;
    float2 uv : TEXCOORD0;
    float2 lightingUV : TEXCOORD1;
    };
    #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"

    TEXTURE2D(_MainTex);
    SAMPLER(sampler_MainTex);
    TEXTURE2D(_MaskTex);
    SAMPLER(sampler_MaskTex);
    TEXTURE2D(_NormalMap);
    SAMPLER(sampler_NormalMap);
    half4 _MainTex_ST;
    half4 _NormalMap_ST;

    #if USE_SHAPE_LIGHT_TYPE_0
    SHAPE_LIGHT(0)
    #endif

    #if USE_SHAPE_LIGHT_TYPE_1
    SHAPE_LIGHT(1)
    #endif

    #if USE_SHAPE_LIGHT_TYPE_2
    SHAPE_LIGHT(2)
    #endif

    #if USE_SHAPE_LIGHT_TYPE_3
    SHAPE_LIGHT(3)
    #endif
    float _test = 0.5;
    Varyings CombinedShapeLightVertex(Attributes v)
    {
    Varyings o = (Varyings)0;

    o.positionCS = TransformObjectToHClip(v.positionOS);
    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    float4 clipVertex = o.positionCS / o.positionCS.w;
    float3 worldPos = TransformObjectToWorld(v.positionOS);
    float4 lightPos = _AdditionalLightsPosition[2];
    o.lightingUV = ComputeScreenPos(clipVertex).xy;
    o.color = v.color;
    return o;
    }

    #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"

    half4 CombinedShapeLightFragment(Varyings i) : SV_Target
    {
    half4 main = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    half4 mask = SAMPLE_TEXTURE2D(_MaskTex, sampler_MaskTex, i.uv);

    return CombinedShapeLightShared(main, mask, i.lightingUV);
    }
    ENDHLSL
    }

    Pass
    {
    Tags { "LightMode" = "NormalsRendering"}
    HLSLPROGRAM
    #pragma prefer_hlslcc gles
    #pragma vertex NormalsRenderingVertex
    #pragma fragment NormalsRenderingFragment

    struct Attributes
    {
    float3 positionOS : POSITION;
    float4 color : COLOR;
    float2 uv : TEXCOORD0;
    float4 tangent : TANGENT;
    };

    struct Varyings
    {
    float4 positionCS : SV_POSITION;
    float4 color : COLOR;
    float2 uv : TEXCOORD0;
    float3 normalWS : TEXCOORD1;
    float3 tangentWS : TEXCOORD2;
    float3 bitangentWS : TEXCOORD3;
    };

    TEXTURE2D(_MainTex);
    SAMPLER(sampler_MainTex);
    TEXTURE2D(_NormalMap);
    SAMPLER(sampler_NormalMap);
    float4 _NormalMap_ST; // Is this the right way to do this?

    Varyings NormalsRenderingVertex(Attributes attributes)
    {
    Varyings o = (Varyings)0;

    o.positionCS = TransformObjectToHClip(attributes.positionOS);
    o.uv = TRANSFORM_TEX(attributes.uv, _NormalMap);
    o.uv = attributes.uv;
    o.color = attributes.color;
    o.normalWS = TransformObjectToWorldDir(float3(0, 0, -1));
    o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
    o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
    return o;
    }

    #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/NormalsRenderingShared.hlsl"

    float4 NormalsRenderingFragment(Varyings i) : SV_Target
    {
    float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    float3 normalTS = UnpackNormal(SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, i.uv));
    return NormalsRenderingShared(mainTex, normalTS, i.tangentWS.xyz, i.bitangentWS.xyz, i.normalWS.xyz);
    }
    ENDHLSL
    }
    Pass
    {
    Tags { "LightMode" = "UniversalForward" "Queue"="Transparent" "RenderType"="Transparent"}

    HLSLPROGRAM
    #pragma prefer_hlslcc gles
    #pragma vertex UnlitVertex
    #pragma fragment UnlitFragment

    struct Attributes
    {
    float3 positionOS : POSITION;
    float4 color : COLOR;
    float2 uv : TEXCOORD0;
    };

    struct Varyings
    {
    float4 positionCS : SV_POSITION;
    float4 color : COLOR;
    float2 uv : TEXCOORD0;
    };

    TEXTURE2D(_MainTex);
    SAMPLER(sampler_MainTex);
    float4 _MainTex_ST;

    Varyings UnlitVertex(Attributes attributes)
    {
    Varyings o = (Varyings)0;

    o.positionCS = TransformObjectToHClip(attributes.positionOS);
    o.uv = TRANSFORM_TEX(attributes.uv, _MainTex);
    o.uv = attributes.uv;
    o.color = attributes.color;
    return o;
    }

    float4 UnlitFragment(Varyings i) : SV_Target
    {
    float4 mainTex = i.color * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
    return mainTex;
    }
    ENDHLSL
    }
    }

    Fallback "Sprites/Default"
    }






    I give a shot in this vert fucntion, let the gameobject wordpos.z > 2 , then lights up this gameobject, but how to get the lightPoint worldPosition ?


    Varyings CombinedShapeLightVertex(Attributes v)
    {
    Varyings o = (Varyings)0;

    o.positionCS = TransformObjectToHClip(v.positionOS);
    o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    float4 clipVertex = o.positionCS / o.positionCS.w;
    float3 worldPos = TransformObjectToWorld(v.positionOS);
    float4 lightPos = _AdditionalLightsPosition[2];
    // my attemp if worldPos.z > 2 then light up this object
    if (worldPos.z > 2)
    {
    o.lightingUV = ComputeScreenPos(clipVertex).xy;
    }

    o.color = v.color;
    return o;
    }
     

    Attached Files:

  3. luoweihong

    luoweihong

    Joined:
    May 27, 2019
    Posts:
    56
    any body ?
     
  4. toreman

    toreman

    Joined:
    Oct 1, 2016
    Posts:
    7
    @luoweihong did you manage to do this?

    Im also looking to do something like this.
     
  5. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
  6. toreman

    toreman

    Joined:
    Oct 1, 2016
    Posts:
    7
    They write:

    "I solved this problem very easily. There is a shader that calculates the distance between a light source and a sprite on the vertical axis. If it’s positive (the light source is in front of the sprite), we illuminate the sprite like we always do, but if it’s negative (the sprite is blocking the light source), then the intensity of lighting is fading depending on the distance at a very quick rate. There is a rate, not just “not to illuminate”. So if the light source behind the sprite is moving, the sprite is blackening gradually, not at once. Yet it’s still very fast."

    It is not very complicated, it is just a matter of getting the light position. Which I dont know how to do using the LightMode = Universal2D...
     
  7. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    well as you concluded yourself, just because you start the sentence with "I solved this problem very easily" doesn't mean that people who dont know how will be able to do the same