-
Notifications
You must be signed in to change notification settings - Fork 0
/
three-d.js
428 lines (391 loc) · 11.6 KB
/
three-d.js
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import * as THREE from 'three';
const AxesPlane = Object.freeze({
XY: 6,
XZ: 5,
YZ: 3,
});
// Utilities
function gcd(a, b) {
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
}
// Points
function radians(degrees) {
return degrees * Math.PI / 180;
}
/** Detect and remove co-linear points;
*/
function removeColinear(points) {
const numPoints = points.length >> 1;
if (numPoints <= 1) {
return points;
}
let prevX = points[0];
let prevY = points[1];
let x = points[2];
let y = points[3];
const newPoints = [prevX, prevY];
for (let i = 1; i < numPoints; i++) {
const nextIndex = 2 * ((i + 1) % numPoints);
const nextX = points[nextIndex];
const nextY = points[nextIndex + 1];
if (
(x !== prevX || y !== prevY) &&
(nextIndex !== 0 || nextX !== x || nextY !== y)
) {
const m1 = (y - prevY) / (x - prevX);
const m2 = (nextY - y) / (nextX - x);
if (m1 !== m2) {
newPoints.push(x, y);
}
}
prevX = x;
prevY = y;
x = nextX;
y = nextY;
}
return new Float32Array(newPoints);
}
function regularPolygonPoints(
numSides, radii, stretch = 1, rotation = 0, turnThrough = 2 * Math.PI
) {
let points, divisor, index;
if (turnThrough === 2 * Math.PI) {
points = new Float32Array(2 * numSides);
divisor = numSides;
index = 0;
} else {
points = new Float32Array(2 * (numSides + 1));
divisor = numSides - 1;
index = 2;
}
const numRadii = radii.length;
for (let i = 0; i < numSides; i++) {
const angle = -(turnThrough * i / divisor + rotation);
const radius = radii[i % numRadii];
const x = stretch * radius * Math.sin(angle);
const y = radius * Math.cos(angle);
points[index] = x;
index++;
points[index] = y;
index++;
}
return points;
}
/**
* @param {number} dilation Between -1 and 1.
* @param {number} includedSides E.g. a Hexagram reduced to only 3 included sides forms a boat.
*/
function starPolygonPoints(
numSides, starFactor, radius, dilation = 0, rotation = 0, includedSides = numSides
) {
if (starFactor % numSides <= 1) {
return regularPolygonPoints(numSides, [radius], 1, rotation);
}
let radius2;
if (numSides / starFactor === 2) {
radius2 = 0;
} else {
// x11 = 0, y11 = 1, i.e. first line, first point
// c1 = 1
let angle = -2 * Math.PI * starFactor / numSides;
const x12 = Math.sin(angle);
const y12 = Math.cos(angle);
const m1 = (y12 - 1) / x12;
angle = -2 * Math.PI / numSides;
const x21 = Math.sin(angle);
const y21 = Math.cos(angle);
angle = -2 * Math.PI * (1 + starFactor) / numSides;
const x22 = Math.sin(angle);
const y22 = Math.cos(angle);
const m2 = (y22 - y21) / (x22 - x21);
const c2 = y21 - m2 * x21;
// m1 * intersectionX + c1 = m2 * intersectionX + c2 (same y-coordinate)
const intersectionX = (c2 - 1) / (m1 - m2);
const intersectionY = m1 * intersectionX + 1;
// Then by Pythagoras.
radius2 = radius * Math.hypot(intersectionX, intersectionY);
}
if (dilation >= 0) {
radius2 = radius * dilation + radius2 * (1 - dilation);
} else {
radius2 = radius2 * (1 + dilation);
}
let points, index;
if (includedSides === numSides) {
points = new Float32Array(4 * numSides);
index = 0;
} else {
points = new Float32Array(4 * includedSides + 2);
const angle = -(2 * Math.PI * (numSides - 0.5) / numSides + rotation);
points[0] = radius2 * Math.sin(angle);
points[1] = radius2 * Math.cos(angle);
index = 2;
}
for (let i = 0; i < includedSides; i++) {
const angle1 = -(2 * Math.PI * i / numSides + rotation);
const x1 = radius * Math.sin(angle1);
const y1 = radius * Math.cos(angle1);
const angle2 = -(2 * Math.PI * (i + 0.5) / numSides + rotation);
const x2 = radius2 * Math.sin(angle2);
const y2 = radius2 * Math.cos(angle2);
points[index] = x1;
points[index + 1] = y1;
points[index + 2] = x2;
points[index + 3] = y2;
index += 4;
}
return points;
}
function cyclicPoints(angles, radius1, radius2 = radius1) {
const numSides = angles.length;
const points = new Float32Array(2 * numSides);
let index = 0;
for (let i = 0; i < numSides; i++) {
const angle = -angles[i];
const x = radius1 * Math.sin(angle);
const y = radius2 * Math.cos(angle);
points[index] = x;
index++;
points[index] = y;
index++;
}
return points;
}
function rotationalSymmetryPoints(
edgeFractions, offsets, degree, radius, stretch = 1, rotation = 0, includedSides = degree
) {
const pointsPerEdge = edgeFractions.length;
const points = new Float32Array(2 * pointsPerEdge * includedSides);
let index = 0;
let x1 = stretch * radius * Math.sin(-rotation);
let y1 = radius * Math.cos(-rotation);
for (let i = 0; i < includedSides; i++) {
const angle = -(2 * Math.PI * (i + 1) / degree + rotation);
const x2 = stretch * radius * Math.sin(angle);
const y2 = radius * Math.cos(angle);
const dx = x2 - x1;
const dy = y2 - y1;
const angle2 = -Math.atan2(dx, dy);
const sin = Math.sin(angle2);
const cos = Math.cos(angle2);
for (let j = 0; j < pointsPerEdge; j++) {
const fraction = edgeFractions[j];
const x3 = x1 + fraction * dx;
const y3 = y1 + fraction * dy;
points[index] = x3 + stretch * offsets[j] * cos;
index++;
points[index] = y3 + offsets[j] * sin;
index++;
}
x1 = x2;
y1 = y2;
}
return removeColinear(points);
}
function tangentialPolygonPoints(angles, radius1, radius2 = radius1) {
}
function parallelPolylinePoints(points, translateX, translateY, scaleX = 1, scaleY = 1) {
const numPoints = points.length >> 1;
const polygonPoints = new Float32Array(4 * numPoints);
for (let i = 0; i < numPoints; i++) {
let index = 2 * i;
const x = points[index];
polygonPoints[index] = x;
index++;
const y = points[index];
polygonPoints[index] = y;
index = 2 * (2 * numPoints - 1 - i);
const x2 = x * scaleX + translateX;
const y2 = y * scaleY + translateY;
polygonPoints[index] = x2;
polygonPoints[index + 1] = y2;
}
return removeColinear(polygonPoints);
}
function parallelPolylinePoints2(
points, translateY, centreDeltaX1, centreDeltaX2 = centreDeltaX1, mirrored = true
) {
const numPoints = points.length >> 1;
const polygonPoints = new Float32Array(4 * numPoints + 4);
const scaleY = mirrored ? -1 : 1;
let index;
for (let i = 0; i < numPoints; i++) {
index = 2 * i;
const x = points[index];
polygonPoints[index] = x;
index++;
const y = points[index];
polygonPoints[index] = y;
index = 2 * (2 * numPoints - i);
const y2 = y * scaleY + translateY;
polygonPoints[index] = x;
polygonPoints[index + 1] = y2;
}
index = 2 * numPoints;
polygonPoints[index] = points[index - 2] + centreDeltaX1;
const middleY = 0.5 * translateY;
polygonPoints[index + 1] = middleY;
index = 4 * numPoints + 2;
polygonPoints[index] = points[0] + centreDeltaX2;
polygonPoints[index + 1] = middleY;
return removeColinear(polygonPoints);
}
/**Rotates a polyline about (0, 0) and joins the two polylines together.
* @param {number} centreR1 The radius at the angle halfway between the two endpoints, or
* undefined to join the endpoints using a straight line.
*/
function rotatedPolylinePoints(points, angle, centreR1, centreR2, mirrored = true) {
const numPoints = points.length >> 1;
const polygonPoints = new Float32Array(4 * numPoints + 4);
const scaleTheta = mirrored ? -1 : 1;
let index;
for (let i = 0; i < numPoints; i++) {
index = 2 * i;
const x = points[index];
polygonPoints[index] = x;
index++;
const y = points[index];
polygonPoints[index] = y;
index = 2 * (2 * numPoints - i);
const radius = Math.hypot(x, y);
const theta = Math.atan2(y, x);
const theta2 = angle + scaleTheta * theta;
polygonPoints[index] = radius * Math.cos(theta2);
polygonPoints[index + 1] = radius * Math.sin(theta2);
}
index = 2 * numPoints;
const cos = Math.cos(0.5 * angle);
const sin = Math.sin(0.5 * angle);
if (centreR1 === undefined) {
polygonPoints[index] = polygonPoints[index - 2];
polygonPoints[index + 1] = polygonPoints[index - 1];
} else {
polygonPoints[index] = centreR1 * cos;
polygonPoints[index + 1] = centreR1 * sin;
}
index = 4 * numPoints + 2;
if (centreR2 === undefined) {
polygonPoints[index] = polygonPoints[0];
polygonPoints[index + 1] = polygonPoints[1];
} else {
polygonPoints[index] = centreR2 * cos;
polygonPoints[index + 1] = centreR2 * sin;
}
return removeColinear(polygonPoints);
}
function polarToRectPoints(angles, radii) {
const numSides = angles.length;
const points = new Float32Array(2 * numSides);
let index = 0;
for (let i = 0; i < numSides; i++) {
const angle = -angles[i];
const radius = radii[i] ?? radii[0];
const x = radius * Math.sin(angle);
const y = radius * Math.cos(angle);
points[index] = x;
index++;
points[index] = y;
index++;
}
return points;
}
function points2DTo3D(relativePoints, centre, orientation, extraPoints = 0) {
const numPoints = relativePoints.length >> 1;
const vertices = new Float32Array(3 * (numPoints + 1 + extraPoints));
switch (orientation) {
case AxesPlane.XY:
for (let i = 0; i < numPoints; i++) {
const offset2 = i << 1;
const offset3 = offset2 + i;
vertices[offset3] = relativePoints[offset2] + centre.x;
vertices[offset3 + 1] = relativePoints[offset2 + 1] + centre.y;
vertices[offset3 + 2] = centre.z;
}
break;
case AxesPlane.XZ:
for (let i = 0; i < numPoints; i++) {
const offset2 = i << 1;
const offset3 = offset2 + i;
vertices[offset3] = relativePoints[offset2] + centre.x;
vertices[offset3 + 1] = centre.y;
vertices[offset3 + 2] = -relativePoints[offset2 + 1] + centre.z;
}
break;
default:
for (let i = 0; i < numPoints; i++) {
const offset2 = i << 1;
const offset3 = offset2 + i;
vertices[offset3] = centre.x;
vertices[offset3 + 1] = relativePoints[offset2 + 1] + centre.y;
vertices[offset3 + 2] = -relativePoints[offset2] + centre.z;
}
}
// Compute centroid
let totalX = 0, totalY = 0, totalZ = 0;
let offset = 0;
for (let i = 0; i < numPoints; i++) {
totalX += vertices[offset];
totalY += vertices[offset + 1];
totalZ += vertices[offset + 2];
offset += 3;
}
vertices[offset] = totalX / numPoints;
vertices[offset + 1] = totalY / numPoints;
vertices[offset + 2] = totalZ / numPoints;
return vertices;
}
// Transformations
function setRotation(geometry, xDegrees, yDegrees, zDegrees) {
const xRadians = radians(xDegrees);
const yRadians = radians(yDegrees);
const zRadians = radians(zDegrees);
geometry.setRotationFromEuler(new THREE.Euler(xRadians, yRadians, zRadians, 'ZYX'));
}
// Geometries
function polygonGeometry(relativePoints, centre, orientation) {
const numPoints = relativePoints.length >> 1;
const vertices = points2DTo3D(relativePoints, centre, orientation);
const faces = new Array(3 * numPoints);
for (let i = 0; i < numPoints; i++) {
const offset = 3 * i;
faces[offset] = i;
faces[offset + 1] = (i + 1) % numPoints;
faces[offset + 2] = numPoints;
}
const geometry = new THREE.BufferGeometry();
geometry.setIndex(faces);
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
return geometry;
}
// Meshes
/**
* @param {number} [vertexAlignedToApex] Selects the nth and n+1th vertices from the polygon.
* @param {number} [apexVertexInterpolation] How far to move along the line between the two
* selected vertices (between 0 and 1).
* @param {number} [obliqueness] How much to shift the apex away from the centre of the polygon
* and towards the selected edge.
*/
function pyramid(
polygonPoints, centre, orientation, height, hasBase = true, materials = [],
vertexAlignedToApex = 0, apexVertexInterpolation = 0.5, obliqueness = 0
) {
}
export {
AxesPlane,
gcd,
radians,
removeColinear,
regularPolygonPoints,
starPolygonPoints,
cyclicPoints,
rotationalSymmetryPoints,
polarToRectPoints,
parallelPolylinePoints,
parallelPolylinePoints2,
rotatedPolylinePoints,
setRotation,
polygonGeometry,
}