- Home /
Simple shader causing random portions of object to disappear at random camera positions
I have the following shader that adds an outline of variable thickness to a flat plane:
Shader "Custom/Lot" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_BorderWidth ("Border Width", Range(0,1)) = 0
}
SubShader {
Tags {
"Queue" = "Transparent"
"RenderType" = "Overlay"
"IgnoreProjector" = "True"
}
Pass {
Tags {
"LightMode" = "Vertex"
}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// my parameters
uniform float4 _Color;
uniform float _BorderWidth;
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 uv : TEXCOORD0;
};
vertexOutput vert(vertexInput v) {
vertexOutput o;
// assign the position (in clip space?)
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = float4(v.texcoord.xy, 0, 0 );
return o;
}
float4 frag(vertexOutput i) : COLOR {
half4 col = _Color;
col.a = 1 - smoothstep(0.0, _BorderWidth, i.uv.x) *
(1 - smoothstep(1 - _BorderWidth, 1.0, i.uv.x)) *
smoothstep(0.0, _BorderWidth, i.uv.y) *
(1 - smoothstep(1 - _BorderWidth, 1.0, i.uv.y));
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
The problem I have is two fold:
In the area I circled red, portions of those objects (plane meshes) are not being rendered. If I move the camera a bit, they will show again, but other planes in that image partially disappear.
In the area I circled blue; that particular plane is moved well above the terrain (while trying to determine if maybe it was Z fighting.). But you'll notice that the far point hugs the road in the distance. The mesh most certainly is not doing that. I'd like to know WHY that happens, because it is an effect I have needed for a while now, and of course I finally get it to happen by accident...
Also, this happens even when close to the camera. It seems dependent on the angle of the camera, not the distance.