forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject3D.js
More file actions
158 lines (115 loc) · 3.44 KB
/
Copy pathGameObject3D.js
File metadata and controls
158 lines (115 loc) · 3.44 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var Euler = require('../math/Euler');
var Matrix4 = require('../math/Matrix4');
var Quaternion = require('../math/Quaternion');
var Vector3 = require('../math/Vector3');
var id = 0;
var tempMatrix = new Matrix4();
var GameObject3D = new Class({
initialize:
function GameObject3D ()
{
this.id = id++;
this.name = '';
this.type = '';
this.position = new Vector3();
this.scale = new Vector3(1, 1, 1);
this.euler = new Euler();
this.quaternion = new Quaternion();
this.matrix = new Matrix4();
this.worldMatrix = new Matrix4();
this.children = [];
this.parent = null;
this.castShadow = false;
this.receiveShadow = false;
this.shadowType = 0;
this.frustumCulled = true;
this.visible = true;
this.renderOrder = 0;
this.matrixAutoupdate = true;
this.matrixNeedsUpdate = true;
this.worldMatrixNeedsUpdate = true;
var euler = this.euler;
var quat = this.quaternion;
euler.onChangeCallback = function ()
{
quat.setFromEuler(euler, false);
};
quat.onChangeCallback = function ()
{
euler.setFromQuaternion(quat);
};
},
// Swap for already created functions
add: function (child)
{
if (child !== this)
{
if (child.parent !== null)
{
child.parent.remove(child);
}
child.parent = this;
this.children.push(child);
child.worldMatrixNeedsUpdate = true;
}
return this;
},
// Swap for already created functions
remove: function (child)
{
var index = this.children.indexOf(child);
if (index > -1)
{
child.parent = null;
this.children.splice(index, 1);
child.worldMatrixNeedsUpdate = true;
}
return this;
},
updateMatrix: function (force)
{
if (this.matrixAutoupdate || this.matrixNeedsUpdate)
{
this.matrix.transform(this.position, this.scale, this.quaternion);
this.matrixNeedsUpdate = false;
this.worldMatrixNeedsUpdate = true;
}
if (this.worldMatrixNeedsUpdate || force)
{
this.worldMatrix.copy(this.matrix);
if (this.parent)
{
var parentMatrix = this.parent.worldMatrix;
this.worldMatrix.premultiply(parentMatrix);
}
this.worldMatrixNeedsUpdate = false;
force = true;
}
var children = this.children;
for (var i = 0; i < children.length; i++)
{
children[i].updateMatrix(force);
}
},
getWorldDirection: function (target)
{
if (target === undefined) { target = new Vector3(); }
var world = this.worldMatrix.val;
return target.set(world[8], world[9], world[10]).normalize();
},
lookAt: function (target, up)
{
tempMatrix.lookAtRH(target, this.position, up);
this.quaternion.setFromRotationMatrix(tempMatrix);
},
destroy: function ()
{
}
});
module.exports = GameObject3D;