Skip to content

Commit 56fc471

Browse files
authored
Move flow graph math functions into flow graph for now (#17445)
1 parent 4d7a98f commit 56fc471

File tree

4 files changed

+48
-41
lines changed

4 files changed

+48
-41
lines changed

packages/dev/core/src/FlowGraph/Blocks/Data/Math/flowGraphVectorMathBlocks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import type { FlowGraphMatrix, FlowGraphVector } from "core/FlowGraph/utils";
2121
import { _GetClassNameOf } from "core/FlowGraph/utils";
2222
import type { FlowGraphDataConnection } from "../../../flowGraphDataConnection";
2323
import type { FlowGraphContext } from "../../../flowGraphContext";
24-
import { GetAngleBetweenQuaternions, GetQuaternionFromDirections } from "../../../../Maths/math.vector.functions";
2524
import type { Nullable } from "../../../../types";
25+
import { GetAngleBetweenQuaternions, GetQuaternionFromDirections } from "core/FlowGraph/flowGraphMath";
2626

2727
const AxisCacheName = "cachedOperationAxis";
2828
const AngleCacheName = "cachedOperationAngle";
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { IQuaternionLike } from "../Maths/math.like";
2+
import { Clamp } from "../Maths/math.scalar.functions";
3+
import { Quaternion, Vector3 } from "../Maths/math.vector";
4+
import { Vector3Dot, Vector4Dot } from "../Maths/math.vector.functions";
5+
import type { DeepImmutable } from "../types";
6+
7+
// *** NOTE ***
8+
// These functions should ideally go in math.vector.functions.ts, but they require math.vector.ts to
9+
// be imported which is big. To avoid the larger bundle size, they are kept inside flow graph for now.
10+
11+
/**
12+
* Returns the angle in radians between two quaternions
13+
* @param q1 defines the first quaternion
14+
* @param q2 defines the second quaternion
15+
* @returns the angle in radians between the two quaternions
16+
*/
17+
export function GetAngleBetweenQuaternions(q1: DeepImmutable<IQuaternionLike>, q2: DeepImmutable<IQuaternionLike>): number {
18+
return Math.acos(Clamp(Vector4Dot(q1, q2), -1, 1)) * 2;
19+
}
20+
21+
/**
22+
* Creates a quaternion from two direction vectors
23+
* @param a defines the first direction vector
24+
* @param b defines the second direction vector
25+
* @returns the target quaternion
26+
*/
27+
export function GetQuaternionFromDirections<T extends Vector3>(a: DeepImmutable<T>, b: DeepImmutable<T>): Quaternion {
28+
const result = new Quaternion();
29+
GetQuaternionFromDirectionsToRef(a, b, result);
30+
return result;
31+
}
32+
33+
/**
34+
* Creates a quaternion from two direction vectors
35+
* @param a defines the first direction vector
36+
* @param b defines the second direction vector
37+
* @param result defines the target quaternion
38+
* @returns the target quaternion
39+
*/
40+
export function GetQuaternionFromDirectionsToRef<T extends Vector3, ResultT extends Quaternion>(a: DeepImmutable<T>, b: DeepImmutable<T>, result: ResultT): ResultT {
41+
const axis = Vector3.Cross(a, b);
42+
const angle = Math.acos(Clamp(Vector3Dot(a, b), -1, 1));
43+
Quaternion.RotationAxisToRef(axis, angle, result);
44+
return result;
45+
}

packages/dev/core/src/Maths/math.vector.functions.ts

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { Clamp } from "./math.scalar.functions";
21
import type { DeepImmutable } from "../types";
3-
import type { IQuaternionLike, IVector2Like, IVector3Like, IVector4Like } from "./math.like";
4-
import { Quaternion, Vector3 } from "./math.vector";
2+
import type { IVector2Like, IVector3Like, IVector4Like } from "./math.like";
53

64
/**
75
* Creates a string representation of the IVector2Like
@@ -170,39 +168,3 @@ export function Vector4Dot(a: DeepImmutable<IVector4Like>, b: DeepImmutable<IVec
170168
export function Vector4ToFixed(vector: DeepImmutable<IVector4Like>, decimalCount: number): string {
171169
return `{X: ${vector.x.toFixed(decimalCount)} Y: ${vector.y.toFixed(decimalCount)} Z: ${vector.z.toFixed(decimalCount)} W: ${vector.w.toFixed(decimalCount)}}`;
172170
}
173-
174-
/**
175-
* Returns the angle in radians between two quaternions
176-
* @param q1 defines the first quaternion
177-
* @param q2 defines the second quaternion
178-
* @returns the angle in radians between the two quaternions
179-
*/
180-
export function GetAngleBetweenQuaternions(q1: DeepImmutable<IQuaternionLike>, q2: DeepImmutable<IQuaternionLike>): number {
181-
return Math.acos(Clamp(Vector4Dot(q1, q2))) * 2;
182-
}
183-
184-
/**
185-
* Creates a quaternion from two direction vectors
186-
* @param a defines the first direction vector
187-
* @param b defines the second direction vector
188-
* @returns the target quaternion
189-
*/
190-
export function GetQuaternionFromDirections<T extends Vector3>(a: DeepImmutable<T>, b: DeepImmutable<T>): Quaternion {
191-
const result = new Quaternion();
192-
GetQuaternionFromDirectionsToRef(a, b, result);
193-
return result;
194-
}
195-
196-
/**
197-
* Creates a quaternion from two direction vectors
198-
* @param a defines the first direction vector
199-
* @param b defines the second direction vector
200-
* @param result defines the target quaternion
201-
* @returns the target quaternion
202-
*/
203-
export function GetQuaternionFromDirectionsToRef<T extends Vector3, ResultT extends Quaternion>(a: DeepImmutable<T>, b: DeepImmutable<T>, result: ResultT): ResultT {
204-
const axis = Vector3.Cross(a, b);
205-
const angle = Math.acos(Clamp(Vector3Dot(a, b), -1, 1));
206-
Quaternion.RotationAxisToRef(axis, angle, result);
207-
return result;
208-
}

packages/dev/loaders/test/unit/Interactivity/interactivity.math nodes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { NullEngine } from "core/Engines/nullEngine";
44
import { PerformanceConfigurator } from "core/Engines/performanceConfigurator";
55
import { FlowGraphCoordinator } from "core/FlowGraph/flowGraphCoordinator";
66
import { FlowGraphAction } from "core/FlowGraph/flowGraphLogger";
7+
import { GetAngleBetweenQuaternions, GetQuaternionFromDirections } from "core/FlowGraph/flowGraphMath";
78
import { ParseFlowGraphAsync } from "core/FlowGraph/flowGraphParser";
89
import { Quaternion, Vector3 } from "core/Maths/math.vector";
910
import { Logger } from "core/Misc/logger";
1011
import { Scene } from "core/scene";
1112
import { InteractivityGraphToFlowGraphParser } from "loaders/glTF/2.0/Extensions/KHR_interactivity/interactivityGraphParser";
1213
import { GetPathToObjectConverter } from "loaders/glTF/2.0/Extensions/objectModelMapping";
13-
import { GetAngleBetweenQuaternions, GetQuaternionFromDirections } from "../../../../core/src/Maths/math.vector.functions";
1414

1515
const typesAndLengths: {
1616
[key: string]: number;

0 commit comments

Comments
 (0)