Skip to content

Commit e79ec0c

Browse files
committed
Generate Grid can now calculate w/h based on ortho texture
1 parent 675eec8 commit e79ec0c

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

src/geom/mesh/GenerateGridVerts.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ var GenerateGridVerts = function (config)
5959
var rotateY = GetFastValue(config, 'rotateY', 0);
6060
var rotateZ = GetFastValue(config, 'rotateZ', 0);
6161
var zIsUp = GetFastValue(config, 'zIsUp', true);
62+
var isOrtho = GetFastValue(config, 'isOrtho', mesh.dirtyCache[11]);
6263
var colors = GetFastValue(config, 'colors', [ 0xffffff ]);
6364
var alphas = GetFastValue(config, 'alphas', [ 1 ]);
6465
var tile = GetFastValue(config, 'tile', false);
6566

67+
var widthSet = GetFastValue(config, 'width', null);
68+
6669
var result = {
6770
faces: [],
6871
verts: []
@@ -72,6 +75,17 @@ var GenerateGridVerts = function (config)
7275
tempRotation.set(rotateX, rotateY, rotateZ);
7376
tempMatrix.fromRotationXYTranslation(tempRotation, tempPosition, zIsUp);
7477

78+
// If the Mesh is ortho and no width/height is given, we'll default to texture sizes (if set!)
79+
if (!widthSet && isOrtho && texture)
80+
{
81+
var textureFrame = texture.get(frame);
82+
83+
var renderer = mesh.scene.sys.renderer;
84+
85+
width = textureFrame.width / renderer.height;
86+
height = textureFrame.height / renderer.height;
87+
}
88+
7589
var halfWidth = width / 2;
7690
var halfHeight = height / 2;
7791

@@ -95,10 +109,8 @@ var GenerateGridVerts = function (config)
95109
var frameV0 = 0;
96110
var frameV1 = 1;
97111

98-
if (texture)
112+
if (textureFrame)
99113
{
100-
var textureFrame = texture.get(frame);
101-
102114
frameU0 = textureFrame.u0;
103115
frameU1 = textureFrame.u1;
104116
frameV0 = textureFrame.v0;

src/geom/mesh/typedefs/GenerateGridConfig.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
* @property {(string|Phaser.Textures.Texture)} key - The key of the texture to be used for this Grid, as stored in the Texture Manager, or a Texture instance.
66
* @property {(string|integer)} [frame] - The name or index of the frame within the Texture.
77
* @property {Phaser.GameObjects.Mesh} [mesh] - If specified, the vertices of the generated grid will be added to this Mesh Game Object.
8-
* @property {number} [width=128] - The width of the grid in pixels.
9-
* @property {number} [height=width] - The height of the grid in pixels.
8+
* @property {number} [width=1] - The width of the grid in 3D units. If you wish to get a pixel accurate grid based on a texture, you can use an Ortho Mesh or the `isOrtho` parameter.
9+
* @property {number} [height=width] - The height of the grid in 3D units.
1010
* @property {number} [widthSegments=1] - The number of segments to split the grid horizontally in to.
1111
* @property {number} [heightSegments=widthSegments] - The number of segments to split the grid vertically in to.
1212
* @property {number} [x=0] - Offset the grid x position by this amount.
1313
* @property {number} [y=0] - Offset the grid y position by this amount.
1414
* @property {number|number[]} [colors=0xffffff] - An array of colors, one per vertex, or a single color value applied to all vertices.
1515
* @property {number|number[]} [alphas=1] - An array of alpha values, one per vertex, or a single alpha value applied to all vertices.
1616
* @property {boolean} [tile=false] - Should the texture tile (repeat) across the grid segments, or display as a single texture?
17+
* @property {boolean} [isOrtho=false] - If set and using a texture with an ortho Mesh, the `width` and `height` parameters will be calculated based on the frame size for you.
1718
*/

0 commit comments

Comments
 (0)