I’m completely new to shader programming, so I’ve been trying to put together other shaders to make what I want. Unfortunately, I’ve reached the point where my ignorance is preventing any further progress. There used to be several examples of a Ztest shader (if I’m using this term correctly… a shader that would render objects blocked by other objects) floating around, but I can seem to find any that are 3.5 compatible. I’m not so much looking for charity (though it would be accepted
), because I’d like to learn… but if I wanted to render a single color over the portion of an object that is blocked by other meshes, where would I start? I think I can get the fallback working (that looks very simple). I’m also curious about how I could toggle this effect per mesh… like with a replacement shader.
Any pointers would be appreciated (I’m kind of drowning at this point)…
You have to use
ZTest Greater
to rasterize only those parts that are occluded by other geometry.
You should do this after all other geometry has been rendered, e.g. by specifying
Tags {“Queue” = “Transparent” }
http://unity3d.com/support/documentation/Components/SL-SubshaderTags.html
Hope this helps
OK… I think I actually figured it out. Still not sure how to use a custom renderer/ replacement shader to toggle this… but one step at a time. In case anyone cares:
Shader "Custom/ToonZtest" {
Properties {
_Color ("Main Color", Color) = (0.5,0.5,0.5,1)
_Color2 ("Obscuring Color", Color) = (0.5,0.5,0.5,1)
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_Outline ("Outline width", Range (.002, 0.03)) = .005
_MainTex ("Base (RGB)", 2D) = "white" {}
_Ramp ("Toon Ramp (RGB)", 2D) = "gray" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
UsePass "Toon/Lighted/FORWARD"
UsePass "Toon/Basic Outline/OUTLINE"
Pass {
Tags {"Queue" = "Geometry+1"}
ZTest Greater
Color [_Color2]
}
}
Fallback "Toon/Lighted"
}
Also, I can’t quite figure out how to get the outline to surround the whole object (changing render order simply makes the whole object the outline color). If anyone has a suggestion… I’m game…
What the heck!?!?!
The shader I have above works fine, compiles, creates the effect I want. I close Unity3.5 and reopen it, and the shader doesn’t work correctly (the part of the object behind the other objects is rendered black (the outline color). Compile and it still is wrong. If I delete the shader and the cut and paste the exact same shader text into a new copy… viola, it works again!
Something is wrong with the way that Unity 3.5 is parsing this shader when it boots up. That’s the only explanation I can come up with…
EDIT:
OK, this has something to do with the render order (I think). After restarting Unity, the outline is drawn over the Ztested color, rather than underneath it. If anyone can figure out why this is (and why it only happens when Unity is closed and reopened), I’d be grateful…