Repository navigation
Expand file tree
/
Copy pathcaps.ts
More file actions
105 lines (93 loc) · 2.36 KB
/
Copy pathcaps.ts
File metadata and controls
105 lines (93 loc) · 2.36 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
import { Regl } from "regl";
interface Geometry {
positions: number[][];
cells: number[][];
}
export function roundCapGeometry(resolution: number) {
const positions = [[0, 0]];
for (let i = 0; i <= resolution; i++) {
const theta = -0.5 * Math.PI + (Math.PI * i) / resolution;
positions.push([0.5 * Math.cos(theta), 0.5 * Math.sin(theta)]);
}
const cells: number[][] = [];
for (let i = 0; i < resolution; i++) {
cells.push([0, i + 1, i + 2]);
}
return { positions, cells };
}
export function squareCapGeometry() {
return {
positions: [
[0, 0.5],
[0, -0.5],
[0.5, -0.5],
[0.5, 0.5],
],
cells: [
[0, 1, 2],
[0, 2, 3],
],
};
}
export function capCommand(regl: Regl, geometry: Geometry) {
return regl({
vert: `
precision highp float;
attribute vec2 position;
attribute vec2 pA, pB;
uniform float width;
uniform mat4 projection;
void main() {
vec2 xBasis = normalize(pA - pB);
vec2 yBasis = vec2(-xBasis.y, xBasis.x);
vec2 point = pA + xBasis * width * 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 * 4,
},
pB: {
buffer: regl.prop<any, any>("points"),
divisor: 1,
offset: Float32Array.BYTES_PER_ELEMENT * 2,
stride: Float32Array.BYTES_PER_ELEMENT * 4,
},
},
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>("instances"),
viewport: regl.prop<any, any>("viewport"),
});
}