Skip to content

Commit

Permalink
Tensor: Standardize Vector2, Vector3, Vector4, Color3, Color4, Quater…
Browse files Browse the repository at this point in the history
…nion, and Matrix (#14235)

* Added new types

* Added Member, Flatten, and tensor

* Updated classes in vector.ts to use Constructor

* Removed TensorValue and fixed comments

* Fixed wikipedia link

* Added Tensor to vector and color classes
Added FlattenTuple

* Added IsTuple types
Fixed Flatten
Added comment for Vector
Formatting

* Added public to Tensor and Vector members

* Fixed types being or not being DeepImmutable

* Formatting

* Added Tuple

* Updated arrayTools to use new Tuple type

* Updated to use object.defineProperty for dimension

* Fixed dimension values

* Added Dimension
Updated MultidimensionalArray to correctly handle nesting

* Fixed Dimension import

* Fixed Tensor not working with non `number[]` types
Added RecursiveArray

* Fixed equalsToFloats not using Flatten

* Made Matrix Tensor-compatible

* Updated Quaternion to be Tensor compatible

* Renamed I<Math>Like to <Math>Like

* Moved <Math>Like to correct files
Fixed MatrixLike having toArray instead of asArray

* Fixed duplicate function implementation

* Changed dimension to be Readonly
Fixed non-type import of interface

* Fixed mispelling

* Updated Vector3.asArray and Matrix.asArray

* Updated test Playground

* Changed default Tensor value type to unknown[]

* Added TensorValue

* Fixed stuff from merge

* Fixed JSDoc comment parameter order

* Added Tensor.rank

* Fixed typo

* Formatting

* Formatting

* Made Matrix.asArray zero-copy

* Moved from defineProperty to defineProperties

* Reverted *Like changes

* Fixed JSDoc comments

* Formatting

* Formatting (clean install)

* Fixed eslint warnings

* Reverted IViewportLike

* Reverted flloat -> number change in math.like

* Fixed imports

* Fixed more imports

* Reverted more organization changes

* Updated directionalLightFrustumViewer._createGeometry vectors

* Added Matrix.toArray backwards compatibility

* Fixed RecursiveArray spelling

Co-authored-by: Popov72 <[email protected]>

* Fixed Tensor description

Co-authored-by: Popov72 <[email protected]>

* Fixed Vector description

Co-authored-by: Popov72 <[email protected]>

* Comment overhaul
Fixed type spelling
Reverted changes to Vector3.minimizeInPlaceFromFloats and Vector3.maximizeInPlaceFromFloats

* Changed m.length to 16

* Removed some method implementations from Color3 and Color4
Fixed Tensor.divideToRef doc comment

* Added internal to doc comment for color methods

* Removed methods from Quaternion

* Fixed Vector not implementing Vector

* made all Tensor instance methods abstract

* Added abstract to Vector

* Removed unused types

* Removed Tensor.toString

* Seperated instance and static of Tensor and Vector into interfaces
Fixed some type paramters

* Added TensorStatic.prototype

* Removed type parameters in Tensor and Vector methods
Added Vector2 and Vector3 RandomToRef
Removed DistanceOfPointFromSegment from Vector
Added static side checks to Vector2 and Vector3

* Added Vector4 static side

* Added static side to Quaternion
Moved Clamp and ClampToRef from Vector to Tensor

* Formatting

---------

Co-authored-by: Popov72 <[email protected]>
  • Loading branch information
james-pre and Popov72 authored Mar 5, 2024
1 parent 88a438a commit 9258609
Show file tree
Hide file tree
Showing 17 changed files with 2,562 additions and 387 deletions.
4 changes: 2 additions & 2 deletions packages/dev/core/src/Bones/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,8 @@ export class Skeleton implements IAnimatable {
index: bone.getIndex(),
name: bone.name,
id: bone.id,
matrix: bone.getBindMatrix().toArray(),
rest: bone.getRestMatrix().toArray(),
matrix: bone.getBindMatrix().asArray(),
rest: bone.getRestMatrix().asArray(),
linkedTransformNodeId: bone.getTransformNode()?.id,
};

Expand Down
6 changes: 3 additions & 3 deletions packages/dev/core/src/Cameras/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { serialize, serializeAsVector3 } from "../Misc/decorators";
import { SmartArray } from "../Misc/smartArray";
import { Tools } from "../Misc/tools";
import { Observable } from "../Misc/observable";
import type { Nullable } from "../types";
import type { DeepImmutable, Nullable } from "../types";
import type { CameraInputsManager } from "./cameraInputsManager";
import type { Scene } from "../scene";
import { Matrix, Vector3, Quaternion } from "../Maths/math.vector";
Expand Down Expand Up @@ -1419,7 +1419,7 @@ export class Camera extends Node {
* @param localAxis Defines the reference axis to provide a relative direction.
* @returns the direction
*/
public getDirection(localAxis: Vector3): Vector3 {
public getDirection(localAxis: DeepImmutable<Vector3>): Vector3 {
const result = Vector3.Zero();

this.getDirectionToRef(localAxis, result);
Expand All @@ -1441,7 +1441,7 @@ export class Camera extends Node {
* @param localAxis Defines the reference axis to provide a relative direction.
* @param result Defines the vector to store the result in
*/
public getDirectionToRef(localAxis: Vector3, result: Vector3): void {
public getDirectionToRef(localAxis: DeepImmutable<Vector3>, result: Vector3): void {
Vector3.TransformNormalToRef(localAxis, this.getWorldMatrix(), result);
}

Expand Down
12 changes: 6 additions & 6 deletions packages/dev/core/src/Debug/directionalLightFrustumViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,32 +247,32 @@ export class DirectionalLightFrustumViewer {
this._rootNode = new TransformNode("directionalLightHelperRoot_" + this._light.name, this._scene);
this._rootNode.parent = this._light.parent;

this._nearLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
this._nearLinesPoints = [TmpVectors.Vector3[0], TmpVectors.Vector3[1], TmpVectors.Vector3[2], TmpVectors.Vector3[3], TmpVectors.Vector3[4]];
const nearLines = CreateLines("nearlines", { updatable: true, points: this._nearLinesPoints }, this._scene);
nearLines.parent = this._rootNode;
nearLines.alwaysSelectAsActiveMesh = true;

this._farLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
this._farLinesPoints = [TmpVectors.Vector3[5], TmpVectors.Vector3[6], TmpVectors.Vector3[7], TmpVectors.Vector3[8], TmpVectors.Vector3[9]];
const farLines = CreateLines("farlines", { updatable: true, points: this._farLinesPoints }, this._scene);
farLines.parent = this._rootNode;
farLines.alwaysSelectAsActiveMesh = true;

this._trLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
this._trLinesPoints = [TmpVectors.Vector3[10], TmpVectors.Vector3[11]];
const trLines = CreateLines("trlines", { updatable: true, points: this._trLinesPoints }, this._scene);
trLines.parent = this._rootNode;
trLines.alwaysSelectAsActiveMesh = true;

this._brLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
this._brLinesPoints = [TmpVectors.Vector3[12], TmpVectors.Vector3[0]];
const brLines = CreateLines("brlines", { updatable: true, points: this._brLinesPoints }, this._scene);
brLines.parent = this._rootNode;
brLines.alwaysSelectAsActiveMesh = true;

this._tlLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
this._tlLinesPoints = [TmpVectors.Vector3[1], TmpVectors.Vector3[2]];
const tlLines = CreateLines("tllines", { updatable: true, points: this._tlLinesPoints }, this._scene);
tlLines.parent = this._rootNode;
tlLines.alwaysSelectAsActiveMesh = true;

this._blLinesPoints = [Vector3.ZeroReadOnly, Vector3.ZeroReadOnly];
this._blLinesPoints = [TmpVectors.Vector3[3], TmpVectors.Vector3[4]];
const blLines = CreateLines("bllines", { updatable: true, points: this._blLinesPoints }, this._scene);
blLines.parent = this._rootNode;
blLines.alwaysSelectAsActiveMesh = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ export class NativePipelineContext implements IPipelineContext {
*/
public setMatrix(uniformName: string, matrix: IMatrixLike): void {
if (this._cacheMatrix(uniformName, matrix)) {
if (!this._engine.setMatrices(this._uniforms[uniformName]!, matrix.toArray() as Float32Array)) {
if (!this._engine.setMatrices(this._uniforms[uniformName]!, matrix.asArray() as Float32Array)) {
this._valueCache[uniformName] = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ export class WebGLPipelineContext implements IPipelineContext {
*/
public setMatrix(uniformName: string, matrix: IMatrixLike): void {
if (this._cacheMatrix(uniformName, matrix)) {
if (!this.engine.setMatrices(this._uniforms[uniformName], matrix.toArray() as Float32Array)) {
if (!this.engine.setMatrices(this._uniforms[uniformName], matrix.asArray() as Float32Array)) {
this._valueCache[uniformName] = null;
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/dev/core/src/Materials/uniformBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import type { DataBuffer } from "../Buffers/dataBuffer";
import type { ThinEngine } from "../Engines/thinEngine";
import type { InternalTexture } from "./Textures/internalTexture";
import { Tools } from "../Misc/tools";

import "../Engines/Extensions/engine.uniformBuffer";

/**
Expand Down Expand Up @@ -466,7 +465,7 @@ export class UniformBuffer {
* @param mat A 4x4 matrix.
*/
public addMatrix(name: string, mat: IMatrixLike) {
this.addUniform(name, Array.prototype.slice.call(mat.toArray()));
this.addUniform(name, Array.prototype.slice.call(mat.asArray()));
}

/**
Expand Down Expand Up @@ -941,7 +940,7 @@ export class UniformBuffer {

private _updateMatrixForUniform(name: string, mat: IMatrixLike) {
if (this._cacheMatrix(name, mat)) {
this.updateUniform(name, <any>mat.toArray(), 16);
this.updateUniform(name, <any>mat.asArray(), 16);
}
}

Expand Down
Loading

0 comments on commit 9258609

Please sign in to comment.