Shader on 3D model not seen over 2D sprites

Hello!!

I have a question about shader coding.
I’m only begginer in this thing, so anyone can tell me is it possible to 3D models with alpha shders to be seen over 2D sprites?
Example:
There is a sphere on scene and beyond it some sprites
while in sphere shader setting are
#pragma surface surf Lambert
it seen ok

but I need it to be transparent, so i change shader to
#pragma surface surf Lambert alpha
and then I got this:

my sphere is visible only over free space. Looks like it rendering behind all sprites now.
Is there a way to render it as it does on first pic, but with alpha transparency on?

Shader is included. Alpha on line 22-23

2918596–215450–RimEffect.shader (1.13 KB)


If you take a look at the Unity sprite shader (by downloading unity shaders here) you will see this:

        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }
        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask [_ColorMask]

This mean Sprite are in the Transparent Queue (3000 like your shader but it doesn’t write depth values into the Z-buffer (ZWrite Off). This basically mean it can be renderer in “front of” or “behind of” but not cut in half by a shape or it cannot cut a shape in half.
Anyway, on a sprite, the parameter “Order in Layer” will make an offset on the Transparent Queue. Either you reduce the Order in layer, either you give an higher queue value to your Rim Shader (Into the Material Inspector itself: “Render Queue”.

[Edit]
It only works if your sprite is on Sorting layer “default” and have 0 or less than 0 in order layer…
It seems you have to setup a Sorting layer and ID into your Mesh Renderer in C#:

Can you help me to do it?
I cant make it work. Ive changed layers, trying to turn on ZWrite on sprite shaders (they all are custom). Tried to change Queue by setting it to “Transparent(±)100” in shader code… Havent found where to chage it at material. Ill give you all source code that you`ll need, and can estable connection by any program.
Really need this kind of shader…
For futher… Is there an any way to mix zDepth of 2D and 3D?
For exaple if 2D sprite is in the middle of the 3D sphere, back side of sphere will be beyond sprite?

PS: Very new to shaders, gracefull for help. Please, if you can, help.

1 Like

Sorry, I don’t really have time to do it for you…
As I said, you just have to change the sorting layer and ID on your 3D Mesh in C# and use the shader you pasted in your topic (transparent version).

Renderer r = GetComponent<Renderer>();
r.sortingLayerName = "LayerName";
r.sortingOrder = 0;

An opaque sphere can contains a sprite but not if it’s transparent.
The sprite will either be in front or behind depending of the sortingLayer and sortingOrder.

Anyway, I won’t recommend mixing both if you don’t understand what’s going on with your Z-Buffer here.

Unity 2D and Sprite is not really 2D. It’s a 3D quad with a material that ignore depth.

1 Like

Thank you a lot!
Changing sortingLayer via script do help me make sphere visible.

And about that:

To understand whats going on and how to do something with it, I need to try, to fall, to reserch and then I`ll be able to do smth I wanted to. =)

Anyway, as I said, Thank you. For now I`ve got what I was needed.

2919469--215566--wAlphaInFrontOfWall.png

You are right!

If you are interested into shader, here are some link you should study/keep

What’s a z-buffer: https://en.wikipedia.org/wiki/Z-buffering
How GPU works: http://www.cs.virginia.edu/kim/courses/cs3330/notes/HowGPUsWork.pdf
Grab Z-buffer and use it into a shader: https://chrismflynn.wordpress.com/2012/09/06/fun-with-shaders-and-the-depth-buffer/
Built-in CGFX functions (usable in shaders code): http://http.developer.nvidia.com/CgTutorial/cg_tutorial_appendix_e.html
Shaders explained in Unity: https://unity3d.com/fr/learn/tutorials/topics/graphics/gentle-introduction-shaders

Your shader is a Surface Shader, a Unity creation hidding what a shader really is. If you want to learn more about this, check the last link.

Thanks a lot.
I`ll read it for sure.

Hi I was having the same problem as this.
I’m also new to shaders and couldn’t make the code work.