Skip to content

Commit 1057ead

Browse files
committed
Dump all of the old mega arrays and replace with an array of Vertex instances.
1 parent 36a3374 commit 1057ead

1 file changed

Lines changed: 88 additions & 64 deletions

File tree

src/gameobjects/mesh/Mesh.js

Lines changed: 88 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
var AnimationState = require('../../animations/AnimationState');
88
var Class = require('../../utils/Class');
99
var Components = require('../components');
10+
var Face = require('./Face');
1011
var GameObject = require('../GameObject');
1112
var GameObjectEvents = require('../events');
1213
var MeshRender = require('./MeshRender');
14+
var Vertex = require('./Vertex');
1315

1416
/**
1517
* @classdesc
@@ -82,53 +84,41 @@ var Mesh = new Class({
8284
this.anims = new AnimationState(this);
8385

8486
/**
85-
* An array containing the vertices data for this Mesh.
87+
* An array containing the Face instances belonging to this Mesh.
8688
*
87-
* @name Phaser.GameObjects.Mesh#vertices
88-
* @type {Float32Array}
89-
* @since 3.0.0
90-
*/
91-
this.vertices;
92-
93-
/**
94-
* An array containing the uv data for this Mesh.
89+
* A Face consists of 3 Vertex objects.
9590
*
96-
* @name Phaser.GameObjects.Mesh#uv
97-
* @type {Float32Array}
98-
* @since 3.0.0
99-
*/
100-
this.uv;
101-
102-
/**
103-
* An array containing the color data for this Mesh.
91+
* This array is populated during the `setVertices` method.
10492
*
105-
* @name Phaser.GameObjects.Mesh#colors
106-
* @type {Uint32Array}
107-
* @since 3.0.0
93+
* @name Phaser.GameObjects.Mesh#faces
94+
* @type {Phaser.GameObjects.Face[]}
95+
* @since 3.50.0
10896
*/
109-
this.colors;
97+
this.faces;
11098

11199
/**
112-
* An array containing the alpha data for this Mesh.
100+
* An array containing Vertex instances. One instance per vertex in this Mesh.
101+
*
102+
* This array is populated during the `setVertices` method.
113103
*
114-
* @name Phaser.GameObjects.Mesh#alphas
115-
* @type {Float32Array}
116-
* @since 3.0.0
104+
* @name Phaser.GameObjects.Mesh#vertices
105+
* @type {Phaser.GameObjects.Vertex[]}
106+
* @since 3.50.0
117107
*/
118-
this.alphas;
108+
this.vertices;
119109

120110
/**
121111
* The tint fill mode.
122112
*
123-
* 0 = An additive tint (the default), where vertices colors are blended with the texture.
124-
* 1 = A fill tint, where the vertices colors replace the texture, but respects texture alpha.
125-
* 2 = A complete tint, where the vertices colors replace the texture, including alpha, entirely.
113+
* `false` = An additive tint (the default), where vertices colors are blended with the texture.
114+
* `true` = A fill tint, where the vertices colors replace the texture, but respects texture alpha.
126115
*
127116
* @name Phaser.GameObjects.Mesh#tintFill
128-
* @type {integer}
129-
* @since 3.11.0
117+
* @type {boolean}
118+
* @default false
119+
* @since 3.50.0
130120
*/
131-
this.tintFill = 0;
121+
this.tintFill = false;
132122

133123
/**
134124
* You can optionally choose to render the vertices of this Mesh to a Graphics instance.
@@ -195,57 +185,93 @@ var Mesh = new Class({
195185
throw new Error('Mesh - vertices and uv count not equal');
196186
}
197187

188+
var i;
189+
var vert;
190+
var verts = [];
191+
var faces = [];
192+
193+
var isColorArray = Array.isArray(colors);
194+
var isAlphaArray = Array.isArray(alphas);
195+
198196
if (Array.isArray(indicies))
199197
{
200-
var verticesFull = [];
201-
var uvsFull = [];
202-
203-
for (var i = 0; i < indicies.length; i++)
198+
for (i = 0; i < indicies.length; i++)
204199
{
205200
var index = indicies[i] * 2;
206201

207-
verticesFull.push(vertices[index], vertices[index + 1]);
208-
uvsFull.push(uvs[index], uvs[index + 1]);
209-
}
202+
vert = new Vertex(
203+
vertices[index],
204+
vertices[index + 1],
205+
uvs[index],
206+
uvs[index + 1],
207+
(isColorArray) ? colors[i] : colors,
208+
(isAlphaArray) ? alphas[i] : alphas
209+
);
210210

211-
vertices = verticesFull;
212-
uvs = uvsFull;
211+
verts.push(vert);
212+
}
213213
}
214-
215-
var halfVerts = Math.floor(vertices.length / 2);
216-
217-
if (!Array.isArray(colors))
214+
else
218215
{
219-
var tempColor = colors;
220-
221-
colors = [];
216+
var colorIndex = 0;
222217

223-
for (var c = 0; c < halfVerts; c++)
218+
for (i = 0; i < vertices.length; i += 2)
224219
{
225-
colors.push(tempColor);
220+
vert = new Vertex(
221+
vertices[i],
222+
vertices[i + 1],
223+
uvs[i],
224+
uvs[i + 1],
225+
(isColorArray) ? colors[colorIndex] : colors,
226+
(isAlphaArray) ? alphas[colorIndex] : alphas
227+
);
228+
229+
verts.push(vert);
230+
231+
colorIndex++;
226232
}
227233
}
228234

229-
if (!Array.isArray(alphas))
235+
for (i = 0; i < verts.length; i += 3)
230236
{
231-
var tempAlpha = alphas;
237+
var vert1 = verts[i];
238+
var vert2 = verts[i + 1];
239+
var vert3 = verts[i + 2];
232240

233-
alphas = [];
241+
var face = new Face(vert1, vert2, vert3);
234242

235-
for (var a = 0; a < halfVerts; a++)
236-
{
237-
alphas.push(tempAlpha);
238-
}
243+
faces.push(face);
239244
}
240245

241-
this.vertices = new Float32Array(vertices);
242-
this.uv = new Float32Array(uvs);
243-
this.colors = new Uint32Array(colors);
244-
this.alphas = new Float32Array(alphas);
246+
this.vertices = verts;
247+
this.faces = faces;
245248

246249
return this;
247250
},
248251

252+
getFaceCount: function ()
253+
{
254+
return this.faces.length;
255+
},
256+
257+
getVertexCount: function ()
258+
{
259+
return this.vertices.length;
260+
},
261+
262+
getFace: function (index)
263+
{
264+
return this.faces[index];
265+
},
266+
267+
getFaceAt: function (x, y, camera)
268+
{
269+
if (camera === undefined) { camera = this.scene.sys.cameras.main; }
270+
271+
272+
273+
},
274+
249275
/**
250276
* This method enables rendering of the Mesh vertices to the given Graphics instance.
251277
*
@@ -356,9 +382,7 @@ var Mesh = new Class({
356382
this.anims = undefined;
357383

358384
this.vertices = null;
359-
this.uv = null;
360-
this.colors = null;
361-
this.alphas = null;
385+
this.faces = null;
362386

363387
this.debugCallback = null;
364388
this.debugGraphic = null;

0 commit comments

Comments
 (0)