UNPKG

@polygonjs/polygonjs

Version:

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

55 lines (54 loc) 2.1 kB
"use strict"; import { Mesh } from "three"; import { BaseSopOperation } from "./_Base"; import { InputCloneMode } from "../../../engine/poly/InputCloneMode"; import { SopType } from "../../poly/registers/nodes/types/Sop"; import { copyObjectAllProperties } from "../../../core/geometry/modules/three/ThreejsObjectUtils"; import { isArray } from "../../../core/Type"; export class InstancedMeshToMeshSopOperation extends BaseSopOperation { static type() { return SopType.INSTANCED_MESH_TO_MESH; } async cook(coreGroups, params) { var _a; const coreGroup = coreGroups[0]; const inputObjects = coreGroup.threejsObjects(); const newObjects = []; const childrenToRemove = []; for (const inputObject of inputObjects) { inputObject.traverse((child) => { if (child.isInstancedMesh) { childrenToRemove.push(child); const count = child.count; const geometry = child.geometry; const material = child.material; for (let i = 0; i < count; i++) { const newMesh = copyObjectAllProperties(child, new Mesh()); newMesh.geometry = params.cloneGeometry ? geometry.clone() : geometry; newMesh.material = isArray(material) ? material[0] : material; child.getMatrixAt(i, newMesh.matrix); newMesh.matrix.decompose(newMesh.position, newMesh.quaternion, newMesh.scale); const parent = child.parent; if (parent) { parent.add(newMesh); } else { newObjects.push(newMesh); } } } else { if (child === inputObject) { newObjects.push(child); } } }); } for (const childToRemove of childrenToRemove) { (_a = childToRemove.parent) == null ? void 0 : _a.remove(childToRemove); } return this.createCoreGroupFromObjects(newObjects); } } InstancedMeshToMeshSopOperation.DEFAULT_PARAMS = { cloneGeometry: true }; InstancedMeshToMeshSopOperation.INPUT_CLONED_STATE = [InputCloneMode.ALWAYS, InputCloneMode.NEVER];