Skip to content

Commit 4005a5a

Browse files
authored
Create Diffuse Stipple Transparency.shader
1 parent 5ed776a commit 4005a5a

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Diffuse shader with stipple transparency
2+
// by Alex Ocias - https://ocias.com
3+
// source: https://ocias.com/blog/unity-stipple-transparency-shader/
4+
// based on an article by Digital Rune: https://www.digitalrune.com/Blog/Post/1743/Screen-Door-Transparency
5+
6+
// Simplified Diffuse shader. Differences from regular Diffuse one:
7+
// - no Main Color
8+
// - fully supports only 1 directional light. Other lights can affect it, but it will be per-vertex/SH.
9+
10+
Shader "Ocias/Diffuse (Stipple Transparency)" {
11+
Properties {
12+
_MainTex ("Base (RGB)", 2D) = "white" {}
13+
_Transparency ("Transparency", Range(0,1)) = 1.0
14+
}
15+
SubShader {
16+
Tags { "RenderType"="Opaque" }
17+
LOD 150
18+
19+
CGPROGRAM
20+
#pragma surface surf Lambert noforwardadd
21+
22+
sampler2D _MainTex;
23+
24+
struct Input {
25+
float2 uv_MainTex;
26+
float4 screenPos;
27+
};
28+
29+
half _Transparency;
30+
31+
void surf (Input IN, inout SurfaceOutput o) {
32+
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
33+
o.Albedo = c.rgb;
34+
o.Alpha = c.a;
35+
36+
// Screen-door transparency: Discard pixel if below threshold.
37+
float4x4 thresholdMatrix =
38+
{ 1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
39+
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
40+
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
41+
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
42+
};
43+
float4x4 _RowAccess = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
44+
float2 pos = IN.screenPos.xy / IN.screenPos.w;
45+
pos *= _ScreenParams.xy; // pixel position
46+
clip(_Transparency - thresholdMatrix[fmod(pos.x, 4)] * _RowAccess[fmod(pos.y, 4)]);
47+
}
48+
ENDCG
49+
}
50+
51+
Fallback "Mobile/VertexLit"
52+
}

0 commit comments

Comments
 (0)