UNPKG

@polygonjs/polygonjs

Version:

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

51 lines (50 loc) 2.11 kB
"use strict"; import { ThreeToGl } from "../../../core/ThreeToGl"; import CHECKERS from "./gl/checkers.glsl"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { GlConnectionPointType, GlConnectionPoint } from "../utils/io/connections/Gl"; import { FunctionGLDefinition } from "./utils/GLDefinition"; import { TypedGlNode } from "./_Base"; import { isBooleanTrue } from "../../../core/Type"; const OUTPUT_NAME = "checker"; class CheckersGlParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); this.uv = ParamConfig.VECTOR2([0, 0]); this.freq = ParamConfig.VECTOR2([1, 1]); this.freqMult = ParamConfig.FLOAT(1, { range: [0, 10], rangeLocked: [false, false] }); this.filtered = ParamConfig.BOOLEAN(1); } } const ParamsConfig = new CheckersGlParamsConfig(); export class CheckersGlNode extends TypedGlNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "checkers"; } initializeNode() { super.initializeNode(); this.io.connection_points.spare_params.setInputlessParamNames(["filtered"]); this.io.outputs.setNamedOutputConnectionPoints([ new GlConnectionPoint(OUTPUT_NAME, GlConnectionPointType.FLOAT) ]); } setLines(shadersCollectionController) { const uv = ThreeToGl.vector2(this.variableForInputParam(this.p.uv)); const freq = ThreeToGl.vector2(this.variableForInputParam(this.p.freq)); const freqMult = ThreeToGl.float(this.variableForInputParam(this.p.freqMult)); const coord = this.glVarName("coord"); const float = this.glVarName(OUTPUT_NAME); const coordLine = `vec2 ${coord} = ${uv}*${freq}*${freqMult}`; const functionCall = isBooleanTrue(this.pv.filtered) ? `checkersGrad(${coord}, dFdx(${coord}), dFdy(${coord}))` : `checkers(${coord})`; const bodyLine = `float ${float} = ${functionCall}`; shadersCollectionController.addBodyLines(this, [coordLine, bodyLine]); shadersCollectionController.addDefinitions(this, [new FunctionGLDefinition(this, CHECKERS)]); } }