-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinterleaved-strip.ts
More file actions
227 lines (199 loc) · 5.92 KB
/
interleaved-strip.ts
File metadata and controls
227 lines (199 loc) · 5.92 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
import { Regl } from "regl";
const geometry = {
positions: [
[0, -0.5],
[1, -0.5],
[1, 0.5],
[0, 0.5],
],
cells: [
[0, 1, 2],
[0, 2, 3],
],
};
export function interleavedStripCommand(regl: Regl) {
return regl({
vert: `
precision highp float;
attribute vec2 position;
attribute vec2 pA, pB, pC, pD;
uniform float width;
uniform mat4 projection;
void main() {
// Select the three points we'll use and adjust the vertex according to
// the side of the segment the vertex is on and the order of the points.
vec2 p0 = pA;
vec2 p1 = pB;
vec2 p2 = pC;
vec2 pos = position;
if (position.x == 1.0) {
p0 = pD;
p1 = pC;
p2 = pB;
pos = vec2(1.0 - position.x, -position.y);
}
// Find the normal vector.
vec2 tangent = normalize(normalize(p2 - p1) + normalize(p1 - p0));
vec2 normal = vec2(-tangent.y, tangent.x);
// Find the perpendicular vectors.
vec2 p01 = p1 - p0;
vec2 p21 = p1 - p2;
vec2 p01Norm = normalize(vec2(-p01.y, p01.x));
// Determine the bend direction.
float sigma = sign(dot(p01 + p21, normal));
// If this is the intersecting vertex,
if (sign(pos.y) == -sigma) {
vec2 point = 0.5 * normal * -sigma * width / dot(normal, p01Norm);
gl_Position = projection * vec4(p1 + point, 0, 1);
} else {
vec2 xBasis = p2 - p1;
vec2 yBasis = normalize(vec2(-xBasis.y, xBasis.x));
vec2 point = p1 + xBasis * pos.x + yBasis * width * pos.y;
gl_Position = projection * vec4(point, 0, 1);
}
}`,
frag: `
precision highp float;
uniform vec4 color;
void main() {
gl_FragColor = color;
}`,
attributes: {
position: {
buffer: regl.buffer(geometry.positions),
divisor: 0,
},
pA: {
buffer: regl.prop<any, any>("points"),
divisor: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 0,
},
pB: {
buffer: regl.prop<any, any>("points"),
divisor: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 2,
},
pC: {
buffer: regl.prop<any, any>("points"),
divisor: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 4,
},
pD: {
buffer: regl.prop<any, any>("points"),
divisor: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 6,
},
},
uniforms: {
width: regl.prop<any, any>("width"),
color: regl.prop<any, any>("color"),
projection: regl.prop<any, any>("projection"),
},
blend: {
enable: true,
func: {
src: "src alpha",
dst: "one minus src alpha",
},
},
cull: {
enable: true,
face: "back",
},
depth: {
enable: false,
},
elements: regl.elements(geometry.cells),
instances: regl.prop<any, any>("segments"),
viewport: regl.prop<any, any>("viewport"),
});
}
export function interleavedStripTerminalCommand(regl: Regl) {
return regl({
vert: `
precision highp float;
attribute vec2 position;
attribute vec2 pA, pB, pC;
uniform float width;
uniform mat4 projection;
void main() {
if (position.x == 0.0) {
vec2 xBasis = pB - pA;
vec2 yBasis = normalize(vec2(-xBasis.y, xBasis.x));
vec2 point = pA + xBasis * position.x + yBasis * width * position.y;
gl_Position = projection * vec4(point, 0, 1);
return;
}
// Find the normal vector.
vec2 tangent = normalize(normalize(pC - pB) + normalize(pB - pA));
vec2 normal = vec2(-tangent.y, tangent.x);
// Find the perpendicular vectors.
vec2 ab = pB - pA;
vec2 cb = pB - pC;
vec2 abNorm = normalize(vec2(-ab.y, ab.x));
// Determine the bend direction.
float sigma = sign(dot(ab + cb, normal));
if (sign(position.y) == -sigma) {
vec2 position = 0.5 * normal * -sigma * width / dot(normal, abNorm);
gl_Position = projection * vec4(pB + position, 0, 1);
} else {
vec2 xBasis = pB - pA;
vec2 yBasis = normalize(vec2(-xBasis.y, xBasis.x));
vec2 point = pA + xBasis * position.x + yBasis * width * position.y;
gl_Position = projection * vec4(point, 0, 1);
}
}`,
frag: `
precision highp float;
uniform vec4 color;
void main() {
gl_FragColor = color;
}`,
attributes: {
position: {
buffer: regl.buffer(geometry.positions),
divisor: 0,
},
pA: {
buffer: regl.prop<any, any>("points"),
divisor: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 0,
stride: Float32Array.BYTES_PER_ELEMENT * 6,
},
pB: {
buffer: regl.prop<any, any>("points"),
divisor: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 2,
stride: Float32Array.BYTES_PER_ELEMENT * 6,
},
pC: {
buffer: regl.prop<any, any>("points"),
divisor: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 4,
stride: Float32Array.BYTES_PER_ELEMENT * 6,
},
},
uniforms: {
width: regl.prop<any, any>("width"),
color: regl.prop<any, any>("color"),
projection: regl.prop<any, any>("projection"),
},
blend: {
enable: true,
func: {
src: "src alpha",
dst: "one minus src alpha",
},
},
cull: {
enable: true,
face: "back",
},
depth: {
enable: false,
},
elements: regl.elements(geometry.cells),
instances: regl.prop<any, any>("segments"),
viewport: regl.prop<any, any>("viewport"),
});
}