Skip to content

Commit b9e61e0

Browse files
v1.11.0 - Stencils
Added Stencil Writer and Stencil Target
1 parent 0d4052e commit b9e61e0

File tree

6 files changed

+197
-1
lines changed

6 files changed

+197
-1
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Shader "Mabel/Stencils/Stencil Target"
2+
{
3+
Properties
4+
{
5+
_BaseColor ("Base Color", Color) = (1,1,1,1)
6+
_MainTex ("Texture", 2D) = "white" {}
7+
}
8+
9+
SubShader
10+
{
11+
Tags { "RenderType"="Transparent" "Queue"="Overlay+1" }
12+
13+
Stencil
14+
{
15+
Ref 1
16+
Comp Equal
17+
Pass Keep
18+
Fail Keep
19+
ZFail Keep
20+
}
21+
22+
ZTest LEqual
23+
ZWrite On
24+
Cull Off
25+
Blend SrcAlpha OneMinusSrcAlpha
26+
27+
Pass
28+
{
29+
HLSLPROGRAM
30+
#pragma vertex vert
31+
#pragma fragment frag
32+
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
33+
#include "UnityCG.cginc"
34+
35+
struct appdata
36+
{
37+
float4 vertex : POSITION;
38+
float2 uv : TEXCOORD0;
39+
40+
UNITY_VERTEX_INPUT_INSTANCE_ID // For instance ID support
41+
};
42+
43+
struct v2f
44+
{
45+
float2 uv : TEXCOORD0;
46+
float4 vertex : SV_POSITION;
47+
48+
UNITY_VERTEX_OUTPUT_STEREO // For stereo rendering support
49+
};
50+
51+
sampler2D _MainTex;
52+
float4 _BaseColor;
53+
54+
// Vertex shader
55+
v2f vert(appdata v)
56+
{
57+
v2f o;
58+
59+
// Handle instancing
60+
UNITY_SETUP_INSTANCE_ID(v);
61+
UNITY_INITIALIZE_OUTPUT(v2f, o);
62+
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
63+
64+
// Apply the correct transformation for stereo rendering
65+
o.vertex = UnityObjectToClipPos(v.vertex);
66+
67+
o.uv = v.uv;
68+
69+
return o;
70+
}
71+
72+
// Fragment shader
73+
float4 frag(v2f i) : SV_Target
74+
{
75+
// Sample texture and apply color
76+
float4 texColor = tex2D(_MainTex, i.uv);
77+
return texColor * _BaseColor;
78+
}
79+
80+
ENDHLSL
81+
}
82+
}
83+
84+
FallBack Off
85+
}

Assets/Shaders/StencilTarget.shader.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Shader "Mabel/Stencils/Stencil Writer"
2+
{
3+
Properties
4+
{
5+
_Color ("Color", Color) = (1,1,1,1)
6+
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
7+
}
8+
SubShader
9+
{
10+
Tags { "Queue"="Overlay" "RenderType"="Transparent" }
11+
12+
Stencil
13+
{
14+
Ref 1
15+
Comp always
16+
Pass replace
17+
}
18+
19+
Cull Off
20+
ZWrite Off
21+
Blend SrcAlpha OneMinusSrcAlpha
22+
23+
Pass
24+
{
25+
HLSLPROGRAM
26+
#pragma vertex vert
27+
#pragma fragment frag
28+
#pragma multi_compile _ UNITY_SINGLE_PASS_STEREO STEREO_INSTANCING_ON STEREO_MULTIVIEW_ON
29+
#include "UnityCG.cginc"
30+
31+
sampler2D _MainTex;
32+
fixed4 _Color;
33+
float4 _MainTex_ST;
34+
35+
struct appdata
36+
{
37+
float4 vertex : POSITION;
38+
float2 uv : TEXCOORD0;
39+
40+
UNITY_VERTEX_INPUT_INSTANCE_ID // Support for instancing
41+
};
42+
43+
struct v2f
44+
{
45+
float2 uv : TEXCOORD0;
46+
float4 pos : SV_POSITION;
47+
48+
UNITY_VERTEX_OUTPUT_STEREO // Ensure stereo support
49+
};
50+
51+
v2f vert(appdata v)
52+
{
53+
v2f o;
54+
55+
// Setup instance data if required
56+
UNITY_SETUP_INSTANCE_ID(v);
57+
UNITY_INITIALIZE_OUTPUT(v2f, o);
58+
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
59+
60+
// Apply transformation for stereo rendering
61+
o.pos = UnityObjectToClipPos(v.vertex);
62+
63+
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
64+
65+
return o;
66+
}
67+
68+
fixed4 frag(v2f i) : SV_Target
69+
{
70+
return tex2D(_MainTex, i.uv) * _Color;
71+
}
72+
73+
ENDHLSL
74+
}
75+
}
76+
77+
FallBack Off
78+
}

Assets/Shaders/StencilWriter.shader.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# v1.11.0
2+
## Added Shaders
3+
### Stencil Writer
4+
* Only objects with the Stencil Target shader will be visible through this shader
5+
### Stencil Target
6+
* Only visible through Stencil Writers
7+
18
# v1.10.1
29
## Changed Shaders
310
### SLZ Toon

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,10 @@
5353
## SLZ Toon
5454
### Difference from SLZ Anime
5555
* Emissive map support
56-
* No specular highlights, a real toon shader wouldn't be reflective
56+
* No specular highlights, a real toon shader wouldn't be reflective
57+
58+
## Stencils
59+
### Stencil Writer
60+
* Stencil Targets are visible through objects with this shader
61+
### Stencil Target
62+
* Only visible within Stencil Writers

0 commit comments

Comments
 (0)