UNPKG

@polygonjs/polygonjs

Version:

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

60 lines (59 loc) 2.45 kB
"use strict"; import { BaseSDFGlNode } from "./_BaseSDF"; import { ThreeToGl } from "../../../../src/core/ThreeToGl"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { GlConnectionPointType, GlConnectionPoint } from "../utils/io/connections/Gl"; import { isBooleanTrue } from "../../../core/Type"; import { GlType } from "../../poly/registers/nodes/types/Gl"; const OUTPUT_NAME = "float"; class SDFTorusGlParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); this.position = ParamConfig.VECTOR3([0, 0, 0], { hidden: true }); this.center = ParamConfig.VECTOR3([0, 0, 0]); this.radius1 = ParamConfig.FLOAT(1); this.radius2 = ParamConfig.FLOAT(0.1); this.capped = ParamConfig.BOOLEAN(0); this.angle = ParamConfig.FLOAT(0.5 * Math.PI, { range: [0, Math.PI], rangeLocked: [true, true], step: 1e-4, visibleIf: { capped: 1 } }); } } const ParamsConfig = new SDFTorusGlParamsConfig(); export class SDFTorusGlNode extends BaseSDFGlNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return GlType.SDF_TORUS; } initializeNode() { super.initializeNode(); this.io.connection_points.spare_params.setInputlessParamNames(["capped"]); this.io.outputs.setNamedOutputConnectionPoints([ new GlConnectionPoint(OUTPUT_NAME, GlConnectionPointType.FLOAT) ]); } setLines(shadersCollectionController) { const position = this.position(); const center = ThreeToGl.vector3(this.variableForInputParam(this.p.center)); const radius1 = ThreeToGl.float(this.variableForInputParam(this.p.radius1)); const radius2 = ThreeToGl.float(this.variableForInputParam(this.p.radius2)); const float = this.glVarName(OUTPUT_NAME); if (isBooleanTrue(this.pv.capped)) { const angle = ThreeToGl.float(this.variableForInputParam(this.p.angle)); const torusCapped = `sdCappedTorus(${position} - ${center}, ${angle}, ${radius1}, ${radius2})`; const bodyLine = `float ${float} = ${torusCapped}`; shadersCollectionController.addBodyLines(this, [bodyLine]); } else { const torus = `sdTorus(${position} - ${center}, vec2(${radius1}, ${radius2}))`; const bodyLine = `float ${float} = ${torus}`; shadersCollectionController.addBodyLines(this, [bodyLine]); } this._addSDFMethods(shadersCollectionController); } }