A Unity ID allows you to buy and/or subscribe to Unity products and services, shop in the Asset Store and participate
in the Unity community.
Discussion in '2D' started by WrongTarget, Dec 6, 2013.
Anyone have a clue?
by nature sprites aren't going to cast shadows, because they aren't 3D. The only way to get sprites to cast shadows is to write your own special method for it, because its not something most developers use in 2D games, and instead they rely on artists who just "bake in" the shadows to the artwork...
Sprites are 3D (they're similar to the quad primitive), but the SpriteRenderer component doesn't work with shadows. You can use a quad with a transparent/cutout shader instead.
In what way Eric5h5? Do you mean as in using them instead Unity Sprites at all?
Yes; SpriteRenderer is not used by shadows in Unity, so if you want shadows then as far as I know you can't use SpriteRenderer.
Ok! Thanks. Here's to hoping that'll feature come in some future update.
ooh right I should have clarified the 3d part better, listen to this guy lol
Casting and receiving shadows is disabled on SpriteRenderer by default because it is not a common use case.
Use http://docs.unity3d.com/Documentation/ScriptReference/Renderer-receiveShadows.html and http://docs.unity3d.com/Documentation/ScriptReference/Renderer-castShadows.html to reenable the functionality.
I added a script enabling castShadows and receiveShadows to my Sprite gameobject and it still won't cast any shadow.
Will have to agree here.The following pictures show this with a Sprite Rock I tested. The characters in the picture are using a custom CutoutShader (https://www.assetstore.unity3d.com/#/content/5568) which is incompatible with the Sprite Render. It's there to show the desired behavior, but something I want to move away from, and just use sprites.
Also the Sprite is lit just fine, using the supplied Sprite/Diffuse shader, but reverting this back to the default doesn't help with shadows.
If it's hard to see, the shadow on the right is being cast by the little samurai, not the rock, indeed the samurai's shadow is going *through* the rock. The rock is neither recieving, nor projecting a shadow.
Shadows are not supported by the default sprite shaders. Setting castShadows/receiveShadows to true doesn't change that.
You have several options though, dependent on the desired effect:
1. Duplicate the default sprite diffuse shader and add the surface parameter "addshadows". To get cutout shadows you'll need to change the fallback and introduce the _Cutout material parameter as well.
2. Simply use Quads straight away as @Eric5h5 stated (so using the MeshRenderer instead) and alpha cutout shaders.
3. Use invisible shadow casters (although these might not necessarily interact well with you sprite animations) i.e. create you shadow meshes and use a shader only invoking the vertex lit's shadow collector and caster passes (this technique is helpful if you want "shadow shafts")
Pic of options 1:
And option 3 used for "shadow shafts":
Hey Trevex, thanks for the info!
I'm a complete shader n00b - can you post that modified shader code for option 1, or let me know the syntax for adding the surface parameter, and introducing _Cutout material param - I can probably muddle through this, but if you have the code handy much appreciated
3 looks like a cool option - did you use the 2d volumetric lighting plugin for that or is that something you rolled yourself? Your game looks cool! Been playing a lot of starbound lately, and while not for my current 2d project, at some point I want to take a look at doing something similar to their lighting system.
I'm trying option 1 and for some reason I can't get it to work. Could take a look at my shader and point me in the right direction to solve the problem, please?
I'm curious - how did you get the characters' shadows to project large like that? Your lighting is really nice and would love to know how you did this. I'm only learning now about lighting/shaders etc, and would really appreciate the advice.
Also, anyone have any updates yet on projecting shadows from sprites with Unity 2d? I have the CutoutShader mentioned above, too, but as mentioned, it doesn't work. I'm currently using the sprite>diffuse, but no shadows
You need to actually use the Lit3d cutout shader, if you're using the sprite -> diffuse it's not working - that's the reason why I posted
Hopefully @trevex can drop a bit more mad shader knowledge up in here
Since my shader involves a lot more than just the shadow part here is a short guide on how to enable shadow casting on sprites:
1. Get the unity shader sources, duplicate the Sprite-diffuse.shader and rename it
2. Add the addshadow parameter to the surface shader, i.e. Line 28 will look like this:
#pragma surface surf Lambert alpha addshadow vertex:vert
3. As previously mentioned castShadow needs to be set to true manually
4. You should now be able to see shadows casted by your sprite, although they will be most likely squares.
5. To solve this the shadow caster of the cutout shader needs to be used. This can be done by changing the fallback to it, i.e. Line 60 will look like this:
6. The cutout shader also needs the cutoff value, otherwise you'll still have just squares, so add it to your material uniforms, i.e. add this after Line 7:
_Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.5
7. See your shadows in action!
Knowledge dropped! Going to check this out - will post results. Cheers!
The shader I posted above has all these modifications you suggest, trevex, and it still doesn't work :S
I tried doing it from scratch anyway, and no success.
Please Jesse, let me know if you achieve anything.
Nevermind! Got it work. I wasn't enabling castshadow correctly, rookie mistake.
Works perfectly thank you
I have that cutout shader, but it doesn't work on sprites.
It isn't meant to, I was saying that's what I had used in my picture.
I've since changed to Trevex's method for my sprites.
Edit - hmm I got the sprites to cast shadows - I can't seem to get recieve shadows working on the sprites however
The shader works for me but my shadow is hard (rough), I need soft shadows like yours;
Look at the shadow of the box, smooth.
Having a similar issue. I'd love to hear if the aforementioned solution worked for you guys before I went through the trouble myself.
Any chance someone can explain how to get the default Sprite-Diffuse shader to edit as explained by trevex, or post the touched up shader... and how to use it thereafter.... I'm really new to shaders, they baffle me!
oh and do you need Pro for this to work?
Guys, I posted my shader here already. Check it out and let me know if you have any issues.
trialforce: I think that's simply because your sprite doesn't have smooth edges.
Sorry for digging up an old thread but,
I managed to get the sprites to cast shadows but they can't receive shadows. The only objects that are receiving shadows are the non-sprites. Is it possible to get the sprites to receive shadows?
Go to the object which uses the Sprite Renderer. Over the inspector is a lock icon, click the button to the right of it. A menu will appear, select and click debug. Now if you look back at the Sprite Renderer, you should see options like m_CastShadows and m_RecieveShadows. Change them according to your needs.
It works only for CastShadows. Even if you attach a script
GetComponent<Renderer>().receiveShadows = receiveShadow;
GetComponent<Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On;
sprites do not cast shadows on the other sprites.
There's no way around this?
there is a way around this cause i got it to work...
but im not sure how to explain.
in debug mode i select the casting and recieving on my player gameobject.
the material i used for my player gameobject was a new material with
rendering mode: cutout.
mainmaps/albedo: player spritesheet (none works also)
the material on my level mesh is the same as my player object!
the only difference is the mainmap spritesheet.
i hope this helps a little...
I have one question. With this way (using modified shader) object does not become true 3D object. If I rotate it 180 degrees and rays from light source go to its backside, there is no shadow. The same as with Quad.
Is it possible to fix this issue somehow?
It's a simple fix to get shadows from both directions. Now 'cast shadows' has a drop-bar allowing you to select 'two sided'. This along with everything k-skills said will accomplish the task
Thank you soo much! This help me a lot!!!
I'm in the Universal Render Pipeline and I was able to make the sprites cast shadows but I can't make the receive shadows. I tried with the method above but no shadow is displayed over the sprites. My shader is the one WrongTarget made.