@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
48 lines (47 loc) • 2.1 kB
JavaScript
;
import { TypedGlNode } from "./_Base";
import { ThreeToGl } from "../../../../src/core/ThreeToGl";
import DiskMethods from "./gl/disk.glsl";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { GlConnectionPointType, GlConnectionPoint } from "../utils/io/connections/Gl";
import { FunctionGLDefinition } from "./utils/GLDefinition";
const OUTPUT_NAME_VALUE = "value";
const OUTPUT_NAME_GRADIENT = "gradient";
class SphereGlParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
this.position = ParamConfig.VECTOR3([0, 0, 0]);
this.center = ParamConfig.VECTOR3([0, 0, 0]);
this.radius = ParamConfig.FLOAT(1);
this.feather = ParamConfig.FLOAT(0.1);
}
}
const ParamsConfig = new SphereGlParamsConfig();
export class SphereGlNode extends TypedGlNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return "sphere";
}
initializeNode() {
super.initializeNode();
this.io.outputs.setNamedOutputConnectionPoints([
new GlConnectionPoint(OUTPUT_NAME_VALUE, GlConnectionPointType.FLOAT),
new GlConnectionPoint(OUTPUT_NAME_GRADIENT, GlConnectionPointType.VEC3)
]);
}
setLines(shaders_collection_controller) {
const position = ThreeToGl.vector2(this.variableForInputParam(this.p.position));
const center = ThreeToGl.vector3(this.variableForInputParam(this.p.center));
const radius = ThreeToGl.float(this.variableForInputParam(this.p.radius));
const feather = ThreeToGl.float(this.variableForInputParam(this.p.feather));
const value = this.glVarName(OUTPUT_NAME_VALUE);
const gradient = this.glVarName(OUTPUT_NAME_GRADIENT);
const bodyLineValue = `float ${value} = disk3d(${position}, ${center}, ${radius}, ${feather})`;
const bodyLineGradient = `vec3 ${gradient} = ${value}*(${position}-${center})`;
shaders_collection_controller.addBodyLines(this, [bodyLineValue, bodyLineGradient]);
shaders_collection_controller.addDefinitions(this, [new FunctionGLDefinition(this, DiskMethods)]);
}
}