forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixStack.js
More file actions
282 lines (247 loc) · 7.63 KB
/
MatrixStack.js
File metadata and controls
282 lines (247 loc) · 7.63 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Provides methods used for getting and setting the transform values of a Game Object.
* Should be applied as a mixin and not used directly.
*
* @name Phaser.GameObjects.Components.MatrixStack
* @since 3.2.0
*/
var MatrixStack = {
/**
* [description]
*
* @name Phaser.GameObjects.Components.MatrixStack#matrixStack
* @type {Float32Array}
* @private
* @since 3.2.0
*/
matrixStack: null,
/**
* [description]
*
* @name Phaser.GameObjects.Components.MatrixStack#currentMatrix
* @type {Float32Array}
* @private
* @since 3.2.0
*/
currentMatrix: null,
/**
* [description]
*
* @name Phaser.GameObjects.Components.MatrixStack#currentMatrixIndex
* @type {integer}
* @private
* @since 3.2.0
*/
currentMatrixIndex: 0,
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#initMatrixStack
* @since 3.2.0
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
initMatrixStack: function ()
{
this.matrixStack = new Float32Array(6000); // up to 1000 matrices
this.currentMatrix = new Float32Array([ 1.0, 0.0, 0.0, 1.0, 0.0, 0.0 ]);
this.currentMatrixIndex = 0;
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#save
* @since 3.2.0
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
save: function ()
{
if (this.currentMatrixIndex >= this.matrixStack.length) { return this; }
var matrixStack = this.matrixStack;
var currentMatrix = this.currentMatrix;
var currentMatrixIndex = this.currentMatrixIndex;
this.currentMatrixIndex += 6;
matrixStack[currentMatrixIndex + 0] = currentMatrix[0];
matrixStack[currentMatrixIndex + 1] = currentMatrix[1];
matrixStack[currentMatrixIndex + 2] = currentMatrix[2];
matrixStack[currentMatrixIndex + 3] = currentMatrix[3];
matrixStack[currentMatrixIndex + 4] = currentMatrix[4];
matrixStack[currentMatrixIndex + 5] = currentMatrix[5];
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#restore
* @since 3.2.0
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
restore: function ()
{
if (this.currentMatrixIndex <= 0) { return this; }
this.currentMatrixIndex -= 6;
var matrixStack = this.matrixStack;
var currentMatrix = this.currentMatrix;
var currentMatrixIndex = this.currentMatrixIndex;
currentMatrix[0] = matrixStack[currentMatrixIndex + 0];
currentMatrix[1] = matrixStack[currentMatrixIndex + 1];
currentMatrix[2] = matrixStack[currentMatrixIndex + 2];
currentMatrix[3] = matrixStack[currentMatrixIndex + 3];
currentMatrix[4] = matrixStack[currentMatrixIndex + 4];
currentMatrix[5] = matrixStack[currentMatrixIndex + 5];
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#loadIdentity
* @since 3.2.0
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
loadIdentity: function ()
{
this.setTransform(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#transform
* @since 3.2.0
*
* @param {number} a - [description]
* @param {number} b - [description]
* @param {number} c - [description]
* @param {number} d - [description]
* @param {number} tx - [description]
* @param {number} ty - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
transform: function (a, b, c, d, tx, ty)
{
var currentMatrix = this.currentMatrix;
var m0 = currentMatrix[0];
var m1 = currentMatrix[1];
var m2 = currentMatrix[2];
var m3 = currentMatrix[3];
var m4 = currentMatrix[4];
var m5 = currentMatrix[5];
currentMatrix[0] = m0 * a + m2 * b;
currentMatrix[1] = m1 * a + m3 * b;
currentMatrix[2] = m0 * c + m2 * d;
currentMatrix[3] = m1 * c + m3 * d;
currentMatrix[4] = m0 * tx + m2 * ty + m4;
currentMatrix[5] = m1 * tx + m3 * ty + m5;
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#setTransform
* @since 3.2.0
*
* @param {number} a - [description]
* @param {number} b - [description]
* @param {number} c - [description]
* @param {number} d - [description]
* @param {number} tx - [description]
* @param {number} ty - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
setTransform: function (a, b, c, d, tx, ty)
{
var currentMatrix = this.currentMatrix;
currentMatrix[0] = a;
currentMatrix[1] = b;
currentMatrix[2] = c;
currentMatrix[3] = d;
currentMatrix[4] = tx;
currentMatrix[5] = ty;
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#translate
* @since 3.2.0
*
* @param {number} x - [description]
* @param {number} y - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
translate: function (x, y)
{
var currentMatrix = this.currentMatrix;
var m0 = currentMatrix[0];
var m1 = currentMatrix[1];
var m2 = currentMatrix[2];
var m3 = currentMatrix[3];
var m4 = currentMatrix[4];
var m5 = currentMatrix[5];
currentMatrix[4] = m0 * x + m2 * y + m4;
currentMatrix[5] = m1 * x + m3 * y + m5;
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#scale
* @since 3.2.0
*
* @param {number} x - [description]
* @param {number} y - [description]
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
scale: function (x, y)
{
var currentMatrix = this.currentMatrix;
var m0 = currentMatrix[0];
var m1 = currentMatrix[1];
var m2 = currentMatrix[2];
var m3 = currentMatrix[3];
currentMatrix[0] = m0 * x;
currentMatrix[1] = m1 * x;
currentMatrix[2] = m2 * y;
currentMatrix[3] = m3 * y;
return this;
},
/**
* [description]
*
* @method Phaser.GameObjects.Components.MatrixStack#rotate
* @since 3.2.0
*
* @param {number} t - The angle of rotation, in radians.
*
* @return {Phaser.GameObjects.GameObject} This Game Object instance.
*/
rotate: function (t)
{
var currentMatrix = this.currentMatrix;
var m0 = currentMatrix[0];
var m1 = currentMatrix[1];
var m2 = currentMatrix[2];
var m3 = currentMatrix[3];
var st = Math.sin(t);
var ct = Math.cos(t);
currentMatrix[0] = m0 * ct + m2 * st;
currentMatrix[1] = m1 * ct + m3 * st;
currentMatrix[2] = m0 * -st + m2 * ct;
currentMatrix[3] = m1 * -st + m3 * ct;
return this;
}
};
module.exports = MatrixStack;