Hello. I’m very new to shaders and am having trouble trying to achieve a certain effect. I’ve gotten it mostly there but just one thing I don’t know how to do.
I’m using the stencil buffer to achieve a line-of-sight effect. It’s mostly working but the mesh that is being used to render the LOS effect is a solid color and when I lower the alpha the whole effect fades with it. Is there a way I can write data to the LOS mesh about whatever is under it, so it basically becomes invisible?
(There is supposed to be Objects under the “roof” of the building)
Edit: Setting the ColorMask to 0 doesn’t seem to work either. But that may be obvious and I just don’t know it.
Edit 2: If it helps here is the shader code
LOS shader
Shader "Custom/stencil_buffer/write"{
Properties{
_Color("Tint", Color) = (0, 0, 0, 1)
_MainTex("Texture", 2D) = "white" {}
[IntRange] _StencilRef("Stencil Reference Value", Range(0,255)) = 0
}
SubShader{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry-1" "RenderPipeline" = "UniversalRenderPipeline"}
Stencil
{
Ref [_StencilRef]
Comp Always
Pass Replace
Ref 2
Comp Always
Pass Replace
}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Pass{
HLSLPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f {
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
v2f vert(appdata v) {
v2f o;
o.position = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
return o;
}
fixed4 frag(v2f i) : SV_TARGET
{
fixed4 col = tex2D(_MainTex, i.uv);
col *= _Color;
col *= i.color;
return col;
}
ENDHLSL
}
}
}
StencilBufferRead
Shader "Custom/stencil_buffer/roof_read"{
Properties{
_Color("Tint", Color) = (0, 0, 0, 1)
_MainTex("Texture", 2D) = "white" {}
[IntRange] _StencilRefRead("Stencil Read Reference Value", Range(0,255)) = 0
}
SubShader{
Tags{ "RenderType" = "Opaque" "Queue" = "Geometry" "RenderPipeline" = "UniversalRenderPipeline"}
ZWrite Off
Stencil
{
Ref[_StencilRefRead]
Comp NotEqual
Pass Keep
Fail Keep
}
Pass{
HLSLPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f {
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
v2f vert(appdata v) {
v2f o;
o.position = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
return o;
}
fixed4 frag(v2f i) : SV_TARGET{
fixed4 col = tex2D(_MainTex, i.uv) + _Color;
col *= _Color;
col *= i.color;
return col;
}
ENDHLSL
}
}
}