poly-extrude 0.0.9 → 0.2.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,5 +1,5 @@
1
1
  /*!
2
- * poly-extrude v0.0.9
2
+ * poly-extrude v0.2.0
3
3
  */
4
4
  (function (global, factory) {
5
5
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
@@ -1107,28 +1107,30 @@
1107
1107
  var idx0 = i * 3;
1108
1108
  var _leftPoints$i = leftPoints[i],
1109
1109
  x1 = _leftPoints$i[0],
1110
- y1 = _leftPoints$i[1];
1110
+ y1 = _leftPoints$i[1],
1111
+ z1 = _leftPoints$i[2];
1111
1112
  points[idx0] = x1;
1112
1113
  points[idx0 + 1] = y1;
1113
- points[idx0 + 2] = z; // top right
1114
+ points[idx0 + 2] = z + z1; // top right
1114
1115
 
1115
1116
  var _rightPoints$i = rightPoints[i],
1116
1117
  x2 = _rightPoints$i[0],
1117
- y2 = _rightPoints$i[1];
1118
+ y2 = _rightPoints$i[1],
1119
+ z2 = _rightPoints$i[2];
1118
1120
  var idx1 = len * 3 + idx0;
1119
1121
  points[idx1] = x2;
1120
1122
  points[idx1 + 1] = y2;
1121
- points[idx1 + 2] = z; // bottom left
1123
+ points[idx1 + 2] = z + z2; // bottom left
1122
1124
 
1123
1125
  var idx2 = len * 2 * 3 + idx0;
1124
1126
  points[idx2] = x1;
1125
1127
  points[idx2 + 1] = y1;
1126
- points[idx2 + 2] = 0; // bottom right
1128
+ points[idx2 + 2] = z1; // bottom right
1127
1129
 
1128
1130
  var idx3 = len * 2 * 3 + len * 3 + idx0;
1129
1131
  points[idx3] = x2;
1130
1132
  points[idx3 + 1] = y2;
1131
- points[idx3 + 2] = 0;
1133
+ points[idx3 + 2] = z2;
1132
1134
  i++;
1133
1135
  }
1134
1136
 
@@ -1182,7 +1184,7 @@
1182
1184
 
1183
1185
  function addOneSideIndex(v1, v2) {
1184
1186
  var idx = points.length / 3;
1185
- points.push(v1[0], v1[1], z, v2[0], v2[1], z, v1[0], v1[1], 0, v2[0], v2[1], 0);
1187
+ 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]);
1186
1188
  var a = idx + 2,
1187
1189
  b = idx + 3,
1188
1190
  c = idx,
@@ -1311,13 +1313,14 @@
1311
1313
  function calOffsetPoint(rad, radius, p) {
1312
1314
  var x = p[0],
1313
1315
  y = p[1];
1316
+ var z = p[2] || 0;
1314
1317
  var x1 = Math.cos(rad) * radius,
1315
1318
  y1 = Math.sin(rad) * radius;
1316
- var p1 = [x + x1, y + y1];
1319
+ var p1 = [x + x1, y + y1, z];
1317
1320
  var rad1 = rad += Math.PI;
1318
1321
  var x2 = Math.cos(rad1) * radius,
1319
1322
  y2 = Math.sin(rad1) * radius;
1320
- var p2 = [x + x2, y + y2];
1323
+ var p2 = [x + x2, y + y2, z];
1321
1324
  return [p1, p2];
1322
1325
  }
1323
1326
 
@@ -1342,6 +1345,125 @@
1342
1345
  return (y1 - y2) * x + (x2 - x1) * y + x1 * y2 - x2 * y1 > 0;
1343
1346
  }
1344
1347
 
1348
+ function cylinder(point, options) {
1349
+ if (options === void 0) {
1350
+ options = {};
1351
+ }
1352
+
1353
+ options = Object.assign({}, {
1354
+ radius: 1,
1355
+ height: 2,
1356
+ radialSegments: 6
1357
+ }, options);
1358
+ var radialSegments = Math.round(Math.max(4, options.radialSegments));
1359
+ var _options = options,
1360
+ radius = _options.radius,
1361
+ height = _options.height;
1362
+ var aRad = 360 / radialSegments / 360 * Math.PI * 2;
1363
+ var circlePointsLen = radialSegments + 1;
1364
+ var points = new Float32Array(circlePointsLen * 3 * 2);
1365
+ var centerx = point[0],
1366
+ centery = point[1];
1367
+ var idx = 0,
1368
+ uIdx = 0;
1369
+ var offset = circlePointsLen * 3,
1370
+ uOffset = circlePointsLen * 2;
1371
+ var indices = [],
1372
+ uvs = [];
1373
+
1374
+ for (var i = -1; i < radialSegments; i++) {
1375
+ var rad = aRad * i;
1376
+ var x = Math.cos(rad) * radius + centerx,
1377
+ y = Math.sin(rad) * radius + centery; // bottom vertices
1378
+
1379
+ points[idx] = x;
1380
+ points[idx + 1] = y;
1381
+ points[idx + 2] = 0; // top vertices
1382
+
1383
+ points[idx + offset] = x;
1384
+ points[idx + 1 + offset] = y;
1385
+ points[idx + 2 + offset] = height;
1386
+ var u = 0,
1387
+ v = 0;
1388
+ u = 0.5 + x / radius / 2;
1389
+ v = 0.5 + y / radius / 2;
1390
+ uvs[uIdx] = u;
1391
+ uvs[uIdx + 1] = v;
1392
+ uvs[uIdx + uOffset] = u;
1393
+ uvs[uIdx + 1 + uOffset] = v;
1394
+ idx += 3;
1395
+ uIdx += 2;
1396
+
1397
+ if (i > 1) {
1398
+ // bottom indices
1399
+ indices.push(0, i - 1, i);
1400
+ }
1401
+ }
1402
+
1403
+ idx -= 3;
1404
+ points[idx] = points[0];
1405
+ points[idx + 1] = points[1];
1406
+ points[idx + 2] = points[2];
1407
+ var pointsLen = points.length;
1408
+ points[pointsLen - 3] = points[0];
1409
+ points[pointsLen - 2] = points[1];
1410
+ points[pointsLen - 1] = height;
1411
+ var indicesLen = indices.length; // top indices
1412
+
1413
+ for (var _i = 0; _i < indicesLen; _i++) {
1414
+ var index = indices[_i];
1415
+ indices.push(index + circlePointsLen);
1416
+ }
1417
+
1418
+ var sidePoints = new Float32Array((circlePointsLen * 3 * 2 - 6) * 2);
1419
+ var pIndex = -1;
1420
+ idx = circlePointsLen * 2;
1421
+ uIdx = 0;
1422
+
1423
+ for (var _i2 = 0, len = points.length / 2; _i2 < len - 3; _i2 += 3) {
1424
+ var x1 = points[_i2],
1425
+ y1 = points[_i2 + 1],
1426
+ x2 = points[_i2 + 3],
1427
+ y2 = points[_i2 + 4];
1428
+ sidePoints[++pIndex] = x1;
1429
+ sidePoints[++pIndex] = y1;
1430
+ sidePoints[++pIndex] = height;
1431
+ sidePoints[++pIndex] = x2;
1432
+ sidePoints[++pIndex] = y2;
1433
+ sidePoints[++pIndex] = height;
1434
+ sidePoints[++pIndex] = x1;
1435
+ sidePoints[++pIndex] = y1;
1436
+ sidePoints[++pIndex] = 0;
1437
+ sidePoints[++pIndex] = x2;
1438
+ sidePoints[++pIndex] = y2;
1439
+ sidePoints[++pIndex] = 0;
1440
+ var a = idx + 2,
1441
+ b = idx + 3,
1442
+ c = idx,
1443
+ d = idx + 1; // indices.push(a, c, b, c, d, b);
1444
+
1445
+ indices.push(c, a, d, a, b, d);
1446
+ idx += 4;
1447
+ var u1 = uIdx / circlePointsLen,
1448
+ u2 = (uIdx + 1) / circlePointsLen;
1449
+ uvs.push(u1, height / radius / 2, u2, height / radius / 2, u1, 0, u2, 0);
1450
+ uIdx++;
1451
+ }
1452
+
1453
+ var position = new Float32Array(points.length + sidePoints.length);
1454
+ position.set(points, 0);
1455
+ position.set(sidePoints, points.length);
1456
+ var normal = generateNormal(indices, position);
1457
+ return {
1458
+ points: points,
1459
+ indices: new Uint32Array(indices),
1460
+ position: position,
1461
+ normal: normal,
1462
+ uv: new Float32Array(uvs)
1463
+ };
1464
+ }
1465
+
1466
+ exports.cylinder = cylinder;
1345
1467
  exports.expandLine = expandLine;
1346
1468
  exports.extrudePolygons = extrudePolygons;
1347
1469
  exports.extrudePolylines = extrudePolylines;