poly-extrude 0.0.9 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * poly-extrude v0.0.9
2
+ * poly-extrude v0.2.0
3
3
  */
4
4
  var earcut$2 = {exports: {}};
5
5
 
@@ -1101,28 +1101,30 @@ function generateTopAndBottom(result, options) {
1101
1101
  var idx0 = i * 3;
1102
1102
  var _leftPoints$i = leftPoints[i],
1103
1103
  x1 = _leftPoints$i[0],
1104
- y1 = _leftPoints$i[1];
1104
+ y1 = _leftPoints$i[1],
1105
+ z1 = _leftPoints$i[2];
1105
1106
  points[idx0] = x1;
1106
1107
  points[idx0 + 1] = y1;
1107
- points[idx0 + 2] = z; // top right
1108
+ points[idx0 + 2] = z + z1; // top right
1108
1109
 
1109
1110
  var _rightPoints$i = rightPoints[i],
1110
1111
  x2 = _rightPoints$i[0],
1111
- y2 = _rightPoints$i[1];
1112
+ y2 = _rightPoints$i[1],
1113
+ z2 = _rightPoints$i[2];
1112
1114
  var idx1 = len * 3 + idx0;
1113
1115
  points[idx1] = x2;
1114
1116
  points[idx1 + 1] = y2;
1115
- points[idx1 + 2] = z; // bottom left
1117
+ points[idx1 + 2] = z + z2; // bottom left
1116
1118
 
1117
1119
  var idx2 = len * 2 * 3 + idx0;
1118
1120
  points[idx2] = x1;
1119
1121
  points[idx2 + 1] = y1;
1120
- points[idx2 + 2] = 0; // bottom right
1122
+ points[idx2 + 2] = z1; // bottom right
1121
1123
 
1122
1124
  var idx3 = len * 2 * 3 + len * 3 + idx0;
1123
1125
  points[idx3] = x2;
1124
1126
  points[idx3 + 1] = y2;
1125
- points[idx3 + 2] = 0;
1127
+ points[idx3 + 2] = z2;
1126
1128
  i++;
1127
1129
  }
1128
1130
 
@@ -1176,7 +1178,7 @@ function generateSides(result, options) {
1176
1178
 
1177
1179
  function addOneSideIndex(v1, v2) {
1178
1180
  var idx = points.length / 3;
1179
- points.push(v1[0], v1[1], z, v2[0], v2[1], z, v1[0], v1[1], 0, v2[0], v2[1], 0);
1181
+ points.push(v1[0], v1[1], z + v1[2], v2[0], v2[1], z + v2[2], v1[0], v1[1], v1[2], v2[0], v2[1], v2[2]);
1180
1182
  var a = idx + 2,
1181
1183
  b = idx + 3,
1182
1184
  c = idx,
@@ -1305,13 +1307,14 @@ function expandLine(line, options) {
1305
1307
  function calOffsetPoint(rad, radius, p) {
1306
1308
  var x = p[0],
1307
1309
  y = p[1];
1310
+ var z = p[2] || 0;
1308
1311
  var x1 = Math.cos(rad) * radius,
1309
1312
  y1 = Math.sin(rad) * radius;
1310
- var p1 = [x + x1, y + y1];
1313
+ var p1 = [x + x1, y + y1, z];
1311
1314
  var rad1 = rad += Math.PI;
1312
1315
  var x2 = Math.cos(rad1) * radius,
1313
1316
  y2 = Math.sin(rad1) * radius;
1314
- var p2 = [x + x2, y + y2];
1317
+ var p2 = [x + x2, y + y2, z];
1315
1318
  return [p1, p2];
1316
1319
  }
1317
1320
 
@@ -1336,4 +1339,122 @@ function leftOnLine(p, p1, p2) {
1336
1339
  return (y1 - y2) * x + (x2 - x1) * y + x1 * y2 - x2 * y1 > 0;
1337
1340
  }
1338
1341
 
1339
- export { expandLine, extrudePolygons, extrudePolylines };
1342
+ function cylinder(point, options) {
1343
+ if (options === void 0) {
1344
+ options = {};
1345
+ }
1346
+
1347
+ options = Object.assign({}, {
1348
+ radius: 1,
1349
+ height: 2,
1350
+ radialSegments: 6
1351
+ }, options);
1352
+ var radialSegments = Math.round(Math.max(4, options.radialSegments));
1353
+ var _options = options,
1354
+ radius = _options.radius,
1355
+ height = _options.height;
1356
+ var aRad = 360 / radialSegments / 360 * Math.PI * 2;
1357
+ var circlePointsLen = radialSegments + 1;
1358
+ var points = new Float32Array(circlePointsLen * 3 * 2);
1359
+ var centerx = point[0],
1360
+ centery = point[1];
1361
+ var idx = 0,
1362
+ uIdx = 0;
1363
+ var offset = circlePointsLen * 3,
1364
+ uOffset = circlePointsLen * 2;
1365
+ var indices = [],
1366
+ uvs = [];
1367
+
1368
+ for (var i = -1; i < radialSegments; i++) {
1369
+ var rad = aRad * i;
1370
+ var x = Math.cos(rad) * radius + centerx,
1371
+ y = Math.sin(rad) * radius + centery; // bottom vertices
1372
+
1373
+ points[idx] = x;
1374
+ points[idx + 1] = y;
1375
+ points[idx + 2] = 0; // top vertices
1376
+
1377
+ points[idx + offset] = x;
1378
+ points[idx + 1 + offset] = y;
1379
+ points[idx + 2 + offset] = height;
1380
+ var u = 0,
1381
+ v = 0;
1382
+ u = 0.5 + x / radius / 2;
1383
+ v = 0.5 + y / radius / 2;
1384
+ uvs[uIdx] = u;
1385
+ uvs[uIdx + 1] = v;
1386
+ uvs[uIdx + uOffset] = u;
1387
+ uvs[uIdx + 1 + uOffset] = v;
1388
+ idx += 3;
1389
+ uIdx += 2;
1390
+
1391
+ if (i > 1) {
1392
+ // bottom indices
1393
+ indices.push(0, i - 1, i);
1394
+ }
1395
+ }
1396
+
1397
+ idx -= 3;
1398
+ points[idx] = points[0];
1399
+ points[idx + 1] = points[1];
1400
+ points[idx + 2] = points[2];
1401
+ var pointsLen = points.length;
1402
+ points[pointsLen - 3] = points[0];
1403
+ points[pointsLen - 2] = points[1];
1404
+ points[pointsLen - 1] = height;
1405
+ var indicesLen = indices.length; // top indices
1406
+
1407
+ for (var _i = 0; _i < indicesLen; _i++) {
1408
+ var index = indices[_i];
1409
+ indices.push(index + circlePointsLen);
1410
+ }
1411
+
1412
+ var sidePoints = new Float32Array((circlePointsLen * 3 * 2 - 6) * 2);
1413
+ var pIndex = -1;
1414
+ idx = circlePointsLen * 2;
1415
+ uIdx = 0;
1416
+
1417
+ for (var _i2 = 0, len = points.length / 2; _i2 < len - 3; _i2 += 3) {
1418
+ var x1 = points[_i2],
1419
+ y1 = points[_i2 + 1],
1420
+ x2 = points[_i2 + 3],
1421
+ y2 = points[_i2 + 4];
1422
+ sidePoints[++pIndex] = x1;
1423
+ sidePoints[++pIndex] = y1;
1424
+ sidePoints[++pIndex] = height;
1425
+ sidePoints[++pIndex] = x2;
1426
+ sidePoints[++pIndex] = y2;
1427
+ sidePoints[++pIndex] = height;
1428
+ sidePoints[++pIndex] = x1;
1429
+ sidePoints[++pIndex] = y1;
1430
+ sidePoints[++pIndex] = 0;
1431
+ sidePoints[++pIndex] = x2;
1432
+ sidePoints[++pIndex] = y2;
1433
+ sidePoints[++pIndex] = 0;
1434
+ var a = idx + 2,
1435
+ b = idx + 3,
1436
+ c = idx,
1437
+ d = idx + 1; // indices.push(a, c, b, c, d, b);
1438
+
1439
+ indices.push(c, a, d, a, b, d);
1440
+ idx += 4;
1441
+ var u1 = uIdx / circlePointsLen,
1442
+ u2 = (uIdx + 1) / circlePointsLen;
1443
+ uvs.push(u1, height / radius / 2, u2, height / radius / 2, u1, 0, u2, 0);
1444
+ uIdx++;
1445
+ }
1446
+
1447
+ var position = new Float32Array(points.length + sidePoints.length);
1448
+ position.set(points, 0);
1449
+ position.set(sidePoints, points.length);
1450
+ var normal = generateNormal(indices, position);
1451
+ return {
1452
+ points: points,
1453
+ indices: new Uint32Array(indices),
1454
+ position: position,
1455
+ normal: normal,
1456
+ uv: new Float32Array(uvs)
1457
+ };
1458
+ }
1459
+
1460
+ export { cylinder, expandLine, extrudePolygons, extrudePolylines };
package/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  import { extrudePolygons } from './src/polygon';
2
2
  import { extrudePolylines, expandLine } from './src/polyline';
3
- export { extrudePolygons, extrudePolylines, expandLine };
3
+ import { cylinder } from './src/cylinder';
4
+ export { extrudePolygons, extrudePolylines, expandLine, cylinder };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "poly-extrude",
3
- "version": "0.0.9",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "main": "dist/poly-extrude.js",
6
6
  "module": "dist/poly-extrude.mjs",
package/readme.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # poly-extrude
2
2
 
3
+
3
4
  Extrude polygons/polylines. Born in [maptalks.three](https://github.com/maptalks/maptalks.three) project<br>
5
+
6
+ ## Examples
4
7
  [building](https://deyihu.github.io/poly-extrude/test/building.html)<br>
5
8
  ![](./gallery/building.png)<br>
6
9
  [buildings](https://deyihu.github.io/poly-extrude/test/buildings.html)<br>
@@ -11,8 +14,16 @@ Extrude polygons/polylines. Born in [maptalks.three](https://github.com/maptalks
11
14
  ![](./gallery/street.png)<br>
12
15
  [line-uv](https://deyihu.github.io/poly-extrude/test/line-uv.html)<br>
13
16
  ![](./gallery/line-uv.png)
17
+ [ny-building](https://deyihu.github.io/poly-extrude/test/ny-building.html)<br>
18
+ ![](./gallery/ny-building.png)
19
+ [cylinder](https://deyihu.github.io/poly-extrude/test/cylinder.html)<br>
20
+ ![](./gallery/cylinder.png)
21
+ [brige](https://deyihu.github.io/poly-extrude/test/brige.html)<br>
22
+ ![](./gallery/brige.png)
23
+ [spring](https://deyihu.github.io/poly-extrude/test/spring.html)<br>
24
+ ![](./gallery/spring.png)
14
25
 
15
- ## install
26
+ ## Install
16
27
 
17
28
  ```sh
18
29
  npm i poly-extrude
@@ -31,34 +42,79 @@ pnpm i poly-extrude
31
42
  ### ESM
32
43
 
33
44
  ```js
34
- import {extrudePolygons,extrudePolylines} from 'poly-extrude';
35
- const polygons=[
36
- //polygon
37
- [
38
- //outring
39
- [[x,y],[x,y],...........],
40
- //holes
41
- [[x,y],[x,y],...........],
42
- ........
43
-
44
- ],
45
- //other polygons
46
- ......
45
+ import {
46
+ extrudePolygons,
47
+ extrudePolylines,
48
+ cylinder
49
+ } from 'poly-extrude';
50
+ const polygons = [
51
+ //polygon
52
+ [
53
+ //outring
54
+ [
55
+ [x, y],
56
+ [x, y], ...........
57
+ ],
58
+ //holes
59
+ [
60
+ [x, y],
61
+ [x, y], ...........
62
+ ],
63
+ ........
64
+
65
+ ],
66
+ //other polygons
67
+ ......
47
68
  ]
48
69
 
49
- const result = extrudePolygons(polygons,{depth:2});
50
- const {positon,normal,uv,indices}=result;
70
+ const result = extrudePolygons(polygons, {
71
+ depth: 2
72
+ });
73
+ const {
74
+ positon,
75
+ normal,
76
+ uv,
77
+ indices
78
+ } = result;
51
79
  //do something
52
80
 
53
- const polylines=[
54
- // polyline
55
- [[x,y],[x,y],...........],
56
- //polyline
57
- [[x,y],[x,y],...........],
81
+ const polylines = [
82
+ // polyline
83
+ [
84
+ [x, y],
85
+ [x, y], ...........
86
+ ],
87
+ //polyline
88
+ [
89
+ [x, y],
90
+ [x, y], ...........
91
+ ],
58
92
  ];
59
93
 
60
- const result = extrudePolylines(polylines,{depth:2,lineWidth:2});
61
- const {positon,normal,uv,indices}=result;
94
+ const result = extrudePolylines(polylines, {
95
+ depth: 2,
96
+ lineWidth: 2
97
+ });
98
+ const {
99
+ positon,
100
+ normal,
101
+ uv,
102
+ indices
103
+ } = result;
104
+ //do something
105
+
106
+ const center = [0, 0];
107
+ const result = cylinder(center, {
108
+ radius: 1,
109
+ height: 2,
110
+ radialSegments: 6
111
+ });
112
+ const {
113
+ positon,
114
+ normal,
115
+ uv,
116
+ indices
117
+ } = result;
62
118
  //do something
63
119
  ```
64
120
 
@@ -68,13 +124,19 @@ pnpm i poly-extrude
68
124
  <script src="https://unpkg.com/poly-extrude/dist/poly-extrude.js"></script>
69
125
 
70
126
  <script>
71
- const polygons=[
127
+ const polygons = [
72
128
  //polygon
73
129
  [
74
130
  //outring
75
- [[x,y],[x,y],...........],
131
+ [
132
+ [x, y],
133
+ [x, y], ...........
134
+ ],
76
135
  //holes
77
- [[x,y],[x,y],...........],
136
+ [
137
+ [x, y],
138
+ [x, y], ...........
139
+ ],
78
140
  ........
79
141
 
80
142
  ],
@@ -82,19 +144,54 @@ pnpm i poly-extrude
82
144
  ......
83
145
  ]
84
146
 
85
- const result = polyextrude.extrudePolygons(polygons,{depth:2})
86
- const {positon,normal,uv,indices}=result;
147
+ const result = polyextrude.extrudePolygons(polygons, {
148
+ depth: 2
149
+ })
150
+ const {
151
+ positon,
152
+ normal,
153
+ uv,
154
+ indices
155
+ } = result;
87
156
  //do something
88
157
 
89
- const polylines=[
90
- // polyline
91
- [[x,y],[x,y],...........],
92
- //polyline
93
- [[x,y],[x,y],...........],
158
+ const polylines = [
159
+ // polyline
160
+ [
161
+ [x, y],
162
+ [x, y], ...........
163
+ ],
164
+ //polyline
165
+ [
166
+ [x, y],
167
+ [x, y], ...........
168
+ ],
94
169
  ];
95
170
 
96
- const result = polyextrude.extrudePolylines(polylines,{depth:2,lineWidth:2});
97
- const {positon,normal,uv,indices}=result;
171
+ const result = polyextrude.extrudePolylines(polylines, {
172
+ depth: 2,
173
+ lineWidth: 2
174
+ });
175
+ const {
176
+ positon,
177
+ normal,
178
+ uv,
179
+ indices
180
+ } = result;
181
+ //do something
182
+
183
+ const center = [0, 0];
184
+ const result = polyextrude.cylinder(center, {
185
+ radius: 1,
186
+ height: 2,
187
+ radialSegments: 6
188
+ });
189
+ const {
190
+ positon,
191
+ normal,
192
+ uv,
193
+ indices
194
+ } = result;
98
195
  //do something
99
196
  </script>
100
197
  ```
@@ -0,0 +1,89 @@
1
+ import { generateNormal } from './util';
2
+
3
+ export function cylinder(point, options = {}) {
4
+ options = Object.assign({}, { radius: 1, height: 2, radialSegments: 6 }, options);
5
+ const radialSegments = Math.round(Math.max(4, options.radialSegments));
6
+ const { radius, height } = options;
7
+ const aRad = 360 / radialSegments / 360 * Math.PI * 2;
8
+ const circlePointsLen = (radialSegments + 1);
9
+ const points = new Float32Array(circlePointsLen * 3 * 2);
10
+ const [centerx, centery] = point;
11
+ let idx = 0, uIdx = 0;
12
+ const offset = circlePointsLen * 3, uOffset = circlePointsLen * 2;
13
+ const indices = [], uvs = [];
14
+ for (let i = -1; i < radialSegments; i++) {
15
+ const rad = aRad * i;
16
+ const x = Math.cos(rad) * radius + centerx, y = Math.sin(rad) * radius + centery;
17
+ // bottom vertices
18
+ points[idx] = x;
19
+ points[idx + 1] = y;
20
+ points[idx + 2] = 0;
21
+
22
+ // top vertices
23
+ points[idx + offset] = x;
24
+ points[idx + 1 + offset] = y;
25
+ points[idx + 2 + offset] = height;
26
+
27
+ let u = 0, v = 0;
28
+ u = 0.5 + x / radius / 2;
29
+ 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;
34
+
35
+ idx += 3;
36
+ uIdx += 2;
37
+ if (i > 1) {
38
+ // bottom indices
39
+ indices.push(0, i - 1, i);
40
+ }
41
+ }
42
+ idx -= 3;
43
+ points[idx] = points[0];
44
+ points[idx + 1] = points[1];
45
+ points[idx + 2] = points[2];
46
+ const pointsLen = points.length;
47
+ points[pointsLen - 3] = points[0];
48
+ points[pointsLen - 2] = points[1];
49
+ points[pointsLen - 1] = height;
50
+
51
+ const indicesLen = indices.length;
52
+ // top indices
53
+ for (let i = 0; i < indicesLen; i++) {
54
+ const index = indices[i];
55
+ indices.push(index + circlePointsLen);
56
+ }
57
+
58
+ const sidePoints = new Float32Array((circlePointsLen * 3 * 2 - 6) * 2);
59
+ let pIndex = -1;
60
+ idx = circlePointsLen * 2;
61
+ uIdx = 0;
62
+ for (let i = 0, len = points.length / 2; i < len - 3; i += 3) {
63
+ const x1 = points[i], y1 = points[i + 1], x2 = points[i + 3], y2 = points[i + 4];
64
+ sidePoints[++pIndex] = x1;
65
+ sidePoints[++pIndex] = y1;
66
+ sidePoints[++pIndex] = height;
67
+ sidePoints[++pIndex] = x2;
68
+ sidePoints[++pIndex] = y2;
69
+ sidePoints[++pIndex] = height;
70
+ sidePoints[++pIndex] = x1;
71
+ sidePoints[++pIndex] = y1;
72
+ sidePoints[++pIndex] = 0;
73
+ sidePoints[++pIndex] = x2;
74
+ sidePoints[++pIndex] = y2;
75
+ sidePoints[++pIndex] = 0;
76
+ const a = idx + 2, b = idx + 3, c = idx, d = idx + 1;
77
+ // indices.push(a, c, b, c, d, b);
78
+ indices.push(c, a, d, a, b, d);
79
+ idx += 4;
80
+ const u1 = uIdx / circlePointsLen, u2 = (uIdx + 1) / circlePointsLen;
81
+ uvs.push(u1, height / radius / 2, u2, height / radius / 2, u1, 0, u2, 0);
82
+ uIdx++;
83
+ }
84
+ const position = new Float32Array(points.length + sidePoints.length);
85
+ position.set(points, 0);
86
+ position.set(sidePoints, points.length);
87
+ const normal = generateNormal(indices, position);
88
+ return { points, indices: new Uint32Array(indices), position, normal, uv: new Float32Array(uvs) };
89
+ }
package/src/polyline.js CHANGED
@@ -26,29 +26,29 @@ function generateTopAndBottom(result, options) {
26
26
  while (i < len) {
27
27
  // top left
28
28
  const idx0 = i * 3;
29
- const [x1, y1] = leftPoints[i];
29
+ const [x1, y1, z1] = leftPoints[i];
30
30
  points[idx0] = x1;
31
31
  points[idx0 + 1] = y1;
32
- points[idx0 + 2] = z;
32
+ points[idx0 + 2] = z + z1;
33
33
 
34
34
  // top right
35
- const [x2, y2] = rightPoints[i];
35
+ const [x2, y2, z2] = rightPoints[i];
36
36
  const idx1 = len * 3 + idx0;
37
37
  points[idx1] = x2;
38
38
  points[idx1 + 1] = y2;
39
- points[idx1 + 2] = z;
39
+ points[idx1 + 2] = z + z2;
40
40
 
41
41
  // bottom left
42
42
  const idx2 = (len * 2) * 3 + idx0;
43
43
  points[idx2] = x1;
44
44
  points[idx2 + 1] = y1;
45
- points[idx2 + 2] = 0;
45
+ points[idx2 + 2] = z1;
46
46
 
47
47
  // bottom right
48
48
  const idx3 = (len * 2) * 3 + len * 3 + idx0;
49
49
  points[idx3] = x2;
50
50
  points[idx3 + 1] = y2;
51
- points[idx3 + 2] = 0;
51
+ points[idx3 + 2] = z2;
52
52
 
53
53
  i++;
54
54
  }
@@ -88,7 +88,7 @@ function generateSides(result, options) {
88
88
 
89
89
  function addOneSideIndex(v1, v2) {
90
90
  const idx = points.length / 3;
91
- points.push(v1[0], v1[1], z, v2[0], v2[1], z, v1[0], v1[1], 0, v2[0], v2[1], 0);
91
+ points.push(v1[0], v1[1], z + v1[2], v2[0], v2[1], z + v2[2], v1[0], v1[1], v1[2], v2[0], v2[1], v2[2]);
92
92
  const a = idx + 2, b = idx + 3, c = idx, d = idx + 1;
93
93
  index.push(a, c, b, c, d, b);
94
94
  generateSideWallUV(uvs, points, a, b, c, d);
@@ -180,11 +180,12 @@ export function expandLine(line, options) {
180
180
 
181
181
  function calOffsetPoint(rad, radius, p) {
182
182
  const [x, y] = p;
183
+ const z = p[2] || 0;
183
184
  const x1 = Math.cos(rad) * radius, y1 = Math.sin(rad) * radius;
184
- const p1 = [x + x1, y + y1];
185
+ const p1 = [x + x1, y + y1, z];
185
186
  const rad1 = rad += Math.PI;
186
187
  const x2 = Math.cos(rad1) * radius, y2 = Math.sin(rad1) * radius;
187
- const p2 = [x + x2, y + y2];
188
+ const p2 = [x + x2, y + y2, z];
188
189
  return [p1, p2];
189
190
  }
190
191