- Home /
Cull shader
How can I make a shader that doesn't render itself if the distance from the camera is greater than something, here is what I have in the subshader for now:
SubShader
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
AlphaTest Greater .01
LOD 200
CGPROGRAM
#pragma surface surf Lambert vertex:vert
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float4 _Wind;
uniform fixed4 _Color;
uniform float _CullDistance;
struct Input {
float2 uv_MainTex;
};
void vert(inout appdata_full v)
{
v.vertex.z += (sin(_Wind.x )/5.0) * v.color.r;
v.vertex.x += (sin(_Wind.y )/5.0) * v.color.r;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 difTex = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = difTex.rgb * _Color.rgb;
o.Alpha = difTex.a;
}
ENDCG
}
Answer by joelv · Mar 31, 2015 at 11:24 AM
If you want to do it on a per fragment level you can use the clip function which discards fragments that does not match a condition.
clip(threshold - depth);
will discard pixels where depth is greater than the threshold (value to clip is less than 0).
How can I use this in a surface shader?
$$anonymous$$arkus
As the surface shader is part of the fragment shader you can clip from within there.
clip(difTex.r - 0.5f);
works in your example shader for example.
Your answer

Follow this Question
Related Questions
How to force the compilation of a shader in Unity? 5 Answers
TEXCOORD 0 1 2 3 for cg shader scripting? 1 Answer
Multiple tex2D commands in fragment shader needed, is there a workaround? 0 Answers
Custom ambient lighting color in a Cg shader 2 Answers
Can I read `CGPROGRAM` `#define` constants in `.cs` at runtime? 0 Answers