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

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.

1454955–78920–$CutoutBumpwithShadows.shader (786 Bytes)
1454955–79095–$Sprites-Bumped Diffuse.shader (1.38 KB)

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…

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.

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

Hey Trigve,

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

1512758–85916–$CutoutBump.shader (1.39 KB)

1 Like

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 :slight_smile: ). 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

What do you mean exactly with a custom shadow ? As in, you’re using a projector?

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.

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)

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?

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.

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?

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.

I love you. It works like a charm!!!

These seem to work great :slight_smile: 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.

Glad this is helping people out :slight_smile:

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.

1 Like

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!

1 Like

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.
1587118--95011--$shadows for forum.png

Bad news! Everything just stopped working :frowning: Perhaps the shader is faulty?