Skip to content

Commit 8cefdae

Browse files
committed
Camera broken into components.
1 parent d804e05 commit 8cefdae

24 files changed

Lines changed: 511 additions & 444 deletions

v3/src/camera/Camera.js

Lines changed: 48 additions & 443 deletions
Large diffs are not rendered by default.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
var CameraToScreen = function (pointIn, pointOut)
2+
{
3+
var cameraMatrix = this.matrix.matrix;
4+
var mva = cameraMatrix[0];
5+
var mvb = cameraMatrix[1];
6+
var mvc = cameraMatrix[2];
7+
var mvd = cameraMatrix[3];
8+
var mve = cameraMatrix[4];
9+
var mvf = cameraMatrix[5];
10+
11+
/* First Invert Matrix */
12+
var determinant = (mva * mvd) - (mvb * mvc);
13+
14+
if (!determinant)
15+
{
16+
return pointIn;
17+
}
18+
19+
determinant = 1 / determinant;
20+
21+
var ima = mvd * determinant;
22+
var imb = -mvb * determinant;
23+
var imc = -mvc * determinant;
24+
var imd = mva * determinant;
25+
var ime = (mvc * mvf - mvd * mve) * determinant;
26+
var imf = (mvb * mve - mva * mvf) * determinant;
27+
28+
var x = pointIn.x;
29+
var y = pointIn.y;
30+
31+
if (!pointOut)
32+
{
33+
pointOut = {x: 0, y: 0};
34+
}
35+
36+
/* Apply transform to point */
37+
pointOut.x = (x * ima + y * imc + ime);
38+
pointOut.y = (x * imb + y * imd + imf);
39+
40+
return pointOut;
41+
};
42+
43+
module.exports = CameraToScreen;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var CenterToBounds = function ()
2+
{
3+
this.scrollX = (this._bounds.width * 0.5) - (this.width * 0.5);
4+
this.scrollY = (this._bounds.height * 0.5) - (this.height * 0.5);
5+
6+
return this;
7+
};
8+
9+
module.exports = CenterToBounds;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var CenterToSize = function ()
2+
{
3+
this.scrollX = this.width * 0.5;
4+
this.scrollY = this.height * 0.5;
5+
6+
return this;
7+
};
8+
9+
module.exports = CenterToSize;

v3/src/camera/components/Cull.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
var Cull = function (renderableObjects)
2+
{
3+
if (this.disableCull)
4+
{
5+
return renderableObjects;
6+
}
7+
8+
var scrollX = this.scrollX;
9+
var scrollY = this.scrollY;
10+
var cameraW = this.width;
11+
var cameraH = this.height;
12+
var culledObjects = this.culledObjects;
13+
var length = renderableObjects.length;
14+
var cameraMatrix = this.matrix.matrix;
15+
16+
var mva = cameraMatrix[0];
17+
var mvb = cameraMatrix[1];
18+
var mvc = cameraMatrix[2];
19+
var mvd = cameraMatrix[3];
20+
var mve = cameraMatrix[4];
21+
var mvf = cameraMatrix[5];
22+
23+
/* First Invert Matrix */
24+
var determinant = (mva * mvd) - (mvb * mvc);
25+
26+
if (!determinant)
27+
{
28+
return renderableObjects;
29+
}
30+
31+
determinant = 1 / determinant;
32+
33+
culledObjects.length = 0;
34+
35+
for (var index = 0; index < length; ++index)
36+
{
37+
var object = renderableObjects[index];
38+
39+
if (!object.hasOwnProperty('width'))
40+
{
41+
culledObjects.push(object);
42+
continue;
43+
}
44+
45+
var objectW = object.width;
46+
var objectH = object.height;
47+
var objectX = (object.x - (scrollX * object.scrollFactorX)) - (objectW * object.originX);
48+
var objectY = (object.y - (scrollY * object.scrollFactorY)) - (objectH * object.originY);
49+
var tx = (objectX * mva + objectY * mvc + mve);
50+
var ty = (objectX * mvb + objectY * mvd + mvf);
51+
var tw = ((objectX + objectW) * mva + (objectY + objectH) * mvc + mve);
52+
var th = ((objectX + objectW) * mvb + (objectY + objectH) * mvd + mvf);
53+
var cullW = cameraW + objectW;
54+
var cullH = cameraH + objectH;
55+
56+
if (tx > -objectW || ty > -objectH || tx < cullW || ty < cullH ||
57+
tw > -objectW || th > -objectH || tw < cullW || th < cullH)
58+
{
59+
culledObjects.push(object);
60+
}
61+
}
62+
63+
return culledObjects;
64+
};
65+
66+
module.exports = Cull;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var Destroy = function ()
2+
{
3+
this._bounds = undefined;
4+
this.matrix = undefined;
5+
this.culledObjects = [];
6+
this.scene = undefined;
7+
};
8+
9+
module.exports = Destroy;

v3/src/camera/components/Fade.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var Fade = function (duration, red, green, blue, force)
2+
{
3+
if (red === undefined) { red = 0.0; }
4+
if (green === undefined) { green = 0.0; }
5+
if (blue === undefined) { blue = 0.0; }
6+
7+
if (!force && this._fadeAlpha > 0.0)
8+
{
9+
return;
10+
}
11+
12+
this._fadeRed = red;
13+
this._fadeGreen = green;
14+
this._fadeBlue = blue;
15+
16+
if (duration <= 0)
17+
{
18+
duration = Number.MIN_VALUE;
19+
}
20+
21+
this._fadeDuration = duration;
22+
this._fadeAlpha = Number.MIN_VALUE;
23+
};
24+
25+
module.exports = Fade;

v3/src/camera/components/Flash.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var Flash = function (duration, red, green, blue, force)
2+
{
3+
if (!force && this._flashAlpha > 0.0)
4+
{
5+
return;
6+
}
7+
8+
if (red === undefined) { red = 1.0; }
9+
if (green === undefined) { green = 1.0; }
10+
if (blue === undefined) { blue = 1.0; }
11+
12+
this._flashRed = red;
13+
this._flashGreen = green;
14+
this._flashBlue = blue;
15+
16+
if (duration <= 0)
17+
{
18+
duration = Number.MIN_VALUE;
19+
}
20+
21+
this._flashDuration = duration;
22+
this._flashAlpha = 1.0;
23+
};
24+
25+
module.exports = Flash;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
var PreRender = function ()
2+
{
3+
var width = this.width;
4+
var height = this.height;
5+
var zoom = this.zoom;
6+
var matrix = this.matrix;
7+
var originX = width / 2;
8+
var originY = height / 2;
9+
var follow = this._follow;
10+
11+
if (follow !== null)
12+
{
13+
originX = follow.x;
14+
originY = follow.y;
15+
16+
this.scrollX = originX - width * 0.5;
17+
this.scrollY = originY - height * 0.5;
18+
}
19+
20+
if (this.useBounds)
21+
{
22+
var bounds = this._bounds;
23+
var boundsX = bounds.x;
24+
var boundsY = bounds.y;
25+
var boundsR = Math.max(bounds.right - width, width);
26+
var boundsB = Math.max(bounds.bottom - height, height);
27+
28+
if (this.scrollX < boundsX)
29+
{
30+
this.scrollX = boundsX;
31+
}
32+
if (this.scrollX > boundsR)
33+
{
34+
this.scrollX = boundsR;
35+
}
36+
37+
if (this.scrollY < boundsY)
38+
{
39+
this.scrollY = boundsY;
40+
}
41+
if (this.scrollY > boundsB)
42+
{
43+
this.scrollY = boundsB;
44+
}
45+
}
46+
47+
if (this.roundPixels)
48+
{
49+
this.scrollX = Math.round(this.scrollX);
50+
this.scrollY = Math.round(this.scrollY);
51+
}
52+
53+
matrix.loadIdentity();
54+
matrix.translate(this.x + originX, this.y + originY);
55+
matrix.rotate(this.rotation);
56+
matrix.scale(zoom, zoom);
57+
matrix.translate(-originX, -originY);
58+
matrix.translate(this._shakeOffsetX, this._shakeOffsetY);
59+
};
60+
61+
module.exports = PreRender;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
var RemoveBounds = function ()
2+
{
3+
this.useBounds = false;
4+
5+
this._bounds.setEmpty();
6+
7+
return this;
8+
};
9+
10+
module.exports = RemoveBounds;

0 commit comments

Comments
 (0)