UNPKG

@polygonjs/polygonjs

Version:

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

73 lines (72 loc) 2.09 kB
"use strict"; import { ObjectLoader } from "three"; import { TypedSopNode } from "./_Base"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { SopType } from "../../poly/registers/nodes/types/Sop"; class CacheSopParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param content of the cache (hidden) */ this.cache = ParamConfig.STRING("", { hidden: true }); /** @param clears the cache */ this.reset = ParamConfig.BUTTON(null, { callback: (node, param) => { CacheSopNode.PARAM_CALLBACK_reset(node, param); } }); } } const ParamsConfig = new CacheSopParamsConfig(); export class CacheSopNode extends TypedSopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return SopType.CACHE; } initializeNode() { this.io.inputs.setCount(0, 1); } cook(inputCoreGroups) { const isCacheEmpty = this.pv.cache == "" || this.pv.cache == null; const coreGroup = inputCoreGroups[0]; function toJSON() { const json = []; for (const object of coreGroup.threejsObjects()) { json.push(object.toJSON()); } return JSON.stringify(json); } function fromJSON(json) { const objLoader = new ObjectLoader(); const jsons = JSON.parse(json); const allObjects = []; for (const json2 of jsons) { const parent = objLoader.parse(json2); allObjects.push(parent); } return allObjects; } if (isCacheEmpty && coreGroup) { const str = toJSON(); const objects = fromJSON(str); this.setObjects(objects); this.p.cache.set(str); } else { if (this.pv.cache) { const objects = fromJSON(this.pv.cache); this.setObjects(objects); } else { this.setObjects([]); } } } static PARAM_CALLBACK_reset(node, param) { node.param_callback_PARAM_CALLBACK_reset(); } async param_callback_PARAM_CALLBACK_reset() { this.p.cache.set(""); this.compute(); } }