Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add coordinate transform and constant value blocks #14333

Merged
merged 8 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { FlowGraphBlock } from "core/FlowGraph/flowGraphBlock";
import type { FlowGraphContext } from "core/FlowGraph/flowGraphContext";
import type { FlowGraphDataConnection } from "core/FlowGraph/flowGraphDataConnection";
import { getRichTypeFromValue } from "core/FlowGraph/flowGraphRichTypes";

/**
* @experimental
* Configuration for a constant block.
*/
export interface IFlowGraphConstantBlockConfiguration<T> {
/**
* The value of the constant.
*/
value: T;
}
/**
* @experimental
* Block that returns a constant value.
*/
export class FlowGraphConstantBlock<T> extends FlowGraphBlock {
carolhmj marked this conversation as resolved.
Show resolved Hide resolved
/**
* Output connection: The constant value.
*/
public readonly output: FlowGraphDataConnection<T>;

constructor(private _config: IFlowGraphConstantBlockConfiguration<T>) {
super();

this.output = this._registerDataOutput("output", getRichTypeFromValue(_config.value));
}

public _updateOutputs(_context: FlowGraphContext): void {
this.output.value = this._config.value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { FlowGraphBlock } from "../../flowGraphBlock";
import type { FlowGraphContext } from "../../flowGraphContext";
import type { FlowGraphDataConnection } from "../../flowGraphDataConnection";
import { RichTypeAny, RichTypeVector3 } from "../../flowGraphRichTypes";
import { TmpVectors, Vector3 } from "../../../Maths/math.vector";
import type { TransformNode } from "../../../Meshes/transformNode";

/**
* @experimental
* This blocks transforms a vector from one coordinate system to another.
*/
export class FlowGraphCoordinateTransformBlock extends FlowGraphBlock {
/**
* Input connection: The source coordinate system.
*/
public readonly sourceSystem: FlowGraphDataConnection<TransformNode>;
carolhmj marked this conversation as resolved.
Show resolved Hide resolved
/**
* Input connection: The destination coordinate system.
*/
public readonly destinationSystem: FlowGraphDataConnection<TransformNode>;
carolhmj marked this conversation as resolved.
Show resolved Hide resolved
/**
* Input connection: The coordinates to transform.
*/
public readonly inputCoordinates: FlowGraphDataConnection<Vector3>;
/**
* Output connection: The transformed coordinates.
*/
public readonly outputCoordinates: FlowGraphDataConnection<Vector3>;

/**
* Creates a new FlowGraphCoordinateTransformBlock
*/
constructor() {
super();

this.sourceSystem = this._registerDataInput("sourceSystem", RichTypeAny);
this.destinationSystem = this._registerDataInput("destinationSystem", RichTypeAny);
this.inputCoordinates = this._registerDataInput("inputCoordinates", RichTypeVector3);
this.outputCoordinates = this._registerDataOutput("outputCoordinates", RichTypeVector3);
}

public _updateOutputs(_context: FlowGraphContext): void {
const sourceSystemValue = this.sourceSystem.getValue(_context);
const destinationSystemValue = this.destinationSystem.getValue(_context);
const inputCoordinatesValue = this.inputCoordinates.getValue(_context);

// takes coordinates from source space to world space
const sourceWorld = sourceSystemValue.getWorldMatrix();
// takes coordinates from destination space to world space
const destinationWorld = destinationSystemValue.getWorldMatrix();
const destinationWorldInverse = TmpVectors.Matrix[0].copyFrom(destinationWorld);
// takes coordinates from world space to destination space
destinationWorldInverse.invert();

const sourceToDestination = TmpVectors.Matrix[1];
// takes coordinates from source space to world space to destination space
destinationWorldInverse.multiplyToRef(sourceWorld, sourceToDestination);
const outputCoordinatesValue = Vector3.TransformCoordinates(inputCoordinatesValue, sourceToDestination);
carolhmj marked this conversation as resolved.
Show resolved Hide resolved

this.outputCoordinates.value = outputCoordinatesValue;
}
}
2 changes: 2 additions & 0 deletions packages/dev/core/src/FlowGraph/Blocks/Data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export * from "./Math/flowGraphNumberMathBlocks";
export * from "./Math/flowGraphVector3MathBlocks";
export * from "./Math/flowGraphVector2MathBlocks";
export * from "./Math/flowGraphVector4MathBlocks";
export * from "./flowGraphCoordinateTransformBlock";
export * from "./flowGraphConstantBlock";
// eslint-disable-next-line import/no-internal-modules
export * from "./Logic/index";
32 changes: 32 additions & 0 deletions packages/dev/core/src/FlowGraph/flowGraphRichTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,35 @@ export const RichTypeColor4: RichType<Color4> = {
typeName: "Color4",
defaultValueBuilder: () => new Color4(0, 0, 0, 0),
};

/**
* Given a value, try to deduce its rich type.
* @param value the value to deduce the rich type from
* @returns the value's rich type, or RichTypeAny if the type could not be deduced.
*/
export function getRichTypeFromValue<T>(value: T): RichType<T> {
carolhmj marked this conversation as resolved.
Show resolved Hide resolved
switch (typeof value) {
case "string":
return RichTypeString as RichType<T>;
case "number":
return RichTypeNumber as RichType<T>;
case "boolean":
return RichTypeBoolean as RichType<T>;
case "object":
if (value instanceof Vector2) {
return RichTypeVector2 as RichType<T>;
} else if (value instanceof Vector3) {
return RichTypeVector3 as RichType<T>;
} else if (value instanceof Vector4) {
return RichTypeVector4 as RichType<T>;
} else if (value instanceof Color3) {
return RichTypeColor3 as RichType<T>;
} else if (value instanceof Color4) {
return RichTypeColor4 as RichType<T>;
} else {
return RichTypeAny as RichType<T>;
}
default:
return RichTypeAny as RichType<T>;
}
}