(function (global, factory) { if (typeof define === "function" && define.amd) { define(["exports", "three", "../utils/BufferGeometryUtils.js"], factory); } else if (typeof exports !== "undefined") { factory(exports, require("three"), require("../utils/BufferGeometryUtils.js")); } else { var mod = { exports: {} }; factory(mod.exports, global.three, global.BufferGeometryUtils); global.EdgeSplitModifier = mod.exports; } })(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, BufferGeometryUtils) { "use strict"; Object.defineProperty(_exports, "__esModule", { value: true }); _exports.EdgeSplitModifier = void 0; BufferGeometryUtils = _interopRequireWildcard(BufferGeometryUtils); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } var _A = new _three.Vector3(); var _B = new _three.Vector3(); var _C = new _three.Vector3(); var EdgeSplitModifier = /*#__PURE__*/function () { function EdgeSplitModifier() { _classCallCheck(this, EdgeSplitModifier); } _createClass(EdgeSplitModifier, [{ key: "modify", value: function modify(geometry, cutOffAngle) { var tryKeepNormals = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; function computeNormals() { normals = new Float32Array(indexes.length * 3); for (var i = 0; i < indexes.length; i += 3) { var index = indexes[i]; _A.set(positions[3 * index], positions[3 * index + 1], positions[3 * index + 2]); index = indexes[i + 1]; _B.set(positions[3 * index], positions[3 * index + 1], positions[3 * index + 2]); index = indexes[i + 2]; _C.set(positions[3 * index], positions[3 * index + 1], positions[3 * index + 2]); _C.sub(_B); _A.sub(_B); var normal = _C.cross(_A).normalize(); for (var j = 0; j < 3; j++) { normals[3 * (i + j)] = normal.x; normals[3 * (i + j) + 1] = normal.y; normals[3 * (i + j) + 2] = normal.z; } } } function mapPositionsToIndexes() { pointToIndexMap = Array(positions.length / 3); for (var i = 0; i < indexes.length; i++) { var index = indexes[i]; if (pointToIndexMap[index] == null) { pointToIndexMap[index] = []; } pointToIndexMap[index].push(i); } } function edgeSplitToGroups(indexes, cutOff, firstIndex) { _A.set(normals[3 * firstIndex], normals[3 * firstIndex + 1], normals[3 * firstIndex + 2]).normalize(); var result = { splitGroup: [], currentGroup: [firstIndex] }; var _iterator = _createForOfIteratorHelper(indexes), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var j = _step.value; if (j !== firstIndex) { _B.set(normals[3 * j], normals[3 * j + 1], normals[3 * j + 2]).normalize(); if (_B.dot(_A) < cutOff) { result.splitGroup.push(j); } else { result.currentGroup.push(j); } } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return result; } function edgeSplit(indexes, cutOff) { var original = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null; if (indexes.length === 0) return; var groupResults = []; var _iterator2 = _createForOfIteratorHelper(indexes), _step2; try { for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) { var index = _step2.value; groupResults.push(edgeSplitToGroups(indexes, cutOff, index)); } } catch (err) { _iterator2.e(err); } finally { _iterator2.f(); } var result = groupResults[0]; for (var _i = 0, _groupResults = groupResults; _i < _groupResults.length; _i++) { var groupResult = _groupResults[_i]; if (groupResult.currentGroup.length > result.currentGroup.length) { result = groupResult; } } if (original != null) { splitIndexes.push({ original: original, indexes: result.currentGroup }); } if (result.splitGroup.length) { edgeSplit(result.splitGroup, cutOff, original || result.currentGroup[0]); } } if (geometry.isGeometry === true) { console.error('THREE.EdgeSplitModifier no longer supports THREE.Geometry. Use BufferGeometry instead.'); return; } var hadNormals = false; var oldNormals = null; if (geometry.attributes.normal) { hadNormals = true; geometry = geometry.clone(); if (tryKeepNormals === true && geometry.index !== null) { oldNormals = geometry.attributes.normal.array; } geometry.deleteAttribute('normal'); } if (geometry.index == null) { if (BufferGeometryUtils === undefined) { throw 'THREE.EdgeSplitModifier relies on BufferGeometryUtils'; } geometry = BufferGeometryUtils.mergeVertices(geometry); } var indexes = geometry.index.array; var positions = geometry.getAttribute('position').array; var normals; var pointToIndexMap; computeNormals(); mapPositionsToIndexes(); var splitIndexes = []; var _iterator3 = _createForOfIteratorHelper(pointToIndexMap), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var vertexIndexes = _step3.value; edgeSplit(vertexIndexes, Math.cos(cutOffAngle) - 0.001); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } var newAttributes = {}; for (var _i2 = 0, _Object$keys = Object.keys(geometry.attributes); _i2 < _Object$keys.length; _i2++) { var name = _Object$keys[_i2]; var oldAttribute = geometry.attributes[name]; var newArray = new oldAttribute.array.constructor((indexes.length + splitIndexes.length) * oldAttribute.itemSize); newArray.set(oldAttribute.array); newAttributes[name] = new _three.BufferAttribute(newArray, oldAttribute.itemSize, oldAttribute.normalized); } var newIndexes = new Uint32Array(indexes.length); newIndexes.set(indexes); for (var i = 0; i < splitIndexes.length; i++) { var split = splitIndexes[i]; var index = indexes[split.original]; for (var _i3 = 0, _Object$values = Object.values(newAttributes); _i3 < _Object$values.length; _i3++) { var attribute = _Object$values[_i3]; for (var j = 0; j < attribute.itemSize; j++) { attribute.array[(indexes.length + i) * attribute.itemSize + j] = attribute.array[index * attribute.itemSize + j]; } } var _iterator4 = _createForOfIteratorHelper(split.indexes), _step4; try { for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { var _j = _step4.value; newIndexes[_j] = indexes.length + i; } } catch (err) { _iterator4.e(err); } finally { _iterator4.f(); } } geometry = new _three.BufferGeometry(); geometry.setIndex(new _three.BufferAttribute(newIndexes, 1)); for (var _i4 = 0, _Object$keys2 = Object.keys(newAttributes); _i4 < _Object$keys2.length; _i4++) { var _name = _Object$keys2[_i4]; geometry.setAttribute(_name, newAttributes[_name]); } if (hadNormals) { geometry.computeVertexNormals(); if (oldNormals !== null) { var changedNormals = new Array(oldNormals.length / 3).fill(false); var _iterator5 = _createForOfIteratorHelper(splitIndexes), _step5; try { for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) { var splitData = _step5.value; changedNormals[splitData.original] = true; } } catch (err) { _iterator5.e(err); } finally { _iterator5.f(); } for (var _i5 = 0; _i5 < changedNormals.length; _i5++) { if (changedNormals[_i5] === false) { for (var _j2 = 0; _j2 < 3; _j2++) { geometry.attributes.normal.array[3 * _i5 + _j2] = oldNormals[3 * _i5 + _j2]; } } } } } return geometry; } }]); return EdgeSplitModifier; }(); _exports.EdgeSplitModifier = EdgeSplitModifier; });