- Home /
Render object through terrain
Hey everyone, as I am no good at shader coding, I am compelled to turn to unity answers :D anyway, what I want is just a basic defuse shader that render behind everything except the terrain, which I want the shader to render in front of.
So if anyone could help me, ill be very greatful.
Thanks Tz
Answer by whebert · Mar 09, 2013 at 06:04 PM
Try this. I modified the default diffuse texture and added several tags to accomplish what you need I think. The queue order setting should cause rendering after the terrain and before any geometry, and along with the ZWrite Off tag should insure your normal geometry renders over anything with this shader. I added an Offset to help this shader render over the terrain, even when very close to the terrain surface.
Shader "DiffuseOverTerrain" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
Category {
Offset -2, -2
ZWrite Off
SubShader {
Tags { "RenderType"="Opaque" "Queue" = "Geometry-1" }
LOD 200
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
}
Fallback "Diffuse"
}
Forgot to mention, if you wanted your in-between objects to render over the terrain regardless if they are positioned under it or not, you could add a "ZTest Always" just after ZWrite Off and that'd do the trick.
Answer by Loius · Mar 09, 2013 at 06:14 PM
Just use two cameras. One camera renders only the terrain's layer, and the other camera renders the seen-through things. Give the cameras different depths and a 'don't clear' background, and one will render on top of the other.
problem with that is, it will affect performance won't it?
Everything affects performance. It's simple enough to try it and see for yourself if it's going to be a big hit, but I've used it plenty myself and never had an issue. If your camera draw layers are mutually exclusive (you're not drawing anything twice), the only overhead would be repositioning the viewing area between cameras.
Thanks!! The best and easier answer!! One thing to take on care is that in newer versions of unity you have to use the Clear Flag "Depht only" instead of "don't clear"
Your answer
Follow this Question
Related Questions
Custom Render Queue 0 Answers
render object on top of everything with dynamic shadows (for mobile) 1 Answer
Stumped! I have an engine that chooses what cube is where, but I need a way to render them. 1 Answer
How to generate a smooth spherical world? 1 Answer
Using custom vertex shader for terrain? 0 Answers