@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
147 lines (146 loc) • 5.97 kB
JavaScript
;
import { TRIGGER_CONNECTION_NAME, TypedJsNode } from "./_Base";
import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig";
import { JsConnectionPoint, JsConnectionPointType } from "../utils/io/connections/Js";
import { JsType } from "../../poly/registers/nodes/types/Js";
import { Poly } from "../../Poly";
import { InitFunctionJsDefinition, TriggeringJsDefinition } from "./utils/JsDefinition";
import { PerformanceChangeEvent, PERFORMANCE_CHANGE_EVENTS } from "../../functions/_Performance";
import {
getConnectedOutputNodes,
getOutputIndices,
nodeMethodName,
triggerInputIndex
} from "./code/assemblers/actor/ActorAssemblerUtils";
import { setToArray } from "../../../core/SetUtils";
function _outputName(eventName) {
return `${TRIGGER_CONNECTION_NAME}${eventName}`;
}
class OnPerformanceChangeJsParamsConfig extends NodeParamsConfig {
constructor() {
super(...arguments);
this.threshold = ParamConfig.FLOAT(0.8, {
range: [0, 1],
rangeLocked: [true, true]
});
}
// delay = ParamConfig.INTEGER(3000, {
// range: [0, 5000],
// rangeLocked: [true, false],
// });
}
const ParamsConfig = new OnPerformanceChangeJsParamsConfig();
const _OnPerformanceChangeJsNode = class extends TypedJsNode {
constructor() {
super(...arguments);
this.paramsConfig = ParamsConfig;
}
static type() {
return JsType.ON_PERFORMANCE_CHANGE;
}
isTriggering() {
return true;
}
// static OUTPUT_TRIGGER_NAMES = [this.OUTPUT_NAME_ABOVE, this.OUTPUT_NAME_BELOW];
initializeNode() {
this.io.outputs.setNamedOutputConnectionPoints([
new JsConnectionPoint(_OnPerformanceChangeJsNode.OUTPUT_NAME_ABOVE, JsConnectionPointType.TRIGGER),
new JsConnectionPoint(_OnPerformanceChangeJsNode.OUTPUT_NAME_BELOW, JsConnectionPointType.TRIGGER)
// new JsConnectionPoint(OnPerformanceChangeOutputName.aboveThreshold, JsConnectionPointType.BOOLEAN),
// new JsConnectionPoint(OnPerformanceChangeOutputName.performance, JsConnectionPointType.FLOAT),
]);
}
setTriggeringLines(shadersCollectionController, triggeredMethods) {
const listeners = {
[PerformanceChangeEvent.aboveThreshold]: "",
[PerformanceChangeEvent.belowThreshold]: ""
};
PERFORMANCE_CHANGE_EVENTS.forEach((eventName) => {
const triggeredMethods2 = triggerMethod(this, _outputName(eventName));
const _nodeMethodName = nodeMethodName(this, _outputName(eventName));
listeners[eventName] = `this.${_nodeMethodName}.bind(this)`;
const value = triggeredMethods2;
const dataType = JsConnectionPointType.BOOLEAN;
shadersCollectionController.addDefinitions(this, [
new TriggeringJsDefinition(this, shadersCollectionController, dataType, _nodeMethodName, value, {
triggeringMethodName: eventName,
gatherable: false,
nodeMethodName: _nodeMethodName
})
]);
});
const threshold = this.variableForInputParam(shadersCollectionController, this.p.threshold);
const func = Poly.namedFunctionsRegister.getFunction("onPerformanceChange", this, shadersCollectionController);
const bodyLine = func.asString(threshold, JSON.stringify(listeners).replace(/\"/g, ""), `this`);
shadersCollectionController.addDefinitions(this, [
new InitFunctionJsDefinition(
this,
shadersCollectionController,
JsConnectionPointType.OBJECT_3D,
this.path(),
bodyLine
)
]);
}
// initOnPlay() {
// this._currentPerfAboveThreshold = undefined;
// this.scene().perfMonitor.addThreshold(this.pv.threshold);
// }
// disposeOnPause() {
// this.scene().perfMonitor.reset();
// }
// protected _currentPerfAboveThreshold: boolean | undefined;
// private _lastChangeAt: number | undefined;
// runTriggerIfRequired(context: JsNodeTriggerContext) {
// // const now = performance.now();
// // if (this._lastChangeAt != null && now - this._lastChangeAt < this.pv.delay) {
// // return;
// // }
// const perf = this.scene().perfMonitor.performance();
// const perfAboveThreshold = perf > this.pv.threshold;
// if (perfAboveThreshold != this._currentPerfAboveThreshold) {
// this._currentPerfAboveThreshold = perfAboveThreshold;
// // this._lastChangeAt = now;
// const outputIndex = OnPerformanceChangeJsNode.OUTPUT_TRIGGER_NAMES.indexOf(
// perfAboveThreshold
// ? OnPerformanceChangeJsNode.OUTPUT_NAME_ABOVE
// : OnPerformanceChangeJsNode.OUTPUT_NAME_BELOW
// );
// this.runTrigger(context, outputIndex);
// }
// }
// public override outputValue(
// context: JsNodeTriggerContext,
// outputName: OnPerformanceChangeOutputName
// ): boolean | number {
// const perf = this.scene().perfMonitor.performance();
// switch (outputName) {
// case OnPerformanceChangeOutputName.aboveThreshold: {
// return this._currentPerfAboveThreshold || true;
// }
// case OnPerformanceChangeOutputName.performance: {
// return perf;
// }
// }
// TypeAssert.unreachable(outputName);
// }
};
export let OnPerformanceChangeJsNode = _OnPerformanceChangeJsNode;
OnPerformanceChangeJsNode.OUTPUT_NAME_ABOVE = _outputName(PerformanceChangeEvent.aboveThreshold);
OnPerformanceChangeJsNode.OUTPUT_NAME_BELOW = _outputName(PerformanceChangeEvent.belowThreshold);
function triggerMethod(node, outputName) {
const outputIndex = getOutputIndices(node, (c) => c.name() == outputName)[0];
const triggerableNodes = /* @__PURE__ */ new Set();
getConnectedOutputNodes({
node,
triggerOutputIndices: [outputIndex],
triggerableNodes,
recursive: false
});
const triggerableMethodNames = setToArray(triggerableNodes, []).map((triggerableNode) => {
const argIndex = triggerInputIndex(node, triggerableNode);
const m = nodeMethodName(triggerableNode);
return `this.${m}(${argIndex})`;
});
return `${triggerableMethodNames.join(";")}`;
}