@polygonjs/polygonjs
Version:
node-based WebGL 3D engine https://polygonjs.com
68 lines (67 loc) • 2.28 kB
JavaScript
;
import { ParamConfig } from "../../engine/nodes/utils/params/ParamsConfig";
import { CameraAttribute } from "./CoreCamera";
import { CameraFPSSopOperation } from "../../engine/operations/sop/CameraFPS";
import { coreObjectClassFactory } from "../geometry/CoreObjectFactory";
const DEFAULT = CameraFPSSopOperation.DEFAULT_PARAMS;
export function CoreCameraFPSParamConfig(Base) {
return class Mixin extends Base {
constructor() {
super(...arguments);
/** @param expected FPS */
this.maxFPS = ParamConfig.INTEGER(DEFAULT.maxFPS, {
range: [0, 120],
rangeLocked: [true, false]
});
/** @param allow dynamic change. If this is true, the viewer will check the attribute on the camera at every frame, before adapting the FPS. This allows to change the FPS at a later stage */
this.allowDynamicChange = ParamConfig.BOOLEAN(DEFAULT.allowDynamicChange);
}
};
}
const _getMinDelta = (fps) => {
return 1 / fps;
};
const SAFETY_THRESHOLD = 0.1;
export function isDeltaValid(accumulatedDelta, condig) {
const minDelta = condig.minDelta();
if (accumulatedDelta > minDelta) {
return true;
}
const deltaDeltas = minDelta - accumulatedDelta;
if (deltaDeltas > 0 && deltaDeltas / minDelta > SAFETY_THRESHOLD) {
return false;
}
return true;
}
export class CoreCameraViewerFPSController {
static viewerFPSConfig(options) {
const { camera } = options;
const _getMaxFPSIfPreset = () => {
return coreObjectClassFactory(camera).attribValue(camera, CameraAttribute.MAX_FPS);
};
const maxFPS = _getMaxFPSIfPreset();
if (maxFPS == null) {
return;
}
const _minDelta = _getMinDelta(maxFPS);
const allowDynamicChange = coreObjectClassFactory(camera).attribValue(camera, CameraAttribute.MAX_FPS_DYNAMIC_CHANGE) || false;
if (allowDynamicChange) {
const defaultMinDelta = _getMinDelta(60);
const config = {
minDelta: () => {
const maxFPS2 = _getMaxFPSIfPreset();
if (maxFPS2 == null) {
return defaultMinDelta;
}
return _getMinDelta(maxFPS2);
}
};
return config;
} else {
const config = {
minDelta: () => _minDelta
};
return config;
}
}
}