@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
71 lines (70 loc) • 2.63 kB
JavaScript
"use strict";
import { isJsConnectionPointArray, JsConnectionPointType } from "../utils/io/connections/Js";
import { ParamlessTypedJsNode } from "./_Base";
import { Poly } from "../../Poly";
var LengthJsNodeInputName = /* @__PURE__ */ ((LengthJsNodeInputName2) => {
LengthJsNodeInputName2["VALUE"] = "v";
return LengthJsNodeInputName2;
})(LengthJsNodeInputName || {});
const DefaultValues = {
["v" /* VALUE */]: 1
};
const OUTPUT_NAME = "val";
const ALLOWED_INPUT_TYPES = [
JsConnectionPointType.VECTOR2,
JsConnectionPointType.VECTOR3,
JsConnectionPointType.VECTOR4
];
function functionNameByType(type) {
switch (type) {
case JsConnectionPointType.VECTOR2:
case JsConnectionPointType.VECTOR3:
case JsConnectionPointType.VECTOR4: {
return "lengthVector";
}
}
}
export class LengthJsNode extends ParamlessTypedJsNode {
static type() {
return "length";
}
initializeNode() {
super.initializeNode();
this.io.connection_points.set_expected_input_types_function(this._expectedInputTypes.bind(this));
this.io.connection_points.set_expected_output_types_function(this._expectedOutputTypes.bind(this));
this.io.connection_points.set_input_name_function(this._expectedInputName.bind(this));
this.io.connection_points.set_output_name_function(this._expectedOutputName.bind(this));
}
setLines(shadersCollectionController) {
const input = this.variableForInput(shadersCollectionController, this._expectedInputName(0));
const varName = this.jsVarName(this._expectedOutputName(0));
const inputType = this._expectedInputTypes()[0];
const functionName = functionNameByType(inputType);
if (functionName) {
const func = Poly.namedFunctionsRegister.getFunction(functionName, this, shadersCollectionController);
shadersCollectionController.addBodyOrComputed(this, [
{ dataType: inputType, varName, value: func.asString(input) }
]);
return;
}
}
_expectedInputTypes() {
const firstType = this.io.connection_points.first_input_connection_type();
const type = firstType && ALLOWED_INPUT_TYPES.includes(firstType) ? firstType : JsConnectionPointType.VECTOR3;
return [type];
}
_expectedOutputTypes() {
const inputType = this._expectedInputTypes()[0];
const outputType = isJsConnectionPointArray(inputType) ? JsConnectionPointType.FLOAT_ARRAY : JsConnectionPointType.FLOAT;
return [outputType];
}
_expectedInputName(index) {
return ["v" /* VALUE */][index];
}
_expectedOutputName(index) {
return OUTPUT_NAME;
}
paramDefaultValue(name) {
return DefaultValues[name];
}
}