Skip to content

Commit 43fc988

Browse files
committed
Moved crop UV handler to the Frame method. Cleaner and easier.
1 parent ee8c1b4 commit 43fc988

4 files changed

Lines changed: 60 additions & 33 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ There is a third game config property called `pixelArt`. If set to `true` it's t
4646

4747
The Texture Tint Pipeline has been rewritten to tidy up hundreds of lines of duplicate code and to move the responsibility of drawing to the Game Objects themselves. Previously, had you excluded say Tilemaps from your build of Phaser, the renderer would still include masses of code dealing with the drawing of them. Now, this task has been moved to the Game Objects and the pipeline just provides a set of clean utility functions for batching, flushing and drawing.
4848

49+
* You can now set the WebGL batch size in the Game Config via the property `batchSize`. The default is 2000 before the batch will flush, which is a happy average between desktop and mobile. If targeting desktop specifically, you may wish to increase this value to reduce draw calls.
4950
* The `batchTileSprite` method has been removed from the `TextureTintPipeline` class, because it is now handled internally by the Game Object itself.
5051
* The `drawStaticTilemapLayer` method has been removed from the `TextureTintPipeline` class, because it is now handled internally by the Game Object itself.
5152
* The `batchText` method has been removed from the `TextureTintPipeline` class, because it is now handled internally by the Game Object itself.
5253
* The `batchDynamicTilemapLayer` method has been removed from the `TextureTintPipeline` class, because it is now handled internally by the Game Object itself.
54+
* The shader has a new attribute: `tintEffect`. This is a single FLOAT.
55+
* The vertex size has increased by 1 FLOAT to account for the extra shader attribute.
5356

54-
Due to the changes in the Texture Tint Pipeline the Texture Frame class has also been updated. The following changes concern the Frame UV data:
57+
Due to the changes in the Texture Tint Pipeline the `Textures.Frame` class has also been updated. The following changes concern the Frame UV data:
5558

5659
* Previously, the UV data spanned 8 properties: `x0`, `y0`, `x1`, `y1`, `x2`, `y2`, `x3` and `y3` and was stored in the `data.uvs` object. These have been replaced with directly accessible properties: `u0`, `v0`, `u1` and `v1`. These 4 properties are used directly in all renderer code now. Although it was clearer having 8 properties, 4 of them were just duplicates, so we've traded a little clarity for a smaller overall object and less dictionary look-ups.
5760
* `Frame.uvs` (and the corresponding `Frame.data.uvs`) object has been removed.
@@ -68,6 +71,7 @@ The Tint component documentation has been overhauled to explain these difference
6871

6972
### New Features
7073

74+
* You can now crop Sprites and Images using the new `setCrop` method. The crop is a rectangle that limits the area of the texture frame that is visible during rendering. Cropping a Game Object does not change its size, dimensions, physics body or hit area, it just changes what is shown when rendered. This is ideal for hiding part of a Sprite without using a mask, or for effects like displaying a progress bar. Cropping works even when the Game Object is flipped.
7175
* `Graphics.fillRoundedRect` will draw a stroked rounded rectangle to a Graphics object. The radius of the corners can be either a number, or an object, allowing you to specify different radius per corner (thanks @TadejZupancic)
7276
* `Graphics.strokeRoundedRect` will draw a filled rounded rectangle to a Graphics object. The radius of the corners can be either a number, or an object, allowing you to specify different radius per corner (thanks @TadejZupancic)
7377
* `ParticleEmitter.stop` is a new chainable method to stop a particle emitter. It's the same as setting `on` to `false` but means you don't have to break the method flow to do so (thanks @samme)
@@ -77,7 +81,6 @@ The Tint component documentation has been overhauled to explain these difference
7781
* `ScenePlugin.wake` (and the corresponding methods in Scene Systems and the Scene Manager) now has a new optional `data` argument, which is passed to the target Scene and emitted in its 'wake' event.
7882
* `ScenePlugin.setActive` now has a new optional `data` argument, which is passed to the target Scene and emitted in its 'pause' or 'resume' events.
7983
* `TileSprite.tileScaleX` and `tileScaleY` are two new properties that allow you to control the scale of the texture within the Tile Sprite. This impacts the way the repeating texture is scaled, and is independent to scaling the Tile Sprite itself. It works in both Canvas and WebGL mode.
80-
* You can now set the WebGL batch size in the Game Config via the property `batchSize`. The default is 2000 before the batch will flush, which is a happy average between desktop and mobile. If targeting desktop specifically, you may wish to increase this value to reduce draw calls.
8184
* `TransformMatrix.copyFrom` is a new method that will copy the given matrix into the values of the current one.
8285
* `TransformMatrix.multiplyWithOffset` is a new method that will multiply the given matrix with the current one, factoring in an additional offset to the results. This is used internally by the renderer code in various places.
8386

src/gameobjects/components/Texture.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var Texture = {
5353
* @private
5454
* @since 3.11.0
5555
*/
56-
_crop: { u0: 0, v0: 0, u1: 0, v1: 0, width: 0, height: 0, x: 0, y: 0 },
56+
_crop: { u0: 0, v0: 0, u1: 0, v1: 0, width: 0, height: 0, x: 0, y: 0, flipX: false, flipY: false },
5757

5858
/**
5959
* Applies a crop to a texture based Game Object, such as a Sprite or Image.
@@ -91,7 +91,7 @@ var Texture = {
9191
}
9292
else if (this.frame)
9393
{
94-
this._crop = this.frame.getCropUVs(this._crop, x, y, width, height);
94+
this.frame.setCropUVs(this._crop, x, y, width, height, this.flipX, this.flipY);
9595

9696
this.isCropped = true;
9797
}
@@ -171,6 +171,11 @@ var Texture = {
171171
}
172172
}
173173

174+
if (this.isCropped)
175+
{
176+
this.frame.updateCropUVs(this._crop, this.flipX, this.flipY);
177+
}
178+
174179
return this;
175180
}
176181

src/renderer/webgl/pipelines/TextureTintPipeline.js

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -637,12 +637,19 @@ var TextureTintPipeline = new Class({
637637
{
638638
var crop = sprite._crop;
639639

640+
if (crop.flipX !== sprite.flipX || crop.flipY !== sprite.flipY)
641+
{
642+
frame.updateCropUVs(crop, sprite.flipX, sprite.flipY);
643+
}
644+
640645
u0 = crop.u0;
641646
v0 = crop.v0;
642647
u1 = crop.u1;
643648
v1 = crop.v1;
649+
644650
frameWidth = crop.width;
645651
frameHeight = crop.height;
652+
646653
frameX = crop.x;
647654
frameY = crop.y;
648655

@@ -722,29 +729,6 @@ var TextureTintPipeline = new Class({
722729

723730
var tintEffect = (sprite._isTinted && sprite.tintFill);
724731

725-
if (sprite.isCropped && (sprite.flipX || sprite.flipY))
726-
{
727-
var ox = 0;
728-
var oy = 0;
729-
var textureSourceWidth = frame.source.width;
730-
var textureSourceHeight = frame.source.height;
731-
732-
if (sprite.flipX)
733-
{
734-
ox = (textureSourceWidth + frameWidth) - (crop.x * 2);
735-
}
736-
737-
if (sprite.flipY)
738-
{
739-
oy = (textureSourceHeight + frameHeight) - (crop.y * 2);
740-
}
741-
742-
u0 = (crop.x + ox) / textureSourceWidth;
743-
v0 = (crop.y + oy) / textureSourceHeight;
744-
u1 = (crop.x + ox + crop.width) / textureSourceWidth;
745-
v1 = (crop.y + oy + crop.height) / textureSourceHeight;
746-
}
747-
748732
this.batchVertices(tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3, u0, v0, u1, v1, tintTL, tintTR, tintBL, tintBR, tintEffect);
749733
},
750734

src/textures/Frame.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,18 +458,20 @@ var Frame = new Class({
458458
* This is called directly by the Game Object Texture Components `setCrop` method.
459459
* Please use that method to crop a Game Object.
460460
*
461-
* @method Phaser.Textures.Frame#getCropUVs
461+
* @method Phaser.Textures.Frame#setCropUVs
462462
* @since 3.11.0
463463
*
464464
* @param {object} crop - The crop data object. This is the `GameObject._crop` property.
465465
* @param {number} x - The x coordinate to start the crop from. Cannot be negative or exceed the Frame width.
466466
* @param {number} y - The y coordinate to start the crop from. Cannot be negative or exceed the Frame height.
467467
* @param {number} width - The width of the crop rectangle. Cannot exceed the Frame width.
468468
* @param {number} height - The height of the crop rectangle. Cannot exceed the Frame height.
469+
* @param {boolean} flipX - Does the parent Game Object have flipX set?
470+
* @param {boolean} flipY - Does the parent Game Object have flipY set?
469471
*
470472
* @return {object} The updated crop data object.
471473
*/
472-
getCropUVs: function (crop, x, y, width, height)
474+
setCropUVs: function (crop, x, y, width, height, flipX, flipY)
473475
{
474476
// Clamp the input values
475477

@@ -490,19 +492,52 @@ var Frame = new Class({
490492

491493
var tw = this.source.width;
492494
var th = this.source.height;
495+
var ox = 0;
496+
var oy = 0;
497+
498+
if (flipX)
499+
{
500+
ox = (tw - width) - (x * 2);
501+
}
502+
503+
if (flipY)
504+
{
505+
oy = (th - height) - (y * 2);
506+
}
507+
508+
crop.u0 = (x + ox) / tw;
509+
crop.v0 = (y + oy) / th;
510+
crop.u1 = (x + ox + width) / tw;
511+
crop.v1 = (y + oy + height) / th;
493512

494-
crop.u0 = (x / tw);
495-
crop.v0 = (y / th);
496-
crop.u1 = (x + width) / tw;
497-
crop.v1 = (y + height) / th;
498513
crop.width = width;
499514
crop.height = height;
500515
crop.x = x;
501516
crop.y = y;
517+
crop.flipX = flipX;
518+
crop.flipY = flipY;
502519

503520
return crop;
504521
},
505522

523+
/**
524+
* Takes a crop data object and recalculates the UVs based on the dimensions inside the crop object.
525+
* Called automatically by `setFrame`.
526+
*
527+
* @method Phaser.Textures.Frame#updateCropUVs
528+
* @since 3.11.0
529+
*
530+
* @param {object} crop - The crop data object. This is the `GameObject._crop` property.
531+
* @param {boolean} flipX - Does the parent Game Object have flipX set?
532+
* @param {boolean} flipY - Does the parent Game Object have flipY set?
533+
*
534+
* @return {object} The updated crop data object.
535+
*/
536+
updateCropUVs: function (crop, flipX, flipY)
537+
{
538+
return this.setCropUVs(crop, crop.x, crop.y, crop.width, crop.height, flipX, flipY);
539+
},
540+
506541
/**
507542
* Updates the internal WebGL UV cache and the drawImage cache.
508543
*

0 commit comments

Comments
 (0)