@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
56 lines (55 loc) • 2.64 kB
JavaScript
"use strict";
import { TypedGlNode } from "./_Base";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { GlConnectionPointType, GlConnectionPoint } from "../utils/io/connections/Gl";
import { VaryingGLDefinition, FunctionGLDefinition } from "./utils/GLDefinition";
import { ShaderName } from "../utils/shaders/ShaderName";
import FogGlsl from "./gl/fog.glsl";
import { ThreeToGl } from "../../../core/ThreeToGl";
const OUTPUT_NAME = "color";
class FogGlParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
this.mvPosition = ParamConfig.VECTOR4([0, 0, 0, 0]);
this.baseColor = ParamConfig.COLOR([0, 0, 0]);
this.fogColor = ParamConfig.COLOR([1, 1, 1]);
this.near = ParamConfig.FLOAT(0);
this.far = ParamConfig.FLOAT(0);
}
}
const ParamsConfig = new FogGlParamsConfig();
export class FogGlNode extends TypedGlNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return "fog";
}
initializeNode() {
super.initializeNode();
this.io.outputs.setNamedOutputConnectionPoints([
new GlConnectionPoint(OUTPUT_NAME, GlConnectionPointType.VEC3)
]);
}
setLines(shaders_collection_controller) {
if (shaders_collection_controller.currentShaderName() == ShaderName.FRAGMENT) {
const varying_name = this.glVarName(this.name());
const definition = new VaryingGLDefinition(this, GlConnectionPointType.VEC4, varying_name);
const vertex_body_line = `${varying_name} = modelViewMatrix * vec4(position, 1.0)`;
shaders_collection_controller.addDefinitions(this, [definition], ShaderName.VERTEX);
shaders_collection_controller.addBodyLines(this, [vertex_body_line], ShaderName.VERTEX);
const function_definition = new FunctionGLDefinition(this, FogGlsl);
const mvPosition = ThreeToGl.vector4(this.variableForInputParam(this.p.mvPosition));
const baseColor = ThreeToGl.vector3(this.variableForInputParam(this.p.baseColor));
const fogColor = ThreeToGl.vector3(this.variableForInputParam(this.p.fogColor));
const near = ThreeToGl.vector3(this.variableForInputParam(this.p.near));
const far = ThreeToGl.vector3(this.variableForInputParam(this.p.far));
const out_value = this.glVarName(OUTPUT_NAME);
const args = [mvPosition, baseColor, fogColor, near, far].join(", ");
const body_line = `vec3 ${out_value} = compute_fog(${args})`;
shaders_collection_controller.addDefinitions(this, [definition, function_definition]);
shaders_collection_controller.addBodyLines(this, [body_line]);
}
}
}