- Home /
Display complex object when it is behind the wall
In my project a player character can stand behind the wall. I want to draw it in front of all objects. An outline or solid color is good for me. I've found a similar topic(How to make an outline of an object behind a wall?) and implemented a simple shader that displays sphere in different colors when it is behind another object.
Shader "SolidColorWithZTest"
{
Properties
{
_Color1 ("Color1", Color) = (1,1,1,1)
_Color2 ("Color2", Color) = (1,1,1,1)
}
SubShader
{
Tags { "Queue" = "Geometry+1" }
Pass
{
ZTest Greater
Color [_Color1]
}
Pass
{
ZTest Less
Color [_Color2]
}
}
}
I tried this approach on complex model such as player character, and got an issue. A solid color "pops-up" from back of model. I'm using character from Third Person Shooter Demo, and here is shader I'm using:
Shader "Texture With ZTest" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {} }
Category
{
SubShader
{
Tags { "Queue"="Overlay+1" }
UsePass "Specular/BASE"
Pass
{
ZTest Less
SetTexture [_MainTex] {combine texture}
}
Pass
{
ZTest Greater
Lighting Off
Color [_Color]
}
}
}
FallBack "Specular", 1
}
What can be done to hide this white artifacts? Any suggestions are acceptable, including non-shader approach.
Answer by Jessy · Sep 02, 2010 at 04:46 PM
Just put the ZTest Greater pass before the ZTest Less pass.
This is logical step. I tried it before asking a question and this did not work.
That's because of your UsePass, which assumedly uses ZTest LEqual. Considering the UsePass is completely useless in this shader, take it out.
Yes, thats it. I must get some exp in shader writing. I changed my character model to one which our modelers created and removed UsePass as you recommended. $$anonymous$$y new character has only one texture and now everything is good. Thanks a lot.
Would you $$anonymous$$d sharing your final shader? I am having similar problems
Thanks
Answer by peacefulshade · Feb 12, 2014 at 03:34 PM
I tested the shader and it almost worked, what was missing was to avoid writing to the depth buffer in the greater pass:
Shader "Texture With ZTest"{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
}
Category
{
SubShader
{
Tags { "Queue"="Overlay+1" }
Pass
{
ZWrite Off
ZTest Greater
Lighting Off
Color [_Color]
}
Pass
{
ZTest Less
SetTexture [_MainTex] {combine texture}
}
}
}
FallBack "Specular", 1
}
This is exactly what I was looking for except it does not support alpha channel transparency for the texture. Anyone know how to do that?
Edit: I fixed it myself with some changes.
SubShader
{
Tags { "Queue"="Overlay+1"
"RenderType"="Transparent"}
Pass
{
ZWrite Off
ZTest Greater
Lighting Off
Color [_Color]
}
Pass
{
Blend SrcAlpha One$$anonymous$$inusSrcAlpha
ZTest Less
SetTexture [_$$anonymous$$ainTex] {combine texture}
}
}
Answer by incredibleHQ · Mar 25, 2015 at 03:23 PM
I dont know whatyou are going to do. I am hacking Unity Games for a while now and i am using a different way!
region Create Material
void CreateWallhackMaterial()
{
if (!WallhackMaterial)
{
WallhackMaterial = new Material("Shader \"Custom/Cham\"{\tSubShader \t{\t\tPass \t\t{\t\t\tZTest Less\t\t\tZWrite On\t\t\tColor (1, 0.92, 0.016, 1) \t\t}\t\tPass \t\t{\t\t\tZTest Greater\t\t\tZWrite Off\t\t\tColor (1,0,0,1)\t\t}\t}}");
WallhackMaterial.hideFlags = HideFlags.HideAndDontSave;
WallhackMaterial.hideFlags = HideFlags.HideAndDontSave;
WallMats= new Material[]
{
WallhackMaterial
WallhackMaterial
WallhackMaterial
WallhackMaterial
WallhackMaterial
WallhackMaterial,
WallhackMaterial,
WallhackMaterial,
WallhackMaterial,
WallhackMaterial,
WallhackMaterial
};
}
}
endregion
call this void every frame (OnGUI()) and loop through the enemys! if u wanna see how that looks https://www.youtube.com/watch?v=x0n8hx63zag
I dont know if that helped but this is working for me ;)
Answer by Kaanin25 · Jul 29, 2014 at 04:32 PM
I tested the shader myself and I found that everything worked except it did not support any sort of alpha channel transparency so I fiddled with it and came up with this.
Shader "Custom/NewShader" {
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
}
Category
{
SubShader
{
Tags { "Queue"="Overlay+1"
"RenderType"="Transparent"}
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
ZTest Greater
Lighting Off
SetTexture [_MainTex] {combine texture}
}
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
ZTest Less
SetTexture [_MainTex] {combine texture}
}
}
}
FallBack "Diffuse"
}
This works great! Could we somehow add a pass that colours the sprite when it's behind another object though?
Answer by Charly75 · Sep 16, 2018 at 02:27 PM
Hey, the Main Color don't change the color of the texture, so I have a Text Mesh with this material, but the text appear black, I want it white and that don't work, how can I make Main Color work or download the font texture to change it color on photoshop ? thx
Your answer
Follow this Question
Related Questions
Ztest shader issues 0 Answers
Display object with subobjects when it's behind the wall 0 Answers
Is there a shader to draw outline of a solid sphere in unity 20.1.0a12 , that works in unity WebGL? 0 Answers
Silhouette Outline Shader and SpriteRenderer 0 Answers
2D Sprite Always Visible - Shader / Material not working (ZTest, ZWrite, Culling) 1 Answer