Skip to content

Commit efdc489

Browse files
committed
Removed global scene graph. New system works with camera system
1 parent 08da842 commit efdc489

13 files changed

Lines changed: 323 additions & 185 deletions

File tree

v3/src/camera/Camera.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var Transform = require('../components/Transform');
1+
var Transform2DMatrix = require('../components/Transform2DMatrix').Transform2DMatrix;
22

33
var Camera = function (x, y, width, height)
44
{
@@ -7,13 +7,11 @@ var Camera = function (x, y, width, height)
77
this.width = width;
88
this.height = height;
99

10-
this.state = null;
11-
this.statePositionX = 0.0;
12-
this.statePositionY = 0.0;
1310
this.scrollX = 0.0;
1411
this.scrollY = 0.0;
1512
this.zoom = 1.0;
1613
this.rotation = 0.0;
14+
this.matrix = new Transform2DMatrix(1, 0, 0, 1, 0, 0);
1715

1816
// shake
1917
this._shakeDuration = 0.0;
@@ -165,24 +163,10 @@ Camera.prototype = {
165163

166164
preRender: function ()
167165
{
168-
var state = this.state;
169-
var stateTransform = state.sys.transform;
170-
171-
this.statePositionX = stateTransform.positionX;
172-
this.statePositionY = stateTransform.positionY;
173-
174-
stateTransform.positionX = this.statePositionX + this.x;
175-
stateTransform.positionY = this.statePositionY + this.y;
176-
177-
Transform.updateRoot(stateTransform, -this.scrollX + this._shakeOffsetX, -this.scrollY + this._shakeOffsetY, this.zoom, this.rotation);
178-
},
179-
180-
postRender: function ()
181-
{
182-
var stateTransform = this.state.sys.transform;
183-
184-
stateTransform.positionX = this.statePositionX;
185-
stateTransform.positionY = this.statePositionY;
166+
this.matrix.loadIdentity().
167+
translate(this.x + this._shakeOffsetX, this.y + this._shakeOffsetY).
168+
rotate(this.rotation).
169+
scale(this.zoom, this.zoom);
186170
},
187171

188172
destroy: function ()

v3/src/components/Transform.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,4 @@ Object.defineProperties(Transform.prototype,{
262262
}
263263
});
264264

265-
module.exports = Transform;
265+
//module.exports = Transform;
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
var mathCos = Math.cos;
2+
var mathSin = Math.sin;
3+
var mathSqrt = Math.sqrt;
4+
var mathAcos = Math.acos;
5+
var mathAtan = Math.atan;
6+
7+
var Transform2DMatrix = function (a, b, c, d, tx, ty)
8+
{
9+
a = typeof a === 'number' ? a : 1;
10+
b = typeof b === 'number' ? b : 0;
11+
c = typeof c === 'number' ? c : 0;
12+
d = typeof d === 'number' ? d : 1;
13+
tx = typeof tx === 'number' ? tx : 0;
14+
ty = typeof ty === 'number' ? ty : 0;
15+
16+
this.matrix = new Float32Array([a, b, c, d, tx, ty]);
17+
this.decomposedMatrix = {
18+
translateX: 0,
19+
translateY: 0,
20+
scaleX: 1,
21+
scaleY: 1,
22+
rotation: 0
23+
};
24+
};
25+
26+
Transform2DMatrix.prototype.loadIdentity = function ()
27+
{
28+
var matrix = this.matrix;
29+
30+
matrix[0] = 1;
31+
matrix[1] = 0;
32+
matrix[2] = 0;
33+
matrix[3] = 1;
34+
matrix[4] = 0;
35+
matrix[5] = 0;
36+
37+
return this;
38+
};
39+
40+
Transform2DMatrix.prototype.translate = function (x, y)
41+
{
42+
var matrix = this.matrix;
43+
44+
matrix[4] = matrix[0] * x + matrix[2] * y + matrix[4];
45+
matrix[5] = matrix[1] * x + matrix[3] * y + matrix[5];
46+
47+
return this;
48+
};
49+
50+
Transform2DMatrix.prototype.scale = function (x, y)
51+
{
52+
var matrix = this.matrix;
53+
54+
matrix[0] = matrix[0] * x;
55+
matrix[1] = matrix[1] * x;
56+
matrix[2] = matrix[2] * y;
57+
matrix[3] = matrix[3] * y;
58+
59+
return this;
60+
};
61+
62+
Transform2DMatrix.prototype.rotate = function (radian)
63+
{
64+
var matrix = this.matrix;
65+
var a = matrix[0];
66+
var b = matrix[1];
67+
var c = matrix[2];
68+
var d = matrix[3];
69+
var sr = mathSin(radian);
70+
var cr = mathCos(radian);
71+
72+
matrix[0] = a * cr + c * sr;
73+
matrix[1] = b * cr + d * sr;
74+
matrix[2] = a * -sr + c * cr;
75+
matrix[3] = b * -sr + d * cr;
76+
77+
return this;
78+
};
79+
80+
Transform2DMatrix.prototype.multiply = function (otherMatrix)
81+
{
82+
var matrix = this.matrix;
83+
var a0 = matrix[0];
84+
var b0 = matrix[1];
85+
var c0 = matrix[2];
86+
var d0 = matrix[3];
87+
var tx0 = matrix[4];
88+
var ty0 = matrix[5];
89+
var a1 = otherMatrix[0];
90+
var b1 = otherMatrix[1];
91+
var c1 = otherMatrix[2];
92+
var d1 = otherMatrix[3];
93+
var tx1 = otherMatrix[4];
94+
var ty1 = otherMatrix[5];
95+
96+
matrix[0] = a1 * a0 + b1 * c0;
97+
matrix[1] = a1 * b0 + b1 * d0;
98+
matrix[2] = c1 * a0 + d1 * c0;
99+
matrix[3] = c1 * b0 + d1 * d0;
100+
matrix[4] = tx1 * a0 + ty1 * c0 + tx0;
101+
matrix[5] = tx1 * b0 + ty1 * d0 + ty0;
102+
103+
return this;
104+
};
105+
106+
Transform2DMatrix.prototype.transform = function (a, b, c, d, tx, ty)
107+
{
108+
var matrix = this.matrix;
109+
var a0 = matrix[0];
110+
var b0 = matrix[1];
111+
var c0 = matrix[2];
112+
var d0 = matrix[3];
113+
var tx0 = matrix[4];
114+
var ty0 = matrix[5];
115+
116+
matrix[0] = a * a0 + b * c0;
117+
matrix[1] = a * b0 + b * d0;
118+
matrix[2] = c * a0 + d * c0;
119+
matrix[3] = c * b0 + d * d0;
120+
matrix[4] = tx * a0 + ty * c0 + tx0;
121+
matrix[5] = tx * b0 + ty * d0 + ty0;
122+
123+
return this;
124+
};
125+
126+
Transform2DMatrix.prototype.setTransform = function (a, b, c, d, tx, ty)
127+
{
128+
var matrix = this.matrix;
129+
130+
matrix[0] = a;
131+
matrix[1] = b;
132+
matrix[2] = c;
133+
matrix[3] = d;
134+
matrix[4] = tx;
135+
matrix[5] = ty;
136+
137+
return this;
138+
};
139+
140+
Transform2DMatrix.prototype.decomposeMatrix = function ()
141+
{
142+
var decomposedMatrix = this.decomposedMatrix;
143+
var matrix = this.matrix;
144+
var a = matrix[0];
145+
var b = matrix[1];
146+
var c = matrix[2];
147+
var d = matrix[3];
148+
var a2 = a * a;
149+
var b2 = b * b;
150+
var c2 = c * c;
151+
var d2 = d * d;
152+
var sx = mathSqrt(a2 + c2);
153+
var sy = mathSqrt(b2 + d2);
154+
155+
decomposedMatrix.translateX = matrix[4];
156+
decomposedMatrix.translateY = matrix[5];
157+
decomposedMatrix.scaleX = sx;
158+
decomposedMatrix.scaleY = sy;
159+
decomposedMatrix.rotation = mathAcos(a / sx) * (mathAtan(-c / a) < 0 ? -1 : 1);
160+
161+
return decomposedMatrix;
162+
};
163+
164+
Transform2DMatrix.prototype.fromTransform2D = function (transform2D)
165+
{
166+
this.loadIdentity().
167+
translate(transform2D.x, transform2D.y).
168+
rotate(transform2D.angle).
169+
scale(transform2D.scaleX, transform2D.scaleY);
170+
171+
return this;
172+
};
173+
174+
var Transform2D = function (x, y)
175+
{
176+
this.x = x;
177+
this.y = y;
178+
this.scaleX = 1;
179+
this.scaleY = 1;
180+
this.angle = 0;
181+
};
182+
183+
module.exports = {
184+
Transform2DMatrix: Transform2DMatrix,
185+
Transform2D: Transform2D
186+
};

v3/src/components/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module.exports = {
55
Children: require('./Children'),
66
Color: require('./Color'),
77
Data: require('./Data'),
8-
Transform: require('./Transform')
8+
//Transform: require('./Transform')
9+
Transform2D: require('./Transform2DMatrix').Transform2D,
10+
Transform2DMatrix: require('./Transform2DMatrix').Transform2DMatrix
911

1012
};

v3/src/device/Features.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function init ()
171171
return false;
172172
};
173173

174-
Features.webGL = testWebGL();
174+
Features.webGL = true;//testWebGL();
175175

176176
Features.worker = !!window['Worker'];
177177

v3/src/gameobjects/GameObject.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ var GameObject = function (state, x, y, texture, frame, parent)
3939
this.frame = frame;
4040

4141
// All GameObjects have the following components, always:
42-
this.transform = new Component.Transform(this, this.state.sys.transform);
43-
this.transform.positionX = x;
44-
this.transform.positionY = y;
42+
this.transform = new Component.Transform2D(x, y);
4543

4644
this.anchor = new Component.Anchor();
4745

@@ -112,13 +110,12 @@ Object.defineProperties(GameObject.prototype, {
112110

113111
get: function ()
114112
{
115-
return this.transform.positionX;
113+
return this.transform.x;
116114
},
117115

118116
set: function (value)
119117
{
120-
this.transform.positionX = value;
121-
this.transform.dirtyLocal = true;
118+
this.transform.x = value;
122119
}
123120

124121
},
@@ -129,13 +126,12 @@ Object.defineProperties(GameObject.prototype, {
129126

130127
get: function ()
131128
{
132-
return this.transform.positionY;
129+
return this.transform.y;
133130
},
134131

135132
set: function (value)
136133
{
137-
this.transform.positionY = value;
138-
this.transform.dirtyLocal = true;
134+
this.transform.y = value;
139135
}
140136

141137
},
@@ -153,7 +149,6 @@ Object.defineProperties(GameObject.prototype, {
153149
{
154150
this.transform.scaleX = value;
155151
this.transform.scaleY = value;
156-
this.transform.dirtyLocal = true;
157152
}
158153

159154
},
@@ -170,7 +165,6 @@ Object.defineProperties(GameObject.prototype, {
170165
set: function (value)
171166
{
172167
this.transform.scaleX = value;
173-
this.transform.dirtyLocal = true;
174168
}
175169

176170
},
@@ -187,7 +181,6 @@ Object.defineProperties(GameObject.prototype, {
187181
set: function (value)
188182
{
189183
this.transform.scaleY = value;
190-
this.transform.dirtyLocal = true;
191184
}
192185

193186
},
@@ -198,13 +191,12 @@ Object.defineProperties(GameObject.prototype, {
198191

199192
get: function ()
200193
{
201-
return this.transform.rotation;
194+
return this.transform.angle;
202195
},
203196

204197
set: function (value)
205198
{
206-
this.transform.rotation = value;
207-
this.transform.dirtyLocal = true;
199+
this.transform.angle = value;
208200
}
209201

210202
},
@@ -215,14 +207,13 @@ Object.defineProperties(GameObject.prototype, {
215207

216208
get: function ()
217209
{
218-
return WrapAngle(this.transform.rotation * MATH_CONST.RAD_TO_DEG);
210+
return WrapAngle(this.transform.angle * MATH_CONST.RAD_TO_DEG);
219211
},
220212

221213
set: function (value)
222214
{
223215
// value is in degrees
224-
this.transform.rotation = WrapAngle(value * MATH_CONST.DEG_TO_RAD);
225-
this.transform.dirtyLocal = true;
216+
this.transform.angle = WrapAngle(value * MATH_CONST.DEG_TO_RAD);
226217
}
227218

228219
},

0 commit comments

Comments
 (0)