poly-extrude 0.11.0 → 0.13.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/src/polyline.js CHANGED
@@ -1,4 +1,4 @@
1
- import { degToRad, generateNormal, generateSideWallUV, merge, radToDeg } from './util';
1
+ import { calLineDistance, degToRad, generateNormal, generateSideWallUV, merge, radToDeg } from './util';
2
2
 
3
3
  function checkOptions(options) {
4
4
  options.lineWidth = Math.max(0, options.lineWidth);
@@ -7,7 +7,7 @@ function checkOptions(options) {
7
7
  }
8
8
 
9
9
  export function extrudePolylines(lines, options) {
10
- options = Object.assign({}, { depth: 2, lineWidth: 1, bottomStickGround: false }, options);
10
+ options = Object.assign({}, { depth: 2, lineWidth: 1, bottomStickGround: false, pathUV: false }, options);
11
11
  checkOptions(options);
12
12
  const results = lines.map(line => {
13
13
  const result = expandLine(line, options);
@@ -26,7 +26,7 @@ export function extrudePolylines(lines, options) {
26
26
  }
27
27
 
28
28
  export function extrudeSlopes(lines, options) {
29
- options = Object.assign({}, { depth: 2, lineWidth: 1, side: 'left', sideDepth: 0, bottomStickGround: false }, options);
29
+ options = Object.assign({}, { depth: 2, lineWidth: 1, side: 'left', sideDepth: 0, bottomStickGround: false, pathUV: false, isSlope: true }, options);
30
30
  checkOptions(options);
31
31
  const { depth, side, sideDepth } = options;
32
32
  const results = lines.map(line => {
@@ -70,9 +70,17 @@ function generateTopAndBottom(result, options) {
70
70
  lz = depths[0];
71
71
  rz = depths[1];
72
72
  }
73
- const points = [], indices = [], uv = [];
74
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
+ }
75
82
  let i = 0, len = leftPoints.length;
83
+ const points = [], indices = [], uv = [];
76
84
  while (i < len) {
77
85
  // top left
78
86
  const idx0 = i * 3;
@@ -106,18 +114,43 @@ function generateTopAndBottom(result, options) {
106
114
  points[idx3 + 2] = 0;
107
115
  }
108
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
+ }
109
139
  i++;
110
140
  }
111
- i = 0;
112
- len = points.length;
113
- let uIndex = uv.length - 1;
114
- while (i < len) {
115
- const x = points[i], y = points[i + 1];
116
- uv[++uIndex] = x;
117
- uv[++uIndex] = y;
118
- // uvs.push(x, y);
119
- i += 3;
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
+ }
120
152
  }
153
+
121
154
  i = 0;
122
155
  len = leftPoints.length;
123
156
  let iIndex = indices.length - 1;
@@ -168,21 +201,28 @@ function generateSides(result, options) {
168
201
  const bottomStickGround = options.bottomStickGround;
169
202
  const rings = [leftPoints, rightPoints];
170
203
  const depthsEnable = result.depths;
204
+ const pathUV = options.pathUV;
205
+ const lineWidth = options.lineWidth;
171
206
 
172
207
  let pIndex = points.length - 1;
173
208
  let iIndex = indices.length - 1;
209
+ let uIndex = uv.length - 1;
210
+
174
211
  function addOneSideIndex(v1, v2) {
175
212
  const idx = points.length / 3;
176
213
  // let pIndex = points.length - 1;
177
214
 
215
+ const v1Depth = (depthsEnable ? v1.depth : z);
216
+ const v2Depth = (depthsEnable ? v2.depth : z);
217
+
178
218
  // top
179
219
  points[++pIndex] = v1[0];
180
220
  points[++pIndex] = v1[1];
181
- points[++pIndex] = (depthsEnable ? v1.depth : z) + v1[2];
221
+ points[++pIndex] = v1Depth + v1[2];
182
222
 
183
223
  points[++pIndex] = v2[0];
184
224
  points[++pIndex] = v2[1];
185
- points[++pIndex] = (depthsEnable ? v2.depth : z) + v2[2];
225
+ points[++pIndex] = v2Depth + v2[2];
186
226
 
187
227
  // points.push(v1[0], v1[1], (depthsEnable ? v1.depth : z) + v1[2], v2[0], v2[1], (depthsEnable ? v2.depth : z) + v2[2]);
188
228
 
@@ -206,7 +246,21 @@ function generateSides(result, options) {
206
246
  indices[++iIndex] = d;
207
247
  indices[++iIndex] = b;
208
248
  // index.push(a, c, b, c, d, b);
209
- generateSideWallUV(uv, points, a, b, c, d);
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
+ }
210
264
  }
211
265
 
212
266
  for (let i = 0, len = rings.length; i < len; i++) {
@@ -238,7 +292,10 @@ const TEMPV1 = { x: 0, y: 0 }, TEMPV2 = { x: 0, y: 0 };
238
292
 
239
293
  export function expandLine(line, options) {
240
294
  // let preAngle = 0;
241
- const radius = options.lineWidth / 2;
295
+ let radius = options.lineWidth / 2;
296
+ if (options.isSlope) {
297
+ radius *= 2;
298
+ }
242
299
  const points = [], leftPoints = [], rightPoints = [];
243
300
  const len = line.length;
244
301
  let i = 0;
@@ -302,7 +359,7 @@ export function expandLine(line, options) {
302
359
  i++;
303
360
  }
304
361
 
305
- return { offsetPoints: points, leftPoints, rightPoints };
362
+ return { offsetPoints: points, leftPoints, rightPoints, line };
306
363
  }
307
364
 
308
365
  // eslint-disable-next-line no-unused-vars
package/src/util.js CHANGED
@@ -221,3 +221,22 @@ export function line2Vectors(line) {
221
221
  }
222
222
  return points;
223
223
  }
224
+
225
+ export function calLineDistance(line) {
226
+ let distance = 0;
227
+ for (let i = 0, len = line.length; i < len; i++) {
228
+ const p1 = line[i], p2 = line[i + 1];
229
+ if (i === 0) {
230
+ p1.distance = 0;
231
+ }
232
+ if (p1 && p2) {
233
+ const [x1, y1, z1] = p1;
234
+ const [x2, y2, z2] = p2;
235
+ const dx = x1 - x2, dy = y1 - y2, dz = (z1 || 0) - (z2 || 0);
236
+ const dis = Math.sqrt(dx * dx + dy * dy + dz * dz);
237
+ distance += dis;
238
+ p2.distance = distance;
239
+ }
240
+ }
241
+ return distance;
242
+ }