poly-extrude 0.8.0 → 0.10.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "poly-extrude",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "",
5
5
  "main": "dist/poly-extrude.js",
6
6
  "module": "dist/poly-extrude.mjs",
package/src/cylinder.js CHANGED
@@ -10,7 +10,8 @@ export function cylinder(point, options = {}) {
10
10
  const [centerx, centery] = point;
11
11
  let idx = 0, uIdx = 0;
12
12
  const offset = circlePointsLen * 3, uOffset = circlePointsLen * 2;
13
- const indices = [], uvs = [];
13
+ const indices = [], uv = [];
14
+ let iIndex = indices.length - 1;
14
15
  for (let i = -1; i < radialSegments; i++) {
15
16
  const rad = aRad * i;
16
17
  const x = Math.cos(rad) * radius + centerx, y = Math.sin(rad) * radius + centery;
@@ -27,16 +28,19 @@ export function cylinder(point, options = {}) {
27
28
  let u = 0, v = 0;
28
29
  u = 0.5 + x / radius / 2;
29
30
  v = 0.5 + y / radius / 2;
30
- uvs[uIdx] = u;
31
- uvs[uIdx + 1] = v;
32
- uvs[uIdx + uOffset] = u;
33
- uvs[uIdx + 1 + uOffset] = v;
31
+ uv[uIdx] = u;
32
+ uv[uIdx + 1] = v;
33
+ uv[uIdx + uOffset] = u;
34
+ uv[uIdx + 1 + uOffset] = v;
34
35
 
35
36
  idx += 3;
36
37
  uIdx += 2;
37
38
  if (i > 1) {
38
39
  // bottom indices
39
- indices.push(0, i - 1, i);
40
+ // indices.push(0, i - 1, i);
41
+ indices[++iIndex] = 0;
42
+ indices[++iIndex] = i - 1;
43
+ indices[++iIndex] = i;
40
44
  }
41
45
  }
42
46
  idx -= 3;
@@ -50,15 +54,19 @@ export function cylinder(point, options = {}) {
50
54
 
51
55
  const indicesLen = indices.length;
52
56
  // top indices
57
+ iIndex = indices.length - 1;
53
58
  for (let i = 0; i < indicesLen; i++) {
54
59
  const index = indices[i];
55
- indices.push(index + circlePointsLen);
60
+ indices[++iIndex] = index + circlePointsLen;
61
+ // indices.push(index + circlePointsLen);
56
62
  }
57
63
 
58
64
  const sidePoints = new Float32Array((circlePointsLen * 3 * 2 - 6) * 2);
59
65
  let pIndex = -1;
60
66
  idx = circlePointsLen * 2;
61
67
  uIdx = 0;
68
+ iIndex = indices.length - 1;
69
+ let uvIndex = uv.length - 1;
62
70
  for (let i = 0, len = points.length / 2; i < len - 3; i += 3) {
63
71
  const x1 = points[i], y1 = points[i + 1], x2 = points[i + 3], y2 = points[i + 4];
64
72
  sidePoints[++pIndex] = x1;
@@ -75,15 +83,29 @@ export function cylinder(point, options = {}) {
75
83
  sidePoints[++pIndex] = 0;
76
84
  const a = idx + 2, b = idx + 3, c = idx, d = idx + 1;
77
85
  // indices.push(a, c, b, c, d, b);
78
- indices.push(c, a, d, a, b, d);
86
+ indices[++iIndex] = c;
87
+ indices[++iIndex] = a;
88
+ indices[++iIndex] = d;
89
+ indices[++iIndex] = a;
90
+ indices[++iIndex] = b;
91
+ indices[++iIndex] = d;
92
+ // indices.push(c, a, d, a, b, d);
79
93
  idx += 4;
80
94
  const u1 = uIdx / circlePointsLen, u2 = (uIdx + 1) / circlePointsLen;
81
- uvs.push(u1, height / radius / 2, u2, height / radius / 2, u1, 0, u2, 0);
95
+ uv[++uvIndex] = u1;
96
+ uv[++uvIndex] = height / radius / 2;
97
+ uv[++uvIndex] = u2;
98
+ uv[++uvIndex] = height / radius / 2;
99
+ uv[++uvIndex] = u1;
100
+ uv[++uvIndex] = 0;
101
+ uv[++uvIndex] = u2;
102
+ uv[++uvIndex] = 0;
103
+ // uvs.push(u1, height / radius / 2, u2, height / radius / 2, u1, 0, u2, 0);
82
104
  uIdx++;
83
105
  }
84
106
  const position = new Float32Array(points.length + sidePoints.length);
85
107
  position.set(points, 0);
86
108
  position.set(sidePoints, points.length);
87
109
  const normal = generateNormal(indices, position);
88
- return { points, indices: new Uint32Array(indices), position, normal, uv: new Float32Array(uvs) };
110
+ return { points, indices: new Uint32Array(indices), position, normal, uv: new Float32Array(uv) };
89
111
  }
@@ -672,16 +672,16 @@ class Quaternion {
672
672
 
673
673
  }
674
674
 
675
- _onChangeCallback() { }
675
+ // _onChangeCallback() { }
676
676
 
677
- * [Symbol.iterator]() {
677
+ // * [Symbol.iterator]() {
678
678
 
679
- yield this._x;
680
- yield this._y;
681
- yield this._z;
682
- yield this._w;
679
+ // yield this._x;
680
+ // yield this._y;
681
+ // yield this._z;
682
+ // yield this._w;
683
683
 
684
- }
684
+ // }
685
685
 
686
686
  }
687
687
 
package/src/path.js CHANGED
@@ -1,23 +1,29 @@
1
1
  import { Vector3 } from './math/Vector3';
2
2
  import { PathPoint } from './path/PathPoint';
3
3
  import { PathPointList } from './path/PathPointList';
4
- import { merge } from './util';
4
+ import { line2Vectors, merge } from './util';
5
5
  const UP = new Vector3(0, 0, 1);
6
6
 
7
+ const right = new Vector3();
8
+ const left = new Vector3();
9
+
10
+ // for sharp corners
11
+ const leftOffset = new Vector3();
12
+ const rightOffset = new Vector3();
13
+ const tempPoint1 = new Vector3();
14
+ const tempPoint2 = new Vector3();
15
+
7
16
  export function expandPaths(lines, options) {
8
17
  options = Object.assign({}, { lineWidth: 1, cornerRadius: 0, cornerSplit: 10 }, options);
9
18
  const results = lines.map(line => {
10
- const points = line.map(p => {
11
- const [x, y, z] = p;
12
- return new Vector3(x, y, z || 0);
13
- });
19
+ const points = line2Vectors(line);
14
20
  const pathPointList = new PathPointList();
15
21
  pathPointList.set(points, options.cornerRadius, options.cornerSplit, UP);
16
22
  const result = generatePathVertexData(pathPointList, options);
17
23
  result.line = line;
18
- result.position = new Float32Array(result.points);
19
- result.indices = new Uint32Array(result.index);
20
- result.uv = new Float32Array(result.uvs);
24
+ result.position = new Float32Array(result.position);
25
+ result.indices = new Uint32Array(result.indices);
26
+ result.uv = new Float32Array(result.uv);
21
27
  result.normal = new Float32Array(result.normal);
22
28
  return result;
23
29
  });
@@ -53,15 +59,10 @@ function generatePathVertexData(pathPointList, options) {
53
59
  const indices = [];
54
60
  let verticesCount = 0;
55
61
 
56
- const right = new Vector3();
57
- const left = new Vector3();
58
-
59
- // for sharp corners
60
- const leftOffset = new Vector3();
61
- const rightOffset = new Vector3();
62
- const tempPoint1 = new Vector3();
63
- const tempPoint2 = new Vector3();
64
-
62
+ let pIndex = position.length - 1;
63
+ let nIndex = normal.length - 1;
64
+ let uIndex = uv.length - 1;
65
+ let iIndex = indices.length - 1;
65
66
  function addVertices(pathPoint) {
66
67
  const first = position.length === 0;
67
68
  const sharpCorner = pathPoint.sharp && !first;
@@ -118,66 +119,144 @@ function generatePathVertexData(pathPointList, options) {
118
119
  tempPoint2.copy(dir).setLength(_dist).add(tempPoint1);
119
120
 
120
121
  if (sideOffset > 0) {
121
- position.push(
122
- tempPoint1.x, tempPoint1.y, tempPoint1.z, // 6
123
- right.x, right.y, right.z, // 5
124
- left.x, left.y, left.z, // 4
125
- right.x, right.y, right.z, // 3
126
- tempPoint2.x, tempPoint2.y, tempPoint2.z, // 2
127
- right.x, right.y, right.z // 1
128
- );
122
+ position[++pIndex] = tempPoint1.x;
123
+ position[++pIndex] = tempPoint1.y;
124
+ position[++pIndex] = tempPoint1.z;
125
+ position[++pIndex] = right.x;
126
+ position[++pIndex] = right.y;
127
+ position[++pIndex] = right.z;
128
+ position[++pIndex] = left.x;
129
+ position[++pIndex] = left.y;
130
+ position[++pIndex] = left.z;
131
+ position[++pIndex] = right.x;
132
+ position[++pIndex] = right.y;
133
+ position[++pIndex] = right.z;
134
+ position[++pIndex] = tempPoint2.x;
135
+ position[++pIndex] = tempPoint2.y;
136
+ position[++pIndex] = tempPoint2.z;
137
+ position[++pIndex] = right.x;
138
+ position[++pIndex] = right.y;
139
+ position[++pIndex] = right.z;
140
+ // position.push(
141
+ // tempPoint1.x, tempPoint1.y, tempPoint1.z, // 6
142
+ // right.x, right.y, right.z, // 5
143
+ // left.x, left.y, left.z, // 4
144
+ // right.x, right.y, right.z, // 3
145
+ // tempPoint2.x, tempPoint2.y, tempPoint2.z, // 2
146
+ // right.x, right.y, right.z // 1
147
+ // );
129
148
 
130
149
  verticesCount += 6;
131
150
 
132
- indices.push(
133
- verticesCount - 6, verticesCount - 8, verticesCount - 7,
134
- verticesCount - 6, verticesCount - 7, verticesCount - 5,
135
-
136
- verticesCount - 4, verticesCount - 6, verticesCount - 5,
137
- verticesCount - 2, verticesCount - 4, verticesCount - 1
138
- );
151
+ indices[++iIndex] = verticesCount - 6;
152
+ indices[++iIndex] = verticesCount - 8;
153
+ indices[++iIndex] = verticesCount - 7;
154
+ indices[++iIndex] = verticesCount - 6;
155
+ indices[++iIndex] = verticesCount - 7;
156
+ indices[++iIndex] = verticesCount - 5;
157
+ indices[++iIndex] = verticesCount - 4;
158
+ indices[++iIndex] = verticesCount - 6;
159
+ indices[++iIndex] = verticesCount - 5;
160
+ indices[++iIndex] = verticesCount - 2;
161
+ indices[++iIndex] = verticesCount - 4;
162
+ indices[++iIndex] = verticesCount - 1;
163
+
164
+ // indices.push(
165
+ // verticesCount - 6, verticesCount - 8, verticesCount - 7,
166
+ // verticesCount - 6, verticesCount - 7, verticesCount - 5,
167
+
168
+ // verticesCount - 4, verticesCount - 6, verticesCount - 5,
169
+ // verticesCount - 2, verticesCount - 4, verticesCount - 1
170
+ // );
139
171
 
140
172
  count += 12;
141
173
  } else {
142
- position.push(
143
- left.x, left.y, left.z, // 6
144
- tempPoint1.x, tempPoint1.y, tempPoint1.z, // 5
145
- left.x, left.y, left.z, // 4
146
- right.x, right.y, right.z, // 3
147
- left.x, left.y, left.z, // 2
148
- tempPoint2.x, tempPoint2.y, tempPoint2.z // 1
149
- );
174
+ position[++pIndex] = left.x;
175
+ position[++pIndex] = left.y;
176
+ position[++pIndex] = left.z;
177
+ position[++pIndex] = tempPoint1.x;
178
+ position[++pIndex] = tempPoint1.y;
179
+ position[++pIndex] = tempPoint1.z;
180
+ position[++pIndex] = left.x;
181
+ position[++pIndex] = left.y;
182
+ position[++pIndex] = left.z;
183
+ position[++pIndex] = right.x;
184
+ position[++pIndex] = right.y;
185
+ position[++pIndex] = right.z;
186
+ position[++pIndex] = left.x;
187
+ position[++pIndex] = left.y;
188
+ position[++pIndex] = left.z;
189
+ position[++pIndex] = tempPoint2.x;
190
+ position[++pIndex] = tempPoint2.y;
191
+ position[++pIndex] = tempPoint2.z;
192
+ // position.push(
193
+ // left.x, left.y, left.z, // 6
194
+ // tempPoint1.x, tempPoint1.y, tempPoint1.z, // 5
195
+ // left.x, left.y, left.z, // 4
196
+ // right.x, right.y, right.z, // 3
197
+ // left.x, left.y, left.z, // 2
198
+ // tempPoint2.x, tempPoint2.y, tempPoint2.z // 1
199
+ // );
150
200
 
151
201
  verticesCount += 6;
152
-
153
- indices.push(
154
- verticesCount - 6, verticesCount - 8, verticesCount - 7,
155
- verticesCount - 6, verticesCount - 7, verticesCount - 5,
156
-
157
- verticesCount - 6, verticesCount - 5, verticesCount - 3,
158
- verticesCount - 2, verticesCount - 3, verticesCount - 1
159
- );
202
+ indices[++iIndex] = verticesCount - 6;
203
+ indices[++iIndex] = verticesCount - 8;
204
+ indices[++iIndex] = verticesCount - 7;
205
+ indices[++iIndex] = verticesCount - 6;
206
+ indices[++iIndex] = verticesCount - 7;
207
+ indices[++iIndex] = verticesCount - 5;
208
+ indices[++iIndex] = verticesCount - 6;
209
+ indices[++iIndex] = verticesCount - 5;
210
+ indices[++iIndex] = verticesCount - 3;
211
+ indices[++iIndex] = verticesCount - 2;
212
+ indices[++iIndex] = verticesCount - 3;
213
+ indices[++iIndex] = verticesCount - 1;
214
+
215
+ // indices.push(
216
+ // verticesCount - 6, verticesCount - 8, verticesCount - 7,
217
+ // verticesCount - 6, verticesCount - 7, verticesCount - 5,
218
+
219
+ // verticesCount - 6, verticesCount - 5, verticesCount - 3,
220
+ // verticesCount - 2, verticesCount - 3, verticesCount - 1
221
+ // );
160
222
 
161
223
  count += 12;
162
224
  }
225
+ for (let i = 0; i < 6; i++) {
226
+ normal[++nIndex] = up.x;
227
+ normal[++nIndex] = up.y;
228
+ normal[++nIndex] = up.z;
229
+ }
163
230
 
164
- normal.push(
165
- up.x, up.y, up.z,
166
- up.x, up.y, up.z,
167
- up.x, up.y, up.z,
168
- up.x, up.y, up.z,
169
- up.x, up.y, up.z,
170
- up.x, up.y, up.z
171
- );
172
-
173
- uv.push(
174
- uvDist - sharpUvOffset, 0,
175
- uvDist - sharpUvOffset, 1,
176
- uvDist, 0,
177
- uvDist, 1,
178
- uvDist + sharpUvOffset, 0,
179
- uvDist + sharpUvOffset, 1
180
- );
231
+ // normal.push(
232
+ // up.x, up.y, up.z,
233
+ // up.x, up.y, up.z,
234
+ // up.x, up.y, up.z,
235
+ // up.x, up.y, up.z,
236
+ // up.x, up.y, up.z,
237
+ // up.x, up.y, up.z
238
+ // );
239
+
240
+ uv[++uIndex] = uvDist - sharpUvOffset;
241
+ uv[++uIndex] = 0;
242
+ uv[++uIndex] = uvDist - sharpUvOffset;
243
+ uv[++uIndex] = 1;
244
+ uv[++uIndex] = uvDist;
245
+ uv[++uIndex] = 0;
246
+ uv[++uIndex] = uvDist;
247
+ uv[++uIndex] = 1;
248
+ uv[++uIndex] = uvDist + sharpUvOffset;
249
+ uv[++uIndex] = 0;
250
+ uv[++uIndex] = uvDist + sharpUvOffset;
251
+ uv[++uIndex] = 1;
252
+ // uv.push(
253
+ // uvDist - sharpUvOffset, 0,
254
+ // uvDist - sharpUvOffset, 1,
255
+ // uvDist, 0,
256
+ // uvDist, 1,
257
+ // uvDist + sharpUvOffset, 0,
258
+ // uvDist + sharpUvOffset, 1
259
+ // );
181
260
 
182
261
  // if (generateUv2) {
183
262
  // uv2.push(
@@ -190,20 +269,36 @@ function generatePathVertexData(pathPointList, options) {
190
269
  // );
191
270
  // }
192
271
  } else {
193
- position.push(
194
- left.x, left.y, left.z,
195
- right.x, right.y, right.z
196
- );
197
-
198
- normal.push(
199
- up.x, up.y, up.z,
200
- up.x, up.y, up.z
201
- );
202
-
203
- uv.push(
204
- uvDist, 0,
205
- uvDist, 1
206
- );
272
+ position[++pIndex] = left.x;
273
+ position[++pIndex] = left.y;
274
+ position[++pIndex] = left.z;
275
+ position[++pIndex] = right.x;
276
+ position[++pIndex] = right.y;
277
+ position[++pIndex] = right.z;
278
+ // position.push(
279
+ // left.x, left.y, left.z,
280
+ // right.x, right.y, right.z
281
+ // );
282
+
283
+ normal[++nIndex] = up.x;
284
+ normal[++nIndex] = up.y;
285
+ normal[++nIndex] = up.z;
286
+ normal[++nIndex] = up.x;
287
+ normal[++nIndex] = up.y;
288
+ normal[++nIndex] = up.z;
289
+ // normal.push(
290
+ // up.x, up.y, up.z,
291
+ // up.x, up.y, up.z
292
+ // );
293
+
294
+ uv[++uIndex] = uvDist;
295
+ uv[++uIndex] = 0;
296
+ uv[++uIndex] = uvDist;
297
+ uv[++uIndex] = 1;
298
+ // uv.push(
299
+ // uvDist, 0,
300
+ // uvDist, 1
301
+ // );
207
302
 
208
303
  // if (generateUv2) {
209
304
  // uv2.push(
@@ -215,10 +310,16 @@ function generatePathVertexData(pathPointList, options) {
215
310
  verticesCount += 2;
216
311
 
217
312
  if (!first) {
218
- indices.push(
219
- verticesCount - 2, verticesCount - 4, verticesCount - 3,
220
- verticesCount - 2, verticesCount - 3, verticesCount - 1
221
- );
313
+ indices[++iIndex] = verticesCount - 2;
314
+ indices[++iIndex] = verticesCount - 4;
315
+ indices[++iIndex] = verticesCount - 3;
316
+ indices[++iIndex] = verticesCount - 2;
317
+ indices[++iIndex] = verticesCount - 3;
318
+ indices[++iIndex] = verticesCount - 1;
319
+ // indices.push(
320
+ // verticesCount - 2, verticesCount - 4, verticesCount - 3,
321
+ // verticesCount - 2, verticesCount - 3, verticesCount - 1
322
+ // );
222
323
 
223
324
  count += 6;
224
325
  }
@@ -250,10 +351,10 @@ function generatePathVertexData(pathPointList, options) {
250
351
  }
251
352
 
252
353
  return {
253
- points: position,
354
+ position: position,
254
355
  normal,
255
- uvs: uv,
256
- index: indices,
356
+ uv: uv,
357
+ indices: indices,
257
358
  count
258
359
  };
259
360
  }
package/src/polygon.js CHANGED
@@ -25,8 +25,8 @@ export function extrudePolygons(polygons, options) {
25
25
  generateTopAndBottom(result, triangles);
26
26
  generateSides(result, options);
27
27
  result.position = new Float32Array(result.points);
28
- result.indices = new Uint32Array(result.index);
29
- result.uv = new Float32Array(result.uvs);
28
+ result.indices = new Uint32Array(result.indices);
29
+ result.uv = new Float32Array(result.uv);
30
30
  result.normal = generateNormal(result.indices, result.position);
31
31
  return result;
32
32
  });
@@ -37,29 +37,29 @@ export function extrudePolygons(polygons, options) {
37
37
  }
38
38
 
39
39
  function generateTopAndBottom(result, triangles) {
40
- const index = [];
40
+ const indices = [];
41
41
  const { count } = result;
42
42
  for (let i = 0, len = triangles.length; i < len; i += 3) {
43
43
  // top
44
44
  const a = triangles[i], b = triangles[i + 1], c = triangles[i + 2];
45
- index[i] = a;
46
- index[i + 1] = b;
47
- index[i + 2] = c;
45
+ indices[i] = a;
46
+ indices[i + 1] = b;
47
+ indices[i + 2] = c;
48
48
  // bottom
49
49
  const idx = len + i;
50
50
  const a1 = count + a, b1 = count + b, c1 = count + c;
51
- index[idx] = a1;
52
- index[idx + 1] = b1;
53
- index[idx + 2] = c1;
51
+ indices[idx] = a1;
52
+ indices[idx + 1] = b1;
53
+ indices[idx + 2] = c1;
54
54
  }
55
- result.index = index;
55
+ result.indices = indices;
56
56
  }
57
57
 
58
58
  function generateSides(result, options) {
59
- const { points, index, polygon, uvs } = result;
59
+ const { points, indices, polygon, uv } = result;
60
60
  const depth = options.depth;
61
61
  let pIndex = points.length - 1;
62
- let iIndex = index.length - 1;
62
+ let iIndex = indices.length - 1;
63
63
  for (let i = 0, len = polygon.length; i < len; i++) {
64
64
  const ring = polygon[i];
65
65
  let j = 0;
@@ -88,15 +88,15 @@ function generateSides(result, options) {
88
88
  const a = idx + 2, b = idx + 3, c = idx, d = idx + 1;
89
89
  // points.push(p3, p4, p1, p2);
90
90
  // index.push(a, c, b, c, d, b);
91
- index[++iIndex] = a;
92
- index[++iIndex] = c;
93
- index[++iIndex] = b;
94
- index[++iIndex] = c;
95
- index[++iIndex] = d;
96
- index[++iIndex] = b;
91
+ indices[++iIndex] = a;
92
+ indices[++iIndex] = c;
93
+ indices[++iIndex] = b;
94
+ indices[++iIndex] = c;
95
+ indices[++iIndex] = d;
96
+ indices[++iIndex] = b;
97
97
  // index.push(c, d, b);
98
98
 
99
- generateSideWallUV(uvs, points, a, b, c, d);
99
+ generateSideWallUV(uv, points, a, b, c, d);
100
100
  j++;
101
101
  }
102
102
  }
@@ -116,7 +116,7 @@ function calPolygonPointsCount(polygon) {
116
116
  function flatVertices(polygon, options) {
117
117
  const count = calPolygonPointsCount(polygon);
118
118
  const len = polygon.length;
119
- const holes = [], flatVertices = new Float32Array(count * 2), points = [], uvs = [];
119
+ const holes = [], flatVertices = new Float32Array(count * 2), points = [], uv = [];
120
120
  const pOffset = count * 3, uOffset = count * 2;
121
121
  const depth = options.depth;
122
122
 
@@ -145,11 +145,11 @@ function flatVertices(polygon, options) {
145
145
  points[pOffset + idx1 + 1] = y;
146
146
  points[pOffset + idx1 + 2] = z;
147
147
 
148
- uvs[idx2] = x;
149
- uvs[idx2 + 1] = y;
148
+ uv[idx2] = x;
149
+ uv[idx2 + 1] = y;
150
150
 
151
- uvs[uOffset + idx2] = x;
152
- uvs[uOffset + idx2 + 1] = y;
151
+ uv[uOffset + idx2] = x;
152
+ uv[uOffset + idx2 + 1] = y;
153
153
 
154
154
  idx1 += 3;
155
155
  idx2 += 2;
@@ -161,7 +161,7 @@ function flatVertices(polygon, options) {
161
161
  holes,
162
162
  points,
163
163
  count,
164
- uvs
164
+ uv
165
165
  };
166
166
 
167
167
  }