@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
108 lines (107 loc) • 4.17 kB
JavaScript
"use strict";
import { TypedGlNode } from "./_Base";
import TextureBlur from "./gl/textureBlur.glsl";
import { GlConnectionPointType, GlConnectionPoint } from "../utils/io/connections/Gl";
import { ThreeToGl } from "../../../core/ThreeToGl";
import { FunctionGLDefinition, UniformGLDefinition } from "./utils/GLDefinition";
import { ParamConfigsController } from "../utils/code/controllers/ParamConfigsController";
import { ParamType } from "../../poly/ParamType";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { GlParamConfig } from "./code/utils/GLParamConfig";
import { isBooleanTrue } from "../../../core/Type";
import { UNIFORM_TEXTURE_PREFIX } from "../../../core/material/uniform";
import { GlType } from "../../poly/registers/nodes/types/Gl";
const blurParamVisibility = {
visibleIf: { tblur: 1 }
};
class TextureGlParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
this.paramName = ParamConfig.STRING("texture1");
// defaultValue = ParamConfig.STRING('');
this.uv = ParamConfig.VECTOR2([0, 0]);
this.tblur = ParamConfig.BOOLEAN(0);
this.resolution = ParamConfig.VECTOR2([256, 256], blurParamVisibility);
this.blurPixelsCountX = ParamConfig.INTEGER(1, {
range: [1, 4],
rangeLocked: [true, true],
...blurParamVisibility
});
this.blurPixelsCountY = ParamConfig.INTEGER(1, {
range: [1, 4],
rangeLocked: [true, true],
...blurParamVisibility
});
}
}
const ParamsConfig = new TextureGlParamsConfig();
const _TextureGlNode = class extends TypedGlNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return GlType.TEXTURE;
}
initializeNode() {
this.addPostDirtyHook("_setMatToRecompile", this._setMatToRecompile.bind(this));
this.lifecycle.onAfterAdded(this._setMatToRecompile.bind(this));
this.lifecycle.onBeforeDeleted(this._setMatToRecompile.bind(this));
this.io.outputs.setNamedOutputConnectionPoints([
new GlConnectionPoint(_TextureGlNode.OUTPUT_NAME, GlConnectionPointType.VEC4)
]);
this.io.connection_points.spare_params.setInputlessParamNames(["tblur", "resolution"]);
}
setLines(shaders_collection_controller) {
const uv = ThreeToGl.vector2(this.variableForInputParam(this.p.uv));
const rgba = this.glVarName(_TextureGlNode.OUTPUT_NAME);
const map = this.uniformName();
const definitions = [new UniformGLDefinition(this, GlConnectionPointType.SAMPLER_2D, map)];
const bodyLines = [];
if (isBooleanTrue(this.pv.tblur)) {
const resolution = ThreeToGl.vector2(this.variableForInputParam(this.p.resolution));
const blurPixelsCountX = ThreeToGl.integer(this.variableForInputParam(this.p.blurPixelsCountX));
const blurPixelsCountY = ThreeToGl.integer(this.variableForInputParam(this.p.blurPixelsCountY));
const body_line = `vec4 ${rgba} = textureBlur(
${map},
${uv},
${resolution},
${blurPixelsCountX},
${blurPixelsCountY}
)`;
bodyLines.push(body_line);
definitions.push(new FunctionGLDefinition(this, TextureBlur));
} else {
const body_line = `vec4 ${rgba} = texture2D(${map}, ${uv})`;
bodyLines.push(body_line);
}
shaders_collection_controller.addDefinitions(this, definitions);
shaders_collection_controller.addBodyLines(this, bodyLines);
}
paramsGenerating() {
return true;
}
setParamConfigs() {
this._param_configs_controller = this._param_configs_controller || new ParamConfigsController();
this._param_configs_controller.reset();
const param_config = new GlParamConfig(
ParamType.NODE_PATH,
this.pv.paramName,
"",
//this.pv.defaultValue,
this.uniformName()
);
this._param_configs_controller.push(param_config);
}
// override glVarName(name?: string) {
// if (name) {
// return super.glVarName(name);
// }
// return `v_POLY_texture_${this.pv.paramName}`;
// }
uniformName() {
return `${UNIFORM_TEXTURE_PREFIX}${this.pv.paramName}`;
}
};
export let TextureGlNode = _TextureGlNode;
TextureGlNode.OUTPUT_NAME = "rgba";