Search Unity

Lighting Sprites uniformly from behind

Discussion in 'Shaders' started by coding_kyle, May 29, 2019.

  1. coding_kyle

    coding_kyle

    Joined:
    Oct 5, 2015
    Posts:
    2
    I'm trying to get sprites lit from behind to look the same as if the light was in front. This is for a sprite based 3d isometric game.
    Here's how it looks with the light from behind:
    light_behind_player.png

    And in front:
    light_in_front_player.png

    I'm using the deferred rendering path and I tried to adjust the normal for the sprite to always face the light direction in an attempt to get this working, but it makes the light no longer lite up anything that is above it. Only parts of the sprite below the lights world position get any lighting.

    Here's the shader:
    Code (CSharp):
    1. Shader "Custom/Uniform Lighting Z Sorting"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _Color ("Color", Color) = (1,1,1,1)
    6.         [PerRendererData] _MainTex ("Albedo (RGB)", 2D) = "white" {}
    7.         [PerRendererData] _Emission ("Emission", 2D) = "black" {}
    8.         [PerRendererData] _EmissionFactor ("Emission Factor", Float) = 1.0
    9.         [PerRendererData] _EmissionTint ("Emission Tint", Color) = (1,1,1,1)
    10.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    11.         _Metallic ("Metallic", Range(0,1)) = 0.0
    12.         _Cutoff ("Alpha Cutoff", Range(0,1)) = 0.1
    13.     }
    14.     SubShader
    15.     {
    16.         Tags { "Queue"="AlphaTest" "RenderType"="TransparentCutout" "LightMode"="Deferred" }
    17.  
    18.         LOD 200
    19.         ZWrite On
    20.         ZTest LEqual
    21.         Cull Off
    22.                
    23.         CGPROGRAM
    24.         // Physically based Standard lighting model, and enable shadows on all light types
    25.         #pragma surface surf CustomGI alphatest:_Cutoff nometa
    26.  
    27.         // Use shader model 3.0 target, to get nicer looking lighting
    28.         #pragma target 3.0
    29.  
    30.         #include "UnityPBSLighting.cginc"
    31.  
    32.         sampler2D _MainTex;
    33.         sampler2D _Emission;
    34.  
    35.         struct Input
    36.         {
    37.             float2 uv_MainTex;
    38.             float vertexFace : VFACE;
    39.         };
    40.  
    41.         half _Glossiness;
    42.         half _Metallic;
    43.         fixed4 _Color;
    44.         half _EmissionFactor;
    45.         fixed4 _EmissionTint;
    46.  
    47.         // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
    48.         // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
    49.         // #pragma instancing_options assumeuniformscaling
    50.         UNITY_INSTANCING_BUFFER_START(Props)
    51.             // put more per-instance properties here
    52.         UNITY_INSTANCING_BUFFER_END(Props)
    53.  
    54.         inline void LightingCustomGI_GI(
    55.             SurfaceOutputStandard s,
    56.             UnityGIInput data,
    57.             inout UnityGI gi)
    58.         {
    59.             LightingStandard_GI(s, data, gi);
    60.         }
    61.  
    62.         inline half4 LightingCustomGI_Deferred(SurfaceOutputStandard s, float3 viewDir, UnityGI gi, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2)
    63.         {
    64.             // Set the Normal to be the same as the light direction, so the light will always light the surface, even if the light is behind
    65.             s.Normal = normalize(gi.light.dir);
    66.  
    67.             return LightingStandard_Deferred(s, viewDir, gi, outGBuffer0, outGBuffer1, outGBuffer2);
    68.         }
    69.  
    70.         void surf (Input IN, inout SurfaceOutputStandard o)
    71.         {
    72.             // Albedo comes from a texture tinted by color
    73.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    74.             o.Albedo = c.rgb;
    75.  
    76.             o.Emission = tex2D(_Emission, IN.uv_MainTex) * _EmissionTint * _EmissionFactor;
    77.  
    78.             // Metallic and smoothness come from slider variables
    79.             o.Metallic = _Metallic;
    80.             o.Smoothness = _Glossiness;
    81.             o.Alpha = c.a;
    82.         }
    83.         ENDCG
    84.     }
    85.     FallBack "Diffuse"
    86. }
    87.  
    Any help would be a life saver!
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Won't be perfectly accurate but you can always set the normal straight up.
     
  3. Ishkur

    Ishkur

    Joined:
    Nov 18, 2011
    Posts:
    26
    Could you please show the syntax to be used for pointing the normals up? I'm having trouble finding beginner documentation for shader programming in general.