UNPKG

@polygonjs/polygonjs

Version:

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

79 lines (78 loc) 3.11 kB
"use strict"; import { BaseSopOperation } from "./_Base"; import { TypedNodePathParamValue } from "../../../core/Walker"; import { GlobalsGeometryHandler } from "../../../engine/nodes/gl/code/globals/Geometry"; import { objectTypeFromObject } from "../../../core/geometry/Constant"; import { applyCustomMaterials } from "../../../core/geometry/Material"; import { NodeContext } from "../../../engine/poly/NodeContext"; import { CoreInstancer } from "../../../core/geometry/Instancer"; import { InputCloneMode } from "../../../engine/poly/InputCloneMode"; import { isBooleanTrue } from "../../../core/BooleanValue"; import { SopType } from "../../poly/registers/nodes/types/Sop"; export class InstanceSopOperation extends BaseSopOperation { static type() { return SopType.INSTANCE; } async cook(inputCoreGroups, params) { const coreGroupToInstance = inputCoreGroups[0]; this._geometry = void 0; const objectToInstance = coreGroupToInstance.threejsObjectsWithGeo()[0]; if (objectToInstance) { const geometryToInstance = objectToInstance.geometry; if (geometryToInstance) { const coreGroup = inputCoreGroups[1]; this._createInstance(geometryToInstance, coreGroup, params); } } if (this._geometry) { const type = objectTypeFromObject(objectToInstance); if (type) { const object = this.createObject(this._geometry, type); if (object) { if (isBooleanTrue(params.applyMaterial)) { const material = await this._getMaterial(params); if (material) { await this._applyMaterial(object, material); } } return this.createCoreGroupFromObjects([object]); } } } return this.createCoreGroupFromObjects([]); } async _getMaterial(params) { var _a; if (isBooleanTrue(params.applyMaterial)) { const materialNode = params.material.nodeWithContext(NodeContext.MAT, (_a = this.states) == null ? void 0 : _a.error); if (materialNode) { this._globalsHandler = this._globalsHandler || new GlobalsGeometryHandler(); const matBuilderNode = materialNode; const matNodeAssemblerController = matBuilderNode.assemblerController(); if (matNodeAssemblerController) { matNodeAssemblerController.setAssemblerGlobalsHandler(this._globalsHandler); } const container = await materialNode.compute(); const material = container.material(); return material; } } } async _applyMaterial(object, material) { object.material = material; applyCustomMaterials(object, material); } _createInstance(geometryToInstance, templateCoreGroup, params) { this._geometry = CoreInstancer.createInstanceBufferGeometry( geometryToInstance, templateCoreGroup, params.attributesToCopy ); } } InstanceSopOperation.DEFAULT_PARAMS = { attributesToCopy: "instance*", applyMaterial: true, material: new TypedNodePathParamValue("") }; InstanceSopOperation.INPUT_CLONED_STATE = [InputCloneMode.NEVER, InputCloneMode.NEVER];