poly-extrude 0.4.0 → 0.6.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.4.0
2
+ * poly-extrude v0.6.0
3
3
  */
4
4
  (function (global, factory) {
5
5
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
@@ -1071,11 +1071,19 @@
1071
1071
  return x1 === x2 && y1 === y2;
1072
1072
  }
1073
1073
 
1074
+ function checkOptions(options) {
1075
+ options.lineWidth = Math.max(0, options.lineWidth);
1076
+ options.depth = Math.max(0, options.depth);
1077
+ options.sideDepth = Math.max(0, options.sideDepth);
1078
+ }
1079
+
1074
1080
  function extrudePolylines(lines, options) {
1075
1081
  options = Object.assign({}, {
1076
1082
  depth: 2,
1077
- lineWidth: 1
1083
+ lineWidth: 1,
1084
+ bottomStickGround: false
1078
1085
  }, options);
1086
+ checkOptions(options);
1079
1087
  var results = lines.map(function (line) {
1080
1088
  var result = expandLine(line, options);
1081
1089
  result.line = line;
@@ -1091,9 +1099,69 @@
1091
1099
  result.lines = lines;
1092
1100
  return result;
1093
1101
  }
1102
+ function extrudeSlopes(lines, options) {
1103
+ options = Object.assign({}, {
1104
+ depth: 2,
1105
+ lineWidth: 1,
1106
+ side: 'left',
1107
+ sideDepth: 0,
1108
+ bottomStickGround: false
1109
+ }, options);
1110
+ checkOptions(options);
1111
+ var _options = options,
1112
+ depth = _options.depth,
1113
+ side = _options.side,
1114
+ sideDepth = _options.sideDepth;
1115
+ var results = lines.map(function (line) {
1116
+ var tempResult = expandLine(line, options);
1117
+ tempResult.line = line;
1118
+ var leftPoints = tempResult.leftPoints,
1119
+ rightPoints = tempResult.rightPoints;
1120
+ var result = {
1121
+ line: line
1122
+ };
1123
+ var depths;
1124
+
1125
+ for (var i = 0, len = line.length; i < len; i++) {
1126
+ line[i][2] = line[i][2] || 0;
1127
+ }
1128
+
1129
+ if (side === 'left') {
1130
+ result.leftPoints = leftPoints;
1131
+ result.rightPoints = line;
1132
+ depths = [sideDepth, depth];
1133
+ } else {
1134
+ result.leftPoints = line;
1135
+ result.rightPoints = rightPoints;
1136
+ depths = [depth, sideDepth];
1137
+ }
1138
+
1139
+ result.depths = depths;
1140
+ generateTopAndBottom(result, options);
1141
+ generateSides(result, options);
1142
+ result.position = new Float32Array(result.points);
1143
+ result.indices = new Uint32Array(result.index);
1144
+ result.uv = new Float32Array(result.uvs);
1145
+ result.normal = generateNormal(result.indices, result.position);
1146
+ return result;
1147
+ });
1148
+ var result = merge(results);
1149
+ result.lines = lines;
1150
+ return result;
1151
+ }
1094
1152
 
1095
1153
  function generateTopAndBottom(result, options) {
1154
+ var bottomStickGround = options.bottomStickGround;
1096
1155
  var z = options.depth;
1156
+ var depths = result.depths;
1157
+ var lz = z,
1158
+ rz = z;
1159
+
1160
+ if (depths) {
1161
+ lz = depths[0];
1162
+ rz = depths[1];
1163
+ }
1164
+
1097
1165
  var points = [],
1098
1166
  index = [],
1099
1167
  uvs = [];
@@ -1111,7 +1179,7 @@
1111
1179
  z1 = _leftPoints$i[2];
1112
1180
  points[idx0] = x1;
1113
1181
  points[idx0 + 1] = y1;
1114
- points[idx0 + 2] = z + z1; // top right
1182
+ points[idx0 + 2] = lz + z1; // top right
1115
1183
 
1116
1184
  var _rightPoints$i = rightPoints[i],
1117
1185
  x2 = _rightPoints$i[0],
@@ -1120,17 +1188,27 @@
1120
1188
  var idx1 = len * 3 + idx0;
1121
1189
  points[idx1] = x2;
1122
1190
  points[idx1 + 1] = y2;
1123
- points[idx1 + 2] = z + z2; // bottom left
1191
+ points[idx1 + 2] = rz + z2; // bottom left
1124
1192
 
1125
1193
  var idx2 = len * 2 * 3 + idx0;
1126
1194
  points[idx2] = x1;
1127
1195
  points[idx2 + 1] = y1;
1128
- points[idx2 + 2] = z1; // bottom right
1196
+ points[idx2 + 2] = z1;
1197
+
1198
+ if (bottomStickGround) {
1199
+ points[idx2 + 2] = 0;
1200
+ } // bottom right
1201
+
1129
1202
 
1130
1203
  var idx3 = len * 2 * 3 + len * 3 + idx0;
1131
1204
  points[idx3] = x2;
1132
1205
  points[idx3 + 1] = y2;
1133
1206
  points[idx3 + 2] = z2;
1207
+
1208
+ if (bottomStickGround) {
1209
+ points[idx3 + 2] = 0;
1210
+ }
1211
+
1134
1212
  i++;
1135
1213
  }
1136
1214
 
@@ -1171,6 +1249,17 @@
1171
1249
  result.index = index;
1172
1250
  result.points = points;
1173
1251
  result.uvs = uvs;
1252
+
1253
+ if (depths) {
1254
+ len = leftPoints.length;
1255
+ i = 0;
1256
+
1257
+ while (i < len) {
1258
+ leftPoints[i].depth = lz;
1259
+ rightPoints[i].depth = rz;
1260
+ i++;
1261
+ }
1262
+ }
1174
1263
  }
1175
1264
 
1176
1265
  function generateSides(result, options) {
@@ -1180,11 +1269,29 @@
1180
1269
  rightPoints = result.rightPoints,
1181
1270
  uvs = result.uvs;
1182
1271
  var z = options.depth;
1272
+ var bottomStickGround = options.bottomStickGround;
1183
1273
  var rings = [leftPoints, rightPoints];
1274
+ var depthsEnable = result.depths;
1184
1275
 
1185
1276
  function addOneSideIndex(v1, v2) {
1186
1277
  var idx = points.length / 3;
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]);
1278
+ var pIndex = points.length - 1; // top
1279
+
1280
+ points[++pIndex] = v1[0];
1281
+ points[++pIndex] = v1[1];
1282
+ points[++pIndex] = (depthsEnable ? v1.depth : z) + v1[2];
1283
+ points[++pIndex] = v2[0];
1284
+ points[++pIndex] = v2[1];
1285
+ points[++pIndex] = (depthsEnable ? v2.depth : z) + v2[2]; // points.push(v1[0], v1[1], (depthsEnable ? v1.depth : z) + v1[2], v2[0], v2[1], (depthsEnable ? v2.depth : z) + v2[2]);
1286
+ // bottom
1287
+
1288
+ points[++pIndex] = v1[0];
1289
+ points[++pIndex] = v1[1];
1290
+ points[++pIndex] = bottomStickGround ? 0 : v1[2];
1291
+ points[++pIndex] = v2[0];
1292
+ points[++pIndex] = v2[1];
1293
+ points[++pIndex] = bottomStickGround ? 0 : v2[2]; // points.push(v1[0], v1[1], v1[2], v2[0], v2[1], v2[2]);
1294
+
1188
1295
  var a = idx + 2,
1189
1296
  b = idx + 3,
1190
1297
  c = idx,
@@ -1348,7 +1455,6 @@
1348
1455
  * @returns
1349
1456
  */
1350
1457
 
1351
-
1352
1458
  function translateLine(p1, p2, distance) {
1353
1459
  var dy = p2[1] - p1[1],
1354
1460
  dx = p2[0] - p1[0];
@@ -3507,6 +3613,8 @@
3507
3613
  exports.expandPaths = expandPaths;
3508
3614
  exports.extrudePolygons = extrudePolygons;
3509
3615
  exports.extrudePolylines = extrudePolylines;
3616
+ exports.extrudeSlopes = extrudeSlopes;
3617
+ exports.leftOnLine = leftOnLine;
3510
3618
 
3511
3619
  Object.defineProperty(exports, '__esModule', { value: true });
3512
3620