Skip to content

Commit 7ca2f9f

Browse files
committed
Merge branch 'master' of https://github.com/svipal/phaser into master
2 parents a9e8c49 + 030a948 commit 7ca2f9f

17 files changed

Lines changed: 725 additions & 395 deletions

File tree

src/gameobjects/mesh/Mesh.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ var GameObjectEvents = require('../events');
1111
var GetCalcMatrix = require('../GetCalcMatrix');
1212
var MeshRender = require('./MeshRender');
1313
var MeshCamera = require('./MeshCamera');
14+
var MeshLight = require('./MeshLight');
1415
var Model = require('../../geom/mesh/Model');
16+
var Vector3 = require('../../math/Vector3');
1517

1618
/**
1719
* @classdesc
@@ -84,6 +86,8 @@ var Mesh = new Class({
8486
*/
8587
this.camera = new MeshCamera(45, 0, 0, -10, 0.01, 1000);
8688

89+
this.light = new MeshLight();
90+
8791
/**
8892
* An array of Model instances that have been created in this Mesh.
8993
*
@@ -152,7 +156,8 @@ var Mesh = new Class({
152156

153157
this.setSize(renderer.width, renderer.height);
154158

155-
this.initPipeline();
159+
// TODO - Change to const
160+
this.initPipeline('MeshPipeline');
156161

157162
if (vertices)
158163
{
@@ -324,6 +329,7 @@ var Mesh = new Class({
324329

325330
var vertices = modelData.vertices;
326331
var textureCoords = modelData.textureCoords;
332+
var normals = modelData.vertexNormals;
327333
var faces = modelData.faces;
328334

329335
var defaultUV1 = { u: 0, v: 1 };
@@ -334,14 +340,20 @@ var Mesh = new Class({
334340
{
335341
var face = faces[i];
336342

343+
// {textureCoordsIndex: 0, vertexIndex: 16, vertexNormalIndex: 16}
337344
var v1 = face.vertices[0];
338345
var v2 = face.vertices[1];
339346
var v3 = face.vertices[2];
340347

348+
// {x: 0.19509, y: 0.980785, z: 0}
341349
var m1 = vertices[v1.vertexIndex];
342350
var m2 = vertices[v2.vertexIndex];
343351
var m3 = vertices[v3.vertexIndex];
344352

353+
var n1 = normals[v1.vertexNormalIndex];
354+
var n2 = normals[v2.vertexNormalIndex];
355+
var n3 = normals[v3.vertexNormalIndex];
356+
345357
var t1 = v1.textureCoordsIndex;
346358
var t2 = v2.textureCoordsIndex;
347359
var t3 = v3.textureCoordsIndex;
@@ -350,9 +362,9 @@ var Mesh = new Class({
350362
var uv2 = (t2 === -1) ? defaultUV2 : textureCoords[t2];
351363
var uv3 = (t3 === -1) ? defaultUV3 : textureCoords[t3];
352364

353-
var vert1 = model.addVertex(originX + m1.x * scale, originY + m1.y * scale, originZ + m1.z * scale, uv1.u, uv1.v);
354-
var vert2 = model.addVertex(originX + m2.x * scale, originY + m2.y * scale, originZ + m2.z * scale, uv2.u, uv2.v);
355-
var vert3 = model.addVertex(originX + m3.x * scale, originY + m3.y * scale, originZ + m3.z * scale, uv3.u, uv3.v);
365+
var vert1 = model.addVertex(originX + m1.x * scale, originY + m1.y * scale, originZ + m1.z * scale, uv1.u, uv1.v, n1.x, n1.y, n1.z);
366+
var vert2 = model.addVertex(originX + m2.x * scale, originY + m2.y * scale, originZ + m2.z * scale, uv2.u, uv2.v, n2.x, n2.y, n2.z);
367+
var vert3 = model.addVertex(originX + m3.x * scale, originY + m3.y * scale, originZ + m3.z * scale, uv3.u, uv3.v, n3.x, n3.y, n3.z);
356368

357369
model.addFace(vert1, vert2, vert3);
358370
}
@@ -533,6 +545,7 @@ var Mesh = new Class({
533545

534546
var camera = this.camera;
535547

548+
/*
536549
if (camera.dirty || width !== this._prevWidth || height !== this._prevHeight)
537550
{
538551
// Mesh has resized, flow that down to the Camera
@@ -541,6 +554,9 @@ var Mesh = new Class({
541554
this._prevWidth = width;
542555
this._prevHeight = height;
543556
}
557+
*/
558+
559+
camera.update(width, height);
544560

545561
var models = this.models;
546562

src/gameobjects/mesh/MeshCamera.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ var MeshCamera = new Class({
3939
this.up = new Vector4(); // What the up direction is, invert to get bottom
4040
this.right = new Vector4(); // What the right direction is, invert to get left
4141

42-
this.matView = new Matrix4();
43-
this.viewMatrix = new Matrix4();
44-
this.projectionMatrix = new Matrix4();
42+
this.matrix = new Matrix4(); // the transform matrix
43+
this.viewMatrix = new Matrix4(); // the inverse of the transform matrix
44+
this.projectionMatrix = new Matrix4(); // perspective projection matrix
45+
this.viewProjectionMatrix = new Matrix4(); // perspective projection matrix multiplied by the view matrix
4546

4647
this.mode = MeshCamera.MODE_ORBIT;
4748
},
@@ -88,30 +89,20 @@ var MeshCamera = new Class({
8889
// To have different modes of movements, this function handles the view matrix update for the transform object.
8990
updateViewMatrix: function ()
9091
{
91-
var d = Math.PI / 180;
92-
var matView = this.matView;
93-
var rotation = this.rotation;
92+
var matView = this.matrix;
9493

95-
matView.identity();
96-
97-
// Optimize camera transform update, no need for scale nor rotateZ
9894
if (this.mode === MeshCamera.MODE_FREE)
9995
{
100-
matView.translate(this.position);
101-
matView.rotateX(rotation.x * d);
102-
matView.rotateY(rotation.y * d);
96+
matView.fromRotationXYTranslation(this.rotation, this.position, true);
10397
}
10498
else
10599
{
106-
matView.rotateX(rotation.x * d);
107-
matView.rotateY(rotation.y * d);
108-
matView.translate(this.position);
100+
matView.fromRotationXYTranslation(this.rotation, this.position, false);
109101
}
110102

111103
this.updateDirection();
112104

113-
this.viewMatrix.copy(matView);
114-
this.viewMatrix.invert();
105+
this.viewMatrix.copy(matView).invert();
115106

116107
this.dirty = true;
117108
},
@@ -120,28 +111,21 @@ var MeshCamera = new Class({
120111
{
121112
this.aspectRatio = width / height;
122113

123-
this.updateViewMatrix();
124-
114+
// TODO - Only needs changing when the fov/etc does
125115
this.projectionMatrix.perspective(DegToRad(this._fov), this.aspectRatio, this._near, this._far);
116+
117+
// TODO - Only needs calculating when this camera is dirty
118+
this.projectionMatrix.multiplyToMat4(this.viewMatrix, this.viewProjectionMatrix);
126119
},
127120

128121
updateDirection: function ()
129122
{
130-
var matView = this.matView;
123+
var matView = this.matrix;
131124

132125
this.forward.set(0, 0, 1, 0).transformMat4(matView);
133126
this.up.set(0, 1, 0, 0).transformMat4(matView);
134127
this.right.set(1, 0, 0, 0).transformMat4(matView);
135128
},
136-
137-
reset: function ()
138-
{
139-
this.position.set();
140-
this.rotation.set();
141-
142-
this.updateViewMatrix();
143-
},
144-
145129
fov: {
146130

147131
get: function ()

src/gameobjects/mesh/MeshLight.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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 Class = require('../../utils/Class');
8+
var Vector3 = require('../../math/Vector3');
9+
10+
/**
11+
* @classdesc
12+
* The Mesh Light.
13+
*
14+
* @class MeshLight
15+
* @memberof Phaser.GameObjects
16+
* @constructor
17+
* @since 3.50.0
18+
*/
19+
var MeshLight = new Class({
20+
21+
initialize:
22+
23+
function MeshLight (x, y, z)
24+
{
25+
this.position = new Vector3(x, y, z);
26+
this.ambient = new Vector3(1, 1, 1);
27+
this.diffuse = new Vector3(1, 1, 1);
28+
this.specular = new Vector3(1, 1, 1);
29+
},
30+
31+
setPosition: function (x, y, z)
32+
{
33+
this.position.set(x, y, z);
34+
35+
return this;
36+
},
37+
38+
setAmbient: function (r, g, b)
39+
{
40+
this.ambient.set(r, g, b);
41+
42+
return this;
43+
},
44+
45+
setDiffuse: function (r, g, b)
46+
{
47+
this.diffuse.set(r, g, b);
48+
49+
return this;
50+
},
51+
52+
setSpecular: function (r, g, b)
53+
{
54+
this.specular.set(r, g, b);
55+
56+
return this;
57+
},
58+
59+
x: {
60+
61+
get: function ()
62+
{
63+
return this.position.x;
64+
},
65+
66+
set: function (value)
67+
{
68+
this.position.x = value;
69+
}
70+
71+
},
72+
73+
y: {
74+
75+
get: function ()
76+
{
77+
return this.position.y;
78+
},
79+
80+
set: function (value)
81+
{
82+
this.position.y = value;
83+
}
84+
85+
},
86+
87+
z: {
88+
89+
get: function ()
90+
{
91+
return this.position.z;
92+
},
93+
94+
set: function (value)
95+
{
96+
this.position.z = value;
97+
}
98+
99+
}
100+
101+
});
102+
103+
module.exports = MeshLight;

src/gameobjects/mesh/MeshWebGLRenderer.js

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* @license {@link https://opensource.org/licenses/MIT|MIT License}
55
*/
66

7-
var GetCalcMatrix = require('../GetCalcMatrix');
8-
97
/**
108
* Renders this Game Object with the WebGL Renderer to the given Camera.
119
* The object will not render if any of its renderFlags are set or it is being actively filtered out by the Camera.
@@ -20,7 +18,7 @@ var GetCalcMatrix = require('../GetCalcMatrix');
2018
* @param {Phaser.Cameras.Scene2D.Camera} camera - The Camera that is rendering the Game Object.
2119
* @param {Phaser.GameObjects.Components.TransformMatrix} parentMatrix - This transform matrix is defined if the game object is nested
2220
*/
23-
var MeshWebGLRenderer = function (renderer, src, camera, parentMatrix)
21+
var MeshWebGLRenderer = function (renderer, src)
2422
{
2523
var models = src.models;
2624
var totalModels = models.length;
@@ -30,88 +28,18 @@ var MeshWebGLRenderer = function (renderer, src, camera, parentMatrix)
3028
return;
3129
}
3230

33-
var pipeline = renderer.pipelines.set(src.pipeline, src);
34-
35-
var calcMatrix = GetCalcMatrix(src, camera, parentMatrix).calc;
36-
37-
var vertexOffset = (pipeline.vertexCount * pipeline.vertexComponentCount) - 1;
38-
39-
var debugVerts;
40-
var debugCallback = src.debugCallback;
31+
renderer.pipelines.clear();
4132

42-
var a = calcMatrix.a;
43-
var b = calcMatrix.b;
44-
var c = calcMatrix.c;
45-
var d = calcMatrix.d;
46-
var e = calcMatrix.e;
47-
var f = calcMatrix.f;
48-
49-
var F32 = pipeline.vertexViewF32;
50-
var U32 = pipeline.vertexViewU32;
51-
52-
var globalAlpha = camera.alpha * src.alpha;
33+
var pipeline = renderer.pipelines.set(src.pipeline, src);
5334

5435
for (var m = 0; m < totalModels; m++)
5536
{
5637
var model = models[m];
5738

58-
var faces = model.faces;
59-
var totalFaces = faces.length;
60-
var alpha = globalAlpha * model.alpha;
61-
62-
if (totalFaces === 0 || !model.visible || alpha <= 0)
63-
{
64-
continue;
65-
}
66-
67-
if (debugCallback)
68-
{
69-
debugVerts = [];
70-
}
71-
72-
var tintEffect = model.tintFill;
73-
var textureUnit = pipeline.setGameObject(model);
74-
75-
for (var i = 0; i < totalFaces; i++)
76-
{
77-
var face = faces[i];
78-
79-
if (model.hideCCW && !face.isCounterClockwise())
80-
{
81-
continue;
82-
}
83-
84-
if (pipeline.shouldFlush(3))
85-
{
86-
pipeline.flush();
87-
88-
vertexOffset = 0;
89-
}
90-
91-
vertexOffset = face.vertex1.load(F32, U32, vertexOffset, textureUnit, tintEffect, alpha, a, b, c, d, e, f);
92-
vertexOffset = face.vertex2.load(F32, U32, vertexOffset, textureUnit, tintEffect, alpha, a, b, c, d, e, f);
93-
vertexOffset = face.vertex3.load(F32, U32, vertexOffset, textureUnit, tintEffect, alpha, a, b, c, d, e, f);
94-
95-
pipeline.vertexCount += 3;
96-
97-
if (debugCallback && model.drawDebug)
98-
{
99-
debugVerts.push(
100-
F32[vertexOffset - 20],
101-
F32[vertexOffset - 19],
102-
F32[vertexOffset - 13],
103-
F32[vertexOffset - 12],
104-
F32[vertexOffset - 6],
105-
F32[vertexOffset - 5]
106-
);
107-
}
108-
}
109-
110-
if (debugCallback && model.drawDebug)
111-
{
112-
debugCallback.call(src, src, debugVerts);
113-
}
39+
pipeline.drawModel(src, model);
11440
}
41+
42+
renderer.pipelines.rebind();
11543
};
11644

11745
module.exports = MeshWebGLRenderer;

0 commit comments

Comments
 (0)