@babylonjs/core
Version:
Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.
113 lines • 5.47 kB
JavaScript
import { FlowGraphPathConverterComponent } from "../../../flowGraphPathConverterComponent.js";
import { RichTypeAny } from "../../../flowGraphRichTypes.js";
import { RegisterClass } from "../../../../Misc/typeStore.js";
import { Color3, Color4 } from "../../../../Maths/math.color.js";
import { FlowGraphCachedOperationBlock } from "../flowGraphCachedOperationBlock.js";
/**
* This block will take a JSON pointer and parse it to get the value from the JSON object.
* The output is an object and a property name.
* Optionally, the block can also output the value of the property. This is configurable.
*/
export class FlowGraphJsonPointerParserBlock extends FlowGraphCachedOperationBlock {
constructor(
/**
* the configuration of the block
*/
config) {
super(RichTypeAny, config);
this.config = config;
this.object = this.registerDataOutput("object", RichTypeAny);
this.propertyName = this.registerDataOutput("propertyName", RichTypeAny);
this.setterFunction = this.registerDataOutput("setFunction", RichTypeAny, this._setPropertyValue.bind(this));
this.getterFunction = this.registerDataOutput("getFunction", RichTypeAny, this._getPropertyValue.bind(this));
this.generateAnimationsFunction = this.registerDataOutput("generateAnimationsFunction", RichTypeAny, this._getInterpolationAnimationPropertyInfo.bind(this));
this.templateComponent = new FlowGraphPathConverterComponent(config.jsonPointer, this);
}
_doOperation(context) {
const accessorContainer = this.templateComponent.getAccessor(this.config.pathConverter, context);
const value = accessorContainer.info.get(accessorContainer.object);
const object = accessorContainer.info.getTarget?.(accessorContainer.object);
const propertyName = accessorContainer.info.getPropertyName?.[0](accessorContainer.object);
if (!object) {
throw new Error("Object is undefined");
}
else {
this.object.setValue(object, context);
if (propertyName) {
this.propertyName.setValue(propertyName, context);
}
}
return value;
}
_setPropertyValue(_target, _propertyName, value, context) {
const accessorContainer = this.templateComponent.getAccessor(this.config.pathConverter, context);
const type = accessorContainer.info.type;
if (type.startsWith("Color")) {
value = ToColor(value, type);
}
accessorContainer.info.set?.(value, accessorContainer.object);
}
_getPropertyValue(_target, _propertyName, context) {
const accessorContainer = this.templateComponent.getAccessor(this.config.pathConverter, context);
return accessorContainer.info.get(accessorContainer.object);
}
_getInterpolationAnimationPropertyInfo(_target, _propertyName, context) {
const accessorContainer = this.templateComponent.getAccessor(this.config.pathConverter, context);
return (keys, fps, animationType, easingFunction) => {
const animations = [];
// make sure keys are of the right type (in case of float3 color/vector)
const type = accessorContainer.info.type;
if (type.startsWith("Color")) {
keys = keys.map((key) => {
return {
frame: key.frame,
value: ToColor(key.value, type),
};
});
}
accessorContainer.info.interpolation?.forEach((info, index) => {
const name = accessorContainer.info.getPropertyName?.[index](accessorContainer.object) || "Animation-interpolation-" + index;
// generate the keys based on interpolation info
let newKeys = keys;
if (animationType !== info.type) {
// convert the keys to the right type
newKeys = keys.map((key) => {
return {
frame: key.frame,
value: info.getValue(undefined, key.value.asArray ? key.value.asArray() : [key.value], 0, 1),
};
});
}
const animationData = info.buildAnimations(accessorContainer.object, name, 60, newKeys);
for (const animation of animationData) {
if (easingFunction) {
animation.babylonAnimation.setEasingFunction(easingFunction);
}
animations.push(animation.babylonAnimation);
}
});
return animations;
};
}
/**
* Gets the class name of this block
* @returns the class name
*/
getClassName() {
return "FlowGraphJsonPointerParserBlock" /* FlowGraphBlockNames.JsonPointerParser */;
}
}
function ToColor(value, expectedValue) {
if (value.getClassName().startsWith("Color")) {
return value;
}
if (expectedValue === "Color3") {
return new Color3(value.x, value.y, value.z);
}
else if (expectedValue === "Color4") {
return new Color4(value.x, value.y, value.z, value.w);
}
return value;
}
RegisterClass("FlowGraphJsonPointerParserBlock" /* FlowGraphBlockNames.JsonPointerParser */, FlowGraphJsonPointerParserBlock);
//# sourceMappingURL=flowGraphJsonPointerParserBlock.js.map