poly-extrude 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/dist/cylinder.d.ts +11 -0
  2. package/{src → dist}/cylinder.js +108 -111
  3. package/dist/cylinder.js.map +1 -0
  4. package/dist/index.d.ts +7 -0
  5. package/dist/index.js +8 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/math/Curve.d.ts +41 -0
  8. package/dist/math/Curve.js +142 -0
  9. package/dist/math/Curve.js.map +1 -0
  10. package/dist/math/Interpolations.d.ts +8 -0
  11. package/dist/math/Interpolations.js +48 -0
  12. package/dist/math/Interpolations.js.map +1 -0
  13. package/dist/math/Matrix4.d.ts +8 -0
  14. package/dist/math/Matrix4.js +582 -0
  15. package/dist/math/Matrix4.js.map +1 -0
  16. package/dist/math/QuadraticBezierCurve3.d.ts +10 -0
  17. package/dist/math/QuadraticBezierCurve3.js +22 -0
  18. package/dist/math/QuadraticBezierCurve3.js.map +1 -0
  19. package/dist/math/Quaternion.d.ts +46 -0
  20. package/dist/math/Quaternion.js +415 -0
  21. package/dist/math/Quaternion.js.map +1 -0
  22. package/dist/math/Vector3.d.ts +42 -0
  23. package/dist/math/Vector3.js +403 -0
  24. package/dist/math/Vector3.js.map +1 -0
  25. package/dist/path/PathPoint.d.ts +15 -0
  26. package/dist/path/PathPoint.js +35 -0
  27. package/dist/path/PathPoint.js.map +1 -0
  28. package/dist/path/PathPointList.d.ts +27 -0
  29. package/dist/path/PathPointList.js +212 -0
  30. package/dist/path/PathPointList.js.map +1 -0
  31. package/dist/path.d.ts +11 -0
  32. package/{src → dist}/path.js +334 -360
  33. package/dist/path.js.map +1 -0
  34. package/dist/plane.d.ts +2 -0
  35. package/{src → dist}/plane.js +57 -58
  36. package/dist/plane.js.map +1 -0
  37. package/dist/poly-extrude.js +1286 -1581
  38. package/dist/poly-extrude.js.map +1 -1
  39. package/dist/poly-extrude.min.js +2 -2
  40. package/dist/poly-extrude.mjs +1286 -1581
  41. package/dist/poly-extrude.mjs.map +1 -0
  42. package/dist/polygon.d.ts +9 -0
  43. package/{src → dist}/polygon.js +163 -179
  44. package/dist/polygon.js.map +1 -0
  45. package/dist/polyline.d.ts +24 -0
  46. package/{src → dist}/polyline.js +420 -456
  47. package/dist/polyline.js.map +1 -0
  48. package/dist/tube.d.ts +12 -0
  49. package/{src → dist}/tube.js +124 -142
  50. package/dist/tube.js.map +1 -0
  51. package/dist/type.d.ts +9 -0
  52. package/dist/type.js +2 -0
  53. package/dist/type.js.map +1 -0
  54. package/dist/util.d.ts +20 -0
  55. package/dist/util.js +217 -0
  56. package/dist/util.js.map +1 -0
  57. package/package.json +53 -48
  58. package/readme.md +12 -2
  59. package/src/cylinder.ts +124 -0
  60. package/src/index.ts +7 -0
  61. package/src/path.ts +385 -0
  62. package/src/plane.ts +60 -0
  63. package/src/polygon.ts +189 -0
  64. package/src/polyline.ts +473 -0
  65. package/src/tube.ts +155 -0
  66. package/src/type.ts +9 -0
  67. package/src/{util.js → util.ts} +1 -1
  68. package/index.js +0 -7
@@ -1,456 +1,420 @@
1
- import { calLineDistance, degToRad, generateNormal, generateSideWallUV, merge, radToDeg } from './util';
2
-
3
- function checkOptions(options) {
4
- options.lineWidth = Math.max(0, options.lineWidth);
5
- options.depth = Math.max(0, options.depth);
6
- options.sideDepth = Math.max(0, options.sideDepth);
7
- }
8
-
9
- export function extrudePolylines(lines, options) {
10
- options = Object.assign({}, { depth: 2, lineWidth: 1, bottomStickGround: false, pathUV: false }, options);
11
- checkOptions(options);
12
- const results = lines.map(line => {
13
- const result = expandLine(line, options);
14
- result.line = line;
15
- generateTopAndBottom(result, options);
16
- generateSides(result, options);
17
- result.position = new Float32Array(result.points);
18
- result.indices = new Uint32Array(result.indices);
19
- result.uv = new Float32Array(result.uv);
20
- result.normal = generateNormal(result.indices, result.position);
21
- return result;
22
- });
23
- const result = merge(results);
24
- result.lines = lines;
25
- return result;
26
- }
27
-
28
- export function extrudeSlopes(lines, options) {
29
- options = Object.assign({}, { depth: 2, lineWidth: 1, side: 'left', sideDepth: 0, bottomStickGround: false, pathUV: false, isSlope: true }, options);
30
- checkOptions(options);
31
- const { depth, side, sideDepth } = options;
32
- const results = lines.map(line => {
33
- const tempResult = expandLine(line, options);
34
- tempResult.line = line;
35
- const { leftPoints, rightPoints } = tempResult;
36
- const result = { line };
37
- let depths;
38
- for (let i = 0, len = line.length; i < len; i++) {
39
- line[i][2] = line[i][2] || 0;
40
- }
41
- if (side === 'left') {
42
- result.leftPoints = leftPoints;
43
- result.rightPoints = line;
44
- depths = [sideDepth, depth];
45
- } else {
46
- result.leftPoints = line;
47
- result.rightPoints = rightPoints;
48
- depths = [depth, sideDepth];
49
- }
50
- result.depths = depths;
51
- generateTopAndBottom(result, options);
52
- generateSides(result, options);
53
- result.position = new Float32Array(result.points);
54
- result.indices = new Uint32Array(result.indices);
55
- result.uv = new Float32Array(result.uv);
56
- result.normal = generateNormal(result.indices, result.position);
57
- return result;
58
- });
59
- const result = merge(results);
60
- result.lines = lines;
61
- return result;
62
- }
63
-
64
- function generateTopAndBottom(result, options) {
65
- const bottomStickGround = options.bottomStickGround;
66
- const z = options.depth;
67
- const depths = result.depths;
68
- let lz = z, rz = z;
69
- if (depths) {
70
- lz = depths[0];
71
- rz = depths[1];
72
- }
73
- const { leftPoints, rightPoints } = result;
74
- const line = result.line;
75
- const pathUV = options.pathUV;
76
- if (pathUV) {
77
- calLineDistance(line);
78
- for (let i = 0, len = line.length; i < len; i++) {
79
- leftPoints[i].distance = rightPoints[i].distance = line[i].distance;
80
- }
81
- }
82
- let i = 0, len = leftPoints.length;
83
- const points = [], indices = [], uv = [];
84
- while (i < len) {
85
- // top left
86
- const idx0 = i * 3;
87
- const [x1, y1, z1] = leftPoints[i];
88
- points[idx0] = x1;
89
- points[idx0 + 1] = y1;
90
- points[idx0 + 2] = lz + z1;
91
-
92
- // top right
93
- const [x2, y2, z2] = rightPoints[i];
94
- const idx1 = len * 3 + idx0;
95
- points[idx1] = x2;
96
- points[idx1 + 1] = y2;
97
- points[idx1 + 2] = rz + z2;
98
-
99
- // bottom left
100
- const idx2 = (len * 2) * 3 + idx0;
101
- points[idx2] = x1;
102
- points[idx2 + 1] = y1;
103
- points[idx2 + 2] = z1;
104
- if (bottomStickGround) {
105
- points[idx2 + 2] = 0;
106
- }
107
-
108
- // bottom right
109
- const idx3 = (len * 2) * 3 + len * 3 + idx0;
110
- points[idx3] = x2;
111
- points[idx3 + 1] = y2;
112
- points[idx3 + 2] = z2;
113
- if (bottomStickGround) {
114
- points[idx3 + 2] = 0;
115
- }
116
-
117
- // generate path uv
118
- if (pathUV) {
119
- const p = line[i];
120
- const uvx = p.distance;
121
-
122
- const uIndex0 = i * 2;
123
- uv[uIndex0] = uvx;
124
- uv[uIndex0 + 1] = 1;
125
-
126
- const uIndex1 = len * 2 + uIndex0;
127
- uv[uIndex1] = uvx;
128
- uv[uIndex1 + 1] = 0;
129
-
130
- const uIndex2 = (len * 2) * 2 + uIndex0;
131
- uv[uIndex2] = uvx;
132
- uv[uIndex2 + 1] = 1;
133
-
134
- const uIndex3 = (len * 2) * 2 + len * 2 + uIndex0;
135
- uv[uIndex3] = uvx;
136
- uv[uIndex3 + 1] = 0;
137
-
138
- }
139
- i++;
140
- }
141
- if (!pathUV) {
142
- i = 0;
143
- len = points.length;
144
- let uIndex = uv.length - 1;
145
- while (i < len) {
146
- const x = points[i], y = points[i + 1];
147
- uv[++uIndex] = x;
148
- uv[++uIndex] = y;
149
- // uvs.push(x, y);
150
- i += 3;
151
- }
152
- }
153
-
154
- i = 0;
155
- len = leftPoints.length;
156
- let iIndex = indices.length - 1;
157
- while (i < len - 1) {
158
- // top
159
- // left1 left2 right1,right2
160
- const a1 = i, b1 = i + 1, c1 = a1 + len, d1 = b1 + len;
161
- indices[++iIndex] = a1;
162
- indices[++iIndex] = c1;
163
- indices[++iIndex] = b1;
164
- indices[++iIndex] = c1;
165
- indices[++iIndex] = d1;
166
- indices[++iIndex] = b1;
167
- // index.push(a1, c1, b1);
168
- // index.push(c1, d1, b1);
169
-
170
- // bottom
171
- // left1 left2 right1,right2
172
- const len2 = len * 2;
173
- const a2 = i + len2, b2 = a2 + 1, c2 = a2 + len, d2 = b2 + len;
174
- indices[++iIndex] = a2;
175
- indices[++iIndex] = c2;
176
- indices[++iIndex] = b2;
177
- indices[++iIndex] = c2;
178
- indices[++iIndex] = d2;
179
- indices[++iIndex] = b2;
180
- // index.push(a2, c2, b2);
181
- // index.push(c2, d2, b2);
182
- i++;
183
- }
184
- result.indices = indices;
185
- result.points = points;
186
- result.uv = uv;
187
- if (depths) {
188
- len = leftPoints.length;
189
- i = 0;
190
- while (i < len) {
191
- leftPoints[i].depth = lz;
192
- rightPoints[i].depth = rz;
193
- i++;
194
- }
195
- }
196
- }
197
-
198
- function generateSides(result, options) {
199
- const { points, indices, leftPoints, rightPoints, uv } = result;
200
- const z = options.depth;
201
- const bottomStickGround = options.bottomStickGround;
202
- const rings = [leftPoints, rightPoints];
203
- const depthsEnable = result.depths;
204
- const pathUV = options.pathUV;
205
- const lineWidth = options.lineWidth;
206
-
207
- let pIndex = points.length - 1;
208
- let iIndex = indices.length - 1;
209
- let uIndex = uv.length - 1;
210
-
211
- function addOneSideIndex(v1, v2) {
212
- const idx = points.length / 3;
213
- // let pIndex = points.length - 1;
214
-
215
- const v1Depth = (depthsEnable ? v1.depth : z);
216
- const v2Depth = (depthsEnable ? v2.depth : z);
217
-
218
- // top
219
- points[++pIndex] = v1[0];
220
- points[++pIndex] = v1[1];
221
- points[++pIndex] = v1Depth + v1[2];
222
-
223
- points[++pIndex] = v2[0];
224
- points[++pIndex] = v2[1];
225
- points[++pIndex] = v2Depth + v2[2];
226
-
227
- // points.push(v1[0], v1[1], (depthsEnable ? v1.depth : z) + v1[2], v2[0], v2[1], (depthsEnable ? v2.depth : z) + v2[2]);
228
-
229
- // bottom
230
-
231
- points[++pIndex] = v1[0];
232
- points[++pIndex] = v1[1];
233
- points[++pIndex] = bottomStickGround ? 0 : v1[2];
234
-
235
- points[++pIndex] = v2[0];
236
- points[++pIndex] = v2[1];
237
- points[++pIndex] = bottomStickGround ? 0 : v2[2];
238
-
239
- // points.push(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2]);
240
-
241
- const a = idx + 2, b = idx + 3, c = idx, d = idx + 1;
242
- indices[++iIndex] = a;
243
- indices[++iIndex] = c;
244
- indices[++iIndex] = b;
245
- indices[++iIndex] = c;
246
- indices[++iIndex] = d;
247
- indices[++iIndex] = b;
248
- // index.push(a, c, b, c, d, b);
249
- if (!pathUV) {
250
- generateSideWallUV(uv, points, a, b, c, d);
251
- } else {
252
- uv[++uIndex] = v1.distance;
253
- uv[++uIndex] = v1Depth / lineWidth;
254
-
255
- uv[++uIndex] = v2.distance;
256
- uv[++uIndex] = v2Depth / lineWidth;
257
-
258
- uv[++uIndex] = v1.distance;
259
- uv[++uIndex] = 0;
260
-
261
- uv[++uIndex] = v2.distance;
262
- uv[++uIndex] = 0;
263
- }
264
- }
265
-
266
- for (let i = 0, len = rings.length; i < len; i++) {
267
- let ring = rings[i];
268
- if (i > 0) {
269
- ring = ring.map(p => {
270
- return p;
271
- });
272
- ring = ring.reverse();
273
- }
274
- let j = 0;
275
- const len1 = ring.length - 1;
276
- while (j < len1) {
277
- const v1 = ring[j];
278
- const v2 = ring[j + 1];
279
- addOneSideIndex(v1, v2);
280
- j++;
281
- }
282
- }
283
- const len = leftPoints.length;
284
- const vs = [rightPoints[0], leftPoints[0], leftPoints[len - 1], rightPoints[len - 1]];
285
- for (let i = 0; i < vs.length; i += 2) {
286
- const v1 = vs[i], v2 = vs[i + 1];
287
- addOneSideIndex(v1, v2);
288
- }
289
- }
290
-
291
- const TEMPV1 = { x: 0, y: 0 }, TEMPV2 = { x: 0, y: 0 };
292
-
293
- export function expandLine(line, options) {
294
- // let preAngle = 0;
295
- let radius = options.lineWidth / 2;
296
- if (options.isSlope) {
297
- radius *= 2;
298
- }
299
- const points = [], leftPoints = [], rightPoints = [];
300
- const len = line.length;
301
- let i = 0;
302
- while (i < len) {
303
- let p1 = line[i],
304
- p2 = line[i + 1];
305
- const currentp = line[i];
306
- // last vertex
307
- if (i === len - 1) {
308
- p1 = line[len - 2];
309
- p2 = line[len - 1];
310
- }
311
- const dy = p2[1] - p1[1],
312
- dx = p2[0] - p1[0];
313
- let rAngle = 0;
314
- const rad = Math.atan(dy / dx);
315
- const angle = radToDeg(rad);
316
- // preAngle = angle;
317
- if (i === 0 || i === len - 1) {
318
- rAngle = angle;
319
- rAngle -= 90;
320
- } else {
321
- // 至少3个顶点才会触发
322
- const p0 = line[i - 1];
323
- TEMPV1.x = p0[0] - p1[0];
324
- TEMPV1.y = p0[1] - p1[1];
325
- TEMPV2.x = p2[0] - p1[0];
326
- TEMPV2.y = p2[1] - p1[1];
327
- const vAngle = getAngle(TEMPV1, TEMPV2);
328
- rAngle = angle - vAngle / 2;
329
- }
330
- const rRad = degToRad(rAngle);
331
- const p3 = currentp;
332
- const x = Math.cos(rRad) + p3[0], y = Math.sin(rRad) + p3[1];
333
- const p4 = [x, y];
334
- const [line1, line2] = translateLine(p1, p2, radius);
335
- let op1 = lineIntersection(line1[0], line1[1], p3, p4);
336
- let op2 = lineIntersection(line2[0], line2[1], p3, p4);
337
- // 平行,回头路
338
- if (!op1 || !op2) {
339
- const len1 = points.length;
340
- const point1 = points[len1 - 2];
341
- const point2 = points[len1 - 1];
342
- if (!point1 || !point2) {
343
- continue;
344
- }
345
- op1 = [point1[0], point1[1]];
346
- op2 = [point2[0], point2[1]];
347
- }
348
- op1[2] = currentp[2] || 0;
349
- op2[2] = currentp[2] || 0;
350
- // const [op1, op2] = calOffsetPoint(rRad, radius, p1);
351
- points.push(op1, op2);
352
- if (leftOnLine(op1, p1, p2)) {
353
- leftPoints.push(op1);
354
- rightPoints.push(op2);
355
- } else {
356
- leftPoints.push(op2);
357
- rightPoints.push(op1);
358
- }
359
- i++;
360
- }
361
-
362
- return { offsetPoints: points, leftPoints, rightPoints, line };
363
- }
364
-
365
- // eslint-disable-next-line no-unused-vars
366
- function calOffsetPoint(rad, radius, p) {
367
- const [x, y] = p;
368
- const z = p[2] || 0;
369
- const x1 = Math.cos(rad) * radius, y1 = Math.sin(rad) * radius;
370
- const p1 = [x + x1, y + y1, z];
371
- const rad1 = rad += Math.PI;
372
- const x2 = Math.cos(rad1) * radius, y2 = Math.sin(rad1) * radius;
373
- const p2 = [x + x2, y + y2, z];
374
- return [p1, p2];
375
- }
376
-
377
- const getAngle = ({ x: x1, y: y1 }, { x: x2, y: y2 }) => {
378
- const dot = x1 * x2 + y1 * y2;
379
- const det = x1 * y2 - y1 * x2;
380
- const angle = Math.atan2(det, dot) / Math.PI * 180;
381
- return (angle + 360) % 360;
382
- };
383
-
384
- export function leftOnLine(p, p1, p2) {
385
- const [x1, y1] = p1;
386
- const [x2, y2] = p2;
387
- const [x, y] = p;
388
- return (y1 - y2) * x + (x2 - x1) * y + x1 * y2 - x2 * y1 > 0;
389
- }
390
-
391
- /**
392
- * 平移线
393
- * @param {*} p1
394
- * @param {*} p2
395
- * @param {*} distance
396
- * @returns
397
- */
398
- function translateLine(p1, p2, distance) {
399
- const dy = p2[1] - p1[1], dx = p2[0] - p1[0];
400
- const rad = Math.atan2(dy, dx);
401
- const rad1 = rad + Math.PI / 2;
402
- let offsetX = Math.cos(rad1) * distance, offsetY = Math.sin(rad1) * distance;
403
- const tp1 = [p1[0] + offsetX, p1[1] + offsetY];
404
- const tp2 = [p2[0] + offsetX, p2[1] + offsetY];
405
- const rad2 = rad - Math.PI / 2;
406
- offsetX = Math.cos(rad2) * distance;
407
- offsetY = Math.sin(rad2) * distance;
408
- const tp3 = [p1[0] + offsetX, p1[1] + offsetY];
409
- const tp4 = [p2[0] + offsetX, p2[1] + offsetY];
410
- return [[tp1, tp2], [tp3, tp4]];
411
- }
412
-
413
- /**
414
- * 直线交点
415
- * @param {*} p1
416
- * @param {*} p2
417
- * @param {*} p3
418
- * @param {*} p4
419
- * @returns
420
- */
421
- function lineIntersection(p1, p2, p3, p4) {
422
- const dx1 = p2[0] - p1[0], dy1 = p2[1] - p1[1];
423
- const dx2 = p4[0] - p3[0], dy2 = p4[1] - p3[1];
424
- if (dx1 === 0 && dx2 === 0) {
425
- return null;
426
- }
427
- if (dy1 === 0 && dy2 === 0) {
428
- return null;
429
- }
430
-
431
- const k1 = dy1 / dx1;
432
- const k2 = dy2 / dx2;
433
-
434
- const b1 = p1[1] - k1 * p1[0];
435
- const b2 = p3[1] - k2 * p3[0];
436
-
437
- let x, y;
438
-
439
- if (dx1 === 0) {
440
- x = p1[0];
441
- y = k2 * x + b2;
442
- } else if (dx2 === 0) {
443
- x = p3[0];
444
- y = k1 * x + b1;
445
- } else if (dy1 === 0) {
446
- y = p1[1];
447
- x = (y - b2) / k2;
448
- } else if (dy2 === 0) {
449
- y = p3[1];
450
- x = (y - b1) / k1;
451
- } else {
452
- x = (b2 - b1) / (k1 - k2);
453
- y = k1 * x + b1;
454
- }
455
- return [x, y];
456
- }
1
+ import { calLineDistance, degToRad, generateNormal, generateSideWallUV, merge, radToDeg } from './util';
2
+ function checkOptions(options) {
3
+ options.lineWidth = Math.max(0, options.lineWidth);
4
+ options.depth = Math.max(0, options.depth);
5
+ options.sideDepth = Math.max(0, options.sideDepth);
6
+ }
7
+ export function extrudePolylines(lines, options) {
8
+ options = Object.assign({}, { depth: 2, lineWidth: 1, bottomStickGround: false, pathUV: false }, options);
9
+ checkOptions(options);
10
+ const results = lines.map(line => {
11
+ const result = expandLine(line, options);
12
+ result.line = line;
13
+ generateTopAndBottom(result, options);
14
+ generateSides(result, options);
15
+ result.position = new Float32Array(result.points);
16
+ result.indices = new Uint32Array(result.indices);
17
+ result.uv = new Float32Array(result.uv);
18
+ result.normal = generateNormal(result.indices, result.position);
19
+ return result;
20
+ });
21
+ const result = merge(results);
22
+ result.lines = lines;
23
+ return result;
24
+ }
25
+ export function extrudeSlopes(lines, options) {
26
+ options = Object.assign({}, { depth: 2, lineWidth: 1, side: 'left', sideDepth: 0, bottomStickGround: false, pathUV: false, isSlope: true }, options);
27
+ checkOptions(options);
28
+ const { depth, side, sideDepth } = options;
29
+ const results = lines.map(line => {
30
+ const tempResult = expandLine(line, options);
31
+ tempResult.line = line;
32
+ const { leftPoints, rightPoints } = tempResult;
33
+ const result = { line };
34
+ let depths;
35
+ for (let i = 0, len = line.length; i < len; i++) {
36
+ line[i][2] = line[i][2] || 0;
37
+ }
38
+ if (side === 'left') {
39
+ result.leftPoints = leftPoints;
40
+ result.rightPoints = line;
41
+ depths = [sideDepth, depth];
42
+ }
43
+ else {
44
+ result.leftPoints = line;
45
+ result.rightPoints = rightPoints;
46
+ depths = [depth, sideDepth];
47
+ }
48
+ result.depths = depths;
49
+ generateTopAndBottom(result, options);
50
+ generateSides(result, options);
51
+ result.position = new Float32Array(result.points);
52
+ result.indices = new Uint32Array(result.indices);
53
+ result.uv = new Float32Array(result.uv);
54
+ result.normal = generateNormal(result.indices, result.position);
55
+ return result;
56
+ });
57
+ const result = merge(results);
58
+ result.lines = lines;
59
+ return result;
60
+ }
61
+ function generateTopAndBottom(result, options) {
62
+ const bottomStickGround = options.bottomStickGround;
63
+ const z = options.depth;
64
+ const depths = result.depths;
65
+ let lz = z, rz = z;
66
+ if (depths) {
67
+ lz = depths[0];
68
+ rz = depths[1];
69
+ }
70
+ const { leftPoints, rightPoints } = result;
71
+ const line = result.line;
72
+ const pathUV = options.pathUV;
73
+ if (pathUV) {
74
+ calLineDistance(line);
75
+ for (let i = 0, len = line.length; i < len; i++) {
76
+ leftPoints[i].distance = rightPoints[i].distance = line[i].distance;
77
+ }
78
+ }
79
+ let i = 0, len = leftPoints.length;
80
+ const points = [], indices = [], uv = [];
81
+ while (i < len) {
82
+ // top left
83
+ const idx0 = i * 3;
84
+ const [x1, y1, z1] = leftPoints[i];
85
+ points[idx0] = x1;
86
+ points[idx0 + 1] = y1;
87
+ points[idx0 + 2] = lz + z1;
88
+ // top right
89
+ const [x2, y2, z2] = rightPoints[i];
90
+ const idx1 = len * 3 + idx0;
91
+ points[idx1] = x2;
92
+ points[idx1 + 1] = y2;
93
+ points[idx1 + 2] = rz + z2;
94
+ // bottom left
95
+ const idx2 = (len * 2) * 3 + idx0;
96
+ points[idx2] = x1;
97
+ points[idx2 + 1] = y1;
98
+ points[idx2 + 2] = z1;
99
+ if (bottomStickGround) {
100
+ points[idx2 + 2] = 0;
101
+ }
102
+ // bottom right
103
+ const idx3 = (len * 2) * 3 + len * 3 + idx0;
104
+ points[idx3] = x2;
105
+ points[idx3 + 1] = y2;
106
+ points[idx3 + 2] = z2;
107
+ if (bottomStickGround) {
108
+ points[idx3 + 2] = 0;
109
+ }
110
+ // generate path uv
111
+ if (pathUV) {
112
+ const p = line[i];
113
+ const uvx = p.distance;
114
+ const uIndex0 = i * 2;
115
+ uv[uIndex0] = uvx;
116
+ uv[uIndex0 + 1] = 1;
117
+ const uIndex1 = len * 2 + uIndex0;
118
+ uv[uIndex1] = uvx;
119
+ uv[uIndex1 + 1] = 0;
120
+ const uIndex2 = (len * 2) * 2 + uIndex0;
121
+ uv[uIndex2] = uvx;
122
+ uv[uIndex2 + 1] = 1;
123
+ const uIndex3 = (len * 2) * 2 + len * 2 + uIndex0;
124
+ uv[uIndex3] = uvx;
125
+ uv[uIndex3 + 1] = 0;
126
+ }
127
+ i++;
128
+ }
129
+ if (!pathUV) {
130
+ i = 0;
131
+ len = points.length;
132
+ let uIndex = uv.length - 1;
133
+ while (i < len) {
134
+ const x = points[i], y = points[i + 1];
135
+ uv[++uIndex] = x;
136
+ uv[++uIndex] = y;
137
+ // uvs.push(x, y);
138
+ i += 3;
139
+ }
140
+ }
141
+ i = 0;
142
+ len = leftPoints.length;
143
+ let iIndex = indices.length - 1;
144
+ while (i < len - 1) {
145
+ // top
146
+ // left1 left2 right1,right2
147
+ const a1 = i, b1 = i + 1, c1 = a1 + len, d1 = b1 + len;
148
+ indices[++iIndex] = a1;
149
+ indices[++iIndex] = c1;
150
+ indices[++iIndex] = b1;
151
+ indices[++iIndex] = c1;
152
+ indices[++iIndex] = d1;
153
+ indices[++iIndex] = b1;
154
+ // index.push(a1, c1, b1);
155
+ // index.push(c1, d1, b1);
156
+ // bottom
157
+ // left1 left2 right1,right2
158
+ const len2 = len * 2;
159
+ const a2 = i + len2, b2 = a2 + 1, c2 = a2 + len, d2 = b2 + len;
160
+ indices[++iIndex] = a2;
161
+ indices[++iIndex] = c2;
162
+ indices[++iIndex] = b2;
163
+ indices[++iIndex] = c2;
164
+ indices[++iIndex] = d2;
165
+ indices[++iIndex] = b2;
166
+ // index.push(a2, c2, b2);
167
+ // index.push(c2, d2, b2);
168
+ i++;
169
+ }
170
+ result.indices = indices;
171
+ result.points = points;
172
+ result.uv = uv;
173
+ if (depths) {
174
+ len = leftPoints.length;
175
+ i = 0;
176
+ while (i < len) {
177
+ leftPoints[i].depth = lz;
178
+ rightPoints[i].depth = rz;
179
+ i++;
180
+ }
181
+ }
182
+ }
183
+ function generateSides(result, options) {
184
+ const { points, indices, leftPoints, rightPoints, uv } = result;
185
+ const z = options.depth;
186
+ const bottomStickGround = options.bottomStickGround;
187
+ const rings = [leftPoints, rightPoints];
188
+ const depthsEnable = result.depths;
189
+ const pathUV = options.pathUV;
190
+ const lineWidth = options.lineWidth;
191
+ let pIndex = points.length - 1;
192
+ let iIndex = indices.length - 1;
193
+ let uIndex = uv.length - 1;
194
+ function addOneSideIndex(v1, v2) {
195
+ const idx = points.length / 3;
196
+ // let pIndex = points.length - 1;
197
+ const v1Depth = (depthsEnable ? v1.depth : z);
198
+ const v2Depth = (depthsEnable ? v2.depth : z);
199
+ // top
200
+ points[++pIndex] = v1[0];
201
+ points[++pIndex] = v1[1];
202
+ points[++pIndex] = v1Depth + v1[2];
203
+ points[++pIndex] = v2[0];
204
+ points[++pIndex] = v2[1];
205
+ points[++pIndex] = v2Depth + v2[2];
206
+ // points.push(v1[0], v1[1], (depthsEnable ? v1.depth : z) + v1[2], v2[0], v2[1], (depthsEnable ? v2.depth : z) + v2[2]);
207
+ // bottom
208
+ points[++pIndex] = v1[0];
209
+ points[++pIndex] = v1[1];
210
+ points[++pIndex] = bottomStickGround ? 0 : v1[2];
211
+ points[++pIndex] = v2[0];
212
+ points[++pIndex] = v2[1];
213
+ points[++pIndex] = bottomStickGround ? 0 : v2[2];
214
+ // points.push(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2]);
215
+ const a = idx + 2, b = idx + 3, c = idx, d = idx + 1;
216
+ indices[++iIndex] = a;
217
+ indices[++iIndex] = c;
218
+ indices[++iIndex] = b;
219
+ indices[++iIndex] = c;
220
+ indices[++iIndex] = d;
221
+ indices[++iIndex] = b;
222
+ // index.push(a, c, b, c, d, b);
223
+ if (!pathUV) {
224
+ generateSideWallUV(uv, points, a, b, c, d);
225
+ }
226
+ else {
227
+ uv[++uIndex] = v1.distance;
228
+ uv[++uIndex] = v1Depth / lineWidth;
229
+ uv[++uIndex] = v2.distance;
230
+ uv[++uIndex] = v2Depth / lineWidth;
231
+ uv[++uIndex] = v1.distance;
232
+ uv[++uIndex] = 0;
233
+ uv[++uIndex] = v2.distance;
234
+ uv[++uIndex] = 0;
235
+ }
236
+ }
237
+ for (let i = 0, len = rings.length; i < len; i++) {
238
+ let ring = rings[i];
239
+ if (i > 0) {
240
+ ring = ring.map(p => {
241
+ return p;
242
+ });
243
+ ring = ring.reverse();
244
+ }
245
+ let j = 0;
246
+ const len1 = ring.length - 1;
247
+ while (j < len1) {
248
+ const v1 = ring[j];
249
+ const v2 = ring[j + 1];
250
+ addOneSideIndex(v1, v2);
251
+ j++;
252
+ }
253
+ }
254
+ const len = leftPoints.length;
255
+ const vs = [rightPoints[0], leftPoints[0], leftPoints[len - 1], rightPoints[len - 1]];
256
+ for (let i = 0; i < vs.length; i += 2) {
257
+ const v1 = vs[i], v2 = vs[i + 1];
258
+ addOneSideIndex(v1, v2);
259
+ }
260
+ }
261
+ const TEMPV1 = { x: 0, y: 0 }, TEMPV2 = { x: 0, y: 0 };
262
+ export function expandLine(line, options) {
263
+ // let preAngle = 0;
264
+ let radius = options.lineWidth / 2;
265
+ if (options.isSlope) {
266
+ radius *= 2;
267
+ }
268
+ const points = [], leftPoints = [], rightPoints = [];
269
+ const len = line.length;
270
+ let i = 0;
271
+ while (i < len) {
272
+ let p1 = line[i], p2 = line[i + 1];
273
+ const currentp = line[i];
274
+ // last vertex
275
+ if (i === len - 1) {
276
+ p1 = line[len - 2];
277
+ p2 = line[len - 1];
278
+ }
279
+ const dy = p2[1] - p1[1], dx = p2[0] - p1[0];
280
+ let rAngle = 0;
281
+ const rad = Math.atan(dy / dx);
282
+ const angle = radToDeg(rad);
283
+ // preAngle = angle;
284
+ if (i === 0 || i === len - 1) {
285
+ rAngle = angle;
286
+ rAngle -= 90;
287
+ }
288
+ else {
289
+ // 至少3个顶点才会触发
290
+ const p0 = line[i - 1];
291
+ TEMPV1.x = p0[0] - p1[0];
292
+ TEMPV1.y = p0[1] - p1[1];
293
+ TEMPV2.x = p2[0] - p1[0];
294
+ TEMPV2.y = p2[1] - p1[1];
295
+ const vAngle = getAngle(TEMPV1, TEMPV2);
296
+ rAngle = angle - vAngle / 2;
297
+ }
298
+ const rRad = degToRad(rAngle);
299
+ const p3 = currentp;
300
+ const x = Math.cos(rRad) + p3[0], y = Math.sin(rRad) + p3[1];
301
+ const p4 = [x, y];
302
+ const [line1, line2] = translateLine(p1, p2, radius);
303
+ let op1 = lineIntersection(line1[0], line1[1], p3, p4);
304
+ let op2 = lineIntersection(line2[0], line2[1], p3, p4);
305
+ // 平行,回头路
306
+ if (!op1 || !op2) {
307
+ const len1 = points.length;
308
+ const point1 = points[len1 - 2];
309
+ const point2 = points[len1 - 1];
310
+ if (!point1 || !point2) {
311
+ continue;
312
+ }
313
+ op1 = [point1[0], point1[1]];
314
+ op2 = [point2[0], point2[1]];
315
+ }
316
+ op1[2] = currentp[2] || 0;
317
+ op2[2] = currentp[2] || 0;
318
+ // const [op1, op2] = calOffsetPoint(rRad, radius, p1);
319
+ points.push(op1, op2);
320
+ if (leftOnLine(op1, p1, p2)) {
321
+ leftPoints.push(op1);
322
+ rightPoints.push(op2);
323
+ }
324
+ else {
325
+ leftPoints.push(op2);
326
+ rightPoints.push(op1);
327
+ }
328
+ i++;
329
+ }
330
+ return { offsetPoints: points, leftPoints, rightPoints, line };
331
+ }
332
+ // eslint-disable-next-line no-unused-vars
333
+ function calOffsetPoint(rad, radius, p) {
334
+ const [x, y] = p;
335
+ const z = p[2] || 0;
336
+ const x1 = Math.cos(rad) * radius, y1 = Math.sin(rad) * radius;
337
+ const p1 = [x + x1, y + y1, z];
338
+ const rad1 = rad += Math.PI;
339
+ const x2 = Math.cos(rad1) * radius, y2 = Math.sin(rad1) * radius;
340
+ const p2 = [x + x2, y + y2, z];
341
+ return [p1, p2];
342
+ }
343
+ const getAngle = ({ x: x1, y: y1 }, { x: x2, y: y2 }) => {
344
+ const dot = x1 * x2 + y1 * y2;
345
+ const det = x1 * y2 - y1 * x2;
346
+ const angle = Math.atan2(det, dot) / Math.PI * 180;
347
+ return (angle + 360) % 360;
348
+ };
349
+ export function leftOnLine(p, p1, p2) {
350
+ const [x1, y1] = p1;
351
+ const [x2, y2] = p2;
352
+ const [x, y] = p;
353
+ return (y1 - y2) * x + (x2 - x1) * y + x1 * y2 - x2 * y1 > 0;
354
+ }
355
+ /**
356
+ * 平移线
357
+ * @param {*} p1
358
+ * @param {*} p2
359
+ * @param {*} distance
360
+ * @returns
361
+ */
362
+ function translateLine(p1, p2, distance) {
363
+ const dy = p2[1] - p1[1], dx = p2[0] - p1[0];
364
+ const rad = Math.atan2(dy, dx);
365
+ const rad1 = rad + Math.PI / 2;
366
+ let offsetX = Math.cos(rad1) * distance, offsetY = Math.sin(rad1) * distance;
367
+ const tp1 = [p1[0] + offsetX, p1[1] + offsetY];
368
+ const tp2 = [p2[0] + offsetX, p2[1] + offsetY];
369
+ const rad2 = rad - Math.PI / 2;
370
+ offsetX = Math.cos(rad2) * distance;
371
+ offsetY = Math.sin(rad2) * distance;
372
+ const tp3 = [p1[0] + offsetX, p1[1] + offsetY];
373
+ const tp4 = [p2[0] + offsetX, p2[1] + offsetY];
374
+ return [[tp1, tp2], [tp3, tp4]];
375
+ }
376
+ /**
377
+ * 直线交点
378
+ * @param {*} p1
379
+ * @param {*} p2
380
+ * @param {*} p3
381
+ * @param {*} p4
382
+ * @returns
383
+ */
384
+ function lineIntersection(p1, p2, p3, p4) {
385
+ const dx1 = p2[0] - p1[0], dy1 = p2[1] - p1[1];
386
+ const dx2 = p4[0] - p3[0], dy2 = p4[1] - p3[1];
387
+ if (dx1 === 0 && dx2 === 0) {
388
+ return null;
389
+ }
390
+ if (dy1 === 0 && dy2 === 0) {
391
+ return null;
392
+ }
393
+ const k1 = dy1 / dx1;
394
+ const k2 = dy2 / dx2;
395
+ const b1 = p1[1] - k1 * p1[0];
396
+ const b2 = p3[1] - k2 * p3[0];
397
+ let x, y;
398
+ if (dx1 === 0) {
399
+ x = p1[0];
400
+ y = k2 * x + b2;
401
+ }
402
+ else if (dx2 === 0) {
403
+ x = p3[0];
404
+ y = k1 * x + b1;
405
+ }
406
+ else if (dy1 === 0) {
407
+ y = p1[1];
408
+ x = (y - b2) / k2;
409
+ }
410
+ else if (dy2 === 0) {
411
+ y = p3[1];
412
+ x = (y - b1) / k1;
413
+ }
414
+ else {
415
+ x = (b2 - b1) / (k1 - k2);
416
+ y = k1 * x + b1;
417
+ }
418
+ return [x, y];
419
+ }
420
+ //# sourceMappingURL=polyline.js.map