poly-extrude 0.12.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.
- package/dist/cylinder.d.ts +11 -0
- package/{src → dist}/cylinder.js +108 -111
- package/dist/cylinder.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/math/Curve.d.ts +41 -0
- package/dist/math/Curve.js +142 -0
- package/dist/math/Curve.js.map +1 -0
- package/dist/math/Interpolations.d.ts +8 -0
- package/dist/math/Interpolations.js +48 -0
- package/dist/math/Interpolations.js.map +1 -0
- package/dist/math/Matrix4.d.ts +8 -0
- package/dist/math/Matrix4.js +582 -0
- package/dist/math/Matrix4.js.map +1 -0
- package/dist/math/QuadraticBezierCurve3.d.ts +10 -0
- package/dist/math/QuadraticBezierCurve3.js +22 -0
- package/dist/math/QuadraticBezierCurve3.js.map +1 -0
- package/dist/math/Quaternion.d.ts +46 -0
- package/dist/math/Quaternion.js +415 -0
- package/dist/math/Quaternion.js.map +1 -0
- package/dist/math/Vector3.d.ts +42 -0
- package/dist/math/Vector3.js +403 -0
- package/dist/math/Vector3.js.map +1 -0
- package/dist/path/PathPoint.d.ts +15 -0
- package/dist/path/PathPoint.js +35 -0
- package/dist/path/PathPoint.js.map +1 -0
- package/dist/path/PathPointList.d.ts +27 -0
- package/dist/path/PathPointList.js +212 -0
- package/dist/path/PathPointList.js.map +1 -0
- package/dist/path.d.ts +11 -0
- package/{src → dist}/path.js +334 -360
- package/dist/path.js.map +1 -0
- package/dist/plane.d.ts +2 -0
- package/dist/plane.js +57 -0
- package/dist/plane.js.map +1 -0
- package/dist/poly-extrude.js +1290 -1511
- package/dist/poly-extrude.js.map +1 -1
- package/dist/poly-extrude.min.js +2 -2
- package/dist/poly-extrude.mjs +1290 -1512
- package/dist/poly-extrude.mjs.map +1 -0
- package/dist/polygon.d.ts +9 -0
- package/{src → dist}/polygon.js +163 -179
- package/dist/polygon.js.map +1 -0
- package/dist/polyline.d.ts +24 -0
- package/{src → dist}/polyline.js +420 -456
- package/dist/polyline.js.map +1 -0
- package/dist/tube.d.ts +12 -0
- package/{src → dist}/tube.js +124 -142
- package/dist/tube.js.map +1 -0
- package/dist/type.d.ts +9 -0
- package/dist/type.js +2 -0
- package/dist/type.js.map +1 -0
- package/dist/util.d.ts +20 -0
- package/dist/util.js +217 -0
- package/dist/util.js.map +1 -0
- package/package.json +53 -48
- package/readme.md +39 -3
- package/src/cylinder.ts +124 -0
- package/src/index.ts +7 -0
- package/src/path.ts +385 -0
- package/src/plane.ts +60 -0
- package/src/polygon.ts +189 -0
- package/src/polyline.ts +473 -0
- package/src/tube.ts +155 -0
- package/src/type.ts +9 -0
- package/src/{util.js → util.ts} +1 -1
- package/index.js +0 -6
package/src/path.ts
ADDED
@@ -0,0 +1,385 @@
|
|
1
|
+
import { Vector3 } from './math/Vector3';
|
2
|
+
import { PathPoint } from './path/PathPoint';
|
3
|
+
import { PathPointList } from './path/PathPointList';
|
4
|
+
import { PolylineType, ResultType } from './type';
|
5
|
+
import { line2Vectors, merge } from './util';
|
6
|
+
const UP = new Vector3(0, 0, 1);
|
7
|
+
|
8
|
+
const right = new Vector3();
|
9
|
+
const left = new Vector3();
|
10
|
+
|
11
|
+
// for sharp corners
|
12
|
+
const leftOffset = new Vector3();
|
13
|
+
const rightOffset = new Vector3();
|
14
|
+
const tempPoint1 = new Vector3();
|
15
|
+
const tempPoint2 = new Vector3();
|
16
|
+
|
17
|
+
type PathsOptions = {
|
18
|
+
lineWidth?: number;
|
19
|
+
cornerRadius?: number;
|
20
|
+
cornerSplit?: number;
|
21
|
+
}
|
22
|
+
|
23
|
+
type PathsResult = ResultType & {
|
24
|
+
lines: Array<PolylineType>;
|
25
|
+
}
|
26
|
+
|
27
|
+
export function expandPaths(lines: Array<PolylineType>, options?: PathsOptions): PathsResult {
|
28
|
+
options = Object.assign({}, { lineWidth: 1, cornerRadius: 0, cornerSplit: 10 }, options);
|
29
|
+
const results = lines.map(line => {
|
30
|
+
const points = line2Vectors(line);
|
31
|
+
const pathPointList = new PathPointList();
|
32
|
+
//@ts-ignore
|
33
|
+
pathPointList.set(points, options.cornerRadius, options.cornerSplit, UP);
|
34
|
+
const params = generatePathVertexData(pathPointList, options);
|
35
|
+
|
36
|
+
const result = {
|
37
|
+
position: new Float32Array(params.position),
|
38
|
+
indices: new Uint32Array(params.indices),
|
39
|
+
uv: new Float32Array(params.uv),
|
40
|
+
normal: new Float32Array(params.normal),
|
41
|
+
line,
|
42
|
+
count: params.count
|
43
|
+
}
|
44
|
+
return result;
|
45
|
+
});
|
46
|
+
const result = merge(results) as PathsResult;
|
47
|
+
result.lines = lines;
|
48
|
+
return result;
|
49
|
+
}
|
50
|
+
|
51
|
+
// Vertex Data Generate Functions
|
52
|
+
// code copy from https://github.com/shawn0326/three.path/blob/master/src/PathGeometry.js
|
53
|
+
function generatePathVertexData(pathPointList, options: PathsOptions) {
|
54
|
+
const width = options.lineWidth || 0.1;
|
55
|
+
const progress = 1;
|
56
|
+
const side = 'both';
|
57
|
+
|
58
|
+
const halfWidth = width / 2;
|
59
|
+
const sideWidth = (side !== 'both' ? width / 2 : width);
|
60
|
+
const totalDistance = pathPointList.distance();
|
61
|
+
const progressDistance = progress * totalDistance;
|
62
|
+
|
63
|
+
let count = 0;
|
64
|
+
|
65
|
+
// modify data
|
66
|
+
const position: number[] = [];
|
67
|
+
const normal: number[] = [];
|
68
|
+
const uv: number[] = [];
|
69
|
+
const indices: number[] = [];
|
70
|
+
let verticesCount = 0;
|
71
|
+
|
72
|
+
if (totalDistance === 0) {
|
73
|
+
return {
|
74
|
+
position: position,
|
75
|
+
normal,
|
76
|
+
uv: uv,
|
77
|
+
indices: indices,
|
78
|
+
count
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
const sharpUvOffset = halfWidth / sideWidth;
|
83
|
+
// const sharpUvOffset2 = halfWidth / totalDistance;
|
84
|
+
|
85
|
+
|
86
|
+
let pIndex = position.length - 1;
|
87
|
+
let nIndex = normal.length - 1;
|
88
|
+
let uIndex = uv.length - 1;
|
89
|
+
let iIndex = indices.length - 1;
|
90
|
+
function addVertices(pathPoint) {
|
91
|
+
const first = position.length === 0;
|
92
|
+
const sharpCorner = pathPoint.sharp && !first;
|
93
|
+
|
94
|
+
const uvDist = pathPoint.dist / sideWidth;
|
95
|
+
// const uvDist2 = pathPoint.dist / totalDistance;
|
96
|
+
|
97
|
+
const dir = pathPoint.dir;
|
98
|
+
const up = pathPoint.up;
|
99
|
+
const _right = pathPoint.right;
|
100
|
+
|
101
|
+
//@ts-ignore
|
102
|
+
if (side !== 'left') {
|
103
|
+
right.copy(_right).multiplyScalar(halfWidth * pathPoint.widthScale);
|
104
|
+
} else {
|
105
|
+
right.set(0, 0, 0);
|
106
|
+
}
|
107
|
+
//@ts-ignore
|
108
|
+
if (side !== 'right') {
|
109
|
+
left.copy(_right).multiplyScalar(-halfWidth * pathPoint.widthScale);
|
110
|
+
} else {
|
111
|
+
left.set(0, 0, 0);
|
112
|
+
}
|
113
|
+
|
114
|
+
right.add(pathPoint.pos);
|
115
|
+
left.add(pathPoint.pos);
|
116
|
+
|
117
|
+
if (sharpCorner) {
|
118
|
+
leftOffset.fromArray(position, position.length - 6).sub(left);
|
119
|
+
rightOffset.fromArray(position, position.length - 3).sub(right);
|
120
|
+
|
121
|
+
const leftDist = leftOffset.length();
|
122
|
+
const rightDist = rightOffset.length();
|
123
|
+
|
124
|
+
const sideOffset = leftDist - rightDist;
|
125
|
+
let longerOffset, longEdge;
|
126
|
+
|
127
|
+
if (sideOffset > 0) {
|
128
|
+
longerOffset = leftOffset;
|
129
|
+
longEdge = left;
|
130
|
+
} else {
|
131
|
+
longerOffset = rightOffset;
|
132
|
+
longEdge = right;
|
133
|
+
}
|
134
|
+
|
135
|
+
tempPoint1.copy(longerOffset).setLength(Math.abs(sideOffset)).add(longEdge);
|
136
|
+
|
137
|
+
// eslint-disable-next-line prefer-const
|
138
|
+
let _cos = tempPoint2.copy(longEdge).sub(tempPoint1).normalize().dot(dir);
|
139
|
+
// eslint-disable-next-line prefer-const
|
140
|
+
let _len = tempPoint2.copy(longEdge).sub(tempPoint1).length();
|
141
|
+
// eslint-disable-next-line prefer-const
|
142
|
+
let _dist = _cos * _len * 2;
|
143
|
+
|
144
|
+
tempPoint2.copy(dir).setLength(_dist).add(tempPoint1);
|
145
|
+
|
146
|
+
if (sideOffset > 0) {
|
147
|
+
position[++pIndex] = tempPoint1.x;
|
148
|
+
position[++pIndex] = tempPoint1.y;
|
149
|
+
position[++pIndex] = tempPoint1.z;
|
150
|
+
position[++pIndex] = right.x;
|
151
|
+
position[++pIndex] = right.y;
|
152
|
+
position[++pIndex] = right.z;
|
153
|
+
position[++pIndex] = left.x;
|
154
|
+
position[++pIndex] = left.y;
|
155
|
+
position[++pIndex] = left.z;
|
156
|
+
position[++pIndex] = right.x;
|
157
|
+
position[++pIndex] = right.y;
|
158
|
+
position[++pIndex] = right.z;
|
159
|
+
position[++pIndex] = tempPoint2.x;
|
160
|
+
position[++pIndex] = tempPoint2.y;
|
161
|
+
position[++pIndex] = tempPoint2.z;
|
162
|
+
position[++pIndex] = right.x;
|
163
|
+
position[++pIndex] = right.y;
|
164
|
+
position[++pIndex] = right.z;
|
165
|
+
// position.push(
|
166
|
+
// tempPoint1.x, tempPoint1.y, tempPoint1.z, // 6
|
167
|
+
// right.x, right.y, right.z, // 5
|
168
|
+
// left.x, left.y, left.z, // 4
|
169
|
+
// right.x, right.y, right.z, // 3
|
170
|
+
// tempPoint2.x, tempPoint2.y, tempPoint2.z, // 2
|
171
|
+
// right.x, right.y, right.z // 1
|
172
|
+
// );
|
173
|
+
|
174
|
+
verticesCount += 6;
|
175
|
+
|
176
|
+
indices[++iIndex] = verticesCount - 6;
|
177
|
+
indices[++iIndex] = verticesCount - 8;
|
178
|
+
indices[++iIndex] = verticesCount - 7;
|
179
|
+
indices[++iIndex] = verticesCount - 6;
|
180
|
+
indices[++iIndex] = verticesCount - 7;
|
181
|
+
indices[++iIndex] = verticesCount - 5;
|
182
|
+
indices[++iIndex] = verticesCount - 4;
|
183
|
+
indices[++iIndex] = verticesCount - 6;
|
184
|
+
indices[++iIndex] = verticesCount - 5;
|
185
|
+
indices[++iIndex] = verticesCount - 2;
|
186
|
+
indices[++iIndex] = verticesCount - 4;
|
187
|
+
indices[++iIndex] = verticesCount - 1;
|
188
|
+
|
189
|
+
// indices.push(
|
190
|
+
// verticesCount - 6, verticesCount - 8, verticesCount - 7,
|
191
|
+
// verticesCount - 6, verticesCount - 7, verticesCount - 5,
|
192
|
+
|
193
|
+
// verticesCount - 4, verticesCount - 6, verticesCount - 5,
|
194
|
+
// verticesCount - 2, verticesCount - 4, verticesCount - 1
|
195
|
+
// );
|
196
|
+
|
197
|
+
count += 12;
|
198
|
+
} else {
|
199
|
+
position[++pIndex] = left.x;
|
200
|
+
position[++pIndex] = left.y;
|
201
|
+
position[++pIndex] = left.z;
|
202
|
+
position[++pIndex] = tempPoint1.x;
|
203
|
+
position[++pIndex] = tempPoint1.y;
|
204
|
+
position[++pIndex] = tempPoint1.z;
|
205
|
+
position[++pIndex] = left.x;
|
206
|
+
position[++pIndex] = left.y;
|
207
|
+
position[++pIndex] = left.z;
|
208
|
+
position[++pIndex] = right.x;
|
209
|
+
position[++pIndex] = right.y;
|
210
|
+
position[++pIndex] = right.z;
|
211
|
+
position[++pIndex] = left.x;
|
212
|
+
position[++pIndex] = left.y;
|
213
|
+
position[++pIndex] = left.z;
|
214
|
+
position[++pIndex] = tempPoint2.x;
|
215
|
+
position[++pIndex] = tempPoint2.y;
|
216
|
+
position[++pIndex] = tempPoint2.z;
|
217
|
+
// position.push(
|
218
|
+
// left.x, left.y, left.z, // 6
|
219
|
+
// tempPoint1.x, tempPoint1.y, tempPoint1.z, // 5
|
220
|
+
// left.x, left.y, left.z, // 4
|
221
|
+
// right.x, right.y, right.z, // 3
|
222
|
+
// left.x, left.y, left.z, // 2
|
223
|
+
// tempPoint2.x, tempPoint2.y, tempPoint2.z // 1
|
224
|
+
// );
|
225
|
+
|
226
|
+
verticesCount += 6;
|
227
|
+
indices[++iIndex] = verticesCount - 6;
|
228
|
+
indices[++iIndex] = verticesCount - 8;
|
229
|
+
indices[++iIndex] = verticesCount - 7;
|
230
|
+
indices[++iIndex] = verticesCount - 6;
|
231
|
+
indices[++iIndex] = verticesCount - 7;
|
232
|
+
indices[++iIndex] = verticesCount - 5;
|
233
|
+
indices[++iIndex] = verticesCount - 6;
|
234
|
+
indices[++iIndex] = verticesCount - 5;
|
235
|
+
indices[++iIndex] = verticesCount - 3;
|
236
|
+
indices[++iIndex] = verticesCount - 2;
|
237
|
+
indices[++iIndex] = verticesCount - 3;
|
238
|
+
indices[++iIndex] = verticesCount - 1;
|
239
|
+
|
240
|
+
// indices.push(
|
241
|
+
// verticesCount - 6, verticesCount - 8, verticesCount - 7,
|
242
|
+
// verticesCount - 6, verticesCount - 7, verticesCount - 5,
|
243
|
+
|
244
|
+
// verticesCount - 6, verticesCount - 5, verticesCount - 3,
|
245
|
+
// verticesCount - 2, verticesCount - 3, verticesCount - 1
|
246
|
+
// );
|
247
|
+
|
248
|
+
count += 12;
|
249
|
+
}
|
250
|
+
for (let i = 0; i < 6; i++) {
|
251
|
+
normal[++nIndex] = up.x;
|
252
|
+
normal[++nIndex] = up.y;
|
253
|
+
normal[++nIndex] = up.z;
|
254
|
+
}
|
255
|
+
|
256
|
+
// normal.push(
|
257
|
+
// up.x, up.y, up.z,
|
258
|
+
// up.x, up.y, up.z,
|
259
|
+
// up.x, up.y, up.z,
|
260
|
+
// up.x, up.y, up.z,
|
261
|
+
// up.x, up.y, up.z,
|
262
|
+
// up.x, up.y, up.z
|
263
|
+
// );
|
264
|
+
|
265
|
+
uv[++uIndex] = uvDist - sharpUvOffset;
|
266
|
+
uv[++uIndex] = 0;
|
267
|
+
uv[++uIndex] = uvDist - sharpUvOffset;
|
268
|
+
uv[++uIndex] = 1;
|
269
|
+
uv[++uIndex] = uvDist;
|
270
|
+
uv[++uIndex] = 0;
|
271
|
+
uv[++uIndex] = uvDist;
|
272
|
+
uv[++uIndex] = 1;
|
273
|
+
uv[++uIndex] = uvDist + sharpUvOffset;
|
274
|
+
uv[++uIndex] = 0;
|
275
|
+
uv[++uIndex] = uvDist + sharpUvOffset;
|
276
|
+
uv[++uIndex] = 1;
|
277
|
+
// uv.push(
|
278
|
+
// uvDist - sharpUvOffset, 0,
|
279
|
+
// uvDist - sharpUvOffset, 1,
|
280
|
+
// uvDist, 0,
|
281
|
+
// uvDist, 1,
|
282
|
+
// uvDist + sharpUvOffset, 0,
|
283
|
+
// uvDist + sharpUvOffset, 1
|
284
|
+
// );
|
285
|
+
|
286
|
+
// if (generateUv2) {
|
287
|
+
// uv2.push(
|
288
|
+
// uvDist2 - sharpUvOffset2, 0,
|
289
|
+
// uvDist2 - sharpUvOffset2, 1,
|
290
|
+
// uvDist2, 0,
|
291
|
+
// uvDist2, 1,
|
292
|
+
// uvDist2 + sharpUvOffset2, 0,
|
293
|
+
// uvDist2 + sharpUvOffset2, 1
|
294
|
+
// );
|
295
|
+
// }
|
296
|
+
} else {
|
297
|
+
position[++pIndex] = left.x;
|
298
|
+
position[++pIndex] = left.y;
|
299
|
+
position[++pIndex] = left.z;
|
300
|
+
position[++pIndex] = right.x;
|
301
|
+
position[++pIndex] = right.y;
|
302
|
+
position[++pIndex] = right.z;
|
303
|
+
// position.push(
|
304
|
+
// left.x, left.y, left.z,
|
305
|
+
// right.x, right.y, right.z
|
306
|
+
// );
|
307
|
+
|
308
|
+
normal[++nIndex] = up.x;
|
309
|
+
normal[++nIndex] = up.y;
|
310
|
+
normal[++nIndex] = up.z;
|
311
|
+
normal[++nIndex] = up.x;
|
312
|
+
normal[++nIndex] = up.y;
|
313
|
+
normal[++nIndex] = up.z;
|
314
|
+
// normal.push(
|
315
|
+
// up.x, up.y, up.z,
|
316
|
+
// up.x, up.y, up.z
|
317
|
+
// );
|
318
|
+
|
319
|
+
uv[++uIndex] = uvDist;
|
320
|
+
uv[++uIndex] = 0;
|
321
|
+
uv[++uIndex] = uvDist;
|
322
|
+
uv[++uIndex] = 1;
|
323
|
+
// uv.push(
|
324
|
+
// uvDist, 0,
|
325
|
+
// uvDist, 1
|
326
|
+
// );
|
327
|
+
|
328
|
+
// if (generateUv2) {
|
329
|
+
// uv2.push(
|
330
|
+
// uvDist2, 0,
|
331
|
+
// uvDist2, 1
|
332
|
+
// );
|
333
|
+
// }
|
334
|
+
|
335
|
+
verticesCount += 2;
|
336
|
+
|
337
|
+
if (!first) {
|
338
|
+
indices[++iIndex] = verticesCount - 2;
|
339
|
+
indices[++iIndex] = verticesCount - 4;
|
340
|
+
indices[++iIndex] = verticesCount - 3;
|
341
|
+
indices[++iIndex] = verticesCount - 2;
|
342
|
+
indices[++iIndex] = verticesCount - 3;
|
343
|
+
indices[++iIndex] = verticesCount - 1;
|
344
|
+
// indices.push(
|
345
|
+
// verticesCount - 2, verticesCount - 4, verticesCount - 3,
|
346
|
+
// verticesCount - 2, verticesCount - 3, verticesCount - 1
|
347
|
+
// );
|
348
|
+
|
349
|
+
count += 6;
|
350
|
+
}
|
351
|
+
}
|
352
|
+
}
|
353
|
+
|
354
|
+
let lastPoint;
|
355
|
+
|
356
|
+
if (progressDistance > 0) {
|
357
|
+
for (let i = 0; i < pathPointList.count; i++) {
|
358
|
+
const pathPoint = pathPointList.array[i];
|
359
|
+
|
360
|
+
if (pathPoint.dist > progressDistance) {
|
361
|
+
const prevPoint = pathPointList.array[i - 1];
|
362
|
+
lastPoint = new PathPoint();
|
363
|
+
|
364
|
+
// linear lerp for progress
|
365
|
+
const alpha = (progressDistance - prevPoint.dist) / (pathPoint.dist - prevPoint.dist);
|
366
|
+
lastPoint.lerpPathPoints(prevPoint, pathPoint, alpha);
|
367
|
+
|
368
|
+
addVertices(lastPoint);
|
369
|
+
break;
|
370
|
+
} else {
|
371
|
+
addVertices(pathPoint);
|
372
|
+
}
|
373
|
+
}
|
374
|
+
} else {
|
375
|
+
lastPoint = pathPointList.array[0];
|
376
|
+
}
|
377
|
+
|
378
|
+
return {
|
379
|
+
position: position,
|
380
|
+
normal,
|
381
|
+
uv: uv,
|
382
|
+
indices: indices,
|
383
|
+
count
|
384
|
+
};
|
385
|
+
}
|
package/src/plane.ts
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
import { ResultType } from "./type";
|
2
|
+
|
3
|
+
export function plane(width: number, height: number, devideW: number, devideH: number): ResultType {
|
4
|
+
devideW = Math.max(1, devideW);
|
5
|
+
devideH = Math.max(1, devideH);
|
6
|
+
const dx = width / devideW, dy = height / devideH;
|
7
|
+
const minX = -width / 2, maxY = height / 2, minY = -height / 2;
|
8
|
+
const len = (devideW + 1) * (devideH + 1);
|
9
|
+
const position = new Float32Array(len * 3), uv = new Float32Array(len * 2), normal = new Float32Array(len * 3), indices = new Uint32Array(len * 10);
|
10
|
+
let index = 0, uIndex = 0, iIndex = 0;
|
11
|
+
for (let j = 0; j <= devideH; j++) {
|
12
|
+
for (let i = 0; i <= devideW; i++) {
|
13
|
+
const x = minX + dx * i;
|
14
|
+
const y = maxY - dy * j;
|
15
|
+
position[index] = x;
|
16
|
+
position[index + 1] = y;
|
17
|
+
position[index + 2] = 0;
|
18
|
+
|
19
|
+
normal[index] = 0;
|
20
|
+
normal[index + 1] = 0;
|
21
|
+
normal[index + 2] = 1;
|
22
|
+
// position.push(x, y, 0);
|
23
|
+
// normal.push(0, 0, 1);
|
24
|
+
const uvx = (x - minX) / width, uvy = (y - minY) / height;
|
25
|
+
// uv.push(uvx, uvy);
|
26
|
+
uv[uIndex] = uvx;
|
27
|
+
uv[uIndex + 1] = uvy;
|
28
|
+
|
29
|
+
index += 3;
|
30
|
+
uIndex += 2;
|
31
|
+
if (i < devideW && j < devideH) {
|
32
|
+
const a = j * (devideW + 1) + i, b = a + 1, c = (devideW + 1) * (j + 1) + i, d = c + 1;
|
33
|
+
indices[iIndex] = a;
|
34
|
+
indices[iIndex + 1] = c;
|
35
|
+
indices[iIndex + 2] = b;
|
36
|
+
indices[iIndex + 3] = c;
|
37
|
+
indices[iIndex + 4] = d;
|
38
|
+
indices[iIndex + 5] = b;
|
39
|
+
iIndex += 6;
|
40
|
+
// indexs.push(a, c, b, c, d, b);
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}
|
44
|
+
const indexArray = new Uint32Array(iIndex);
|
45
|
+
for (let i = 0, len = indexArray.length; i < len; i++) {
|
46
|
+
indexArray[i] = indices[i];
|
47
|
+
}
|
48
|
+
// for (let j = 0; j < devideH; j++) {
|
49
|
+
// for (let i = 0; i < devideW; i++) {
|
50
|
+
// const a = j * (devideW + 1) + i, b = a + 1, c = (devideW + 1) * (j + 1) + i, d = c + 1;
|
51
|
+
// indexs.push(a, c, b, c, d, b);
|
52
|
+
// }
|
53
|
+
// }
|
54
|
+
return {
|
55
|
+
position,
|
56
|
+
uv,
|
57
|
+
normal,
|
58
|
+
indices: indexArray
|
59
|
+
};
|
60
|
+
}
|
package/src/polygon.ts
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
|
2
|
+
import earcut from 'earcut';
|
3
|
+
import { generateNormal, generateSideWallUV, isClockwise, merge } from './util';
|
4
|
+
import { PolylineType, PolygonType, ResultType } from './type';
|
5
|
+
|
6
|
+
type PolygonsOptions = {
|
7
|
+
depth?: number
|
8
|
+
}
|
9
|
+
|
10
|
+
type PolygonsResult = ResultType & {
|
11
|
+
polygons: Array<PolygonType>;
|
12
|
+
}
|
13
|
+
|
14
|
+
|
15
|
+
export function extrudePolygons(polygons: Array<PolygonType>, options?: PolygonsOptions): PolygonsResult {
|
16
|
+
options = Object.assign({}, { depth: 2 }, options);
|
17
|
+
const results = polygons.map(polygon => {
|
18
|
+
for (let i = 0, len = polygon.length; i < len; i++) {
|
19
|
+
const ring = polygon[i];
|
20
|
+
validateRing(ring);
|
21
|
+
if (i === 0) {
|
22
|
+
if (!isClockwise(ring)) {
|
23
|
+
polygon[i] = ring.reverse();
|
24
|
+
}
|
25
|
+
} else if (isClockwise(ring)) {
|
26
|
+
polygon[i] = ring.reverse();
|
27
|
+
}
|
28
|
+
if (isClosedRing(ring)) {
|
29
|
+
ring.splice(ring.length - 1, 1);
|
30
|
+
}
|
31
|
+
}
|
32
|
+
const result = flatVertices(polygon, options) as Record<string, any>;
|
33
|
+
result.polygon = polygon;
|
34
|
+
const triangles = earcut(result.flatVertices, result.holes, 2);
|
35
|
+
generateTopAndBottom(result, triangles);
|
36
|
+
generateSides(result, options);
|
37
|
+
result.position = new Float32Array(result.points);
|
38
|
+
result.indices = new Uint32Array(result.indices);
|
39
|
+
result.uv = new Float32Array(result.uv);
|
40
|
+
result.normal = generateNormal(result.indices, result.position);
|
41
|
+
return result;
|
42
|
+
});
|
43
|
+
const result = merge(results) as PolygonsResult;
|
44
|
+
result.polygons = polygons;
|
45
|
+
return result;
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
function generateTopAndBottom(result, triangles) {
|
50
|
+
const indices: number[] = [];
|
51
|
+
const { count } = result;
|
52
|
+
for (let i = 0, len = triangles.length; i < len; i += 3) {
|
53
|
+
// top
|
54
|
+
const a = triangles[i], b = triangles[i + 1], c = triangles[i + 2];
|
55
|
+
indices[i] = a;
|
56
|
+
indices[i + 1] = b;
|
57
|
+
indices[i + 2] = c;
|
58
|
+
// bottom
|
59
|
+
const idx = len + i;
|
60
|
+
const a1 = count + a, b1 = count + b, c1 = count + c;
|
61
|
+
indices[idx] = a1;
|
62
|
+
indices[idx + 1] = b1;
|
63
|
+
indices[idx + 2] = c1;
|
64
|
+
}
|
65
|
+
result.indices = indices;
|
66
|
+
}
|
67
|
+
|
68
|
+
function generateSides(result, options) {
|
69
|
+
const { points, indices, polygon, uv } = result;
|
70
|
+
const depth = options.depth;
|
71
|
+
let pIndex = points.length - 1;
|
72
|
+
let iIndex = indices.length - 1;
|
73
|
+
for (let i = 0, len = polygon.length; i < len; i++) {
|
74
|
+
const ring = polygon[i];
|
75
|
+
let j = 0;
|
76
|
+
const len1 = ring.length;
|
77
|
+
while (j < len1) {
|
78
|
+
const v1 = ring[j];
|
79
|
+
let v2 = ring[j + 1];
|
80
|
+
if (j === len1 - 1) {
|
81
|
+
v2 = ring[0];
|
82
|
+
}
|
83
|
+
const idx = points.length / 3;
|
84
|
+
const x1 = v1[0], y1 = v1[1], z1 = v1[2] || 0, x2 = v2[0], y2 = v2[1], z2 = v2[2] || 0;
|
85
|
+
points[++pIndex] = x1;
|
86
|
+
points[++pIndex] = y1;
|
87
|
+
points[++pIndex] = z1 + depth;
|
88
|
+
points[++pIndex] = x2;
|
89
|
+
points[++pIndex] = y2;
|
90
|
+
points[++pIndex] = z2 + depth;
|
91
|
+
points[++pIndex] = x1;
|
92
|
+
points[++pIndex] = y1;
|
93
|
+
points[++pIndex] = z1;
|
94
|
+
points[++pIndex] = x2;
|
95
|
+
points[++pIndex] = y2;
|
96
|
+
points[++pIndex] = z2;
|
97
|
+
// points.push(x1, y1, z, x2, y2, z, x1, y1, 0, x2, y2, 0);
|
98
|
+
const a = idx + 2, b = idx + 3, c = idx, d = idx + 1;
|
99
|
+
// points.push(p3, p4, p1, p2);
|
100
|
+
// index.push(a, c, b, c, d, b);
|
101
|
+
indices[++iIndex] = a;
|
102
|
+
indices[++iIndex] = c;
|
103
|
+
indices[++iIndex] = b;
|
104
|
+
indices[++iIndex] = c;
|
105
|
+
indices[++iIndex] = d;
|
106
|
+
indices[++iIndex] = b;
|
107
|
+
// index.push(c, d, b);
|
108
|
+
|
109
|
+
generateSideWallUV(uv, points, a, b, c, d);
|
110
|
+
j++;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
function calPolygonPointsCount(polygon) {
|
116
|
+
let count = 0;
|
117
|
+
let i = 0;
|
118
|
+
const len = polygon.length;
|
119
|
+
while (i < len) {
|
120
|
+
count += (polygon[i].length);
|
121
|
+
i++;
|
122
|
+
}
|
123
|
+
return count;
|
124
|
+
}
|
125
|
+
|
126
|
+
function flatVertices(polygon, options) {
|
127
|
+
const count = calPolygonPointsCount(polygon);
|
128
|
+
const len = polygon.length;
|
129
|
+
const holes: number[] = [], flatVertices = new Float32Array(count * 2), points: number[] = [], uv: number[] = [];
|
130
|
+
const pOffset = count * 3, uOffset = count * 2;
|
131
|
+
const depth = options.depth;
|
132
|
+
|
133
|
+
let idx0 = 0, idx1 = 0, idx2 = 0;
|
134
|
+
for (let i = 0; i < len; i++) {
|
135
|
+
const ring = polygon[i];
|
136
|
+
if (i > 0) {
|
137
|
+
holes.push(idx0 / 2);
|
138
|
+
}
|
139
|
+
let j = 0;
|
140
|
+
const len1 = ring.length;
|
141
|
+
while (j < len1) {
|
142
|
+
const c = ring[j];
|
143
|
+
const x = c[0], y = c[1], z = c[2] || 0;
|
144
|
+
|
145
|
+
flatVertices[idx0++] = x;
|
146
|
+
flatVertices[idx0++] = y;
|
147
|
+
|
148
|
+
// top vertices
|
149
|
+
points[idx1] = x;
|
150
|
+
points[idx1 + 1] = y;
|
151
|
+
points[idx1 + 2] = depth + z;
|
152
|
+
|
153
|
+
// bottom vertices
|
154
|
+
points[pOffset + idx1] = x;
|
155
|
+
points[pOffset + idx1 + 1] = y;
|
156
|
+
points[pOffset + idx1 + 2] = z;
|
157
|
+
|
158
|
+
uv[idx2] = x;
|
159
|
+
uv[idx2 + 1] = y;
|
160
|
+
|
161
|
+
uv[uOffset + idx2] = x;
|
162
|
+
uv[uOffset + idx2 + 1] = y;
|
163
|
+
|
164
|
+
idx1 += 3;
|
165
|
+
idx2 += 2;
|
166
|
+
j++;
|
167
|
+
}
|
168
|
+
}
|
169
|
+
return {
|
170
|
+
flatVertices,
|
171
|
+
holes,
|
172
|
+
points,
|
173
|
+
count,
|
174
|
+
uv
|
175
|
+
};
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
function validateRing(ring: PolylineType) {
|
180
|
+
if (!isClosedRing(ring)) {
|
181
|
+
ring.push(ring[0]);
|
182
|
+
}
|
183
|
+
}
|
184
|
+
|
185
|
+
function isClosedRing(ring: PolylineType) {
|
186
|
+
const len = ring.length;
|
187
|
+
const [x1, y1] = ring[0], [x2, y2] = ring[len - 1];
|
188
|
+
return (x1 === x2 && y1 === y2);
|
189
|
+
}
|