Search Unity

Sprites Bumped Diffuse Shader and Transparent Cutout Bumped Diffuse (With Shadows)

Discussion in 'Shaders' started by WrongTarget, Dec 16, 2013.

  1. WrongTarget

    WrongTarget

    Joined:
    Nov 18, 2013
    Posts:
    43
    UPDATE: The Sprites-Bumped Diffuse should now cast shadows properly if it's enabled in the Sprite Gameobject through renderer.castShadows = true and renderer.receiveShadows = true.
    Also normal maps are working better with it now. Cheers

    This is the shader that a friend (Ronan Tumelty) shared with me when I asked him for someway for Sprites in Unity to take normal maps.
    I attempted some modifications so they would cast shadows. I even tried enabling casting shadows for the Spriterenderer in the game object (as suggested per one of the Unity developers).
    So far though, the attempts to cast shadows have been unsuccessful. If anybody has any other suggestion, please post it.

    The second shader is one I wrote for my 2D toolkit sprites. They weren't casting shadows, so I made some simple modifications and they can now take normals and cast shadows.

    Cheers.
     

    Attached Files:

    Last edited: Dec 18, 2013
  2. DigiScot

    DigiScot

    Joined:
    Aug 23, 2013
    Posts:
    27
    Hi WrongTarget, can you explain how you would go about actually using these, I'm adding them to my sprites, but still getting no shadows casting from my lights, I turned it on via code like you said but nothing is happening, I'm probably doing something stupid....
     
  3. WrongTarget

    WrongTarget

    Joined:
    Nov 18, 2013
    Posts:
    43
    Are you by any chance trying to use a point light and deferred lighting with an ortographic camera? Unfortunately, Unity's ortographic rendering doesn't cast shadows with deferred lighting. As soon as you switch to perspective, you'll see your shadows.
    The work around is to use the fullforwardshadows parameter in your shader with a forward rendering instead of deferred. THEN you can use ortographic.
     
  4. Trigve

    Trigve

    Joined:
    Mar 17, 2013
    Posts:
    139
    Hey WrongTarget, I wan to receive shadows in my sprites and found your shader. I've tried to use it but my sprites doesn't receive any shadow. I'm, using orthographic camera, forward rendering with only one directional light. I've tried also "fullforwardshadows" but without success. Don't you where could be the problem?

    Thank You
     
  5. WrongTarget

    WrongTarget

    Joined:
    Nov 18, 2013
    Posts:
    43
    Hey Trigve,

    Thank you for noticing this.
    Try this shader and let me know if it works. :)
     

    Attached Files:

    perracolabs likes this.
  6. Trigve

    Trigve

    Joined:
    Mar 17, 2013
    Posts:
    139
    WrongTarget,
    thanks for reply. I've tried it and now it partially works. I say partially because there are some problems with. So I try to describe the situattion.

    I'm using the 3D space with 2D sprites where Up is Along the Y axis. I don't use bump-map. I don't want to cast shadows only receive it. I make my custom shadows with custom shadow texture and placing it "below" the main sprite using the lower "Order in Layer" in the SpriteRenderer.

    When using the default sprite shader everything is ok but without light of course.

    When using your shader, the main sprite does receive the shadow, but it is now rendered below the "main's shadow" sprite. I think this is because of "Queue"="AlphaTest". But to worse the situation. There is another problem not related to your shader. If I've got the 2 sprites with same "Order in Layer" value and using the SpriteDiffuse shader (or yours) then they are darkened when both of them are shown in the camera (http://answers.unity3d.com/questions/618599/spritesdiffuse-shader-overlap-problem.html) . When only one is shown everything is good (but it is useless :) ). So I've spent some time yesterday and also today to find the issue but gave up.

    So I think I'll fill a bug because I couldn't reliable test if your shader is working ok or not. Even if your shader is correct it could produce results which could be or not correct based on the "bug" in the sprite renderer.

    Hope my post doesn't sound complicated. If yes just ask and I'll try to explain it better.

    Thanks

    Trigve
     
  7. WrongTarget

    WrongTarget

    Joined:
    Nov 18, 2013
    Posts:
    43
    What do you mean exactly with a custom shadow ? As in, you're using a projector?
     
  8. Trigve

    Trigve

    Joined:
    Mar 17, 2013
    Posts:
    139
    With custom shadow I mean I have another sprite in low intensity black of the original sprite, which is shown below the original sprite (below as lower in "Order in layer"). Of course it is a fake shadow.
     
  9. SlySIy

    SlySIy

    Joined:
    Jul 10, 2012
    Posts:
    16
    Hello,

    I found this topic cause I want to receive shadow/cast shadow, and use light on my sprite render.

    It's seems to not working on my sprite render. But I want to be sure to do the thing correctly.
    I'm a little bit newbie with this. So for using your shader I'm not sure how to do it.

    Do I need to create a new material and drag drop your shader on it?

    For example I create a new material, a basic one and set it into the material field into my sprite render. After this, I drag and drop your shader on my new material for apply it to my object. Nothing realy change.
    Is it the correct way to do it?

    Note: I'm using a directional light (type directional)
     
    Last edited: Feb 14, 2014
  10. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Should just be a simple case of dragging and dropping the shader onto the material when its attached to an object.
    Does the material say it is using the shader? It should say it next to the little preview in the inspector, on a drop down list.

    Also, are you supplying correct textures to the object - Both diffuse and normal maps probably?
     
  11. SlySIy

    SlySIy

    Joined:
    Jul 10, 2012
    Posts:
    16
    Oh ok, so I'm doing it right.
    So yes, the material says it's using the shader.
    Following this I attached my custom script for activate the shadow (renderer.receiveShadows = true;
    renderer.castShadows = true;).

    Actually my object is castin shadow, so that's great. But he's not receiving shadow. I don't know what to do for this.

    I attached a picture of my inspector.

    $MaBiteDansMagog.png
     
  12. SlySIy

    SlySIy

    Joined:
    Jul 10, 2012
    Posts:
    16
    I've got a clue about what's going wrong. It's seems to not work with Directionnal Light.
    When I remove it and add a spotlight it's working.
    Is it normal? Or my directionnal light is not set correctly?
     
  13. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Maybe check your settings and see what the pixel light count is set to. If it is quite low, then you will find that only a certain number of lights can affect an object at any given time unless your using deferred rendering techniques.
     
  14. nhimmisha

    nhimmisha

    Joined:
    Nov 26, 2013
    Posts:
    5
    I love you. It works like a charm!!!
     
  15. enhawk

    enhawk

    Joined:
    Aug 22, 2013
    Posts:
    833
    These seem to work great :) I wonder, how would one go about adding another layer to the shader that would let you add features that are non-lit (for example, red light eyes on a terminators skull) that are not effected by lighting.
     
  16. WrongTarget

    WrongTarget

    Joined:
    Nov 18, 2013
    Posts:
    43
    Glad this is helping people out :)
     
  17. Boi

    Boi

    Joined:
    Apr 10, 2010
    Posts:
    65
    Hello,

    I got the shadows working in the scene view but not in the game view. You can see my settings for both camera and light in the two inspector views. What am I doing wrong?

    I use your CutoutBump shader on all Culumns.

    $shadow issue.jpg
     
    IgorUnity3D likes this.
  18. jamescovenant

    jamescovenant

    Joined:
    Apr 1, 2014
    Posts:
    4
    Hello Wrong Target, I'm still having a bit of trouble. Do you think you could make a sample project? that would be the easiest way for me to learn. Thanks for your help so far!
     
    AnnaBarnhart likes this.
  19. jamescovenant

    jamescovenant

    Joined:
    Apr 1, 2014
    Posts:
    4
    I figured it out! Sometimes it helps if you delete your game objects and start over from scratch. When I did this, everything worked perfectly. Here are my results with CutOutBump shader (credit to WrongTarget)1 point light with hard shadows, and a camera in orthographic mode.
    $shadows for forum.png
     
  20. jamescovenant

    jamescovenant

    Joined:
    Apr 1, 2014
    Posts:
    4
    Bad news! Everything just stopped working :( Perhaps the shader is faulty?
     
  21. jamescovenant

    jamescovenant

    Joined:
    Apr 1, 2014
    Posts:
    4
    Sorry to spam this forum! I got everything working once again, but I had to start a new scene. If you are having trouble getting shadows to work in your game, then create a new scene and re-add the desired materials and shaders to your sprites. Feel free to ask me for help,
     
  22. CptKelloggs

    CptKelloggs

    Joined:
    Jun 4, 2014
    Posts:
    1
    Im really new to this and just cant get it to work.

    Can someone provide a very small example project for this? Would be really helpful for me.

    I hope someone can help me. Thanks in advance. :)
     
  23. rattmeeves

    rattmeeves

    Joined:
    Nov 18, 2013
    Posts:
    2
    WrongTarget, thank you so much! This is something I've wanted to do for years, and just the other day it dawned on me that Unity could probably do it no problem. I used to use Game Maker, which was a great starting point, but as soon as you want to do cool things like rigid bodies and ragdolls, it gets way too complicated. Especially graphically. Anyway, I'm so stoked I found this, because it does literally exactly what I want. However, there is one other thing that I tried to do, and it didn't really work. I want to make armor for my character using a reflective bump shader, but as soon as I change the material type to reflective, my guy turns into a blocky disaster (he's made out of jointed sprites). Would it be difficult to make the same type of shader, only reflective?
     
  24. Alex_4x

    Alex_4x

    Joined:
    Feb 2, 2014
    Posts:
    21
    Hello people!
    I trying to use normal map on quad sprite, but have problem
    first i have color sprite

    based on this figure, create a new drawing with depth map (black is backward, white is upper)


    in Unity3D make quad and select shader named Transparent/Bumped specular

    And add one light in scene.

    However, I see no effect "Bump".
    What went wrong?
     
  25. DieHappyGames

    DieHappyGames

    Joined:
    Feb 24, 2014
    Posts:
    44
    Would also appreciate a simple sample project with this working as intended. The shader accepts normal maps great, which is the main thing I'm interested it, but I can't get shadow casting to work at all with either the sprite shader or the cutout...?
     
  26. DieHappyGames

    DieHappyGames

    Joined:
    Feb 24, 2014
    Posts:
    44
    Anyone been able to get this working with animated sprites? In a perfect world, is there a way to have a spritesheet of a given animation and then a spritesheet of the corresponding normals and somehow tell the material to update the normal accordingly every time the sprite renderer advances to the next frame in animation?
     
    RemDust likes this.
  27. eddie312

    eddie312

    Joined:
    Sep 13, 2012
    Posts:
    24
    Hy.

    animated sprites no problems. (im using usual SpriteRenderers)
    if you put the inspector in debug mid you can make the cast and receive shadow on.

    i still have some flikering problem with my shadow because of the ortho camera but its a work in progress.
    this presentation was pretty usfull in my case
     
  28. jmills

    jmills

    Joined:
    Apr 15, 2013
    Posts:
    11
    Has anyone gotten this to work with iOS? I am using these shaders to do pixel shadows and when I switch to mobile, the shadows don't appear. :/
     
  29. AnnaBarnhart

    AnnaBarnhart

    Joined:
    Jan 9, 2015
    Posts:
    2
    I would love to use this, but am not much of a coder. What tips would you give someone who has very little experience coding?

    And thank you very much for sharing!
     
  30. WrongTarget

    WrongTarget

    Joined:
    Nov 18, 2013
    Posts:
    43
    Hey guys,

    I'm surprised this thread has gone on. This is definitely something I wish was documented better.
    These days have been pretty hectic for me, but I'll try setting a sample scene soon.
     
  31. DennisBergkamp

    DennisBergkamp

    Joined:
    Nov 10, 2014
    Posts:
    1
    I would be very interested in this; no matter what I try, I can't seem to get any shadows show up using SpriteRenderers.
     
  32. Drowning-Monkeys

    Drowning-Monkeys

    Joined:
    Mar 6, 2013
    Posts:
    328
    I have no problems with shadows, but there are a few caveats, the one i remember off the top of my head is that if you lightmap, i have to use dual lightmaps to get the shadows to appear. With no lightmaps, shadows appear fine
     
  33. kaelexx95

    kaelexx95

    Joined:
    Jan 27, 2014
    Posts:
    1
    Does anyone have a simple step by step on how to do the sprite shadow?
     
  34. six981

    six981

    Joined:
    Apr 20, 2015
    Posts:
    2
    I'm having an odd issue with this shader. For some reason the shadow is drawn at the wrong place. The box is actually flush against the ground but the shadow makes it appear in the air a bit.


    Has anyone else had this issue?
     
    GreenCubeGames likes this.
  35. kickassqc

    kickassqc

    Joined:
    Jul 7, 2015
    Posts:
    4
    This is the new procedure whit unity 5

    GetComponent<Renderer>().receiveShadows = true;
    GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On ;
     
  36. GreenCubeGames

    GreenCubeGames

    Joined:
    Jan 9, 2015
    Posts:
    19
    im with this problem too, any tip?

    Edit: Got it, its the "Bias" at the spotlight... =D work perfect here, but without recieve shadows =(
     
    Last edited: Jul 9, 2015
  37. DChristy87

    DChristy87

    Joined:
    Jan 23, 2014
    Posts:
    19
    Hello, I'm using the CutoutBump.shader and it's working great with shadows but I'm currently using just a plain white sprite and coloring it with the sprite renderer. However, with this shader it's not reading the sprite renderer color at all. Anyone have any ideas on how to fix this?
     
  38. robin-gallimard

    robin-gallimard

    Joined:
    Feb 2, 2015
    Posts:
    6
    If you reach this page, you might be struggling to get the shader working (like I was today). Here are a few things that I was not aware of (first time working with shadows):

    1. By default, unity disable light shadows. So make sure that in the editor, the "Shadow Type" of your Light gameObject is NOT set to "No Shadows".

    2. Create a script that enables Shadows for your sprite and attach it to your gameobjet:
    void Start()
    {
    GetComponent<Renderer>().receiveShadows = receiveShadow;
    GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
    }

    3. To answer the question above: to change the color of your sprite, change the "Tint" of your material not the color of your sprite.

    4. Edit -> Project Settings -> Quality. Make sure that shadows are enabled in your quality settings.
     
  39. vasyatko

    vasyatko

    Joined:
    Jan 21, 2015
    Posts:
    4
    I am experiencing similar difficulties.
    Please
     
  40. Eingya

    Eingya

    Joined:
    Jan 10, 2014
    Posts:
    8
    Also having this problem, changing the bias on my point light doesn't seem to affect anything
     
    Last edited: Aug 30, 2015
  41. Eingya

    Eingya

    Joined:
    Jan 10, 2014
    Posts:
    8
    this is a picture of the problem
     
  42. Yesitsdave

    Yesitsdave

    Joined:
    Mar 19, 2014
    Posts:
    9
    I'm trying this with the standard shader and ShadowCastingMode set to on, also seeing the shadow position issue - did anyone figure it out?
     
  43. trialforce

    trialforce

    Joined:
    Apr 15, 2012
    Posts:
    22
  44. Ndiaz

    Ndiaz

    Joined:
    Nov 11, 2015
    Posts:
    1
    Hi Guys,

    I, too, would appreciate a sample scene. I'm sure there's some very obvious, rookie mistake I'm making. I'm an intermediate level C# programmer, but a Unity n00b.

    I have a scene set up with an orthographic camera, two lights, and two sprites. One light (a directional light) only lights the background sprite, which should receive shadows but doesn't have to cast them (if I understand those two concepts correctly). The other light (a spotlight) lights the second sprite, which is a placeholder for a structure (really just the pink box sprite from Standard 2D assets). I've attached the shader to both sprites, and also the following script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CastAndReceiveShads : MonoBehaviour {
    5.     // Use this for initialization
    6.     void Start () {
    7.         GetComponent<Renderer>().shadowCastingMode =  UnityEngine.Rendering.ShadowCastingMode.On;
    8.         GetComponent<Renderer>().receiveShadows = true;
    9.     }
    10. }
    I've enabled shadows in the lights and in the quality settings. I've attached photos for the configuration of my scene, and what I see when pressing play in the Editor. Again, I'm sure my mistake is silly, but I'm quite new to this and am honestly lost.
     

    Attached Files:

    Last edited: Nov 12, 2015
  45. RemDust

    RemDust

    Joined:
    Aug 28, 2015
    Posts:
    432
    Hi guys, I tried to use the shadow shader from WrongTarget.
    My normal maps seems to act weird : depending on the height of the light, it works either fine or "reversed" ?!
    on the first image : the light is low on the y axes : the falling of the light is pretty nit !
    Capture d’écran 2015-11-23 à 19.54.26.png Capture d’écran 2015-11-23 à 19.55.13.png
    on the second one : the light is set a little bit higher, and the falling of the light ( so, on the low part of the column) looks reversed (darker in the center) oO

    Any idea about what's going on here ?
     
  46. kernjiro

    kernjiro

    Joined:
    Apr 5, 2014
    Posts:
    3
    Am I the only one that can't get transparency to work?
     
  47. IgorUnity3D

    IgorUnity3D

    Joined:
    Aug 28, 2014
    Posts:
    66
    Hello guys,

    I am trying this shader on Unity 5.4, BUT, I did not succeed :(



    I am using the Shader "Sprites/Bumped Diffuse with Shadows"

    Code (CSharp):
    1. Shader "Sprites/Bumped Diffuse with Shadows"
    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"="AlphaTest"
    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 On
    30.         Fog { Mode Off }
    31.      
    32.  
    33.         CGPROGRAM
    34.         #pragma surface surf Lambert alpha vertex:vert  alphatest:_Cutoff fullforwardshadows
    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.  
    What am I doing wrong? Does this still work in older versions of Unity?

    Could someone please help me?
     
    Last edited: Sep 25, 2016
  48. Airos

    Airos

    Joined:
    Jan 10, 2018
    Posts:
    1
    hi,sorry to interrupted at this time, but i want to know if there is a sample scene ? cause i cant make it and totally dont know how to do, i use your shader and no shadow shows 1.png 2.png 3.png 4.png the shader code is from $CutoutBump.shader
     

    Attached Files:

  49. OG_oliver

    OG_oliver

    Joined:
    Aug 11, 2019
    Posts:
    15
    I've been able to take a few bits of the code from $CutoutBump.shader but I can not make my sprites receive shadows at all. If anyone knows other sources of this issue don't hesitate to show it (is my first time working with shaders). I might help if someone doesn't know how to work with this in the URP.
     
  50. GreenCubeGames

    GreenCubeGames

    Joined:
    Jan 9, 2015
    Posts:
    19