/
interleaved-shapes.ts
156 lines (139 loc) · 4.64 KB
/
interleaved-shapes.ts
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
import { Buffer } from "regl";
import { CandyGraph } from "../candygraph";
import { NumberArray } from "../common";
import { Primitive, NamedDrawCommands } from "./primitive";
import { Dataset, createDataset } from "../dataset";
export interface InterleavedShapesOptions {
/** The color of the shapes. If this value is a single Vector4, it will apply to all the shapes. Default [0, 0, 0, 0.5]. */
colors?: NumberArray | Dataset;
/** The scale of the shapes. If this value is a single Vector2, it will apply to all the shapes. Default [1, 1]. */
scales?: NumberArray | Dataset;
/** The rotation of the shapes in radians. If this value is a single float, it will apply to all the shapes. Default 0. */
rotations?: number | NumberArray | Dataset;
}
const DEFAULT_OPTIONS = {
colors: [0, 0, 0, 0.5],
scales: [1, 1],
rotations: 0,
};
interface Props {
position: Buffer;
xy: Buffer;
scale: Buffer;
rotation: Buffer;
color: Buffer;
positionDivisor: number;
scaleDivisor: number;
rotationDivisor: number;
colorDivisor: number;
count: number;
instances: number;
}
/** Renders colored shapes. Useful for custom trace points. */
export class InterleavedShapes extends Primitive {
public shape: Dataset;
public xys: Dataset;
public scales: Dataset;
public rotations: Dataset;
public colors: Dataset;
/**
* @param shape Set of 2D points in the form `[x0, y0, x1, y1, ...]` that describe the (unindexed) set of triangles representing the shape to render.
* @param xys The x, y coordinates of the shape positions in the form `[x0, y0, x1, y1, ...]`.
*/
constructor(
private cg: CandyGraph,
shape: NumberArray | Dataset,
xys: NumberArray | Dataset,
options: InterleavedShapesOptions = {}
) {
super();
const opts = { ...DEFAULT_OPTIONS, ...options };
this.shape = createDataset(cg, shape);
this.xys = createDataset(cg, xys);
this.scales = createDataset(cg, opts.scales);
this.rotations = createDataset(cg, opts.rotations);
this.colors = createDataset(cg, opts.colors);
}
/** @internal */
public commands(glsl: string): NamedDrawCommands {
return {
shapes: this.cg.regl({
vert: `
precision highp float;
attribute vec2 position;
attribute vec2 xy, scale;
attribute float rotation;
attribute vec4 color;
varying vec4 vColor;
${glsl}
void main() {
vec2 pos = scale * position;
float sint = sin(rotation);
float cost = cos(rotation);
pos = vec2(
cost * pos.x - sint * pos.y,
sint * pos.x + cost * pos.y
);
vec2 screenPosition = toRange(xy) + pos;
gl_Position = rangeToClip(screenPosition);
vColor = color;
}`,
frag: `
precision highp float;
varying vec4 vColor;
void main() {
gl_FragColor = vColor;
}`,
attributes: {
position: {
buffer: this.cg.regl.prop<Props, "position">("position"),
divisor: 0,
},
xy: {
buffer: this.cg.regl.prop<Props, "xy">("xy"),
divisor: 1,
},
scale: {
buffer: this.cg.regl.prop<Props, "scale">("scale"),
divisor: this.cg.regl.prop<Props, "scaleDivisor">("scaleDivisor"),
},
rotation: {
buffer: this.cg.regl.prop<Props, "rotation">("rotation"),
divisor: this.cg.regl.prop<Props, "rotationDivisor">("rotationDivisor"),
},
color: {
buffer: this.cg.regl.prop<Props, "color">("color"),
divisor: this.cg.regl.prop<Props, "colorDivisor">("colorDivisor"),
},
},
count: this.cg.regl.prop<Props, "count">("count"),
instances: this.cg.regl.prop<Props, "instances">("instances"),
}),
};
}
/** @internal */
public render(commands: NamedDrawCommands): void {
const { shape, xys, scales, rotations, colors } = this;
const instances = xys.count(2);
commands.shapes({
instances,
xy: xys.buffer,
position: shape.buffer,
scale: scales.buffer,
rotation: rotations.buffer,
color: colors.buffer,
scaleDivisor: scales.divisor(instances, 2),
rotationDivisor: rotations.divisor(instances, 1),
colorDivisor: colors.divisor(instances, 4),
count: shape.count(2),
});
}
/** Releases all GPU resources and renders this instance unusable. */
public dispose(): void {
this.xys.dispose();
this.shape.dispose();
this.scales.dispose();
this.rotations.dispose();
this.colors.dispose();
}
}