UNPKG

@polygonjs/polygonjs

Version:

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

152 lines (151 loc) 4.7 kB
"use strict"; import { TypedSopNode } from "./_Base"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { InputCloneMode } from "../../poly/InputCloneMode"; import { SopType } from "../../poly/registers/nodes/types/Sop"; import { filterThreejsObjectsWithGroup } from "../../../core/geometry/Mask"; export var LayerUpdateMode = /* @__PURE__ */ ((LayerUpdateMode2) => { LayerUpdateMode2["SET"] = "set"; LayerUpdateMode2["ADD"] = "add"; LayerUpdateMode2["REMOVE"] = "remove"; return LayerUpdateMode2; })(LayerUpdateMode || {}); export const UPDATE_MODES = ["set" /* SET */, "add" /* ADD */, "remove" /* REMOVE */]; const UPDATE_MODE_ENTRIES = UPDATE_MODES.map((name, value) => { return { name, value }; }); function visibleOption(options) { const computedOptions = []; for (let i = 1; i <= 4; i++) { if (i >= options.layersCount) { computedOptions.push({ layersCount: i }); } } return { visibleIf: computedOptions }; } class LayerSopParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param group to assign the material to */ this.group = ParamConfig.STRING("*", { objectMask: true }); this.layersCount = ParamConfig.INTEGER(1, { range: [1, 4], rangeLocked: [true, true], separatorAfter: true }); /** @param updateMode */ this.updateMode0 = ParamConfig.INTEGER(UPDATE_MODES.indexOf("add" /* ADD */), { menu: { entries: UPDATE_MODE_ENTRIES }, ...visibleOption({ layersCount: 1 }) }); /** @param layer */ this.layer0 = ParamConfig.INTEGER(0, { range: [0, 31], rangeLocked: [true, true], ...visibleOption({ layersCount: 1 }) }); /** @param updateMode */ this.updateMode1 = ParamConfig.INTEGER(UPDATE_MODES.indexOf("add" /* ADD */), { menu: { entries: UPDATE_MODE_ENTRIES }, ...visibleOption({ layersCount: 2 }) }); /** @param layer */ this.layer1 = ParamConfig.INTEGER(0, { range: [0, 31], rangeLocked: [true, true], ...visibleOption({ layersCount: 2 }) }); /** @param updateMode */ this.updateMode2 = ParamConfig.INTEGER(UPDATE_MODES.indexOf("add" /* ADD */), { menu: { entries: UPDATE_MODE_ENTRIES }, ...visibleOption({ layersCount: 3 }) }); /** @param layer */ this.layer2 = ParamConfig.INTEGER(0, { range: [0, 31], rangeLocked: [true, true], ...visibleOption({ layersCount: 3 }) }); /** @param updateMode */ this.updateMode3 = ParamConfig.INTEGER(UPDATE_MODES.indexOf("add" /* ADD */), { menu: { entries: UPDATE_MODE_ENTRIES }, ...visibleOption({ layersCount: 4 }) }); /** @param layer */ this.layer3 = ParamConfig.INTEGER(0, { range: [0, 31], rangeLocked: [true, true], ...visibleOption({ layersCount: 4 }) }); } } const ParamsConfig = new LayerSopParamsConfig(); export class LayerSopNode extends TypedSopNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return SopType.LAYER; } initializeNode() { this.io.inputs.setCount(1); this.io.inputs.initInputsClonedState(InputCloneMode.FROM_NODE); } cook(inputCoreGroups) { const coreGroup = inputCoreGroups[0]; const objects = filterThreejsObjectsWithGroup(coreGroup, this.pv); for (const object of objects) { this._updateLayers(object); } this.setCoreGroup(coreGroup); } _updateLayers(object) { this._updateLayer(0, object, UPDATE_MODES[this.pv.updateMode0], this.pv.layer0); this._updateLayer(1, object, UPDATE_MODES[this.pv.updateMode1], this.pv.layer1); this._updateLayer(2, object, UPDATE_MODES[this.pv.updateMode2], this.pv.layer2); this._updateLayer(3, object, UPDATE_MODES[this.pv.updateMode3], this.pv.layer3); } _updateLayer(index, object, updateMode, layer) { if (index > this.pv.layersCount - 1) { return; } switch (updateMode) { case "set" /* SET */: { object.layers.set(layer); return; } case "add" /* ADD */: { object.layers.enable(layer); return; } case "remove" /* REMOVE */: { object.layers.disable(layer); return; } } } // // helper methods // setMode(index, mode) { const param = [this.p.updateMode0, this.p.updateMode1, this.p.updateMode2, this.p.updateMode3]; param[index].set(UPDATE_MODES.indexOf(mode)); } setLayer(index, layer) { const param = [this.p.layer0, this.p.layer1, this.p.layer2, this.p.layer3]; param[index].set(layer); } }