UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

93 lines (92 loc) 3.58 kB
"use strict"; import { isJsConnectionPointArray, JsConnectionPointType, JsConnectionPointTypeFromArrayTypeMap, JsConnectionPointTypeToArrayTypeMap } from "../utils/io/connections/Js"; import { ParamlessTypedJsNode } from "./_Base"; import { Poly } from "../../Poly"; import { createVariable } from "./code/assemblers/_BaseJsPersistedConfigUtils"; var CrossJsNodeInputName = /* @__PURE__ */ ((CrossJsNodeInputName2) => { CrossJsNodeInputName2["VALUE0"] = "v0"; CrossJsNodeInputName2["VALUE1"] = "v1"; return CrossJsNodeInputName2; })(CrossJsNodeInputName || {}); const DefaultValues = { ["v0" /* VALUE0 */]: 1, ["v1" /* VALUE1 */]: 1 }; const OUTPUT_NAME = "val"; const ALLOWED_INPUT_TYPES = [JsConnectionPointType.VECTOR2, JsConnectionPointType.VECTOR3]; function functionNameByType(type) { switch (type) { case JsConnectionPointType.VECTOR2: { return "crossVector2"; } case JsConnectionPointType.VECTOR3: { return "crossVector3"; } } } export class CrossJsNode extends ParamlessTypedJsNode { static type() { return "cross"; } 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 value0 = this.variableForInput(shadersCollectionController, "v0" /* VALUE0 */); const value1 = this.variableForInput(shadersCollectionController, "v1" /* VALUE1 */); 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 functionName = functionNameByType(inputType); if (functionName && tmpVarName) { const func = Poly.namedFunctionsRegister.getFunction(functionName, this, shadersCollectionController); shadersCollectionController.addBodyOrComputed(this, [ { dataType: inputType, varName, value: func.asString(value0, value1, tmpVarName) } ]); 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, type]; } _expectedOutputTypes() { const inputType = this._expectedInputTypes()[0]; const elementInputType = JsConnectionPointTypeFromArrayTypeMap[inputType]; function arrayOrElement(type) { return isJsConnectionPointArray(inputType) ? JsConnectionPointTypeToArrayTypeMap[type] : type; } function _outputType() { switch (elementInputType) { case JsConnectionPointType.VECTOR2: { return arrayOrElement(JsConnectionPointType.FLOAT); } case JsConnectionPointType.VECTOR3: { return arrayOrElement(JsConnectionPointType.VECTOR3); } } return arrayOrElement(JsConnectionPointType.VECTOR3); } return [_outputType()]; } _expectedInputName(index) { return ["v0" /* VALUE0 */, "v1" /* VALUE1 */][index]; } _expectedOutputName(index) { return OUTPUT_NAME; } paramDefaultValue(name) { return DefaultValues[name]; } }