UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

58 lines (57 loc) 2.1 kB
"use strict"; import { BufferGeometry, Float32BufferAttribute, Vector3 } from "three"; import { ObjectType } from "../../../Constant"; import { BaseSopOperation } from "../../../../../engine/operations/sop/_Base"; import { tetCenter } from "../utils/tetCenter"; const _center0 = new Vector3(); const _center1 = new Vector3(); const idPairs = /* @__PURE__ */ new Map(); export function tetSharedFacesToLines(tetGeometry, tesselationParams) { idPairs.clear(); let facesCount = 0; tetGeometry.tetrahedrons.forEach((tet) => { for (let i = 0; i < 4; i++) { const neighbour = tet.neighbours[i]; if (neighbour != null) { const neighbourId = neighbour.id; const neighbourTet = tetGeometry.tetrahedrons.get(neighbourId); if (neighbourTet) { const key = tet.id < neighbourId ? tet.id : neighbourId; const value = tet.id < neighbourId ? neighbourId : tet.id; let set = idPairs.get(key); if (!set) { set = /* @__PURE__ */ new Set(); idPairs.set(key, set); } if (!set.has(value)) { facesCount++; } set.add(value); } } } }); const newGeometry = new BufferGeometry(); const positions = new Array(facesCount * 2 * 3); const indices = new Array(facesCount * 1 * 2); let positionsCount = 0; let indicesCount = 0; let indexCount = 0; idPairs.forEach((neighbourIds, tetId) => { neighbourIds.forEach((neighbourId) => { tetCenter(tetGeometry, tetId, _center0); tetCenter(tetGeometry, neighbourId, _center1); indices[indicesCount] = indexCount; indices[indicesCount + 1] = indexCount + 1; _center0.toArray(positions, positionsCount); positionsCount += 3; _center1.toArray(positions, positionsCount); positionsCount += 3; indicesCount += 2; indexCount += 2; }); }); newGeometry.setAttribute("position", new Float32BufferAttribute(positions, 3)); newGeometry.setIndex(indices); return BaseSopOperation.createObject(newGeometry, ObjectType.LINE_SEGMENTS); }