UNPKG

@polygonjs/polygonjs

Version:

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

96 lines (95 loc) 3.63 kB
"use strict"; import ADJACENT_UV_ATTRIB_SMOOTH from "./gl/neighbour/adjacentUvAttribSmooth.glsl"; import { TypedGlNode } from "./_Base"; import { ThreeToGl } from "../../../../src/core/ThreeToGl"; import { ParamConfig, NodeParamsConfig } from "../utils/params/ParamsConfig"; import { GlConnectionPointType, GlConnectionPoint } from "../utils/io/connections/Gl"; import { FunctionGLDefinition } from "./utils/GLDefinition"; import { GlType } from "../../poly/registers/nodes/types/Gl"; const OUTPUT_NAME = "attrib"; class AdjacentUvAttribSmoothGlParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); this.textureSize = ParamConfig.VECTOR2([128, 128]); this.attribName = ParamConfig.STRING("h"); this.attribValue = ParamConfig.FLOAT(0); this.deltaThreshold = ParamConfig.FLOAT(1, { range: [0, 1], rangeLocked: [true, false] }); this.smoothAmount = ParamConfig.FLOAT(0.01, { range: [0, 1], rangeLocked: [true, false] }); } } const ParamsConfig = new AdjacentUvAttribSmoothGlParamsConfig(); export class AdjacentUvAttribSmoothGlNode extends TypedGlNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return GlType.ADJACENT_UV_ATTRIB_SMOOTH; } initializeNode() { super.initializeNode(); this.io.outputs.setNamedOutputConnectionPoints([ new GlConnectionPoint(OUTPUT_NAME, GlConnectionPointType.FLOAT) ]); } glType() { return GlConnectionPointType.FLOAT; } attributeName() { return this.pv.attribName; } setLines(linesController) { const bodyLines = []; const textureSize = ThreeToGl.vector2(this.variableForInputParam(this.p.textureSize)); const attribValue = ThreeToGl.float(this.variableForInputParam(this.p.attribValue)); const deltaThreshold = ThreeToGl.float(this.variableForInputParam(this.p.deltaThreshold)); const smoothAmount = ThreeToGl.float(this.variableForInputParam(this.p.smoothAmount)); const out = this.glVarName(OUTPUT_NAME); const assembler = linesController.assembler(); const globalsHandler = assembler.globalsHandler(); if (!globalsHandler) { return; } if (globalsHandler.attribTextureData) { const globalsTextureHandler = globalsHandler; const attribTextureData = globalsTextureHandler.attribTextureData( this.pv.attribName ); if (attribTextureData) { const { uvName } = attribTextureData; const attribTextureName = attribTextureData.textureName; const attribComponent = attribTextureData.component; const args = [ uvName, textureSize, // attribTextureName, attribValue, deltaThreshold, smoothAmount ]; const { functionName, functionDeclaration } = this._templateFunctionDefinition({ attribComponent }); linesController.addDefinitions(this, [new FunctionGLDefinition(this, functionDeclaration)]); bodyLines.push(`float ${out} = ${functionName}(${args.join(",\n")})`); } } linesController.addBodyLines(this, bodyLines); } _templateFunctionDefinition(options) { const functionName = `${this.type()}${this.graphNodeId()}`; const subFunctionName = `sub${this.type()}${this.graphNodeId()}`; const functionDeclaration = ADJACENT_UV_ATTRIB_SMOOTH.replace("__FUNCTION__NAME__", functionName).replace(/__SUB_FUNCTION__NAME__/g, subFunctionName).replace("__COMPONENT_ATTRIB__", options.attribComponent); return { functionName, functionDeclaration }; } }