forked from vvvv/VL.StandardLibs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWireframeShader.sdsl
More file actions
242 lines (199 loc) · 8.49 KB
/
Copy pathWireframeShader.sdsl
File metadata and controls
242 lines (199 loc) · 8.49 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
shader WireframeShader : ShaderBase, Transformation, Texturing, PositionStream4
{
noperspective stage stream float4 EdgeA;
noperspective stage stream float4 EdgeB;
stage stream float3 Normal : NORMAL;
stage stream uint Case;
static const uint infoA[] = { 0, 0, 0, 0, 1, 1, 2 };
static const uint infoB[] = { 1, 1, 2, 0, 2, 1, 2 };
static const uint infoAd[] = { 2, 2, 1, 1, 0, 0, 0 };
static const uint infoBd[] = { 2, 2, 1, 2, 0, 2, 1 };
static const uint infoEdge0[] = { 0, 2, 0, 0, 0, 0, 2 };
float LineWidth;
[Color]
float4 LineColor;
cbuffer PerView
{
stage float4 Viewport;
}
float4 projToWorld(in float4 pos, in float3 normal)
{
float3 scaling = normal * (WorldScale - 1);
return mul(pos + float4(scaling.x, scaling.y, scaling.z, 0), WorldViewProjection);
}
float2 projToWindow(in float4 pos)
{
return float2(
Viewport.x * 0.5 * (1 + (pos.x / pos.w)) + Viewport.z,
Viewport.y * 0.5 * (1 - (pos.y / pos.w)) + Viewport.w
);
}
float evalMinDistanceToEdges()
{
float dist;
if (streams.Case == 0)
{
// the easy case, the 3 distances of the fragment to the 3 edges
// is already computed, get the min
dist = min(min(streams.EdgeA.x, streams.EdgeA.y), streams.EdgeA.z);
}
else
{
// the tricky case, compute the distances and get the min from 2D lines
// given from the geometry shader
float2 af = streams.Position.xy - streams.EdgeA.xy;
float sqaf = dot(af, af);
float afCosA = dot(af, streams.EdgeA.zw);
dist = abs(sqaf - afCosA * afCosA);
float2 bf = streams.Position.xy - streams.EdgeB.xy;
float sqbf = dot(bf, bf);
float bfCosB = dot(bf, streams.EdgeB.zw);
dist = min(dist, abs(sqbf - bfCosB * bfCosB));
// only need to care about the 3rd edge for some cases
if (streams.Case == 1 || streams.Case == 2 || streams.Case == 4)
{
float afCosA0 = dot(af, normalize(streams.EdgeB.xy - streams.EdgeA.xy));
dist = min(dist, abs(sqaf - afCosA0 * afCosA0));
}
dist = sqrt(dist);
}
return dist;
}
// geometry shader
[maxvertexcount(3)]
void GSMain(triangle Input input[3], inout TriangleStream<Output> triangleStream)
{
// project to world
float4 positionWS[3];
positionWS[0] = projToWorld(input[0].Position, input[0].Normal);
positionWS[1] = projToWorld(input[1].Position, input[1].Normal);
positionWS[2] = projToWorld(input[2].Position, input[2].Normal);
// Compute the case from the positions of point in space.
uint vertexCase
= (positionWS[0].z < 0 ? 1 : 0) * 4
+ (positionWS[1].z < 0 ? 1 : 0) * 2
+ (positionWS[2].z < 0 ? 1 : 0);
// Compute the case from the positions of point in space.
if (vertexCase == 7)
{
return;
}
float2 points[3];
points[0] = projToWindow(positionWS[0]);
points[1] = projToWindow(positionWS[1]);
points[2] = projToWindow(positionWS[2]);
float3 vertExclude = float3(0, 0, 0);
float excludeEdgeLength = LineWidth + 100;
// general computation
if (vertexCase == 0)
{
// compute the edges vectors of transformed triangle
float2 edges[3];
edges[0] = points[1] - points[0];
edges[1] = points[2] - points[1];
edges[2] = points[0] - points[2];
// store the length of the edges
float lengths[3];
lengths[0] = length(edges[0]);
lengths[1] = length(edges[1]);
lengths[2] = length(edges[2]);
float3 edgesWS[3];
edgesWS[0] = input[1].Position.xyz - input[0].Position.xyz;
edgesWS[1] = input[2].Position.xyz - input[1].Position.xyz;
edgesWS[2] = input[0].Position.xyz - input[2].Position.xyz;
float lengthsWS[3];
lengthsWS[0] = length(edgesWS[0]);
lengthsWS[1] = length(edgesWS[1]);
lengthsWS[2] = length(edgesWS[2]);
float maxLength = max(max(lengthsWS[0], lengthsWS[1]), lengthsWS[2]);
vertExclude.x = lengthsWS[0] == maxLength ? 1 : 0;
vertExclude.y = lengthsWS[1] == maxLength ? 1 : 0;
vertExclude.z = lengthsWS[2] == maxLength ? 1 : 0;
// compute the cos angle of each vertices
float cosAngles[3];
cosAngles[0] = dot(-edges[2], edges[0]) / (lengths[2] * lengths[0]);
cosAngles[1] = dot(-edges[0], edges[1]) / (lengths[0] * lengths[1]);
cosAngles[2] = dot(-edges[1], edges[2]) / (lengths[1] * lengths[2]);
// the height for each vertices of triangle
float heights[3];
heights[1] = lengths[0] * sqrt(1 - cosAngles[0] * cosAngles[0]);
heights[2] = lengths[1] * sqrt(1 - cosAngles[1] * cosAngles[1]);
heights[0] = lengths[2] * sqrt(1 - cosAngles[2] * cosAngles[2]);
float edgeSigns[3];
edgeSigns[0] = (edges[0] > 0 ? 1 : -1);
edgeSigns[1] = (edges[1] > 0 ? 1 : -1);
edgeSigns[2] = (edges[2] > 0 ? 1 : -1);
float edgeOffsets[3];
edgeOffsets[0] = lengths[0] * (0.5 - 0.5 * edgeSigns[0]);
edgeOffsets[1] = lengths[1] * (0.5 - 0.5 * edgeSigns[1]);
edgeOffsets[2] = lengths[2] * (0.5 - 0.5 * edgeSigns[2]);
// vertex 0
streams = input[0];
streams.Case = vertexCase;
streams.Position = input[0].Position;
streams.EdgeA[0] = vertExclude.x * excludeEdgeLength;
streams.EdgeA[1] = heights[0];
streams.EdgeA[2] = vertExclude.z * excludeEdgeLength;
streams.EdgeB[0] = edgeOffsets[0];
streams.EdgeB[1] = edgeOffsets[1] + edgeSigns[1] * cosAngles[1] * lengths[0];
streams.EdgeB[2] = edgeOffsets[2] + edgeSigns[2] * lengths[2];
triangleStream.Append(streams);
// vertex 1
streams = input[1];
streams.Case = vertexCase;
streams.Position = input[1].Position;
streams.EdgeA[0] = vertExclude.x * excludeEdgeLength;
streams.EdgeA[1] = vertExclude.y * excludeEdgeLength;
streams.EdgeA[2] = heights[1];
streams.EdgeB[0] = edgeOffsets[0] + edgeSigns[0] * lengths[0];
streams.EdgeB[1] = edgeOffsets[1];
streams.EdgeB[2] = edgeOffsets[2] * edgeSigns[2] * cosAngles[2] * lengths[1];
triangleStream.Append(streams);
// vertex 2
streams = input[2];
streams.Case = vertexCase;
streams.Position = input[2].Position;
streams.EdgeA[0] = heights[2];
streams.EdgeA[1] = vertExclude.y * excludeEdgeLength;
streams.EdgeA[2] = vertExclude.z * excludeEdgeLength;
streams.EdgeB[0] = edgeOffsets[0] + edgeSigns[0] * cosAngles[0] * lengths[2];
streams.EdgeB[1] = edgeOffsets[1] + edgeSigns[1] * lengths[1];
streams.EdgeB[2] = edgeOffsets[2];
triangleStream.Append(streams);
triangleStream.RestartStrip();
}
else
{
// Else need some tricky computations
for (int i = 0; i < 3; i++)
{
streams = input[i];
streams.EdgeA = float4(0, 0, 0, 0);
streams.EdgeB = float4(0, 0, 0, 0);
streams.Case = vertexCase;
streams.EdgeA.xy = points[infoA[vertexCase]];
streams.EdgeB.xy = points[infoB[vertexCase]];
streams.EdgeA.zw = normalize(streams.EdgeA.xy - points[infoAd[vertexCase]]);
streams.EdgeB.zw = normalize(streams.EdgeB.xy - points[infoBd[vertexCase]]);
triangleStream.Append(streams);
}
triangleStream.RestartStrip();
}
}
// vertex shader
stage override void VSMain()
{
streams.ShadingPosition = projToWorld(streams.Position, streams.Normal);
}
// pixel shader
stage override void PSMain()
{
float dist = evalMinDistanceToEdges();
if (dist > 0.5 * LineWidth)
{
// too far from edge
discard;
}
streams.ColorTarget = LineColor;
}
};