@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
174 lines (173 loc) • 7.28 kB
JavaScript
"use strict";
import ADJACENT_POINTS_ATTRIB_SMOOTH from "./gl/neighbour/adjacentPointsAttribSmooth.glsl";
import GET_UV from "./gl/geometryAttributes/geometryAttributesLookupUv.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 { AttribAdjacency, adjacencyAttribName } from "../../../core/geometry/operation/Adjacency";
import { GlType } from "../../poly/registers/nodes/types/Gl";
import { UniformGLDefinition } from "./utils/GLDefinition";
const OUTPUT_NAME = "attrib";
class AdjacentPointsAttribSmoothGlParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
this.positionAttribName = ParamConfig.STRING("position");
this.position = ParamConfig.VECTOR3([0, 0, 0], {
separatorAfter: true
});
// attribType = ParamConfig.INTEGER(GL_CONNECTION_POINT_TYPES.indexOf(GlConnectionPointType.FLOAT), {
// menu: {
// entries: GL_CONNECTION_POINT_TYPES.map((name, i) => {
// return {name: name, value: i};
// }),
// },
// separatorBefore: true,
// });
this.textureSize = ParamConfig.VECTOR2([128, 128]);
this.adjacencyCount = ParamConfig.FLOAT(6, {
range: [0, 8],
rangeLocked: [true, false]
});
this.adjacencyBaseName = ParamConfig.STRING(AttribAdjacency.BASE_NAME);
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 AdjacentPointsAttribSmoothGlParamsConfig();
export class AdjacentPointsAttribSmoothGlNode extends TypedGlNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return GlType.ADJACENT_POINTS_ATTRIB_SMOOTH;
}
initializeNode() {
super.initializeNode();
this.io.connection_points.spare_params.setInputlessParamNames(["adjacencyCount", "adjacencyBaseName"]);
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 position = ThreeToGl.vector3(this.variableForInputParam(this.p.position));
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 positionTextureData = globalsTextureHandler.attribTextureData(
this.pv.positionAttribName
);
const attribTextureData = globalsTextureHandler.attribTextureData(
this.pv.attribName
);
const textureAllocationData = this.textureAllocationData();
const adjacencyTextureDatas = textureAllocationData.map(
(d) => globalsTextureHandler.attribTextureData(d)
);
if (positionTextureData && attribTextureData && adjacencyTextureDatas.length == textureAllocationData.length) {
const { textureName, uvName } = positionTextureData;
const attribTextureName = attribTextureData.textureName;
const attribComponent = attribTextureData.component;
const positionComponent = positionTextureData.component;
const args = [
textureName,
uvName,
position,
textureSize,
//
attribTextureName,
attribValue,
deltaThreshold,
smoothAmount
];
const { functionName, functionDeclaration } = this._templateFunctionDefinition({
positionComponent,
attribComponent,
adjacencyTextureDatas
});
linesController.addDefinitions(this, [new FunctionGLDefinition(this, GET_UV)]);
linesController.addDefinitions(this, [new FunctionGLDefinition(this, functionDeclaration)]);
for (const textureData of adjacencyTextureDatas) {
if (textureData) {
linesController.addDefinitions(this, [
new UniformGLDefinition(this, GlConnectionPointType.SAMPLER_2D, textureData.textureName)
]);
if (!args.includes(textureData.textureName)) {
args.push(textureData.textureName);
}
}
}
bodyLines.push(`float ${out} = ${functionName}(${args.join(",\n")})`);
}
}
linesController.addBodyLines(this, bodyLines);
}
textureAllocationData() {
const count = this.pv.adjacencyCount;
const attribNames = [];
for (let i = 0; i < count; i++) {
const attribName = adjacencyAttribName(this.pv.adjacencyBaseName, i);
attribNames.push(attribName);
}
return attribNames;
}
_templateFunctionDefinition(options) {
const adjacencyCount = ThreeToGl.integer(this.pv.adjacencyCount);
const adjacencyVariableNames = [];
const adjacencyTextureRead = [];
const textureNames = [];
let i = 0;
for (const textureData of options.adjacencyTextureDatas) {
if (textureData) {
const { textureName, uvName, component } = textureData;
const varName = adjacencyAttribName(this.pv.adjacencyBaseName, i);
adjacencyVariableNames.push(varName);
adjacencyTextureRead.push(`vec2 ${varName} = texture2D( ${textureName}, ${uvName} ).${component};`);
if (!textureNames.includes(textureName)) {
textureNames.push(textureName);
}
}
i++;
}
const adjacencySamplersArguments = textureNames.map((textureName) => `sampler2D ${textureName}`).join(`,
`);
const adjacencyArrayFromSamplers = `
${adjacencyTextureRead.join("\n")}
`;
const adjacencyArrayValue = `[${adjacencyCount}] = vec2[${adjacencyCount}](
${adjacencyVariableNames.join(",\n")}
)`;
const functionName = `${this.type()}${this.graphNodeId()}`;
const functionDeclaration = ADJACENT_POINTS_ATTRIB_SMOOTH.replace("__FUNCTION__NAME__", functionName).replace("__COMPONENT__", options.positionComponent).replace("__COMPONENT_ATTRIB__", options.attribComponent).replace("__ADJACENCY_COUNT__", adjacencyCount).replace("__ADJACENCY_VALUES_FROM_SAMPLERS__", adjacencyArrayFromSamplers).replace("__ADJACENCY_ARRAY_VALUE__", adjacencyArrayValue).replace("__ADJACENCY_SAMPLERS_ARGUMENTS__", adjacencySamplersArguments);
return {
functionName,
functionDeclaration
};
}
}