1 line
11 KiB
Plaintext
1 line
11 KiB
Plaintext
|
|
{"version":3,"file":"EdgeSplitModifier.cjs","sources":["../../src/modifiers/EdgeSplitModifier.ts"],"sourcesContent":["import { BufferAttribute, BufferGeometry, Vector3 } from 'three'\nimport * as BufferGeometryUtils from '../utils/BufferGeometryUtils'\n\ninterface EdgeSplitToGroupsResult {\n splitGroup: number[]\n currentGroup: number[]\n}\n\ninterface SplitIndexes {\n original: number\n indexes: number[]\n}\n\nclass EdgeSplitModifier {\n private A = new Vector3()\n private B = new Vector3()\n private C = new Vector3()\n\n private positions: ArrayLike<number> = []\n private normals: Float32Array = new Float32Array()\n private indexes: ArrayLike<number> = []\n private pointToIndexMap: number[][] = []\n private splitIndexes: SplitIndexes[] = []\n private oldNormals: ArrayLike<number> = []\n\n constructor() {}\n\n private computeNormals = (): void => {\n this.normals = new Float32Array(this.indexes.length * 3)\n\n for (let i = 0; i < this.indexes.length; i += 3) {\n let index = this.indexes[i]\n\n this.A.set(this.positions[3 * index], this.positions[3 * index + 1], this.positions[3 * index + 2])\n\n index = this.indexes[i + 1]\n this.B.set(this.positions[3 * index], this.positions[3 * index + 1], this.positions[3 * index + 2])\n\n index = this.indexes[i + 2]\n this.C.set(this.positions[3 * index], this.positions[3 * index + 1], this.positions[3 * index + 2])\n\n this.C.sub(this.B)\n this.A.sub(this.B)\n\n const normal = this.C.cross(this.A).normalize()\n\n for (let j = 0; j < 3; j++) {\n this.normals[3 * (i + j)] = normal.x\n this.normals[3 * (i + j) + 1] = normal.y\n this.normals[3 * (i + j) + 2] = normal.z\n }\n }\n }\n\n private mapPositionsToIndexes = (): void => {\n this.pointToIndexMap = Array(this.positions.length / 3)\n\n for (let i = 0; i < this.indexes.length; i++) {\n const index = this.indexes[i]\n\n if (this.pointToIndexMap[index] == null) {\n this.pointToIndexMap[index] = []\n }\n\n this.pointToIndexMap[index].push(i)\n }\n }\n\n private edgeSplitToGroups = (indexes: number[], cutOff: number, firstIndex: number): EdgeSplitToGroupsResult => {\n this.A.set(\n this.normals[3 * firstIndex],\n this.normals[3 * firstIndex + 1],\n this.normals[3 * firstIndex + 2],\n ).normalize()\n\n const result: EdgeSplitToGroupsResult = {\n splitGroup: [],\n currentGroup: [firstIndex],\n }\n\n for (let j of indexes) {\n if (j !== firstIndex) {\n this.B.set(this.normals[3 * j], this.normals[3 * j + 1], this.normals[3 * j + 2]).normalize()\n\n if (this.B.dot(this.A) < cutOff) {\n result.splitGroup.push(j)\n } else {\n result.currentGroup.push(j)\n }\n }\n }\n\n return result\n }\n\n private edgeSplit = (indexes: number[], cutOff: number, original: number | null = null): void => {\n if (indexes.length === 0) return\n\n const groupResults: EdgeSplitToGroupsResult[] = []\n\n for (let index of indexes) {\n groupResults.push(this.edgeSplitToGroups(indexes, cutOff, index))\n }\n\n let result = groupResults[0]\n\n for (let groupResult of groupResults) {\n if (groupResult.currentGroup.length > result.currentGroup.length) {\n result = groupResult\n }\n }\n\n if (original != null) {\n this.splitIndexes.push({\n original,\n indexes: result.currentGroup,\n })\n }\n\n if (result.splitGroup.length) {\n this.edgeSplit(result.splitGroup, cutOff, original || result.currentGroup[0])\n }\n }\n\n public modify = (geometry: BufferGeometry, cutOffAngle: number, tryKeepNormals = true): BufferGeometry => {\n let hadNormals = false\n\n if (geometry.attributes.normal) {\n hadNormals = true\n\n geometry = geometry.clone()\n\n if (tryKeepNormals === true && geometry.index !== null) {\n this.oldNormals = geometry.attributes.normal.array\n }\n\n geometry.deleteAttribute('normal')\n }\n\n if (ge
|