UNPKG

@polygonjs/polygonjs

Version:

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

116 lines (115 loc) 3.36 kB
"use strict"; import { TypedSopNode } from "./_Base"; import { InputCloneMode } from "../../poly/InputCloneMode"; import { StringParamLanguage } from "../../params/utils/OptionsController"; import { TranspiledFilter } from "../utils/code/controllers/TranspiledFilter"; import { Poly } from "../../Poly"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { removeTypes } from "../../../core/code/tsUtils"; import { BaseCodeProcessor, buildCodeNodeFunction } from "../../../core/code/FunctionBuilderUtils"; const DEFAULT_TS = ` export class CodeSopProcessor extends BaseCodeSopProcessor { override initializeProcessor(){ } override cook(inputCoreGroups: CoreGroup[]){ const inputCoreGroup = inputCoreGroups[0]; const object = inputCoreGroup.threejsObjects()[0]; object.position.y = 1; this.setCoreGroup(inputCoreGroup); } } `; const DEFAULT_JS = removeTypes(DEFAULT_TS); export class BaseCodeSopProcessor extends BaseCodeProcessor { constructor(node) { super(node); this.node = node; this.initializeProcessor(); } get pv() { return this.node.pv; } get p() { return this.node.p; } initializeProcessor() { } cook(inputCoreGroups) { } setCoreGroup(coreGroup) { this.node.setCoreGroup(coreGroup); } setObjects(objects) { this.node.setObjects(objects); } } class CodeSopParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); this.codeTypescript = ParamConfig.STRING(DEFAULT_TS, { hideLabel: true, language: StringParamLanguage.TYPESCRIPT }); this.codeJavascript = ParamConfig.STRING(DEFAULT_JS, { hidden: true }); } } const ParamsConfig = new CodeSopParamsConfig(); export class CodeSopNode extends TypedSopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "code"; } initializeNode() { this.io.inputs.setCount(0, 4); this.io.inputs.initInputsClonedState([ InputCloneMode.FROM_NODE, InputCloneMode.NEVER, InputCloneMode.NEVER, InputCloneMode.NEVER ]); } cook(inputCoreGroups) { this._compileIfRequired(); if (this._processor) { this._processor.cook(inputCoreGroups); } else { this.setCoreGroup(inputCoreGroups[0]); } } _compileIfRequired() { if (!this._processor || this._lastCompiledCode != this.pv.codeJavascript) { this._compile(); } } _compile() { this._processor = void 0; try { const functionBody = `try { ${TranspiledFilter.filter(this.pv.codeJavascript)} } catch(e) { states.error.set(e); }`; const ProcessorClass = buildCodeNodeFunction({ BaseCodeProcessor: BaseCodeSopProcessor, BaseCodeProcessorName: "BaseCodeSopProcessor", node: this, functionBody }); if (ProcessorClass) { this._processor = new ProcessorClass(this); this._lastCompiledCode = this.pv.codeJavascript; } else { this.states.error.set(`cannot generate function`); Poly.warn(functionBody); } } catch (e) { Poly.warn(e); this.states.error.set(`cannot generate function (${e})`); } } } // adding BaseCodeSopProcessor seems necessary to have the bundled types include it CodeSopNode.BaseCodeSopProcessor = BaseCodeSopProcessor;