Search Unity

4.3 Sprites and Lighting

Discussion in 'Editor & General Support' started by uweenukr, Nov 12, 2013.

  1. uweenukr

    uweenukr

    Joined:
    Jan 17, 2011
    Posts:
    54
    Is it possible to have a sprite respond to point lights with normal maps? I have a Sprite and a Mesh next to each other sharing the same textures and materials. The Mesh shows the new colors from the point lights but the Sprite does not.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The default sprite shader doesn't use lighting. You'd need to use a different shader.

    --Eric
     
  3. uweenukr

    uweenukr

    Joined:
    Jan 17, 2011
    Posts:
    54
    I am currently using: Transparent/Cutout/Bumped Diffuse on both Mesh and Sprite. The Mesh is the only one that responds
     
  4. trevex

    trevex

    Joined:
    Apr 10, 2013
    Posts:
    17
    I got the same problem. I also tried fiddling around with the shader, but without success. The shader works with a mesh though. Is the SpriteRenderer somehow skipping the lights?
     
  5. uweenukr

    uweenukr

    Joined:
    Jan 17, 2011
    Posts:
    54
    So I am not crazy. :)

    Any official words?
     
  6. uweenukr

    uweenukr

    Joined:
    Jan 17, 2011
    Posts:
    54
    Made a bug report this today: Case 575520
     
  7. bryantdrewjones

    bryantdrewjones

    Joined:
    Aug 29, 2011
    Posts:
    147
    I made a similar bug report recently, and this is the response I got back:

    :(
     
    Poolay likes this.
  8. XGundam05

    XGundam05

    Joined:
    Mar 29, 2012
    Posts:
    473
    Well, that's sad to know. Especially with the development of tools like SpriteLamp for painting normal maps. I haven't had a chance to mess with 4.3 yet, but would there be any way to use the 2d animation tools without a SpriteRenderer? I mean, I highly doubt it (it wouldn't really make much sense), but it would be good to confirm.

    I don't know their underlying implementation of SpriteRenderer, but it would be nice if in a future release they'd allow the enabling of dynamic lights.
     
  9. soulburner

    soulburner

    Joined:
    Mar 25, 2012
    Posts:
    169
    I've found this video:



    but I still don't understand how it was made
     
  10. Kencho

    Kencho

    Joined:
    Dec 26, 2011
    Posts:
    14
  11. d_brane

    d_brane

    Joined:
    Jul 1, 2012
    Posts:
    3
    I wrote an experimental shader, to get normal mapping to work for the SpriteRenderer. At first it seemed like it doesn't work, but after a lot of hours searching for bugs and reading documentation for Shaderlab I couldn't find anything wrong. So after a few days apart I did a small test by multiplying the normal value with 2 and seeing it actually works, but it's A LOT more subtle, than when applied to a quad. Now what the exact reason is I couldn't figure out, but I highly suspect it being correlated with the bug, where lighting disappears from the light sprite after the distance from nearest light is > 1 unit. There is probably a bug filed for that, I don't know? But it's easy to see, using the Sprite-Diffuse shader from the built-in package.

    Along comes the code:
    Code (csharp):
    1. Shader "CustomSprite/Sprite-Normal"
    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.     }
    9.  
    10.     SubShader
    11.     {
    12.         Tags
    13.         {
    14.             "Queue"="Transparent"
    15.             "IgnoreProjector"="True"
    16.             "RenderType"="Transparent"
    17.             "PreviewType"="Plane"
    18.             //"CanUseSpriteAtlas"="True"
    19.         }
    20.  
    21.         Cull Off
    22.         Lighting Off
    23.         ZWrite Off
    24.         Fog { Mode Off }
    25.         Blend SrcAlpha OneMinusSrcAlpha
    26.  
    27.         CGPROGRAM
    28.         #pragma surface surf Lambert vertex:vert
    29.  
    30.         sampler2D _MainTex;
    31.         sampler2D _BumpMap;
    32.         fixed4 _Color;
    33.  
    34.         struct Input
    35.         {
    36.             float2 uv_MainTex;
    37.             float2 uv_BumpMap;
    38.             fixed4 color;
    39.         };
    40.        
    41.         void vert (inout appdata_full v, out Input o)
    42.         {
    43.             v.normal = float3(0,0,-1);
    44.             v.tangent = float4(1, 0, 0, -1);
    45.            
    46.             UNITY_INITIALIZE_OUTPUT(Input, o);
    47.             o.color = _Color;
    48.         }
    49.  
    50.         void surf (Input IN, inout SurfaceOutput o)
    51.         {
    52.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    53.             o.Albedo = c.rgb;
    54.             o.Alpha = c.a;
    55.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    56.         }
    57.         ENDCG
    58.     }
    59.  
    60. Fallback "Transparent/VertexLit"
    61. }
    62.  
    If you find anything wrong or a better way to do stuff, please share :)

    EDIT: SpriteAtlas I deactivated, to not get to a situation, where UV maps of the colormap and bumpmap don't allign. I suspect this could lead to S*** not working. Correct me if I'm wrong, didn't test this!
     
    Last edited: Dec 15, 2013
  12. jquave

    jquave

    Joined:
    Oct 3, 2009
    Posts:
    21
    I implemented a simple system for lighting with normal mapped sprites. Is this worth releasing to the asset store?


    http://i.imgur.com/3B2FzND.jpg
     

    Attached Files:

    Last edited: Dec 21, 2013
  13. trevex

    trevex

    Joined:
    Apr 10, 2013
    Posts:
    17
    Depends if it's just normal mapping I doubt it is worth it because it's simple to implement, but if it includes proper depth mapping then maybe.
     
  14. jquave

    jquave

    Joined:
    Oct 3, 2009
    Posts:
    21
    I'm going to add a lot of commonly needed features, anyone interested in getting an email update when it's out can get on the list here:
    http://eepurl.com/LfGNv

    Also taking feature requests.
     
  15. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Thanks for sharing. I found by chance that changing the Z size of the sprite changes the bump height, so bumps are relative to this Z value. This gives a lot of control over bump height including realtime bump height changes just by scaling the Z size.

    :D
     
  16. d_brane

    d_brane

    Joined:
    Jul 1, 2012
    Posts:
    3
    Oh, you are right! I actually had my Sprite scaled along x&y and therefore had such a low contrast :mad: Playing around with the scale values I found something interesting.
    1st: Scaling a Sprite along X/Y-axis, lowers the bump height - so don't forget to adjust the z-scale, if you adjust the other two.
    2nd: (and this is a weird one) - If I set scale to about 5, lighting disappears from Sprite, if distance of light to object > 1 unit. This doesn't happen at all, if scale is left to one. How come?

    2nd is actually a show stopper, since I need all my sprite scaled, after import.
     
    Last edited: Dec 25, 2013
  17. mmourlam

    mmourlam

    Joined:
    Jan 6, 2014
    Posts:
    5
    Just for future searches: Set material on Sprite to Sprite/Diffuse for lighting sprites.
     
  18. Li0nSword

    Li0nSword

    Joined:
    Feb 22, 2014
    Posts:
    24
    I managed to get lighting working with 2D sprites by changing it to sprites-diffuse instead of sprites-default. However when using sprites-diffuse it seems the color of the sprites can no longer be changed. I use the following to change the color of the sprites: GetComponent<SpriteRenderer> ().color = Color.red;

    Anyone know how I can continue to use sprites-diffuse and allow the color to be changed?

    EDIT:

    I figured it out. I changed the code to renderer.material.color = Color.red; and that worked.
     
    Last edited: Mar 30, 2014
  19. Shawn-Halwes

    Shawn-Halwes

    Joined:
    Jul 17, 2013
    Posts:
    51
    Changing the material that way will increase your draw calls and undercut the benefit of the sprites batching. The color property on the SpriteRenderer just modifies the vertex color so you can "fix" the Sprites-Diffuse shader by changing the line:
    o.color = _Color;
    to:
    o.color = v.color * _Color;

    Now when then sprites render you get the same batching as the "default" sprite shader, but individual sprites can change their color.