@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
60 lines (59 loc) • 2.42 kB
JavaScript
;
import { BaseSopOperation } from "./_Base";
import { TypedNodePathParamValue } from "../../../core/Walker";
import { NodeContext } from "../../../engine/poly/NodeContext";
import { applyRenderHook, applyCustomMaterials } from "../../../core/geometry/Material";
import { InputCloneMode } from "../../../engine/poly/InputCloneMode";
import { CoreMask } from "../../../core/geometry/Mask";
import { SopType } from "../../poly/registers/nodes/types/Sop";
import { CoreParticlesAttribute } from "../../../core/particles/CoreParticlesAttribute";
export class ParticlesSystemGpuMaterialSopOperation extends BaseSopOperation {
static type() {
return SopType.PARTICLES_SYSTEM_GPU_MATERIAL;
}
async cook(inputCoreGroups, params) {
const coreGroup = inputCoreGroups[0];
await this._applyMaterials(coreGroup, params);
return coreGroup;
}
async _getMaterial(params) {
var _a, _b, _c;
const materialNode = params.material.nodeWithContext(NodeContext.MAT, (_a = this.states) == null ? void 0 : _a.error);
if (materialNode) {
const material = await materialNode.material();
if (!material) {
(_b = this.states) == null ? void 0 : _b.error.set(`material invalid. (error: '${materialNode.states.error.message()}')`);
return;
}
return { material, materialNode };
} else {
(_c = this.states) == null ? void 0 : _c.error.set(`no material node found`);
}
}
async _applyMaterials(coreGroup, params) {
const materialData = await this._getMaterial(params);
if (!materialData) {
return;
}
const selectedObjects = CoreMask.filterThreejsObjects(coreGroup, params);
for (let selectedObject of selectedObjects) {
this._applyMaterial(selectedObject, materialData, params);
}
return coreGroup;
}
_applyMaterial(object, materialData, params) {
if (object.isGroup) {
return;
}
const objectWithmaterial = object;
objectWithmaterial.material = materialData.material;
CoreParticlesAttribute.setMaterialNodeId(object, materialData.materialNode.graphNodeId());
applyRenderHook(object, materialData.material);
applyCustomMaterials(object, materialData.material);
}
}
ParticlesSystemGpuMaterialSopOperation.DEFAULT_PARAMS = {
group: "",
material: new TypedNodePathParamValue("")
};
ParticlesSystemGpuMaterialSopOperation.INPUT_CLONED_STATE = InputCloneMode.FROM_NODE;