- Home /
Help with Stencil shaders please
Hello, so I have a couple questions about stencil shaders. So I have a working stencil shader that I'm using as a fog of war, seen here:
Now the first problem I have is to do with the depth test, as when the camera is angled a visual bug appears that stretches everything across the surface of the stencil mask (a sphere for view range). This only began after i added the line -- Blend SrcAlpha OneMinusSrcAlpha -- and return float4(0, 0, 0, 0) in the frag function for the mask shader which i added to make the mask invisible.
Here is the mask shader:
Shader "Unlit/View Mask Shader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry-1"}
LOD 100
Stencil
{
Ref 1
Comp Always
Pass Replace
ZFail Keep
}
//ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert alpha
#pragma fragment frag alpha
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return float4(0, 0, 0, 0);
}
ENDCG
}
}
}
And the terrain shader:
Shader "Custom/Terrain" {
Properties {
testTexture("Texture", 2D) = "white"{}
testScale("Scale", Float) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Stencil
{
Ref 1
comp Equal
Pass keep
}
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
const static int maxLayerCount = 8;
const static float epsilon = 1E-4;
int layerCount;
float3 baseColours[maxLayerCount];
float baseStartHeights[maxLayerCount];
float baseBlends[maxLayerCount];
float baseColourStrength[maxLayerCount];
float baseTextureScales[maxLayerCount];
float minHeight;
float maxHeight;
sampler2D testTexture;
float testScale;
UNITY_DECLARE_TEX2DARRAY(baseTextures);
struct Input {
float3 worldPos;
float3 worldNormal;
};
float inverseLerp(float a, float b, float value){
return saturate((value-a)/(b-a));
}
float3 triplanar(float3 worldPos, float scale, float3 blendAxes, int textureIndex){
float3 scaledWorldPos = worldPos / scale;
float3 xProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.y, scaledWorldPos.z, textureIndex)) * blendAxes.x;
float3 yProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.x, scaledWorldPos.z, textureIndex)) * blendAxes.y;
float3 zProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.x, scaledWorldPos.y, textureIndex)) * blendAxes.z;
return xProjection + yProjection + zProjection;
}
void surf (Input IN, inout SurfaceOutputStandard o) {
float heightPercent = inverseLerp(minHeight, maxHeight, IN.worldPos.y);
float3 blendAxes = abs(IN.worldNormal);
blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;
for(int i=0; i< layerCount; i++){
float drawStrength = inverseLerp(-baseBlends[i]/2 - epsilon, baseBlends[i]/2, heightPercent - baseStartHeights[i]);
float3 baseColour = baseColours[i] * baseColourStrength[i];
float3 textureColour = triplanar(IN.worldPos, baseTextureScales[i], blendAxes, i) * (1-baseColourStrength[i]);
o.Albedo = o.Albedo * (1 - drawStrength) + (baseColour + textureColour) * drawStrength;
}
}
ENDCG
}
FallBack "Diffuse"
}
I'm looking for a way to keep the mask invisible while keeping the depth test enabled without the visual bug. My second question is how to render mesh that fails the stencil test in some kind of undiscovered texture such as being darkened. Thanks, any help is much appreciated!
Your answer
Follow this Question
Related Questions
How to restrict my stencil shader to Y values under 0? ,how do I restrict a stencil to -Y values ? 0 Answers
Shader stencil not working in single pass instanced rendering 0 Answers
Fog Of War/ Visibility 0 Answers
Is it possible to AlphaTest prior to writing to the Stencil Buffer? 1 Answer
Does [PerRendererData] works for ShaderLab Pass Tags? 0 Answers