UNPKG

@polygonjs/polygonjs

Version:

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

121 lines (120 loc) 4.45 kB
"use strict"; import { JsConnectionPointType } from "../utils/io/connections/Js"; import { ParamlessTypedJsNode } from "./_Base"; import { Poly } from "../../Poly"; var NoiseSimplexJsNodeInputName = /* @__PURE__ */ ((NoiseSimplexJsNodeInputName2) => { NoiseSimplexJsNodeInputName2["POSITION"] = "position"; NoiseSimplexJsNodeInputName2["AMP"] = "amp"; NoiseSimplexJsNodeInputName2["FREQ"] = "freq"; NoiseSimplexJsNodeInputName2["OFFSET"] = "offset"; NoiseSimplexJsNodeInputName2["OCTAVES"] = "octaves"; NoiseSimplexJsNodeInputName2["AMP_MULT"] = "ampMult"; NoiseSimplexJsNodeInputName2["FREQ_MULT"] = "freqMult"; NoiseSimplexJsNodeInputName2["SEED"] = "seed"; return NoiseSimplexJsNodeInputName2; })(NoiseSimplexJsNodeInputName || {}); const DefaultValues = { ["position" /* POSITION */]: 0, ["amp" /* AMP */]: 1, ["freq" /* FREQ */]: 1, ["offset" /* OFFSET */]: 0, ["octaves" /* OCTAVES */]: 3, ["ampMult" /* AMP_MULT */]: 0.5, ["freqMult" /* FREQ_MULT */]: 2, ["seed" /* SEED */]: 0 }; const OUTPUT_NAME = "noise"; const ALLOWED_INPUT_TYPES = [ JsConnectionPointType.VECTOR2, JsConnectionPointType.VECTOR3, JsConnectionPointType.VECTOR4 ]; function functionNameByType(type) { switch (type) { case JsConnectionPointType.VECTOR2: { return "noiseSimplexVector2"; } case JsConnectionPointType.VECTOR3: { return "noiseSimplexVector3"; } case JsConnectionPointType.VECTOR4: { return "noiseSimplexVector4"; } } } export class NoiseSimplexJsNode extends ParamlessTypedJsNode { static type() { return "noiseSimplex"; } 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)); } _firstType() { const firstType = this.io.connection_points.first_input_connection_type(); const type = firstType && ALLOWED_INPUT_TYPES.includes(firstType) ? firstType : JsConnectionPointType.VECTOR3; return type; } _expectedInputTypes() { const type = this._firstType(); return [ type, JsConnectionPointType.FLOAT, type, type, JsConnectionPointType.INT, JsConnectionPointType.FLOAT, JsConnectionPointType.FLOAT, JsConnectionPointType.FLOAT ]; } _expectedOutputTypes() { const type = JsConnectionPointType.FLOAT; return [type]; } _expectedInputName(index) { return [ "position" /* POSITION */, "amp" /* AMP */, "freq" /* FREQ */, "offset" /* OFFSET */, "octaves" /* OCTAVES */, "ampMult" /* AMP_MULT */, "freqMult" /* FREQ_MULT */, "seed" /* SEED */ ][index]; } _expectedOutputName(index) { return OUTPUT_NAME; } paramDefaultValue(name) { return DefaultValues[name]; } setLines(shadersCollectionController) { const position = this.variableForInput(shadersCollectionController, "position" /* POSITION */); const amp = this.variableForInput(shadersCollectionController, "amp" /* AMP */); const freq = this.variableForInput(shadersCollectionController, "freq" /* FREQ */); const offset = this.variableForInput(shadersCollectionController, "offset" /* OFFSET */); const octaves = this.variableForInput(shadersCollectionController, "octaves" /* OCTAVES */); const ampMult = this.variableForInput(shadersCollectionController, "ampMult" /* AMP_MULT */); const freqMult = this.variableForInput(shadersCollectionController, "freqMult" /* FREQ_MULT */); const seed = this.variableForInput(shadersCollectionController, "seed" /* SEED */); 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(position, amp, freq, offset, octaves, ampMult, freqMult, seed) } ]); return; } } }