@babylonjs/viewer
Version:
The Babylon Viewer aims to simplify a specific but common Babylon.js use case: loading, viewing, and interacting with a 3D model.
310 lines (304 loc) • 14.3 kB
JavaScript
import { F as FlowGraphBlock, j as _GetClassNameOf } from './KHR_interactivity-CEwUaqxQ.esm.js';
import { f as RichTypeVector3, h as RichTypeQuaternion, d as RichTypeNumber, a as RichTypeBoolean, i as RichTypeMatrix, g as getRichTypeByFlowGraphType, q as RichTypeVector2, R as RichTypeAny } from './declarationMapper-CqO08-WM.esm.js';
import { F as FlowGraphBinaryOperationBlock } from './flowGraphBinaryOperationBlock-D6AJ5o7d.esm.js';
import { F as FlowGraphUnaryOperationBlock } from './flowGraphUnaryOperationBlock-Bewxi-_r.esm.js';
import { aD as Quaternion, j as Clamp, V as Vector3, bc as Vector4, u as RegisterClass } from './index-MZPybX0H.esm.js';
import './objectModelMapping-De5EKNEZ.esm.js';
import './spotLight.pure-CRdZC6Ci.esm.js';
import './flowGraphCachedOperationBlock-oE7khWes.esm.js';
/**
* Creates a string representation of the IVector2Like
* @param vector defines the IVector2Like to stringify
* @param decimalCount defines the number of decimals to use
* @returns a string with the IVector2Like coordinates.
*/
/**
* Computes the dot product of two IVector3Like objects.
* @param a defines the first vector
* @param b defines the second vector
* @returns the dot product
*/
function Vector3Dot(a, b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
/**
* Computes the dot product of two IVector4Like objects
* @param a defines the first vector
* @param b defines the second vector
* @returns the dot product
*/
function Vector4Dot(a, b) {
return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
}
// *** NOTE ***
// These functions should ideally go in math.vector.functions.ts, but they require math.vector.ts to
// be imported which is big. To avoid the larger bundle size, they are kept inside flow graph for now.
/**
* Returns the angle in radians between two quaternions
* @param q1 defines the first quaternion
* @param q2 defines the second quaternion
* @returns the angle in radians between the two quaternions
*/
function GetAngleBetweenQuaternions(q1, q2) {
return Math.acos(Clamp(Vector4Dot(q1, q2), -1, 1)) * 2;
}
/**
* Creates a quaternion from two direction vectors
* @param a defines the first direction vector
* @param b defines the second direction vector
* @returns the target quaternion
*/
function GetQuaternionFromDirections(a, b) {
const result = new Quaternion();
GetQuaternionFromDirectionsToRef(a, b, result);
return result;
}
/**
* Creates a quaternion from two direction vectors
* @param a defines the first direction vector
* @param b defines the second direction vector
* @param result defines the target quaternion
* @returns the target quaternion
*/
function GetQuaternionFromDirectionsToRef(a, b, result) {
const axis = Vector3.Cross(a, b);
const angle = Math.acos(Clamp(Vector3Dot(a, b), -1, 1));
Quaternion.RotationAxisToRef(axis, angle, result);
return result;
}
/** This file must only contain pure code and pure imports */
const AxisCacheName = "cachedOperationAxis";
const AngleCacheName = "cachedOperationAngle";
const CacheExecIdName = "cachedExecutionId";
/**
* Vector length block.
*/
class FlowGraphLengthBlock extends FlowGraphUnaryOperationBlock {
constructor(config) {
super(RichTypeAny, RichTypeNumber, (a) => this._polymorphicLength(a), "FlowGraphLengthBlock" /* FlowGraphBlockNames.Length */, config);
}
_polymorphicLength(a) {
const aClassName = _GetClassNameOf(a);
switch (aClassName) {
case "Vector2" /* FlowGraphTypes.Vector2 */:
case "Vector3" /* FlowGraphTypes.Vector3 */:
case "Vector4" /* FlowGraphTypes.Vector4 */:
case "Quaternion" /* FlowGraphTypes.Quaternion */:
return a.length();
default:
throw new Error(`Cannot compute length of value ${a}`);
}
}
}
/**
* Vector normalize block.
*/
class FlowGraphNormalizeBlock extends FlowGraphUnaryOperationBlock {
constructor(config) {
super(RichTypeAny, RichTypeAny, (a) => this._polymorphicNormalize(a), "FlowGraphNormalizeBlock" /* FlowGraphBlockNames.Normalize */, config);
}
_polymorphicNormalize(a) {
const aClassName = _GetClassNameOf(a);
let normalized;
switch (aClassName) {
case "Vector2" /* FlowGraphTypes.Vector2 */:
case "Vector3" /* FlowGraphTypes.Vector3 */:
case "Vector4" /* FlowGraphTypes.Vector4 */:
case "Quaternion" /* FlowGraphTypes.Quaternion */:
normalized = a.normalizeToNew();
if (this.config?.nanOnZeroLength) {
const length = a.length();
if (length === 0) {
normalized.setAll(NaN);
}
}
return normalized;
default:
throw new Error(`Cannot normalize value ${a}`);
}
}
}
/**
* Dot product block.
*/
class FlowGraphDotBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
super(RichTypeAny, RichTypeAny, RichTypeNumber, (a, b) => this._polymorphicDot(a, b), "FlowGraphDotBlock" /* FlowGraphBlockNames.Dot */, config);
}
_polymorphicDot(a, b) {
const className = _GetClassNameOf(a);
switch (className) {
case "Vector2" /* FlowGraphTypes.Vector2 */:
case "Vector3" /* FlowGraphTypes.Vector3 */:
case "Vector4" /* FlowGraphTypes.Vector4 */:
case "Quaternion" /* FlowGraphTypes.Quaternion */:
// casting is needed because dot requires both to be the same type
return a.dot(b);
default:
throw new Error(`Cannot get dot product of ${a} and ${b}`);
}
}
}
/**
* Cross product block.
*/
class FlowGraphCrossBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
super(RichTypeVector3, RichTypeVector3, RichTypeVector3, (a, b) => Vector3.Cross(a, b), "FlowGraphCrossBlock" /* FlowGraphBlockNames.Cross */, config);
}
}
/**
* 2D rotation block.
*/
class FlowGraphRotate2DBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
super(RichTypeVector2, RichTypeNumber, RichTypeVector2, (a, b) => a.rotate(b), "FlowGraphRotate2DBlock" /* FlowGraphBlockNames.Rotate2D */, config);
}
}
/**
* 3D rotation block.
*/
class FlowGraphRotate3DBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
super(RichTypeVector3, RichTypeQuaternion, RichTypeVector3, (a, b) => a.applyRotationQuaternion(b), "FlowGraphRotate3DBlock" /* FlowGraphBlockNames.Rotate3D */, config);
}
}
function TransformVector(a, b) {
const className = _GetClassNameOf(a);
switch (className) {
case "Vector2" /* FlowGraphTypes.Vector2 */:
return b.transformVector(a);
case "Vector3" /* FlowGraphTypes.Vector3 */:
return b.transformVector(a);
case "Vector4" /* FlowGraphTypes.Vector4 */:
a = a;
// transform the vector 4 with the matrix here. Vector4.TransformCoordinates transforms a 3D coordinate, not Vector4
return new Vector4(a.x * b.m[0] + a.y * b.m[1] + a.z * b.m[2] + a.w * b.m[3], a.x * b.m[4] + a.y * b.m[5] + a.z * b.m[6] + a.w * b.m[7], a.x * b.m[8] + a.y * b.m[9] + a.z * b.m[10] + a.w * b.m[11], a.x * b.m[12] + a.y * b.m[13] + a.z * b.m[14] + a.w * b.m[15]);
default:
throw new Error(`Cannot transform value ${a}`);
}
}
/**
* Transform a vector3 by a matrix.
*/
class FlowGraphTransformBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
const vectorType = config?.vectorType || "Vector3" /* FlowGraphTypes.Vector3 */;
const matrixType = vectorType === "Vector2" /* FlowGraphTypes.Vector2 */ ? "Matrix2D" /* FlowGraphTypes.Matrix2D */ : vectorType === "Vector3" /* FlowGraphTypes.Vector3 */ ? "Matrix3D" /* FlowGraphTypes.Matrix3D */ : "Matrix" /* FlowGraphTypes.Matrix */;
super(getRichTypeByFlowGraphType(vectorType), getRichTypeByFlowGraphType(matrixType), getRichTypeByFlowGraphType(vectorType), TransformVector, "FlowGraphTransformVectorBlock" /* FlowGraphBlockNames.TransformVector */, config);
}
}
/**
* Transform a vector3 by a matrix.
*/
class FlowGraphTransformCoordinatesBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
super(RichTypeVector3, RichTypeMatrix, RichTypeVector3, (a, b) => Vector3.TransformCoordinates(a, b), "FlowGraphTransformCoordinatesBlock" /* FlowGraphBlockNames.TransformCoordinates */, config);
}
}
/**
* Conjugate the quaternion.
*/
class FlowGraphConjugateBlock extends FlowGraphUnaryOperationBlock {
constructor(config) {
super(RichTypeQuaternion, RichTypeQuaternion, (a) => a.conjugate(), "FlowGraphConjugateBlock" /* FlowGraphBlockNames.Conjugate */, config);
}
}
/**
* Get the angle between two quaternions.
*/
class FlowGraphAngleBetweenBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
super(RichTypeQuaternion, RichTypeQuaternion, RichTypeNumber, (a, b) => GetAngleBetweenQuaternions(a, b), "FlowGraphAngleBetweenBlock" /* FlowGraphBlockNames.AngleBetween */, config);
}
}
/**
* Get the quaternion from an axis and an angle.
*/
class FlowGraphQuaternionFromAxisAngleBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
super(RichTypeVector3, RichTypeNumber, RichTypeQuaternion, (a, b) => Quaternion.RotationAxis(a, b), "FlowGraphQuaternionFromAxisAngleBlock" /* FlowGraphBlockNames.QuaternionFromAxisAngle */, config);
}
}
/**
* Get the axis and angle from a quaternion.
*/
class FlowGraphAxisAngleFromQuaternionBlock extends FlowGraphBlock {
constructor(config) {
super(config);
this.a = this.registerDataInput("a", RichTypeQuaternion);
this.axis = this.registerDataOutput("axis", RichTypeVector3);
this.angle = this.registerDataOutput("angle", RichTypeNumber);
this.isValid = this.registerDataOutput("isValid", RichTypeBoolean);
}
/** @override */
_updateOutputs(context) {
const cachedExecutionId = context._getExecutionVariable(this, CacheExecIdName, -1);
const cachedAxis = context._getExecutionVariable(this, AxisCacheName, null);
const cachedAngle = context._getExecutionVariable(this, AngleCacheName, null);
if (cachedAxis !== undefined && cachedAxis !== null && cachedAngle !== undefined && cachedAngle !== null && cachedExecutionId === context.executionId) {
this.axis.setValue(cachedAxis, context);
this.angle.setValue(cachedAngle, context);
}
else {
try {
const { axis, angle } = this.a.getValue(context).toAxisAngle();
context._setExecutionVariable(this, AxisCacheName, axis);
context._setExecutionVariable(this, AngleCacheName, angle);
context._setExecutionVariable(this, CacheExecIdName, context.executionId);
this.axis.setValue(axis, context);
this.angle.setValue(angle, context);
this.isValid.setValue(true, context);
}
catch (e) {
this.isValid.setValue(false, context);
}
}
}
/**
* Gets the class name
* @override
* @returns the class name
*/
getClassName() {
return "FlowGraphAxisAngleFromQuaternionBlock" /* FlowGraphBlockNames.AxisAngleFromQuaternion */;
}
}
/**
* Get the quaternion from two direction vectors.
*/
class FlowGraphQuaternionFromDirectionsBlock extends FlowGraphBinaryOperationBlock {
constructor(config) {
super(RichTypeVector3, RichTypeVector3, RichTypeQuaternion, (a, b) => GetQuaternionFromDirections(a, b), "FlowGraphQuaternionFromDirectionsBlock" /* FlowGraphBlockNames.QuaternionFromDirections */, config);
}
}
let _Registered = false;
/**
* Register side effects for flowGraphVectorMathBlocks.
* Safe to call multiple times; only the first call has an effect.
*/
function RegisterFlowGraphVectorMathBlocks() {
if (_Registered) {
return;
}
_Registered = true;
RegisterClass("FlowGraphLengthBlock" /* FlowGraphBlockNames.Length */, FlowGraphLengthBlock);
RegisterClass("FlowGraphNormalizeBlock" /* FlowGraphBlockNames.Normalize */, FlowGraphNormalizeBlock);
RegisterClass("FlowGraphDotBlock" /* FlowGraphBlockNames.Dot */, FlowGraphDotBlock);
RegisterClass("FlowGraphCrossBlock" /* FlowGraphBlockNames.Cross */, FlowGraphCrossBlock);
RegisterClass("FlowGraphRotate2DBlock" /* FlowGraphBlockNames.Rotate2D */, FlowGraphRotate2DBlock);
RegisterClass("FlowGraphRotate3DBlock" /* FlowGraphBlockNames.Rotate3D */, FlowGraphRotate3DBlock);
RegisterClass("FlowGraphTransformVectorBlock" /* FlowGraphBlockNames.TransformVector */, FlowGraphTransformBlock);
RegisterClass("FlowGraphTransformCoordinatesBlock" /* FlowGraphBlockNames.TransformCoordinates */, FlowGraphTransformCoordinatesBlock);
RegisterClass("FlowGraphConjugateBlock" /* FlowGraphBlockNames.Conjugate */, FlowGraphConjugateBlock);
RegisterClass("FlowGraphAngleBetweenBlock" /* FlowGraphBlockNames.AngleBetween */, FlowGraphAngleBetweenBlock);
RegisterClass("FlowGraphQuaternionFromAxisAngleBlock" /* FlowGraphBlockNames.QuaternionFromAxisAngle */, FlowGraphQuaternionFromAxisAngleBlock);
RegisterClass("FlowGraphAxisAngleFromQuaternionBlock" /* FlowGraphBlockNames.AxisAngleFromQuaternion */, FlowGraphAxisAngleFromQuaternionBlock);
}
/**
* Re-exports pure implementation and applies runtime side effects.
* Import flowGraphVectorMathBlocks.pure for tree-shakeable, side-effect-free usage.
*/
RegisterFlowGraphVectorMathBlocks();
export { FlowGraphAngleBetweenBlock, FlowGraphAxisAngleFromQuaternionBlock, FlowGraphConjugateBlock, FlowGraphCrossBlock, FlowGraphDotBlock, FlowGraphLengthBlock, FlowGraphNormalizeBlock, FlowGraphQuaternionFromAxisAngleBlock, FlowGraphQuaternionFromDirectionsBlock, FlowGraphRotate2DBlock, FlowGraphRotate3DBlock, FlowGraphTransformBlock, FlowGraphTransformCoordinatesBlock, RegisterFlowGraphVectorMathBlocks };
//# sourceMappingURL=flowGraphVectorMathBlocks-0WbNY_wv.esm.js.map