poly-extrude 0.6.0 → 0.8.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.
@@ -1,6 +1,10 @@
1
1
  // import * as MathUtils from './MathUtils.js';
2
2
  // code copy from https://github.com/mrdoob/three.js/blob/dev/src/math/Vector3.js
3
3
 
4
+ import { Quaternion } from './Quaternion';
5
+
6
+ const _quaternion = new Quaternion();
7
+
4
8
  class Vector3 {
5
9
 
6
10
  constructor(x = 0, y = 0, z = 0) {
@@ -206,11 +210,11 @@ class Vector3 {
206
210
 
207
211
  // }
208
212
 
209
- // applyAxisAngle(axis, angle) {
213
+ applyAxisAngle(axis, angle) {
210
214
 
211
- // return this.applyQuaternion(_quaternion.setFromAxisAngle(axis, angle));
215
+ return this.applyQuaternion(_quaternion.setFromAxisAngle(axis, angle));
212
216
 
213
- // }
217
+ }
214
218
 
215
219
  // applyMatrix3(m) {
216
220
 
@@ -246,27 +250,27 @@ class Vector3 {
246
250
 
247
251
  }
248
252
 
249
- // applyQuaternion(q) {
253
+ applyQuaternion(q) {
250
254
 
251
- // const x = this.x, y = this.y, z = this.z;
252
- // const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
255
+ const x = this.x, y = this.y, z = this.z;
256
+ const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
253
257
 
254
- // // calculate quat * vector
258
+ // calculate quat * vector
255
259
 
256
- // const ix = qw * x + qy * z - qz * y;
257
- // const iy = qw * y + qz * x - qx * z;
258
- // const iz = qw * z + qx * y - qy * x;
259
- // const iw = - qx * x - qy * y - qz * z;
260
+ const ix = qw * x + qy * z - qz * y;
261
+ const iy = qw * y + qz * x - qx * z;
262
+ const iz = qw * z + qx * y - qy * x;
263
+ const iw = -qx * x - qy * y - qz * z;
260
264
 
261
- // // calculate result * inverse quat
265
+ // calculate result * inverse quat
262
266
 
263
- // this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
264
- // this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
265
- // this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
267
+ this.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
268
+ this.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
269
+ this.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
266
270
 
267
- // return this;
271
+ return this;
268
272
 
269
- // }
273
+ }
270
274
 
271
275
  // project(camera) {
272
276
 
package/src/polygon.js CHANGED
@@ -57,7 +57,9 @@ function generateTopAndBottom(result, triangles) {
57
57
 
58
58
  function generateSides(result, options) {
59
59
  const { points, index, polygon, uvs } = result;
60
- const z = options.depth;
60
+ const depth = options.depth;
61
+ let pIndex = points.length - 1;
62
+ let iIndex = index.length - 1;
61
63
  for (let i = 0, len = polygon.length; i < len; i++) {
62
64
  const ring = polygon[i];
63
65
  let j = 0;
@@ -69,11 +71,29 @@ function generateSides(result, options) {
69
71
  v2 = ring[0];
70
72
  }
71
73
  const idx = points.length / 3;
72
- const x1 = v1[0], y1 = v1[1], x2 = v2[0], y2 = v2[1];
73
- points.push(x1, y1, z, x2, y2, z, x1, y1, 0, x2, y2, 0);
74
+ const x1 = v1[0], y1 = v1[1], z1 = v1[2] || 0, x2 = v2[0], y2 = v2[1], z2 = v2[2] || 0;
75
+ points[++pIndex] = x1;
76
+ points[++pIndex] = y1;
77
+ points[++pIndex] = z1 + depth;
78
+ points[++pIndex] = x2;
79
+ points[++pIndex] = y2;
80
+ points[++pIndex] = z2 + depth;
81
+ points[++pIndex] = x1;
82
+ points[++pIndex] = y1;
83
+ points[++pIndex] = z1;
84
+ points[++pIndex] = x2;
85
+ points[++pIndex] = y2;
86
+ points[++pIndex] = z2;
87
+ // points.push(x1, y1, z, x2, y2, z, x1, y1, 0, x2, y2, 0);
74
88
  const a = idx + 2, b = idx + 3, c = idx, d = idx + 1;
75
89
  // points.push(p3, p4, p1, p2);
76
- index.push(a, c, b, c, d, b);
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;
77
97
  // index.push(c, d, b);
78
98
 
79
99
  generateSideWallUV(uvs, points, a, b, c, d);
@@ -98,7 +118,7 @@ function flatVertices(polygon, options) {
98
118
  const len = polygon.length;
99
119
  const holes = [], flatVertices = new Float32Array(count * 2), points = [], uvs = [];
100
120
  const pOffset = count * 3, uOffset = count * 2;
101
- const z = options.depth;
121
+ const depth = options.depth;
102
122
 
103
123
  let idx0 = 0, idx1 = 0, idx2 = 0;
104
124
  for (let i = 0; i < len; i++) {
@@ -110,7 +130,7 @@ function flatVertices(polygon, options) {
110
130
  const len1 = ring.length;
111
131
  while (j < len1) {
112
132
  const c = ring[j];
113
- const x = c[0], y = c[1];
133
+ const x = c[0], y = c[1], z = c[2] || 0;
114
134
 
115
135
  flatVertices[idx0++] = x;
116
136
  flatVertices[idx0++] = y;
@@ -118,12 +138,12 @@ function flatVertices(polygon, options) {
118
138
  // top vertices
119
139
  points[idx1] = x;
120
140
  points[idx1 + 1] = y;
121
- points[idx1 + 2] = z;
141
+ points[idx1 + 2] = depth + z;
122
142
 
123
143
  // bottom vertices
124
144
  points[pOffset + idx1] = x;
125
145
  points[pOffset + idx1 + 1] = y;
126
- points[pOffset + idx1 + 2] = 0;
146
+ points[pOffset + idx1 + 2] = z;
127
147
 
128
148
  uvs[idx2] = x;
129
149
  uvs[idx2 + 1] = y;
package/src/tube.js ADDED
@@ -0,0 +1,146 @@
1
+ import { Vector3 } from './math/Vector3';
2
+ import { PathPoint } from './path/PathPoint';
3
+ import { PathPointList } from './path/PathPointList';
4
+ import { merge } from './util';
5
+ const UP = new Vector3(0, 0, 1);
6
+
7
+ export function expandTubes(lines, options) {
8
+ options = Object.assign({}, { radius: 1, cornerSplit: 0, radialSegments: 8, startRad: -Math.PI / 4 }, options);
9
+ 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
+ });
14
+ const pathPointList = new PathPointList();
15
+ pathPointList.set(points, options.cornerRadius, options.cornerSplit, UP);
16
+ const result = generateTubeVertexData(pathPointList, options);
17
+ result.line = line;
18
+ result.position = new Float32Array(result.points);
19
+ result.indices = new Uint32Array(result.index);
20
+ result.uv = new Float32Array(result.uvs);
21
+ result.normal = new Float32Array(result.normal);
22
+ return result;
23
+ });
24
+ const result = merge(results);
25
+ result.lines = lines;
26
+ return result;
27
+ }
28
+
29
+ // Vertex Data Generate Functions
30
+ // code copy from https://github.com/shawn0326/three.path/blob/master/src/PathGeometry.js
31
+ function generateTubeVertexData(pathPointList, options) {
32
+ const radius = Math.max(options.radius || 1, 0.00000001);
33
+ const progress = options.progress !== undefined ? options.progress : 1;
34
+ const radialSegments = Math.max(3, options.radialSegments || 8);
35
+ const startRad = options.startRad || 0;
36
+
37
+ const circum = radius * 2 * Math.PI;
38
+ const totalDistance = pathPointList.distance();
39
+ const progressDistance = progress * totalDistance;
40
+ if (progressDistance === 0) {
41
+ return null;
42
+ }
43
+
44
+ let count = 0;
45
+
46
+ // modify data
47
+ const points = [];
48
+ const normal = [];
49
+ const uvs = [];
50
+ // const uv2 = [];
51
+ const index = [];
52
+ let verticesCount = 0;
53
+
54
+ const normalDir = new Vector3();
55
+
56
+ let pIndex = -1;
57
+ let nIndex = -1;
58
+ let uIndex = -1;
59
+ let iIndex = -1;
60
+ function addVertices(pathPoint, radius, radialSegments) {
61
+ const first = points.length === 0;
62
+ const uvDist = pathPoint.dist / circum;
63
+ // const uvDist2 = pathPoint.dist / totalDistance;
64
+
65
+ for (let i = 0; i <= radialSegments; i++) {
66
+ let r = i;
67
+ if (r === radialSegments) {
68
+ r = 0;
69
+ }
70
+ normalDir.copy(pathPoint.up).applyAxisAngle(pathPoint.dir, startRad + Math.PI * 2 * r / radialSegments).normalize();
71
+
72
+ const scale = radius * pathPoint.widthScale;
73
+ points[++pIndex] = pathPoint.pos.x + normalDir.x * scale;
74
+ points[++pIndex] = pathPoint.pos.y + normalDir.y * scale;
75
+ points[++pIndex] = pathPoint.pos.z + normalDir.z * scale;
76
+
77
+ normal[++nIndex] = normalDir.x;
78
+ normal[++nIndex] = normalDir.y;
79
+ normal[++nIndex] = normalDir.z;
80
+
81
+ uvs[++uIndex] = uvDist;
82
+ uvs[++uIndex] = i / radialSegments;
83
+
84
+ // uvs.push(uvDist, r / radialSegments);
85
+
86
+ // if (generateUv2) {
87
+ // uv2.push(uvDist2, r / radialSegments);
88
+ // }
89
+
90
+ verticesCount++;
91
+ }
92
+
93
+ if (!first) {
94
+ const begin1 = verticesCount - (radialSegments + 1) * 2;
95
+ const begin2 = verticesCount - (radialSegments + 1);
96
+
97
+ for (let i = 0; i < radialSegments; i++) {
98
+ index[++iIndex] = begin2 + i;
99
+ index[++iIndex] = begin1 + i;
100
+ index[++iIndex] = begin1 + i + 1;
101
+ index[++iIndex] = begin2 + i;
102
+ index[++iIndex] = begin1 + i + 1;
103
+ index[++iIndex] = begin2 + i + 1;
104
+ // index.push(
105
+ // begin2 + i,
106
+ // begin1 + i,
107
+ // begin1 + i + 1,
108
+ // begin2 + i,
109
+ // begin1 + i + 1,
110
+ // begin2 + i + 1
111
+ // );
112
+
113
+ count += 6;
114
+ }
115
+ }
116
+ }
117
+
118
+ if (progressDistance > 0) {
119
+ for (let i = 0; i < pathPointList.count; i++) {
120
+ const pathPoint = pathPointList.array[i];
121
+
122
+ if (pathPoint.dist > progressDistance) {
123
+ const prevPoint = pathPointList.array[i - 1];
124
+ const lastPoint = new PathPoint();
125
+
126
+ // linear lerp for progress
127
+ const alpha = (progressDistance - prevPoint.dist) / (pathPoint.dist - prevPoint.dist);
128
+ lastPoint.lerpPathPoints(prevPoint, pathPoint, alpha);
129
+
130
+ addVertices(lastPoint, radius, radialSegments);
131
+ break;
132
+ } else {
133
+ addVertices(pathPoint, radius, radialSegments);
134
+ }
135
+ }
136
+ }
137
+
138
+ return {
139
+ points,
140
+ normal,
141
+ uvs,
142
+ // uv2,
143
+ index,
144
+ count
145
+ };
146
+ }