three-conic-polygon-geometry
Advanced tools
Comparing version 1.4.2 to 1.4.3
@@ -19,2 +19,87 @@ 'use strict'; | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _inherits(subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) _setPrototypeOf(subClass, superClass); | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function _isNativeReflectConstruct() { | ||
if (typeof Reflect === "undefined" || !Reflect.construct) return false; | ||
if (Reflect.construct.sham) return false; | ||
if (typeof Proxy === "function") return true; | ||
try { | ||
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
function _possibleConstructorReturn(self, call) { | ||
if (call && (typeof call === "object" || typeof call === "function")) { | ||
return call; | ||
} | ||
return _assertThisInitialized(self); | ||
} | ||
function _createSuper(Derived) { | ||
var hasNativeReflectConstruct = _isNativeReflectConstruct(); | ||
return function _createSuperInternal() { | ||
var Super = _getPrototypeOf(Derived), | ||
result; | ||
if (hasNativeReflectConstruct) { | ||
var NewTarget = _getPrototypeOf(this).constructor; | ||
result = Reflect.construct(Super, arguments, NewTarget); | ||
} else { | ||
result = Super.apply(this, arguments); | ||
} | ||
return _possibleConstructorReturn(this, result); | ||
}; | ||
} | ||
function _slicedToArray(arr, i) { | ||
@@ -37,14 +122,17 @@ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); | ||
} | ||
function _iterableToArrayLimit(arr, i) { | ||
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; | ||
var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); | ||
if (_i == null) return; | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
var _s, _e; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
@@ -330,120 +418,134 @@ | ||
function ConicPolygonBufferGeometry(polygonGeoJson, startHeight, endHeight, closedBottom, closedTop, includeSides, curvatureResolution) { | ||
var _this = this; | ||
var ConicPolygonBufferGeometry = /*#__PURE__*/function (_THREE$BufferGeometry) { | ||
_inherits(ConicPolygonBufferGeometry, _THREE$BufferGeometry); | ||
THREE.BufferGeometry.call(this); | ||
this.type = 'ConicPolygonBufferGeometry'; | ||
this.parameters = { | ||
polygonGeoJson: polygonGeoJson, | ||
startHeight: startHeight, | ||
endHeight: endHeight, | ||
closedBottom: closedBottom, | ||
closedTop: closedTop, | ||
includeSides: includeSides, | ||
curvatureResolution: curvatureResolution | ||
}; // defaults | ||
var _super = _createSuper(ConicPolygonBufferGeometry); | ||
startHeight = startHeight || 0; | ||
endHeight = endHeight || 1; | ||
closedBottom = closedBottom !== undefined ? closedBottom : true; | ||
closedTop = closedTop !== undefined ? closedTop : true; | ||
includeSides = includeSides !== undefined ? includeSides : true; | ||
curvatureResolution = curvatureResolution || 5; // in angular degrees | ||
// pre-calculate contour and triangulation | ||
function ConicPolygonBufferGeometry(polygonGeoJson, startHeight, endHeight, closedBottom, closedTop, includeSides, curvatureResolution) { | ||
var _this; | ||
var _geoPolygonTriangulat = geoPolygonTriangulate(polygonGeoJson, { | ||
resolution: curvatureResolution | ||
}), | ||
contour = _geoPolygonTriangulat.contour, | ||
triangles = _geoPolygonTriangulat.triangles; | ||
_classCallCheck(this, ConicPolygonBufferGeometry); | ||
var vertices = []; | ||
var indices = []; | ||
var groupCnt = 0; // add groups to apply different materials to torso / caps | ||
_this = _super.call(this); | ||
_this.type = 'ConicPolygonBufferGeometry'; | ||
_this.parameters = { | ||
polygonGeoJson: polygonGeoJson, | ||
startHeight: startHeight, | ||
endHeight: endHeight, | ||
closedBottom: closedBottom, | ||
closedTop: closedTop, | ||
includeSides: includeSides, | ||
curvatureResolution: curvatureResolution | ||
}; // defaults | ||
var addGroup = function addGroup(groupData) { | ||
var prevVertCnt = Math.round(vertices.length / 3); | ||
var prevIndCnt = indices.length; | ||
vertices = vertices.concat(groupData.vertices); | ||
indices = indices.concat(!prevVertCnt ? groupData.indices : groupData.indices.map(function (ind) { | ||
return ind + prevVertCnt; | ||
})); | ||
startHeight = startHeight || 0; | ||
endHeight = endHeight || 1; | ||
closedBottom = closedBottom !== undefined ? closedBottom : true; | ||
closedTop = closedTop !== undefined ? closedTop : true; | ||
includeSides = includeSides !== undefined ? includeSides : true; | ||
curvatureResolution = curvatureResolution || 5; // in angular degrees | ||
// pre-calculate contour and triangulation | ||
_this.addGroup(prevIndCnt, indices.length - prevIndCnt, groupCnt++); | ||
}; | ||
var _geoPolygonTriangulat = geoPolygonTriangulate(polygonGeoJson, { | ||
resolution: curvatureResolution | ||
}), | ||
contour = _geoPolygonTriangulat.contour, | ||
triangles = _geoPolygonTriangulat.triangles; | ||
includeSides && addGroup(generateTorso()); | ||
closedBottom && addGroup(generateCap(startHeight, false)); | ||
closedTop && addGroup(generateCap(endHeight, true)); // build geometry | ||
var vertices = []; | ||
var indices = []; | ||
var groupCnt = 0; // add groups to apply different materials to torso / caps | ||
this.setIndex(indices); | ||
this[setAttributeFn]('position', new THREE.Float32BufferAttribute(vertices, 3)); // auto-calculate normals | ||
var addGroup = function addGroup(groupData) { | ||
var prevVertCnt = Math.round(vertices.length / 3); | ||
var prevIndCnt = indices.length; | ||
vertices = vertices.concat(groupData.vertices); | ||
indices = indices.concat(!prevVertCnt ? groupData.indices : groupData.indices.map(function (ind) { | ||
return ind + prevVertCnt; | ||
})); | ||
this.computeFaceNormals(); | ||
this.computeVertexNormals(); // | ||
_this.addGroup(prevIndCnt, indices.length - prevIndCnt, groupCnt++); | ||
}; | ||
function generateVertices(polygon, altitude) { | ||
var coords3d = polygon.map(function (coords) { | ||
return coords.map(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
lng = _ref2[0], | ||
lat = _ref2[1]; | ||
includeSides && addGroup(generateTorso()); | ||
closedBottom && addGroup(generateCap(startHeight, false)); | ||
closedTop && addGroup(generateCap(endHeight, true)); // build geometry | ||
return polar2Cartesian(lat, lng, altitude); | ||
}); | ||
}); // returns { vertices, holes, coordinates }. Each point generates 3 vertice items (x,y,z). | ||
_this.setIndex(indices); | ||
return earcut__default['default'].flatten(coords3d); | ||
} | ||
_this[setAttributeFn]('position', new THREE.Float32BufferAttribute(vertices, 3)); // auto-calculate normals | ||
function generateTorso() { | ||
var _generateVertices = generateVertices(contour, startHeight), | ||
bottomVerts = _generateVertices.vertices, | ||
holes = _generateVertices.holes; | ||
var _generateVertices2 = generateVertices(contour, endHeight), | ||
topVerts = _generateVertices2.vertices; | ||
_this.computeFaceNormals(); | ||
var vertices = d3Array.merge([topVerts, bottomVerts]); | ||
var numPoints = Math.round(topVerts.length / 3); | ||
var holesIdx = new Set(holes); | ||
var lastHoleIdx = 0; | ||
var indices = []; | ||
_this.computeVertexNormals(); // | ||
for (var v0Idx = 0; v0Idx < numPoints; v0Idx++) { | ||
var v1Idx = v0Idx + 1; // next point | ||
if (v1Idx === numPoints) { | ||
v1Idx = lastHoleIdx; // close final loop | ||
} else if (holesIdx.has(v1Idx)) { | ||
var holeIdx = v1Idx; | ||
v1Idx = lastHoleIdx; // close hole loop | ||
function generateVertices(polygon, altitude) { | ||
var coords3d = polygon.map(function (coords) { | ||
return coords.map(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
lng = _ref2[0], | ||
lat = _ref2[1]; | ||
lastHoleIdx = holeIdx; | ||
} // Each pair of coords generates two triangles (faces) | ||
return polar2Cartesian(lat, lng, altitude); | ||
}); | ||
}); // returns { vertices, holes, coordinates }. Each point generates 3 vertice items (x,y,z). | ||
return earcut__default['default'].flatten(coords3d); | ||
} | ||
indices.push(v0Idx, v0Idx + numPoints, v1Idx + numPoints); | ||
indices.push(v1Idx + numPoints, v1Idx, v0Idx); | ||
function generateTorso() { | ||
var _generateVertices = generateVertices(contour, startHeight), | ||
bottomVerts = _generateVertices.vertices, | ||
holes = _generateVertices.holes; | ||
var _generateVertices2 = generateVertices(contour, endHeight), | ||
topVerts = _generateVertices2.vertices; | ||
var vertices = d3Array.merge([topVerts, bottomVerts]); | ||
var numPoints = Math.round(topVerts.length / 3); | ||
var holesIdx = new Set(holes); | ||
var lastHoleIdx = 0; | ||
var indices = []; | ||
for (var v0Idx = 0; v0Idx < numPoints; v0Idx++) { | ||
var v1Idx = v0Idx + 1; // next point | ||
if (v1Idx === numPoints) { | ||
v1Idx = lastHoleIdx; // close final loop | ||
} else if (holesIdx.has(v1Idx)) { | ||
var holeIdx = v1Idx; | ||
v1Idx = lastHoleIdx; // close hole loop | ||
lastHoleIdx = holeIdx; | ||
} // Each pair of coords generates two triangles (faces) | ||
indices.push(v0Idx, v0Idx + numPoints, v1Idx + numPoints); | ||
indices.push(v1Idx + numPoints, v1Idx, v0Idx); | ||
} | ||
return { | ||
indices: indices, | ||
vertices: vertices | ||
}; | ||
} | ||
return { | ||
indices: indices, | ||
vertices: vertices | ||
}; | ||
} | ||
function generateCap(radius) { | ||
var isTop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
return { | ||
// need to reverse-wind the bottom triangles to make them face outwards | ||
indices: isTop ? triangles.indices : triangles.indices.slice().reverse(), | ||
vertices: generateVertices([triangles.points], radius).vertices | ||
}; | ||
} | ||
function generateCap(radius) { | ||
var isTop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
return { | ||
// need to reverse-wind the bottom triangles to make them face outwards | ||
indices: isTop ? triangles.indices : triangles.indices.slice().reverse(), | ||
vertices: generateVertices([triangles.points], radius).vertices | ||
}; | ||
return _this; | ||
} | ||
} | ||
ConicPolygonBufferGeometry.prototype = Object.create(THREE.BufferGeometry.prototype); | ||
ConicPolygonBufferGeometry.prototype.constructor = ConicPolygonBufferGeometry; // | ||
return ConicPolygonBufferGeometry; | ||
}(THREE.BufferGeometry); // | ||
function polar2Cartesian(lat, lng) { | ||
@@ -450,0 +552,0 @@ var r = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; |
@@ -9,2 +9,87 @@ import { BufferGeometry, Float32BufferAttribute } from 'three'; | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _inherits(subClass, superClass) { | ||
if (typeof superClass !== "function" && superClass !== null) { | ||
throw new TypeError("Super expression must either be null or a function"); | ||
} | ||
subClass.prototype = Object.create(superClass && superClass.prototype, { | ||
constructor: { | ||
value: subClass, | ||
writable: true, | ||
configurable: true | ||
} | ||
}); | ||
if (superClass) _setPrototypeOf(subClass, superClass); | ||
} | ||
function _getPrototypeOf(o) { | ||
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { | ||
return o.__proto__ || Object.getPrototypeOf(o); | ||
}; | ||
return _getPrototypeOf(o); | ||
} | ||
function _setPrototypeOf(o, p) { | ||
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { | ||
o.__proto__ = p; | ||
return o; | ||
}; | ||
return _setPrototypeOf(o, p); | ||
} | ||
function _isNativeReflectConstruct() { | ||
if (typeof Reflect === "undefined" || !Reflect.construct) return false; | ||
if (Reflect.construct.sham) return false; | ||
if (typeof Proxy === "function") return true; | ||
try { | ||
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); | ||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
function _assertThisInitialized(self) { | ||
if (self === void 0) { | ||
throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); | ||
} | ||
return self; | ||
} | ||
function _possibleConstructorReturn(self, call) { | ||
if (call && (typeof call === "object" || typeof call === "function")) { | ||
return call; | ||
} | ||
return _assertThisInitialized(self); | ||
} | ||
function _createSuper(Derived) { | ||
var hasNativeReflectConstruct = _isNativeReflectConstruct(); | ||
return function _createSuperInternal() { | ||
var Super = _getPrototypeOf(Derived), | ||
result; | ||
if (hasNativeReflectConstruct) { | ||
var NewTarget = _getPrototypeOf(this).constructor; | ||
result = Reflect.construct(Super, arguments, NewTarget); | ||
} else { | ||
result = Super.apply(this, arguments); | ||
} | ||
return _possibleConstructorReturn(this, result); | ||
}; | ||
} | ||
function _slicedToArray(arr, i) { | ||
@@ -27,14 +112,17 @@ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); | ||
function _iterableToArray(iter) { | ||
if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); | ||
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); | ||
} | ||
function _iterableToArrayLimit(arr, i) { | ||
if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; | ||
var _i = arr && (typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]); | ||
if (_i == null) return; | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
var _s, _e; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
@@ -320,120 +408,134 @@ | ||
function ConicPolygonBufferGeometry(polygonGeoJson, startHeight, endHeight, closedBottom, closedTop, includeSides, curvatureResolution) { | ||
var _this = this; | ||
var ConicPolygonBufferGeometry = /*#__PURE__*/function (_THREE$BufferGeometry) { | ||
_inherits(ConicPolygonBufferGeometry, _THREE$BufferGeometry); | ||
THREE.BufferGeometry.call(this); | ||
this.type = 'ConicPolygonBufferGeometry'; | ||
this.parameters = { | ||
polygonGeoJson: polygonGeoJson, | ||
startHeight: startHeight, | ||
endHeight: endHeight, | ||
closedBottom: closedBottom, | ||
closedTop: closedTop, | ||
includeSides: includeSides, | ||
curvatureResolution: curvatureResolution | ||
}; // defaults | ||
var _super = _createSuper(ConicPolygonBufferGeometry); | ||
startHeight = startHeight || 0; | ||
endHeight = endHeight || 1; | ||
closedBottom = closedBottom !== undefined ? closedBottom : true; | ||
closedTop = closedTop !== undefined ? closedTop : true; | ||
includeSides = includeSides !== undefined ? includeSides : true; | ||
curvatureResolution = curvatureResolution || 5; // in angular degrees | ||
// pre-calculate contour and triangulation | ||
function ConicPolygonBufferGeometry(polygonGeoJson, startHeight, endHeight, closedBottom, closedTop, includeSides, curvatureResolution) { | ||
var _this; | ||
var _geoPolygonTriangulat = geoPolygonTriangulate(polygonGeoJson, { | ||
resolution: curvatureResolution | ||
}), | ||
contour = _geoPolygonTriangulat.contour, | ||
triangles = _geoPolygonTriangulat.triangles; | ||
_classCallCheck(this, ConicPolygonBufferGeometry); | ||
var vertices = []; | ||
var indices = []; | ||
var groupCnt = 0; // add groups to apply different materials to torso / caps | ||
_this = _super.call(this); | ||
_this.type = 'ConicPolygonBufferGeometry'; | ||
_this.parameters = { | ||
polygonGeoJson: polygonGeoJson, | ||
startHeight: startHeight, | ||
endHeight: endHeight, | ||
closedBottom: closedBottom, | ||
closedTop: closedTop, | ||
includeSides: includeSides, | ||
curvatureResolution: curvatureResolution | ||
}; // defaults | ||
var addGroup = function addGroup(groupData) { | ||
var prevVertCnt = Math.round(vertices.length / 3); | ||
var prevIndCnt = indices.length; | ||
vertices = vertices.concat(groupData.vertices); | ||
indices = indices.concat(!prevVertCnt ? groupData.indices : groupData.indices.map(function (ind) { | ||
return ind + prevVertCnt; | ||
})); | ||
startHeight = startHeight || 0; | ||
endHeight = endHeight || 1; | ||
closedBottom = closedBottom !== undefined ? closedBottom : true; | ||
closedTop = closedTop !== undefined ? closedTop : true; | ||
includeSides = includeSides !== undefined ? includeSides : true; | ||
curvatureResolution = curvatureResolution || 5; // in angular degrees | ||
// pre-calculate contour and triangulation | ||
_this.addGroup(prevIndCnt, indices.length - prevIndCnt, groupCnt++); | ||
}; | ||
var _geoPolygonTriangulat = geoPolygonTriangulate(polygonGeoJson, { | ||
resolution: curvatureResolution | ||
}), | ||
contour = _geoPolygonTriangulat.contour, | ||
triangles = _geoPolygonTriangulat.triangles; | ||
includeSides && addGroup(generateTorso()); | ||
closedBottom && addGroup(generateCap(startHeight, false)); | ||
closedTop && addGroup(generateCap(endHeight, true)); // build geometry | ||
var vertices = []; | ||
var indices = []; | ||
var groupCnt = 0; // add groups to apply different materials to torso / caps | ||
this.setIndex(indices); | ||
this[setAttributeFn]('position', new THREE.Float32BufferAttribute(vertices, 3)); // auto-calculate normals | ||
var addGroup = function addGroup(groupData) { | ||
var prevVertCnt = Math.round(vertices.length / 3); | ||
var prevIndCnt = indices.length; | ||
vertices = vertices.concat(groupData.vertices); | ||
indices = indices.concat(!prevVertCnt ? groupData.indices : groupData.indices.map(function (ind) { | ||
return ind + prevVertCnt; | ||
})); | ||
this.computeFaceNormals(); | ||
this.computeVertexNormals(); // | ||
_this.addGroup(prevIndCnt, indices.length - prevIndCnt, groupCnt++); | ||
}; | ||
function generateVertices(polygon, altitude) { | ||
var coords3d = polygon.map(function (coords) { | ||
return coords.map(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
lng = _ref2[0], | ||
lat = _ref2[1]; | ||
includeSides && addGroup(generateTorso()); | ||
closedBottom && addGroup(generateCap(startHeight, false)); | ||
closedTop && addGroup(generateCap(endHeight, true)); // build geometry | ||
return polar2Cartesian(lat, lng, altitude); | ||
}); | ||
}); // returns { vertices, holes, coordinates }. Each point generates 3 vertice items (x,y,z). | ||
_this.setIndex(indices); | ||
return earcut.flatten(coords3d); | ||
} | ||
_this[setAttributeFn]('position', new THREE.Float32BufferAttribute(vertices, 3)); // auto-calculate normals | ||
function generateTorso() { | ||
var _generateVertices = generateVertices(contour, startHeight), | ||
bottomVerts = _generateVertices.vertices, | ||
holes = _generateVertices.holes; | ||
var _generateVertices2 = generateVertices(contour, endHeight), | ||
topVerts = _generateVertices2.vertices; | ||
_this.computeFaceNormals(); | ||
var vertices = merge([topVerts, bottomVerts]); | ||
var numPoints = Math.round(topVerts.length / 3); | ||
var holesIdx = new Set(holes); | ||
var lastHoleIdx = 0; | ||
var indices = []; | ||
_this.computeVertexNormals(); // | ||
for (var v0Idx = 0; v0Idx < numPoints; v0Idx++) { | ||
var v1Idx = v0Idx + 1; // next point | ||
if (v1Idx === numPoints) { | ||
v1Idx = lastHoleIdx; // close final loop | ||
} else if (holesIdx.has(v1Idx)) { | ||
var holeIdx = v1Idx; | ||
v1Idx = lastHoleIdx; // close hole loop | ||
function generateVertices(polygon, altitude) { | ||
var coords3d = polygon.map(function (coords) { | ||
return coords.map(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
lng = _ref2[0], | ||
lat = _ref2[1]; | ||
lastHoleIdx = holeIdx; | ||
} // Each pair of coords generates two triangles (faces) | ||
return polar2Cartesian(lat, lng, altitude); | ||
}); | ||
}); // returns { vertices, holes, coordinates }. Each point generates 3 vertice items (x,y,z). | ||
return earcut.flatten(coords3d); | ||
} | ||
indices.push(v0Idx, v0Idx + numPoints, v1Idx + numPoints); | ||
indices.push(v1Idx + numPoints, v1Idx, v0Idx); | ||
function generateTorso() { | ||
var _generateVertices = generateVertices(contour, startHeight), | ||
bottomVerts = _generateVertices.vertices, | ||
holes = _generateVertices.holes; | ||
var _generateVertices2 = generateVertices(contour, endHeight), | ||
topVerts = _generateVertices2.vertices; | ||
var vertices = merge([topVerts, bottomVerts]); | ||
var numPoints = Math.round(topVerts.length / 3); | ||
var holesIdx = new Set(holes); | ||
var lastHoleIdx = 0; | ||
var indices = []; | ||
for (var v0Idx = 0; v0Idx < numPoints; v0Idx++) { | ||
var v1Idx = v0Idx + 1; // next point | ||
if (v1Idx === numPoints) { | ||
v1Idx = lastHoleIdx; // close final loop | ||
} else if (holesIdx.has(v1Idx)) { | ||
var holeIdx = v1Idx; | ||
v1Idx = lastHoleIdx; // close hole loop | ||
lastHoleIdx = holeIdx; | ||
} // Each pair of coords generates two triangles (faces) | ||
indices.push(v0Idx, v0Idx + numPoints, v1Idx + numPoints); | ||
indices.push(v1Idx + numPoints, v1Idx, v0Idx); | ||
} | ||
return { | ||
indices: indices, | ||
vertices: vertices | ||
}; | ||
} | ||
return { | ||
indices: indices, | ||
vertices: vertices | ||
}; | ||
} | ||
function generateCap(radius) { | ||
var isTop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
return { | ||
// need to reverse-wind the bottom triangles to make them face outwards | ||
indices: isTop ? triangles.indices : triangles.indices.slice().reverse(), | ||
vertices: generateVertices([triangles.points], radius).vertices | ||
}; | ||
} | ||
function generateCap(radius) { | ||
var isTop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
return { | ||
// need to reverse-wind the bottom triangles to make them face outwards | ||
indices: isTop ? triangles.indices : triangles.indices.slice().reverse(), | ||
vertices: generateVertices([triangles.points], radius).vertices | ||
}; | ||
return _this; | ||
} | ||
} | ||
ConicPolygonBufferGeometry.prototype = Object.create(THREE.BufferGeometry.prototype); | ||
ConicPolygonBufferGeometry.prototype.constructor = ConicPolygonBufferGeometry; // | ||
return ConicPolygonBufferGeometry; | ||
}(THREE.BufferGeometry); // | ||
function polar2Cartesian(lat, lng) { | ||
@@ -440,0 +542,0 @@ var r = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0; |
{ | ||
"name": "three-conic-polygon-geometry", | ||
"version": "1.4.2", | ||
"version": "1.4.3", | ||
"description": "ThreeJS geometry for drawing polygons on a sphere", | ||
@@ -43,6 +43,6 @@ "unpkg": "dist/three-conic-polygon-geometry.min.js", | ||
"@turf/boolean-point-in-polygon": "^6.3.0", | ||
"d3-array": "^2.11.0", | ||
"d3-array": "^2.12.1", | ||
"d3-geo": "^2.0.1", | ||
"d3-geo-voronoi": "^1.6.0", | ||
"delaunator": "^4.0.1", | ||
"delaunator": "^5.0.0", | ||
"earcut": "^2.2.2" | ||
@@ -54,16 +54,16 @@ }, | ||
"devDependencies": { | ||
"@babel/core": "^7.12.10", | ||
"@babel/plugin-proposal-class-properties": "^7.12.1", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.12.1", | ||
"@babel/preset-env": "^7.12.11", | ||
"@rollup/plugin-babel": "^5.2.2", | ||
"@rollup/plugin-commonjs": "^17.0.0", | ||
"@rollup/plugin-node-resolve": "^11.1.0", | ||
"@babel/core": "^7.13.16", | ||
"@babel/plugin-proposal-class-properties": "^7.13.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.13.8", | ||
"@babel/preset-env": "^7.13.15", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@rollup/plugin-commonjs": "^18.0.0", | ||
"@rollup/plugin-node-resolve": "^11.2.1", | ||
"@types/three": ">=0.72.0", | ||
"rimraf": "^3.0.2", | ||
"rollup": "^2.38.0", | ||
"rollup-plugin-dts": "^2.0.1", | ||
"rollup": "^2.45.2", | ||
"rollup-plugin-dts": "^3.0.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"typescript": "^4.1.3" | ||
"typescript": "^4.2.4" | ||
} | ||
} |
150
src/index.js
@@ -21,97 +21,98 @@ import { | ||
function ConicPolygonBufferGeometry(polygonGeoJson, startHeight, endHeight, closedBottom, closedTop, includeSides, curvatureResolution) { | ||
class ConicPolygonBufferGeometry extends THREE.BufferGeometry { | ||
constructor(polygonGeoJson, startHeight, endHeight, closedBottom, closedTop, includeSides, curvatureResolution) { | ||
super(); | ||
THREE.BufferGeometry.call(this); | ||
this.type = 'ConicPolygonBufferGeometry'; | ||
this.type = 'ConicPolygonBufferGeometry'; | ||
this.parameters = { | ||
polygonGeoJson, | ||
startHeight, | ||
endHeight, | ||
closedBottom, | ||
closedTop, | ||
includeSides, | ||
curvatureResolution | ||
}; | ||
this.parameters = { | ||
polygonGeoJson, | ||
startHeight, | ||
endHeight, | ||
closedBottom, | ||
closedTop, | ||
includeSides, | ||
curvatureResolution | ||
}; | ||
// defaults | ||
startHeight = startHeight || 0; | ||
endHeight = endHeight || 1; | ||
closedBottom = closedBottom !== undefined ? closedBottom : true; | ||
closedTop = closedTop !== undefined ? closedTop : true; | ||
includeSides = includeSides !== undefined ? includeSides : true; | ||
curvatureResolution = curvatureResolution || 5; // in angular degrees | ||
// defaults | ||
startHeight = startHeight || 0; | ||
endHeight = endHeight || 1; | ||
closedBottom = closedBottom !== undefined ? closedBottom : true; | ||
closedTop = closedTop !== undefined ? closedTop : true; | ||
includeSides = includeSides !== undefined ? includeSides : true; | ||
curvatureResolution = curvatureResolution || 5; // in angular degrees | ||
// pre-calculate contour and triangulation | ||
const {contour, triangles} = geoPolygonTriangulate(polygonGeoJson, {resolution: curvatureResolution}); | ||
// pre-calculate contour and triangulation | ||
const { contour, triangles } = geoPolygonTriangulate(polygonGeoJson, { resolution: curvatureResolution }); | ||
let vertices = []; | ||
let indices = []; | ||
let groupCnt = 0; // add groups to apply different materials to torso / caps | ||
let vertices = []; | ||
let indices = []; | ||
let groupCnt = 0; // add groups to apply different materials to torso / caps | ||
const addGroup = groupData => { | ||
const prevVertCnt = Math.round(vertices.length / 3); | ||
const prevIndCnt = indices.length; | ||
const addGroup = groupData => { | ||
const prevVertCnt = Math.round(vertices.length / 3); | ||
const prevIndCnt = indices.length; | ||
vertices = vertices.concat(groupData.vertices); | ||
indices = indices.concat(!prevVertCnt ? groupData.indices : groupData.indices.map(ind => ind + prevVertCnt)); | ||
vertices = vertices.concat(groupData.vertices); | ||
indices = indices.concat(!prevVertCnt ? groupData.indices : groupData.indices.map(ind => ind + prevVertCnt)); | ||
this.addGroup(prevIndCnt, indices.length - prevIndCnt, groupCnt++); | ||
}; | ||
this.addGroup(prevIndCnt, indices.length - prevIndCnt, groupCnt++); | ||
}; | ||
includeSides && addGroup(generateTorso()); | ||
closedBottom && addGroup(generateCap(startHeight, false)); | ||
closedTop && addGroup(generateCap(endHeight, true)); | ||
includeSides && addGroup(generateTorso()); | ||
closedBottom && addGroup(generateCap(startHeight, false)); | ||
closedTop && addGroup(generateCap(endHeight, true)); | ||
// build geometry | ||
this.setIndex(indices); | ||
this[setAttributeFn]('position', new THREE.Float32BufferAttribute(vertices, 3)); | ||
// build geometry | ||
this.setIndex(indices); | ||
this[setAttributeFn]('position', new THREE.Float32BufferAttribute(vertices, 3)); | ||
// auto-calculate normals | ||
this.computeFaceNormals(); | ||
this.computeVertexNormals(); | ||
// auto-calculate normals | ||
this.computeFaceNormals(); | ||
this.computeVertexNormals(); | ||
// | ||
// | ||
function generateVertices(polygon, altitude) { | ||
const coords3d = polygon.map(coords => coords.map(([lng, lat]) => polar2Cartesian(lat, lng, altitude))); | ||
// returns { vertices, holes, coordinates }. Each point generates 3 vertice items (x,y,z). | ||
return earcut.flatten(coords3d); | ||
} | ||
function generateVertices(polygon, altitude) { | ||
const coords3d = polygon.map(coords => coords.map(([lng, lat]) => polar2Cartesian(lat, lng, altitude))); | ||
// returns { vertices, holes, coordinates }. Each point generates 3 vertice items (x,y,z). | ||
return earcut.flatten(coords3d); | ||
} | ||
function generateTorso() { | ||
const {vertices: bottomVerts, holes} = generateVertices(contour, startHeight); | ||
const {vertices: topVerts} = generateVertices(contour, endHeight); | ||
function generateTorso() { | ||
const { vertices: bottomVerts, holes } = generateVertices(contour, startHeight); | ||
const { vertices: topVerts } = generateVertices(contour, endHeight); | ||
const vertices = flatten([topVerts, bottomVerts]); | ||
const numPoints = Math.round(topVerts.length / 3); | ||
const vertices = flatten([topVerts, bottomVerts]); | ||
const numPoints = Math.round(topVerts.length / 3); | ||
const holesIdx = new Set(holes); | ||
let lastHoleIdx = 0; | ||
const holesIdx = new Set(holes); | ||
let lastHoleIdx = 0; | ||
const indices = []; | ||
for (let v0Idx = 0; v0Idx < numPoints; v0Idx++) { | ||
let v1Idx = v0Idx + 1; // next point | ||
if (v1Idx === numPoints) { | ||
v1Idx = lastHoleIdx; // close final loop | ||
} else if (holesIdx.has(v1Idx)) { | ||
const holeIdx = v1Idx; | ||
v1Idx = lastHoleIdx; // close hole loop | ||
lastHoleIdx = holeIdx; | ||
} | ||
const indices = []; | ||
for (let v0Idx = 0; v0Idx < numPoints; v0Idx++) { | ||
let v1Idx = v0Idx + 1; // next point | ||
if (v1Idx === numPoints) { | ||
v1Idx = lastHoleIdx; // close final loop | ||
} else if (holesIdx.has(v1Idx)) { | ||
const holeIdx = v1Idx; | ||
v1Idx = lastHoleIdx; // close hole loop | ||
lastHoleIdx = holeIdx; | ||
// Each pair of coords generates two triangles (faces) | ||
indices.push(v0Idx, v0Idx + numPoints, v1Idx + numPoints); | ||
indices.push(v1Idx + numPoints, v1Idx, v0Idx); | ||
} | ||
// Each pair of coords generates two triangles (faces) | ||
indices.push(v0Idx, v0Idx + numPoints, v1Idx + numPoints); | ||
indices.push(v1Idx + numPoints, v1Idx, v0Idx); | ||
return {indices, vertices}; | ||
} | ||
return { indices, vertices }; | ||
} | ||
function generateCap(radius, isTop= true) { | ||
return { | ||
// need to reverse-wind the bottom triangles to make them face outwards | ||
indices: isTop ? triangles.indices : triangles.indices.slice().reverse(), | ||
vertices: generateVertices([triangles.points], radius).vertices | ||
function generateCap(radius, isTop = true) { | ||
return { | ||
// need to reverse-wind the bottom triangles to make them face outwards | ||
indices: isTop ? triangles.indices : triangles.indices.slice().reverse(), | ||
vertices: generateVertices([triangles.points], radius).vertices | ||
} | ||
} | ||
@@ -121,5 +122,2 @@ } | ||
ConicPolygonBufferGeometry.prototype = Object.create(THREE.BufferGeometry.prototype); | ||
ConicPolygonBufferGeometry.prototype.constructor = ConicPolygonBufferGeometry; | ||
// | ||
@@ -126,0 +124,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1487212
7648
+ Addeddelaunator@5.0.1(transitive)
+ Addedrobust-predicates@3.0.2(transitive)
Updatedd3-array@^2.12.1
Updateddelaunator@^5.0.0