Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sprite Bumped Diffuse wrong Light direction

Discussion in 'Shaders' started by Chisokuni, Jun 9, 2014.

  1. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    Hi,

    Im really new to writing shaders and i have a strange problem with my Bumped Diffuse Shader for Sprites. Sometimes the Shaders seems to be "stuck" and doesnt get the right direction of the Directional Light in my Scene. Strangely this only happens to some object and some are normal but all are using the same Material/Shader.

    In the following screenshots the Directional Light is coming from the Left:
    Screenshot2-2014-6-9-13-16 - Kopie.png

    When Rotating the Object the wrong light direction of the False objects seems to be rotating with it:
    Screenshot3-2014-6-9-13-16 - Kopie.png

    The other Thing is that when there is a pointlight goint over These wrong object they are shortly showing the right directional light direction, but switching back to the wrong as soon as the pointlight is gone:
    Screenshot5-2014-6-9-13-16 - Kopie.png


    the other Thing, but maybe unrelated is that when i put an normal map to the shader it has the y axes reversed. im Fixing this by Setting the y-tiling to -1 and the ofset so 1.
    The Normal maps are created from Unity by Greyscale hightmaps.

    heres the shader:
    Code (JavaScript):
    1. Shader "Sprites/Bumped Diffuse"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _BumpMap ("Normalmap", 2D) = "bump" {}
    7.         _Color ("Tint", Color) = (1,1,1,1)
    8.         [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
    9.                 _Cutoff ("Alpha Cutoff", Range (0,1)) = 0.5
    10.  
    11.     }
    12.  
    13.     SubShader
    14.     {
    15.         Tags
    16.         {
    17.             "Queue"="Transparent"
    18.             "IgnoreProjector"="True"
    19.             "RenderType"="TransparentCutOut"
    20.             "PreviewType"="Plane"
    21.             "CanUseSpriteAtlas"="True"
    22.          
    23.         }
    24.             LOD 300
    25.  
    26.  
    27.         Cull Off
    28.         Lighting On
    29.         ZWrite Off
    30.         Fog { Mode Off }
    31.      
    32.  
    33.         CGPROGRAM
    34.         #pragma surface surf Lambert alpha vertex:vert alphatest:_Cutoff
    35.         #pragma multi_compile DUMMY PIXELSNAP_ON
    36.  
    37.         sampler2D _MainTex;
    38.         sampler2D _BumpMap;
    39.         fixed4 _Color;
    40.  
    41.         struct Input
    42.         {
    43.             float2 uv_MainTex;
    44.             float2 uv_BumpMap;
    45.             fixed4 color;
    46.         };
    47.      
    48.         void vert (inout appdata_full v, out Input o)
    49.         {
    50.             #if defined(PIXELSNAP_ON) && !defined(SHADER_API_FLASH)
    51.             v.vertex = UnityPixelSnap (v.vertex);
    52.             #endif
    53.             v.normal = float3(0,0,-1);
    54.             v.tangent =  float4(1, 0, 0, 1);
    55.          
    56.             UNITY_INITIALIZE_OUTPUT(Input, o);
    57.             o.color = _Color;
    58.         }
    59.  
    60.         void surf (Input IN, inout SurfaceOutput o)
    61.         {
    62.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
    63.             o.Albedo = c.rgb;
    64.             o.Alpha = c.a;
    65.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    66.         }
    67.         ENDCG
    68.     }
    69.  
    70. //Fallback "Transparent/Cutout/Diffuse"
    71. }
    72.  

    has anyone encountert that Problem and/or has a sollution?
     
  2. HolyShovel

    HolyShovel

    Joined:
    Jun 19, 2013
    Posts:
    156
    I think that your problem in vert. Try not set the v.tangent and v.normal.
     
  3. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    if if do that the sprite doens't get any lighting at all :/
     
  4. HolyShovel

    HolyShovel

    Joined:
    Jun 19, 2013
    Posts:
    156
    Ok. I'll try. And without
    v.tangent= float4(1,0,0,1);
    all works fine.
     
  5. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    Without v.tangent = float4(1,0,0,1); my Szene Looks like this:
    Screenshot1-2014-6-10-11-21.png

    Same Lighting, same Normalmap as before.
     
  6. HolyShovel

    HolyShovel

    Joined:
    Jun 19, 2013
    Posts:
    156
    Send me an example scene. Maybe I can understand what happened with lighting.
    My e-mail andreich.ne@gmail.com
     
  7. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    Okay while creating the test Szene i noticed something. This error only occures if i instantiate the same prefab twice. If i instantiate the grey, then the green and then the grey again there is no false lighting. So it must have something to do with my instantiation Prozess and the shader is okay right? Looking into that right now..
     
  8. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    Sounds like it's batching together. That would screw up the tangent vector (which is only correct for UVs running exactly vertical/horizontal and a normal pointing straight outwards).
     
  9. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    Okay now its getting crazy. I made this test Scene now and when i pull 2 of the same prefab into the Szene the light seems to rotate. if i delete one of the objects it turns to normal. anyone who would like to test it just pull a "Bridge" gameobject from the prefab into the Scene or delete a "Turret" gameobject.
     

    Attached Files:

  10. HolyShovel

    HolyShovel

    Joined:
    Jun 19, 2013
    Posts:
    156
    Ok. I have result:
    Снимок.PNG
     

    Attached Files:

  11. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    Hm,

    same scene, i get this one:
    ShaderTest.png

    am i missing some options? somethings odly wrong..
     
  12. HolyShovel

    HolyShovel

    Joined:
    Jun 19, 2013
    Posts:
    156
    Hm... it's intresting... very strange, but intresting!
    What kind of unity? Pro, indie? Maybe different versions?
     
    Last edited: Jun 11, 2014
  13. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    I habe the indie 4.3.4f1
     
  14. HolyShovel

    HolyShovel

    Joined:
    Jun 19, 2013
    Posts:
    156
    Indie 4.5.0f, try update your unity...
     
  15. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    Works fine with 4.5.0f6 now.

    Thank you for your help.
     
  16. HolyShovel

    HolyShovel

    Joined:
    Jun 19, 2013
    Posts:
    156
    You're welcome! =)
     
  17. Chisokuni

    Chisokuni

    Joined:
    Apr 30, 2014
    Posts:
    28
    "- SpriteRenderer will now output normals and tangents if the active material require them."

    there it is in the 4.5 release notes ^^