-
Notifications
You must be signed in to change notification settings - Fork 46
/
transform.ts
90 lines (76 loc) · 2.99 KB
/
transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Transform } from "@pixi/math"
import { Matrix4 } from "./matrix4"
import { ObservablePoint3D } from "./observable-point"
import { ObservableQuaternion } from "./observable-quaternion"
import { Mat4 } from "../math/mat4"
/**
* Handles position, scaling and rotation in 3D.
*/
export class Transform3D extends Transform {
/** The position in local space. */
position = new ObservablePoint3D(this.onChange, this, 0, 0, 0)
/** The scale in local space. */
scale = new ObservablePoint3D(this.onChange, this, 1, 1, 1)
/** The rotation in local space. */
rotationQuaternion = new ObservableQuaternion(this.onChange, this, 0, 0, 0, 1)
/** The transformation matrix in world space. */
worldTransform = new Matrix4()
/** The transformation matrix in local space. */
localTransform = new Matrix4()
/** The inverse transformation matrix in world space. */
inverseWorldTransform = new Matrix4()
/** The normal transformation matrix. */
normalTransform = new Matrix4()
/**
* Updates the local transformation matrix.
*/
updateLocalTransform() {
if (this._localID === this._currentLocalID) {
return
}
this.localTransform.setFromRotationPositionScale(
this.rotationQuaternion, this.position, this.scale)
this._parentID = -1
this._currentLocalID = this._localID
}
/**
* Sets position, rotation and scale from a matrix array.
* @param matrix The matrix to set.
*/
setFromMatrix(matrix: Matrix4) {
this.localTransform.copyFrom(matrix)
this.position.set(this.localTransform.position[0], this.localTransform.position[1], this.localTransform.position[2])
this.scale.set(this.localTransform.scaling[0], this.localTransform.scaling[1], this.localTransform.scaling[2])
this.rotationQuaternion.set(this.localTransform.rotation[0], this.localTransform.rotation[1], this.localTransform.rotation[2], this.localTransform.rotation[3])
}
/**
* Updates the world transformation matrix.
* @param parentTransform The parent transform.
*/
updateTransform(parentTransform?: Transform) {
this.updateLocalTransform()
if (parentTransform && this._parentID === parentTransform._worldID) {
return
}
this.worldTransform.copyFrom(this.localTransform)
if (parentTransform instanceof Transform3D) {
this.worldTransform.multiply(parentTransform.worldTransform)
}
Mat4.invert(this.worldTransform.array, this.inverseWorldTransform.array)
Mat4.transpose(this.inverseWorldTransform.array, this.normalTransform.array)
this._worldID++
if (parentTransform) {
this._parentID = parentTransform._worldID
}
}
/**
* Rotates the transform so the forward vector points at specified point.
* @param point The point to look at.
* @param up The upward direction.
*/
lookAt(point: ObservablePoint3D, up = new Float32Array([0, 1, 0])) {
let rot = Mat4.getRotation(
Mat4.targetTo(point.array, this.worldTransform.position, up))
this.rotationQuaternion.set(rot[0], rot[1], rot[2], rot[3])
}
}