Skip to content

Commit 1f1edd3

Browse files
committed
addGrid now takes config object and works with texture frames, with or without repeating
1 parent 440c33d commit 1f1edd3

1 file changed

Lines changed: 65 additions & 32 deletions

File tree

src/gameobjects/mesh/Mesh.js

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -317,38 +317,44 @@ var Mesh = new Class({
317317
var height = GetFastValue(config, 'height', width);
318318
var widthSegments = GetFastValue(config, 'widthSegments', 1);
319319
var heightSegments = GetFastValue(config, 'heightSegments', 1);
320-
var posX = GetFastValue(config, 'posX', 0);
321-
var posY = GetFastValue(config, 'posY', 0);
320+
var posX = GetFastValue(config, 'x', 0);
321+
var posY = GetFastValue(config, 'y', 0);
322322
var colors = GetFastValue(config, 'colors', 0xffffff);
323323
var alphas = GetFastValue(config, 'alphas', 1);
324324
var tile = GetFastValue(config, 'tile', false);
325325

326326
var halfWidth = width / 2;
327327
var halfHeight = height / 2;
328328

329-
var gridX = Math.floor(widthSegments) || 1;
330-
var gridY = Math.floor(heightSegments) || 1;
329+
var gridX = Math.floor(widthSegments);
330+
var gridY = Math.floor(heightSegments);
331331

332332
var gridX1 = gridX + 1;
333333
var gridY1 = gridY + 1;
334334

335335
var segmentWidth = width / gridX;
336336
var segmentHeight = height / gridY;
337337

338-
var vertices = [];
339338
var uvs = [];
339+
var vertices = [];
340+
var indices = [];
341+
340342
var ix;
341343
var iy;
342344

343-
var frameU0 = this.frame.u0;
344-
var frameU1 = this.frame.u1;
345-
var frameV0 = this.frame.v0;
346-
var frameV1 = this.frame.v1;
345+
var frame = this.frame;
346+
347+
var frameU0 = frame.u0;
348+
var frameU1 = frame.u1;
349+
350+
var frameV0 = frame.v0;
351+
var frameV1 = frame.v1;
352+
347353
var frameU = frameU1 - frameU0;
348354
var frameV = frameV1 - frameV0;
349355

350-
console.log('u', frameU0, 'to', frameU1, 'size', frameU);
351-
console.log('v', frameV0, 'to', frameV1, 'size', frameV);
356+
var tv;
357+
var tu;
352358

353359
for (iy = 0; iy < gridY1; iy++)
354360
{
@@ -360,29 +366,17 @@ var Mesh = new Class({
360366

361367
vertices.push(x, -y);
362368

363-
if (tile)
369+
if (!tile)
364370
{
365-
var tu = frameU0 + (ix / frameU);
366-
var tv = frameV0 + -(iy / frameV);
367-
368-
uvs.push(
369-
tu,
370-
tv
371-
);
371+
tu = frameU0 + frameU * (ix / gridX);
372+
tv = frameV0 + frameV * (1 - (iy / gridY));
372373

373-
console.log(ix, iy, '=>', tu, tv, 'vs', ix / gridX, 1 - (iy / gridY));
374-
}
375-
else
376-
{
377-
uvs.push(
378-
ix / gridX,
379-
1 - (iy / gridY)
380-
);
374+
uvs.push(tu, tv);
381375
}
382376
}
383377
}
384378

385-
var indices = [];
379+
var tiledVertices = [];
386380

387381
for (iy = 0; iy < gridY; iy++)
388382
{
@@ -393,12 +387,49 @@ var Mesh = new Class({
393387
var c = (ix + 1) + gridX1 * (iy + 1);
394388
var d = (ix + 1) + gridX1 * iy;
395389

396-
indices.push(a, b, d);
397-
indices.push(b, c, d);
390+
if (!tile)
391+
{
392+
indices.push(a, b, d);
393+
indices.push(b, c, d);
394+
}
395+
else
396+
{
397+
a *= 2;
398+
b *= 2;
399+
c *= 2;
400+
d *= 2;
401+
402+
tiledVertices.push(
403+
vertices[a], vertices[a + 1],
404+
vertices[b], vertices[b + 1],
405+
vertices[d], vertices[d + 1],
406+
407+
vertices[b], vertices[b + 1],
408+
vertices[c], vertices[c + 1],
409+
vertices[d], vertices[d + 1]
410+
);
411+
412+
uvs.push(
413+
frameU0, frameV1,
414+
frameU0, frameV0,
415+
frameU1, frameV1,
416+
417+
frameU0, frameV0,
418+
frameU1, frameV0,
419+
frameU1, frameV1
420+
);
421+
}
398422
}
399423
}
400424

401-
return this.addVertices(vertices, uvs, indices, colors, alphas);
425+
if (tile)
426+
{
427+
return this.addVertices(tiledVertices, uvs, null, colors, alphas);
428+
}
429+
else
430+
{
431+
return this.addVertices(vertices, uvs, indices, colors, alphas);
432+
}
402433
},
403434

404435
/**
@@ -708,7 +739,9 @@ var Mesh = new Class({
708739

709740
if (vertices.length !== uvs.length)
710741
{
711-
throw new Error('Mesh - vertices and uv count not equal');
742+
console.warn('Mesh vertices and uv count not equal');
743+
744+
return this;
712745
}
713746

714747
var i;

0 commit comments

Comments
 (0)