@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
63 lines (62 loc) • 2.42 kB
JavaScript
;
import { Mesh } from "three";
import { mergeGeometries } from "three/examples/jsm/utils/BufferGeometryUtils";
import { CoreGeometryIndexBuilder } from "../../../util/IndexBuilder";
import { pointsFromThreejsObject } from "../CoreThreejsPointUtils";
import { ThreejsPoint } from "../ThreejsPoint";
import { setToArray } from "../../../../SetUtils";
const dummyMesh = new Mesh();
export class CoreGeometryBuilderMerge {
static merge(geometries) {
if (geometries.length === 0) {
return;
}
for (const geometry of geometries) {
CoreGeometryIndexBuilder.createIndexIfNone(geometry);
}
dummyMesh.geometry = geometries[0];
const indexedAttributeNames = ThreejsPoint.indexedAttributeNames(dummyMesh);
const newValuesByAttributeName = {};
for (const indexedAttributeName of indexedAttributeNames) {
const indexByValues = /* @__PURE__ */ new Map();
const valuesSet = /* @__PURE__ */ new Set();
const allGeometriesPoints = [];
for (const geometry of geometries) {
const dummyMesh2 = new Mesh(geometry);
const points = pointsFromThreejsObject(dummyMesh2);
for (const point of points) {
allGeometriesPoints.push(point);
const value = point.indexedAttribValue(indexedAttributeName);
if (value != null) {
if (!valuesSet.has(value)) {
indexByValues.set(value, valuesSet.size);
valuesSet.add(value);
}
}
}
}
for (const point of allGeometriesPoints) {
const value = point.indexedAttribValue(indexedAttributeName);
if (value != null) {
const newIndex = indexByValues.get(value);
if (newIndex != null) {
point.setAttribIndex(indexedAttributeName, newIndex);
}
}
}
const values = [];
setToArray(valuesSet, values);
newValuesByAttributeName[indexedAttributeName] = values;
}
const mergedGeometry = mergeGeometries(geometries);
dummyMesh.geometry = mergedGeometry;
Object.keys(newValuesByAttributeName).forEach((indexedAttributeName) => {
const values = newValuesByAttributeName[indexedAttributeName];
ThreejsPoint.setIndexedAttributeValues(dummyMesh, indexedAttributeName, values);
});
if (mergedGeometry) {
delete mergedGeometry.userData.mergedUserData;
}
return mergedGeometry;
}
}