-
Notifications
You must be signed in to change notification settings - Fork 18
/
build-triangle.ts
214 lines (186 loc) · 4.66 KB
/
build-triangle.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
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
import { Matrix, Point, TextureUvs } from "pixi.js";
import { EShapeStrokeStyle } from "../e-shape-stroke-style";
import { toLength } from "./to-length";
import { toScaleInvariant } from "./to-scale-invariant";
import { toPackedF2x1024, toPackedI4x64 } from "./to-packed";
export const TRIANGLE_VERTEX_COUNT = 7;
export const TRIANGLE_INDEX_COUNT = 3;
export const TRIANGLE_WORLD_SIZE: [number, number, number] = [0, 0, 0];
const TRIANGLE_WORK_POINT: Point = new Point();
export const buildTriangleIndex = (
indices: Uint16Array | Uint32Array,
voffset: number,
ioffset: number
): void => {
// Indices
let ii = ioffset * 3 - 1;
indices[++ii] = voffset + 0;
indices[++ii] = voffset + 1;
indices[++ii] = voffset + 2;
indices[++ii] = voffset + 0;
indices[++ii] = voffset + 3;
indices[++ii] = voffset + 4;
indices[++ii] = voffset + 0;
indices[++ii] = voffset + 5;
indices[++ii] = voffset + 6;
};
export const buildTriangleVertex = (
vertices: Float32Array,
voffset: number,
originX: number,
originY: number,
sizeX: number,
sizeY: number,
strokeAlign: number,
strokeWidth: number,
internalTransform: Matrix,
worldSize: typeof TRIANGLE_WORLD_SIZE
): void => {
const s = strokeAlign * strokeWidth;
const sx = sizeX * 0.5 + (0 <= sizeX ? +s : -s);
const sy = sizeY * 0.5 + (0 <= sizeY ? +s : -s);
const sz = Math.sqrt(sx * sx + 4 * sy * sy);
const sw = (2 * sx * sy) / (sx + sz);
const work = TRIANGLE_WORK_POINT;
work.set(originX, originY - sy);
internalTransform.apply(work, work);
const x0 = work.x;
const y0 = work.y;
work.set(originX, originY);
internalTransform.apply(work, work);
const tx = work.x;
const ty = work.y;
work.set(originX + sx, originY);
internalTransform.apply(work, work);
const dx = tx - x0;
const dy = ty - y0;
const x1 = work.x + dx;
const y1 = work.y + dy;
const x2 = tx + (tx - work.x) + dx;
const y2 = ty + (ty - work.y) + dy;
work.set(originX, originY + sy - sw); // Incenter of a triangle
internalTransform.apply(work, work);
const x3 = work.x;
const y3 = work.y;
// World size
const xb = tx + dx;
const yb = ty + dy;
worldSize[0] = toLength(xb, yb, x3, y3);
worldSize[1] = toLength(x1, y1, xb, yb);
worldSize[2] = toLength(x0, y0, tx, ty);
// Vertices
let iv = (voffset << 1) - 1;
vertices[++iv] = x3;
vertices[++iv] = y3;
vertices[++iv] = x0;
vertices[++iv] = y0;
vertices[++iv] = x1;
vertices[++iv] = y1;
vertices[++iv] = x1;
vertices[++iv] = y1;
vertices[++iv] = x2;
vertices[++iv] = y2;
vertices[++iv] = x2;
vertices[++iv] = y2;
vertices[++iv] = x0;
vertices[++iv] = y0;
};
export const buildTriangleStep = (
steps: Float32Array,
voffset: number,
strokeWidth: number,
strokeStyle: EShapeStrokeStyle,
worldSize: typeof TRIANGLE_WORLD_SIZE
): void => {
const scaleInvariant = toScaleInvariant(strokeStyle);
const s = worldSize[0];
const e = toPackedI4x64(0, scaleInvariant, 1, 1);
const c00 = toPackedF2x1024(0, 0);
const c10 = toPackedF2x1024(1, 0);
const c01 = toPackedF2x1024(0, 1);
// 000
let is = voffset * 6 - 1;
steps[++is] = strokeWidth;
steps[++is] = e;
steps[++is] = s;
steps[++is] = s;
steps[++is] = c00;
steps[++is] = 0;
// 100
steps[++is] = strokeWidth;
steps[++is] = e;
steps[++is] = s;
steps[++is] = s;
steps[++is] = c10;
steps[++is] = 0;
// 100
steps[++is] = strokeWidth;
steps[++is] = e;
steps[++is] = s;
steps[++is] = s;
steps[++is] = c10;
steps[++is] = 0;
// 010
steps[++is] = strokeWidth;
steps[++is] = e;
steps[++is] = s;
steps[++is] = s;
steps[++is] = c01;
steps[++is] = 0;
// 010
steps[++is] = strokeWidth;
steps[++is] = e;
steps[++is] = s;
steps[++is] = s;
steps[++is] = c01;
steps[++is] = 0;
// 100
steps[++is] = strokeWidth;
steps[++is] = e;
steps[++is] = s;
steps[++is] = s;
steps[++is] = c10;
steps[++is] = 0;
// 100
steps[++is] = strokeWidth;
steps[++is] = e;
steps[++is] = s;
steps[++is] = s;
steps[++is] = c10;
steps[++is] = 0;
};
export const buildTriangleUv = (
uvs: Float32Array,
textureUvs: TextureUvs,
voffset: number,
worldSize: typeof TRIANGLE_WORLD_SIZE
): void => {
const x0 = textureUvs.x0;
const x1 = textureUvs.x1;
const x2 = textureUvs.x2;
const x3 = textureUvs.x3;
const y0 = textureUvs.y0;
const y1 = textureUvs.y1;
const y2 = textureUvs.y2;
const y3 = textureUvs.y3;
const x4 = 0.5 * (x0 + x1);
const y4 = 0.5 * (y0 + y1);
const c = 1 - (0.5 * worldSize[0]) / worldSize[2];
const x5 = x4 + c * (x3 - x0);
const y5 = y4 + c * (y3 - y0);
let iuv = (voffset << 1) - 1;
uvs[++iuv] = x5;
uvs[++iuv] = y5;
uvs[++iuv] = x4;
uvs[++iuv] = y4;
uvs[++iuv] = x2;
uvs[++iuv] = y2;
uvs[++iuv] = x2;
uvs[++iuv] = y2;
uvs[++iuv] = x3;
uvs[++iuv] = y3;
uvs[++iuv] = x3;
uvs[++iuv] = y3;
uvs[++iuv] = x4;
uvs[++iuv] = y4;
};