@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
94 lines (93 loc) • 3.35 kB
JavaScript
"use strict";
import Attract from "./gl/neighbour/attract.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";
const OUTPUT_NAME = "force";
class NeighbourAttractGlParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
this.positionAttribName = ParamConfig.STRING("position");
this.position = ParamConfig.VECTOR3([0, 0, 0]);
this.amount = ParamConfig.FLOAT(1, {
range: [0, 1],
rangeLocked: [true, false]
});
this.startDist = ParamConfig.FLOAT(3, {
range: [0, 10],
rangeLocked: [true, false]
});
this.midDist = ParamConfig.FLOAT(4, {
range: [0, 10],
rangeLocked: [true, false]
});
this.endDist = ParamConfig.FLOAT(5, {
range: [0, 10],
rangeLocked: [true, false]
});
}
}
const ParamsConfig = new NeighbourAttractGlParamsConfig();
export class NeighbourAttractGlNode extends TypedGlNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return "neighbourAttract";
}
initializeNode() {
super.initializeNode();
this.io.outputs.setNamedOutputConnectionPoints([
new GlConnectionPoint(OUTPUT_NAME, GlConnectionPointType.VEC3)
]);
}
setLines(shadersCollectionController) {
const bodyLines = [];
const position = ThreeToGl.vector3(this.variableForInputParam(this.p.position));
const amount = ThreeToGl.float(this.variableForInputParam(this.p.amount));
const startDist = ThreeToGl.float(this.variableForInputParam(this.p.startDist));
const midDist = ThreeToGl.float(this.variableForInputParam(this.p.midDist));
const endDist = ThreeToGl.float(this.variableForInputParam(this.p.endDist));
const out = this.glVarName(OUTPUT_NAME);
const assembler = shadersCollectionController.assembler();
const globalsHandler = assembler.globalsHandler();
if (!globalsHandler) {
return;
}
if (globalsHandler.attribTextureData) {
const globalsTextureHandler = globalsHandler;
const textureData = globalsTextureHandler.attribTextureData(this.pv.positionAttribName);
if (textureData) {
const { textureName, component, uvName } = textureData;
const args = [
textureName,
uvName,
position,
// attract
amount,
startDist,
midDist,
endDist
].join(", ");
const { functionName, functionDeclaration } = this._templateFunctionDefinition(component);
shadersCollectionController.addDefinitions(this, [new FunctionGLDefinition(this, functionDeclaration)]);
bodyLines.push(`vec3 ${out} = ${functionName}(${args})`);
}
}
shadersCollectionController.addBodyLines(this, bodyLines);
}
_templateFunctionDefinition(component) {
const functionName = `${this.type()}${this.graphNodeId()}`;
const functionDeclaration = Attract.replace("__FUNCTION__NAME__", functionName).replace(
"__COMPONENT__",
component
);
return {
functionName,
functionDeclaration
};
}
}