@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
100 lines (99 loc) • 3.77 kB
JavaScript
"use strict";
import { isJsConnectionPointPrimitive, JsConnectionPoint, JsConnectionPointType } from "../utils/io/connections/Js";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { TypedJsNode } from "./_Base";
import { EASING_NAMES } from "../../../core/math/Easing";
import { createVariable } from "./code/assemblers/_BaseJsPersistedConfigUtils";
import { Poly } from "../../Poly";
import { _vectorFunctionName_1 } from "../../functions/_MathGeneric";
const ALLOWED_INPUTS = [
JsConnectionPointType.FLOAT,
JsConnectionPointType.VECTOR2,
JsConnectionPointType.VECTOR3,
JsConnectionPointType.VECTOR4
];
const INPUT_NAME = "in";
const OUTPUT_NAME = "out";
const defaultEaseType = EASING_NAMES.indexOf("easeIO3");
class EasingJsParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
this.type = ParamConfig.INTEGER(defaultEaseType, {
menu: {
entries: EASING_NAMES.map((name, i) => {
return { name, value: i };
})
}
});
}
// input = ParamConfig.FLOAT(0);
}
const ParamsConfig = new EasingJsParamsConfig();
export class EasingJsNode extends TypedJsNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return "easing";
}
initializeNode() {
super.initializeNode();
this.io.connection_points.spare_params.setInputlessParamNames(["type"]);
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));
this.io.outputs.setNamedOutputConnectionPoints([
new JsConnectionPoint(OUTPUT_NAME, JsConnectionPointType.FLOAT)
]);
}
_expectedInputTypes() {
const type = this.io.connection_points.first_input_connection_type() || JsConnectionPointType.FLOAT;
if (ALLOWED_INPUTS.includes(type)) {
return [type];
} else {
return [JsConnectionPointType.FLOAT];
}
}
_expectedOutputTypes() {
return [this._expectedInputTypes()[0]];
}
_expectedInputName(index) {
return INPUT_NAME;
}
_expectedOutputName(index) {
return OUTPUT_NAME;
}
setLines(shadersCollectionController) {
const varName = this.jsVarName(this._expectedOutputName(0));
const inputType = this._expectedInputTypes()[0];
const variable = createVariable(inputType);
const tmpVarName = variable ? shadersCollectionController.addVariable(this, variable) : void 0;
const inputValue = this.variableForInput(shadersCollectionController, INPUT_NAME);
const functionName = EASING_NAMES[this.pv.type];
const func = Poly.namedFunctionsRegister.getFunction(functionName, this, shadersCollectionController);
const isPrimitive = isJsConnectionPointPrimitive(inputType);
if (isPrimitive) {
return shadersCollectionController.addBodyOrComputed(this, [
{ dataType: JsConnectionPointType.FLOAT, varName, value: func.asString(inputValue) }
]);
}
const vectorFunctionName = _vectorFunctionName_1(inputType);
if (vectorFunctionName && tmpVarName) {
func.asString("");
const vectorFunc = Poly.namedFunctionsRegister.getFunction(
vectorFunctionName,
this,
shadersCollectionController
);
return shadersCollectionController.addBodyOrComputed(this, [
{
dataType: JsConnectionPointType.FLOAT,
varName,
value: vectorFunc.asString(...[functionName, inputValue, tmpVarName])
}
]);
}
}
}