|
7 | 7 | var AnimationState = require('../../animations/AnimationState'); |
8 | 8 | var Class = require('../../utils/Class'); |
9 | 9 | var Components = require('../components'); |
| 10 | +var Face = require('./Face'); |
10 | 11 | var GameObject = require('../GameObject'); |
11 | 12 | var GameObjectEvents = require('../events'); |
12 | 13 | var MeshRender = require('./MeshRender'); |
| 14 | +var Vertex = require('./Vertex'); |
13 | 15 |
|
14 | 16 | /** |
15 | 17 | * @classdesc |
@@ -82,53 +84,41 @@ var Mesh = new Class({ |
82 | 84 | this.anims = new AnimationState(this); |
83 | 85 |
|
84 | 86 | /** |
85 | | - * An array containing the vertices data for this Mesh. |
| 87 | + * An array containing the Face instances belonging to this Mesh. |
86 | 88 | * |
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. |
95 | 90 | * |
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. |
104 | 92 | * |
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 |
108 | 96 | */ |
109 | | - this.colors; |
| 97 | + this.faces; |
110 | 98 |
|
111 | 99 | /** |
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. |
113 | 103 | * |
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 |
117 | 107 | */ |
118 | | - this.alphas; |
| 108 | + this.vertices; |
119 | 109 |
|
120 | 110 | /** |
121 | 111 | * The tint fill mode. |
122 | 112 | * |
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. |
126 | 115 | * |
127 | 116 | * @name Phaser.GameObjects.Mesh#tintFill |
128 | | - * @type {integer} |
129 | | - * @since 3.11.0 |
| 117 | + * @type {boolean} |
| 118 | + * @default false |
| 119 | + * @since 3.50.0 |
130 | 120 | */ |
131 | | - this.tintFill = 0; |
| 121 | + this.tintFill = false; |
132 | 122 |
|
133 | 123 | /** |
134 | 124 | * You can optionally choose to render the vertices of this Mesh to a Graphics instance. |
@@ -195,57 +185,93 @@ var Mesh = new Class({ |
195 | 185 | throw new Error('Mesh - vertices and uv count not equal'); |
196 | 186 | } |
197 | 187 |
|
| 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 | + |
198 | 196 | if (Array.isArray(indicies)) |
199 | 197 | { |
200 | | - var verticesFull = []; |
201 | | - var uvsFull = []; |
202 | | - |
203 | | - for (var i = 0; i < indicies.length; i++) |
| 198 | + for (i = 0; i < indicies.length; i++) |
204 | 199 | { |
205 | 200 | var index = indicies[i] * 2; |
206 | 201 |
|
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 | + ); |
210 | 210 |
|
211 | | - vertices = verticesFull; |
212 | | - uvs = uvsFull; |
| 211 | + verts.push(vert); |
| 212 | + } |
213 | 213 | } |
214 | | - |
215 | | - var halfVerts = Math.floor(vertices.length / 2); |
216 | | - |
217 | | - if (!Array.isArray(colors)) |
| 214 | + else |
218 | 215 | { |
219 | | - var tempColor = colors; |
220 | | - |
221 | | - colors = []; |
| 216 | + var colorIndex = 0; |
222 | 217 |
|
223 | | - for (var c = 0; c < halfVerts; c++) |
| 218 | + for (i = 0; i < vertices.length; i += 2) |
224 | 219 | { |
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++; |
226 | 232 | } |
227 | 233 | } |
228 | 234 |
|
229 | | - if (!Array.isArray(alphas)) |
| 235 | + for (i = 0; i < verts.length; i += 3) |
230 | 236 | { |
231 | | - var tempAlpha = alphas; |
| 237 | + var vert1 = verts[i]; |
| 238 | + var vert2 = verts[i + 1]; |
| 239 | + var vert3 = verts[i + 2]; |
232 | 240 |
|
233 | | - alphas = []; |
| 241 | + var face = new Face(vert1, vert2, vert3); |
234 | 242 |
|
235 | | - for (var a = 0; a < halfVerts; a++) |
236 | | - { |
237 | | - alphas.push(tempAlpha); |
238 | | - } |
| 243 | + faces.push(face); |
239 | 244 | } |
240 | 245 |
|
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; |
245 | 248 |
|
246 | 249 | return this; |
247 | 250 | }, |
248 | 251 |
|
| 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 | + |
249 | 275 | /** |
250 | 276 | * This method enables rendering of the Mesh vertices to the given Graphics instance. |
251 | 277 | * |
@@ -356,9 +382,7 @@ var Mesh = new Class({ |
356 | 382 | this.anims = undefined; |
357 | 383 |
|
358 | 384 | this.vertices = null; |
359 | | - this.uv = null; |
360 | | - this.colors = null; |
361 | | - this.alphas = null; |
| 385 | + this.faces = null; |
362 | 386 |
|
363 | 387 | this.debugCallback = null; |
364 | 388 | this.debugGraphic = null; |
|
0 commit comments