-
Notifications
You must be signed in to change notification settings - Fork 28
/
Wireframe Overlay.shader
178 lines (146 loc) · 4.95 KB
/
Wireframe Overlay.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// by Neitri, free of charge, free to redistribute
// downloaded from https://github.com/netri/Neitri-Unity-Shaders
Shader "Neitri/Wireframe Overlay"
{
Properties
{
_WireframeColor("Wireframe Color", Color) = (1, 1, 1, 1)
_BackgroundColor("Background Color", Color) = (0, 0, 0, 1)
}
SubShader
{
Tags
{
"Queue" = "Transparent+1000"
"RenderType" = "Transparent"
}
Cull Off
Pass
{
// based on "Neitri/World Normal Nice Slow"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
// based on https://gamedev.stackexchange.com/a/132845/41980
struct appdata
{
float4 vertex :POSITION;
};
struct v2f
{
float4 clipPos : SV_POSITION;
float4 modelPos : TEXCOORD0;
};
sampler2D _CameraDepthTexture;
v2f vert (appdata v)
{
v2f o;
o.clipPos = UnityObjectToClipPos(v.vertex);
o.modelPos = v.vertex;
return o;
}
// taken from http://answers.unity.com/answers/641391/view.html
float4x4 inverse(float4x4 input)
{
#define minor(a,b,c) determinant(float3x3(input.a, input.b, input.c))
float4x4 cofactors = float4x4(
minor(_22_23_24, _32_33_34, _42_43_44),
-minor(_21_23_24, _31_33_34, _41_43_44),
minor(_21_22_24, _31_32_34, _41_42_44),
-minor(_21_22_23, _31_32_33, _41_42_43),
-minor(_12_13_14, _32_33_34, _42_43_44),
minor(_11_13_14, _31_33_34, _41_43_44),
-minor(_11_12_14, _31_32_34, _41_42_44),
minor(_11_12_13, _31_32_33, _41_42_43),
minor(_12_13_14, _22_23_24, _42_43_44),
-minor(_11_13_14, _21_23_24, _41_43_44),
minor(_11_12_14, _21_22_24, _41_42_44),
-minor(_11_12_13, _21_22_23, _41_42_43),
-minor(_12_13_14, _22_23_24, _32_33_34),
minor(_11_13_14, _21_23_24, _31_33_34),
-minor(_11_12_14, _21_22_24, _31_32_34),
minor(_11_12_13, _21_22_23, _31_32_33)
);
#undef minor
return transpose(cofactors) / determinant(input);
}
float3 calculateWorldSpace(float4 vertex, float2 screenOffset)
{
float4 worldPos = mul(unity_ObjectToWorld, float4(vertex.xyz, 1));
float4 screenPos = mul(UNITY_MATRIX_VP, worldPos);
// Adjust positon in screen space
screenPos.xy += screenOffset * screenPos.w;
// Transform back to world pos
worldPos = mul(inverse(UNITY_MATRIX_VP), screenPos);
// Subtract camera position from vertex position in world
// to get a ray pointing from the camera to this vertex.
float3 worldDir = worldPos.xyz - _WorldSpaceCameraPos;
// Calculate screen UV
float2 screenUV = screenPos.xy / screenPos.w;
screenUV.y *= _ProjectionParams.x;
screenUV = screenUV * 0.5f + 0.5f;
// VR stereo support
screenUV = UnityStereoTransformScreenSpaceTex(screenUV);
// Read depth, linearizing into worldspace units.
float depth = LinearEyeDepth(UNITY_SAMPLE_DEPTH(tex2D(_CameraDepthTexture, screenUV))) / screenPos.w;
// Advance by depth along our view ray from the camera position.
// This is the worldspace coordinate of the corresponding fragment
// we retrieved from the depth buffer.
float3 worldSpacePos = worldDir * depth + _WorldSpaceCameraPos;
return worldSpacePos;
}
fixed4 frag (v2f i) : SV_Target
{
// Should be 1.0, but some artefacts appear if we use the perfect value
float2 offset = 1.2 / _ScreenParams.xy;
float3 worldPos1 = calculateWorldSpace(i.modelPos, float2(0,0));
float3 worldPos2 = calculateWorldSpace(i.modelPos, float2(0, offset.y));
float3 worldPos3 = calculateWorldSpace(i.modelPos, float2(-offset.x, 0));
float3 worldNormal = normalize(cross(worldPos2 - worldPos1, worldPos3 - worldPos1));
return float4(worldNormal, 1.0f);
}
ENDCG
}
GrabPass
{
"_WorldSpaceNormal"
}
Pass
{
// Wireframe calculated from normal derivateves, using two pass shader, idea by mel0n
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct v2f
{
float4 grabPos : TEXCOORD0;
float4 pos : SV_POSITION;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.grabPos = ComputeGrabScreenPos(o.pos);
return o;
}
sampler2D _WorldSpaceNormal;
fixed4 _BackgroundColor;
fixed4 _WireframeColor;
fixed4 frag(v2f i) : SV_Target
{
float2 grabPos = i.grabPos.xy / i.grabPos.w;
float2 offset = 1.0 / _ScreenParams.xy;
float3 pos00 = tex2D(_WorldSpaceNormal, grabPos).rgb;
float3 pos01 = tex2D(_WorldSpaceNormal, grabPos + float2(0, offset.y)).rgb;
float3 pos10 = tex2D(_WorldSpaceNormal, grabPos - float2(offset.x, 0)).rgb;
float3 one = float3(1, 1, 1);
float w = dot(one, abs(pos10 - pos00)) + dot(one, abs(pos01 - pos00));
return lerp(_BackgroundColor, _WireframeColor, w);
}
ENDCG
}
}
}