Hi there,
So, I have these 2 shader files, one called Stencilled.shader and one called StencilMask.shader. When the stencil mask is applied to the material of an object, it allows the user to look through it and reveal objects with the Stencilled.shader applied to them.
I have another shader file called Intersect.shader which allows the user to apply it to an object and then intersect the object with another object to highlight the intersection.
What I’m trying to do is basically combine both the StencilMask.shader and the Intersect.shader files so that the intersection of the objects becomes a stencil mask which reveals objects with the Stencilled shader applied to them (if that makes sense).
Shader "Custom/StencilMask" {
Properties {
_StencilMask("Stencil mask", Int) = 0
}
SubShader {
Tags {
"RenderType" = "Opaque"
"Queue" = "Geometry-100"
}
ColorMask 0
ZWrite off
Stencil {
Ref[_StencilMask]
Comp always
Pass replace
}
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : COLOR {
return half4(1, 1, 0, 1);
}
ENDCG
}
}
}
Shader "Custom/Stencilled" {
Properties {
_StencilMask("Stencil mask", Int) = 0
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 200
Stencil {
Ref[_StencilMask]
Comp equal
Pass keep
Fail keep
}
CGPROGRAM
#pragma surface surf Standard fullforwardshadows
#pragma target 3.0
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
void surf (Input IN, inout SurfaceOutputStandard o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
FallBack "Diffuse"
}
Shader "Custom/Intersect"
{
Properties
{
[HDR] _Color("Color", Color) = (1,1,1,1)
}
SubShader
{
//Rendertype: Opaque, Transparent or Overlay all give same result...
//Queue is important here! Must be over 2500 to get front to back rendering, Overlay = 4000 (Transparent also works..)
Tags { "RenderType"="Opaque" "Queue" = "Overlay" }
LOD 100
ZWrite Off
//first pass sets the mask for the "front geometry"
Pass
{
Cull Back
ZTest Greater
ColorMask 0
Stencil {
Ref 1
Comp Always
Pass Replace
}
}
//second pass turn off culling, could be Cull Off or Cull Front (both acheive the same thing)
//use the mask from the first pass and draw the color
Pass
{
Cull Off
ZTest Greater
Stencil {
Ref 1
Comp NotEqual
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
};
float4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return _Color; // just return it
}
ENDCG
}
// reset the stencil buffer so other objects dont mask out this one
Pass
{
//Cull Back
ZTest Greater
ColorMask 0
Stencil {
Ref 0
Comp Always
Pass Zero
}
}
}
}
If anyone could please offer me any help, I’d be extremely grateful.
Kind Regards,
Davy.
