Search Unity

Lighting Sprites

Discussion in 'Shaders' started by Talon09, Sep 22, 2017.

  1. Talon09

    Talon09

    Joined:
    Aug 19, 2017
    Posts:
    1
    Hey everyone,

    I've created a shader but shader writing is very new to me. My shader allows me to apply normal mapping to sprites to give them a "3d feel", the issue is that as the light passes behind/through the sprite, the sprite is still being lit, (unlike in the diffuse sprite shader) my goal is to be able to allow lights to fully move around the sprites in 3d space and affect the sprite accordingly.

    I've been trying to find any sort of answer for a couple of days and it seems I just don't have the proper terms to be able to find any sort of info on why the light is passing through the sprite (aka which component of the shader script is responsible for it)

    I have a hunch a custom lighting model is needed, but any sort of help/general direction to research would be appreciated.

    Code (CSharp):
    1. Shader "Custom/FancySprite" {
    2.     Properties {
    3.         _Color ("Tint", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo Map", 2D) = "white" {}
    5.         _NormalTex ("Normal Map", 2D) = "white" {}
    6.         //_NormalInt ("Normal Strength", Float) = 1
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "QUEUE"="Transparent" "RenderType"="Transparent" "PreviewType"="Plane" }
    11.        
    12.                    
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert alpha
    15.                        
    16.         struct Input {
    17.             float2 uv_MainTex;
    18.             float2 uv_NormalTex;
    19.         };
    20.        
    21.         half4 _Color;
    22.         sampler2D _MainTex;
    23.         sampler2D _NormalTex;
    24.         //float _NormalInt;
    25.         void surf (Input IN, inout SurfaceOutput o) {
    26.        
    27.         half4 c = tex2D (_MainTex, IN.uv_MainTex);
    28.         o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * _Color;
    29.         o.Normal = UnpackNormal( tex2D(_NormalTex, IN.uv_MainTex));
    30.         o.Alpha = tex2D (_MainTex, IN.uv_MainTex).a;
    31.            
    32. }
    33. ENDCG
    34.     }
    35.     FallBack "Diffuse"
    36. }
    37.  
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    So many samples....

    Code (csharp):
    1.  
    2. half4 c = tex2D(_MainTex, IN.uv_MainTex);
    3. o.Albedo = c.rgb * _Color.rgb;
    4. o.Normal = UnpackNormal( tex2D(_NormalTex, IN.uv_MainTex));
    5. o.Alpha = c.a;
    6.  
    There. A billion times faster. Samples are expensive, always do the smallest number possible.


    To solve your actual problem you need to debug the normals of your sprite. Are you actually using a Sprite Renderer? Or just a quad? Quad would probably do what you want.