Skip to content

Commit a8faef6

Browse files
committed
Created new Model View Projection functions
1 parent 5cc387b commit a8faef6

19 files changed

Lines changed: 776 additions & 0 deletions

src/renderer/webgl/mvp/Identity.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var SetIdentity = require('./SetIdentity');
8+
9+
/**
10+
* Loads an identity matrix into the model matrix.
11+
*
12+
* @method Phaser.Renderer.WebGL.MVP.Identity
13+
* @since 3.25.0
14+
*
15+
* @param {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} model - The Model View Projection object.
16+
*
17+
* @return {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} The Model View Projection object.
18+
*/
19+
var Identity = function (model)
20+
{
21+
SetIdentity(model.modelMatrix);
22+
23+
model.modelMatrixDirty = true;
24+
25+
return model;
26+
};
27+
28+
module.exports = Identity;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
var SetIdentity = require('./SetIdentity');
8+
9+
/**
10+
* Loads an identity matrix into the projection matrix.
11+
*
12+
* @method Phaser.Renderer.WebGL.MVP.ProjectIdentity
13+
* @since 3.25.0
14+
*
15+
* @param {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} model - The Model View Projection object.
16+
*
17+
* @return {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} The Model View Projection object.
18+
*/
19+
var ProjectIdentity = function (model)
20+
{
21+
SetIdentity(model.projectionMatrix);
22+
23+
model.projectionMatrixDirty = true;
24+
25+
return model;
26+
};
27+
28+
module.exports = ProjectIdentity;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Translates the model matrix by the given values.
9+
*
10+
* @method Phaser.Renderer.WebGL.MVP.ProjectOrtho
11+
* @since 3.25.0
12+
*
13+
* @param {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} model - The Model View Projection object.
14+
* @param {number} left - The left value.
15+
* @param {number} right - The right value.
16+
* @param {number} bottom - The bottom value.
17+
* @param {number} top - The top value.
18+
* @param {number} near - The near value.
19+
* @param {number} far - The far value.
20+
*
21+
* @return {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} The Model View Projection object.
22+
*/
23+
var ProjectOrtho = function (model, left, right, bottom, top, near, far)
24+
{
25+
var projectionMatrix = model.projectionMatrix;
26+
var leftRight = 1 / (left - right);
27+
var bottomTop = 1 / (bottom - top);
28+
var nearFar = 1 / (near - far);
29+
30+
projectionMatrix[0] = -2 * leftRight;
31+
projectionMatrix[1] = 0;
32+
projectionMatrix[2] = 0;
33+
projectionMatrix[3] = 0;
34+
projectionMatrix[4] = 0;
35+
projectionMatrix[5] = -2 * bottomTop;
36+
projectionMatrix[6] = 0;
37+
projectionMatrix[7] = 0;
38+
projectionMatrix[8] = 0;
39+
projectionMatrix[9] = 0;
40+
projectionMatrix[10] = 2 * nearFar;
41+
projectionMatrix[11] = 0;
42+
projectionMatrix[12] = (left + right) * leftRight;
43+
projectionMatrix[13] = (top + bottom) * bottomTop;
44+
projectionMatrix[14] = (far + near) * nearFar;
45+
projectionMatrix[15] = 1;
46+
47+
model.projectionMatrixDirty = true;
48+
49+
return model;
50+
};
51+
52+
module.exports = ProjectOrtho;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Sets up a perspective projection matrix into the projection matrix.
9+
*
10+
* @method Phaser.Renderer.WebGL.MVP.ProjectPerspective
11+
* @since 3.25.0
12+
*
13+
* @param {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} model - The Model View Projection object.
14+
* @param {number} fovY - The fov value.
15+
* @param {number} aspectRatio - The aspectRatio value.
16+
* @param {number} near - The near value.
17+
* @param {number} far - The far value.
18+
*
19+
* @return {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} The Model View Projection object.
20+
*/
21+
var ProjectPerspective = function (model, fovY, aspectRatio, near, far)
22+
{
23+
var projectionMatrix = model.projectionMatrix;
24+
var fov = 1 / Math.tan(fovY / 2);
25+
var nearFar = 1 / (near - far);
26+
27+
projectionMatrix[0] = fov / aspectRatio;
28+
projectionMatrix[1] = 0;
29+
projectionMatrix[2] = 0;
30+
projectionMatrix[3] = 0;
31+
projectionMatrix[4] = 0;
32+
projectionMatrix[5] = fov;
33+
projectionMatrix[6] = 0;
34+
projectionMatrix[7] = 0;
35+
projectionMatrix[8] = 0;
36+
projectionMatrix[9] = 0;
37+
projectionMatrix[10] = (far + near) * nearFar;
38+
projectionMatrix[11] = -1;
39+
projectionMatrix[12] = 0;
40+
projectionMatrix[13] = 0;
41+
projectionMatrix[14] = (2 * far * near) * nearFar;
42+
projectionMatrix[15] = 0;
43+
44+
model.projectionMatrixDirty = true;
45+
46+
return model;
47+
};
48+
49+
module.exports = ProjectPerspective;

src/renderer/webgl/mvp/RotateX.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Rotates the model matrix around the X axis.
9+
*
10+
* @method Phaser.Renderer.WebGL.MVP.RotateX
11+
* @since 3.25.0
12+
*
13+
* @param {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} model - The Model View Projection object.
14+
* @param {number} radians - The amount to rotate by.
15+
*
16+
* @return {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} The Model View Projection object.
17+
*/
18+
var RotateX = function (model, radians)
19+
{
20+
var modelMatrix = model.modelMatrix;
21+
22+
var s = Math.sin(radians);
23+
var c = Math.cos(radians);
24+
25+
var a10 = modelMatrix[4];
26+
var a11 = modelMatrix[5];
27+
var a12 = modelMatrix[6];
28+
var a13 = modelMatrix[7];
29+
var a20 = modelMatrix[8];
30+
var a21 = modelMatrix[9];
31+
var a22 = modelMatrix[10];
32+
var a23 = modelMatrix[11];
33+
34+
modelMatrix[4] = a10 * c + a20 * s;
35+
modelMatrix[5] = a11 * c + a21 * s;
36+
modelMatrix[6] = a12 * c + a22 * s;
37+
modelMatrix[7] = a13 * c + a23 * s;
38+
modelMatrix[8] = a20 * c - a10 * s;
39+
modelMatrix[9] = a21 * c - a11 * s;
40+
modelMatrix[10] = a22 * c - a12 * s;
41+
modelMatrix[11] = a23 * c - a13 * s;
42+
43+
model.modelMatrixDirty = true;
44+
45+
return model;
46+
};
47+
48+
module.exports = RotateX;

src/renderer/webgl/mvp/RotateY.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Rotates the model matrix around the Y axis.
9+
*
10+
* @method Phaser.Renderer.WebGL.MVP.RotateY
11+
* @since 3.25.0
12+
*
13+
* @param {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} model - The Model View Projection object.
14+
* @param {number} radians - The amount to rotate by.
15+
*
16+
* @return {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} The Model View Projection object.
17+
*/
18+
var RotateY = function (model, radians)
19+
{
20+
var modelMatrix = model.modelMatrix;
21+
22+
var s = Math.sin(radians);
23+
var c = Math.cos(radians);
24+
25+
var a00 = modelMatrix[0];
26+
var a01 = modelMatrix[1];
27+
var a02 = modelMatrix[2];
28+
var a03 = modelMatrix[3];
29+
var a20 = modelMatrix[8];
30+
var a21 = modelMatrix[9];
31+
var a22 = modelMatrix[10];
32+
var a23 = modelMatrix[11];
33+
34+
modelMatrix[0] = a00 * c - a20 * s;
35+
modelMatrix[1] = a01 * c - a21 * s;
36+
modelMatrix[2] = a02 * c - a22 * s;
37+
modelMatrix[3] = a03 * c - a23 * s;
38+
modelMatrix[8] = a00 * s + a20 * c;
39+
modelMatrix[9] = a01 * s + a21 * c;
40+
modelMatrix[10] = a02 * s + a22 * c;
41+
modelMatrix[11] = a03 * s + a23 * c;
42+
43+
model.modelMatrixDirty = true;
44+
45+
return model;
46+
};
47+
48+
module.exports = RotateY;

src/renderer/webgl/mvp/RotateZ.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Rotates the model matrix around the Z axis.
9+
*
10+
* @method Phaser.Renderer.WebGL.MVP.RotateZ
11+
* @since 3.25.0
12+
*
13+
* @param {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} model - The Model View Projection object.
14+
* @param {number} radians - The amount to rotate by.
15+
*
16+
* @return {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} The Model View Projection object.
17+
*/
18+
var RotateZ = function (model, radians)
19+
{
20+
var modelMatrix = model.modelMatrix;
21+
22+
var s = Math.sin(radians);
23+
var c = Math.cos(radians);
24+
25+
var a00 = modelMatrix[0];
26+
var a01 = modelMatrix[1];
27+
var a02 = modelMatrix[2];
28+
var a03 = modelMatrix[3];
29+
var a10 = modelMatrix[4];
30+
var a11 = modelMatrix[5];
31+
var a12 = modelMatrix[6];
32+
var a13 = modelMatrix[7];
33+
34+
modelMatrix[0] = a00 * c + a10 * s;
35+
modelMatrix[1] = a01 * c + a11 * s;
36+
modelMatrix[2] = a02 * c + a12 * s;
37+
modelMatrix[3] = a03 * c + a13 * s;
38+
modelMatrix[4] = a10 * c - a00 * s;
39+
modelMatrix[5] = a11 * c - a01 * s;
40+
modelMatrix[6] = a12 * c - a02 * s;
41+
modelMatrix[7] = a13 * c - a03 * s;
42+
43+
model.modelMatrixDirty = true;
44+
45+
return model;
46+
};
47+
48+
module.exports = RotateZ;

src/renderer/webgl/mvp/Scale.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Scales the model matrix by the given values.
9+
*
10+
* @method Phaser.Renderer.WebGL.MVP.Scale
11+
* @since 3.25.0
12+
*
13+
* @param {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} model - The Model View Projection object.
14+
* @param {number} x - The x component.
15+
* @param {number} y - The y component.
16+
* @param {number} z - The z component.
17+
*
18+
* @return {Phaser.Renderer.WebGL.Pipelines.ModelViewProjection} The Model View Projection object.
19+
*/
20+
var Scale = function (model, x, y, z)
21+
{
22+
var modelMatrix = model.modelMatrix;
23+
24+
modelMatrix[0] = modelMatrix[0] * x;
25+
modelMatrix[1] = modelMatrix[1] * x;
26+
modelMatrix[2] = modelMatrix[2] * x;
27+
modelMatrix[3] = modelMatrix[3] * x;
28+
modelMatrix[4] = modelMatrix[4] * y;
29+
modelMatrix[5] = modelMatrix[5] * y;
30+
modelMatrix[6] = modelMatrix[6] * y;
31+
modelMatrix[7] = modelMatrix[7] * y;
32+
modelMatrix[8] = modelMatrix[8] * z;
33+
modelMatrix[9] = modelMatrix[9] * z;
34+
modelMatrix[10] = modelMatrix[10] * z;
35+
modelMatrix[11] = modelMatrix[11] * z;
36+
37+
model.modelMatrixDirty = true;
38+
39+
return model;
40+
};
41+
42+
module.exports = Scale;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2020 Photon Storm Ltd.
4+
* @license {@link https://opensource.org/licenses/MIT|MIT License}
5+
*/
6+
7+
/**
8+
* Loads an identity matrix into the model matrix.
9+
*
10+
* @method Phaser.Renderer.WebGL.MVP.SetIdentity
11+
* @since 3.25.0
12+
*
13+
* @param {Float32Array} array - The array to set to be an identity matrix.
14+
*/
15+
var SetIdentity = function (array)
16+
{
17+
array[0] = 1;
18+
array[1] = 0;
19+
array[2] = 0;
20+
array[3] = 0;
21+
array[4] = 0;
22+
array[5] = 1;
23+
array[6] = 0;
24+
array[7] = 0;
25+
array[8] = 0;
26+
array[9] = 0;
27+
array[10] = 1;
28+
array[11] = 0;
29+
array[12] = 0;
30+
array[13] = 0;
31+
array[14] = 0;
32+
array[15] = 1;
33+
};
34+
35+
module.exports = SetIdentity;

0 commit comments

Comments
 (0)