UNPKG

@polygonjs/polygonjs

Version:

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

127 lines (126 loc) 3.77 kB
"use strict"; import { NeverStencilFunc, LessStencilFunc, EqualStencilFunc, LessEqualStencilFunc, GreaterStencilFunc, NotEqualStencilFunc, GreaterEqualStencilFunc, AlwaysStencilFunc, ZeroStencilOp, KeepStencilOp, ReplaceStencilOp, IncrementStencilOp, DecrementStencilOp, IncrementWrapStencilOp, DecrementWrapStencilOp, InvertStencilOp } from "three"; import { UpdateMatNode } from "./_Base"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; const STENCIL_FUNCTIONS = [ { NeverStencilFunc }, { LessStencilFunc }, { EqualStencilFunc }, { LessEqualStencilFunc }, { GreaterStencilFunc }, { NotEqualStencilFunc }, { GreaterEqualStencilFunc }, { AlwaysStencilFunc } ]; const STENCIL_FUNCTIONS_NAMES = STENCIL_FUNCTIONS.map((obj) => Object.keys(obj)[0]); const STENCIL_FUNCTION_BY_NAME = {}; for (const k of STENCIL_FUNCTIONS) { const key = Object.keys(k)[0]; const value = Object.values(k)[0]; STENCIL_FUNCTION_BY_NAME[key] = value; } const STENCIL_FUNC_ENTRIES = STENCIL_FUNCTIONS_NAMES.map((name, value) => { return { name, value: STENCIL_FUNCTION_BY_NAME[name] }; }); const STENCIL_OPS = [ { ZeroStencilOp }, { KeepStencilOp }, { ReplaceStencilOp }, { IncrementStencilOp }, { DecrementStencilOp }, { IncrementWrapStencilOp }, { DecrementWrapStencilOp }, { InvertStencilOp } ]; const STENCIL_OPS_NAMES = STENCIL_OPS.map((obj) => Object.keys(obj)[0]); const STENCIL_OP_BY_NAME = {}; for (const k of STENCIL_OPS) { const key = Object.keys(k)[0]; const value = Object.values(k)[0]; STENCIL_OP_BY_NAME[key] = value; } const STENCIL_OP_ENTRIES = STENCIL_OPS_NAMES.map((name, value) => { return { name, value: STENCIL_OP_BY_NAME[name] }; }); class StencilPropertiesMatParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param stencilWrite */ this.stencilWrite = ParamConfig.BOOLEAN(1); /** @param stencilFunc */ this.stencilFunc = ParamConfig.INTEGER(EqualStencilFunc, { menu: { entries: STENCIL_FUNC_ENTRIES } }); /** @param stencilWriteMask */ this.stencilWriteMask = ParamConfig.INTEGER(255, { range: [0, 255], rangeLocked: [true, true] }); /** @param stencilWriteMask */ this.stencilFuncMask = ParamConfig.INTEGER(255, { range: [0, 255], rangeLocked: [true, true] }); /** @param stencilRef */ this.stencilRef = ParamConfig.INTEGER(0); /** @param stencilFail */ this.stencilFail = ParamConfig.INTEGER(KeepStencilOp, { menu: { entries: STENCIL_OP_ENTRIES } }); /** @param stencilZFail */ this.stencilZFail = ParamConfig.INTEGER(KeepStencilOp, { menu: { entries: STENCIL_OP_ENTRIES } }); /** @param stencilZPass */ this.stencilZPass = ParamConfig.INTEGER(KeepStencilOp, { menu: { entries: STENCIL_OP_ENTRIES } }); } } const ParamsConfig = new StencilPropertiesMatParamsConfig(); export class StencilPropertiesMatNode extends UpdateMatNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "stencilProperties"; } async cook(inputMaterials) { const inputMaterial = inputMaterials[0]; inputMaterial.stencilWrite = this.pv.stencilWrite; inputMaterial.stencilFunc = this.pv.stencilFunc; inputMaterial.stencilWriteMask = this.pv.stencilWriteMask; inputMaterial.stencilFuncMask = this.pv.stencilFuncMask; inputMaterial.stencilRef = this.pv.stencilRef; inputMaterial.stencilFail = this.pv.stencilFail; inputMaterial.stencilZFail = this.pv.stencilZFail; inputMaterial.stencilZPass = this.pv.stencilZPass; this.setMaterial(inputMaterial); } }