Tile Sprites updated to use new internal canvas and support crop

This commit is contained in:
Richard Davey 2018-08-03 19:14:06 +01:00
parent e91d95e107
commit 2ab24c49c4
2 changed files with 384 additions and 100 deletions

View file

@ -11,6 +11,10 @@ var CONST = require('../../const');
var GameObject = require('../GameObject');
var GetPowerOfTwo = require('../../math/pow2/GetPowerOfTwo');
var TileSpriteRender = require('./TileSpriteRender');
var Vector2 = require('../../math/Vector2');
// bitmask flag for GameObject.renderMask
var _FLAG = 8; // 1000
/**
* @classdesc
@ -44,6 +48,7 @@ var TileSpriteRender = require('./TileSpriteRender');
* @extends Phaser.GameObjects.Components.Alpha
* @extends Phaser.GameObjects.Components.BlendMode
* @extends Phaser.GameObjects.Components.ComputedSize
* @extends Phaser.GameObjects.Components.Crop
* @extends Phaser.GameObjects.Components.Depth
* @extends Phaser.GameObjects.Components.Flip
* @extends Phaser.GameObjects.Components.GetBounds
@ -52,7 +57,6 @@ var TileSpriteRender = require('./TileSpriteRender');
* @extends Phaser.GameObjects.Components.Pipeline
* @extends Phaser.GameObjects.Components.ScaleMode
* @extends Phaser.GameObjects.Components.ScrollFactor
* @extends Phaser.GameObjects.Components.Texture
* @extends Phaser.GameObjects.Components.Tint
* @extends Phaser.GameObjects.Components.Transform
* @extends Phaser.GameObjects.Components.Visible
@ -62,8 +66,8 @@ var TileSpriteRender = require('./TileSpriteRender');
* @param {number} y - The vertical position of this Game Object in the world.
* @param {number} width - The width of the Game Object.
* @param {number} height - The height of the Game Object.
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
* @param {string} textureKey - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {(string|integer)} [frameKey] - An optional frame from the Texture this Game Object is rendering with.
*/
var TileSprite = new Class({
@ -73,6 +77,7 @@ var TileSprite = new Class({
Components.Alpha,
Components.BlendMode,
Components.ComputedSize,
Components.Crop,
Components.Depth,
Components.Flip,
Components.GetBounds,
@ -81,7 +86,6 @@ var TileSprite = new Class({
Components.Pipeline,
Components.ScaleMode,
Components.ScrollFactor,
Components.Texture,
Components.Tint,
Components.Transform,
Components.Visible,
@ -90,51 +94,31 @@ var TileSprite = new Class({
initialize:
function TileSprite (scene, x, y, width, height, texture, frame)
function TileSprite (scene, x, y, width, height, textureKey, frameKey)
{
var renderer = scene.sys.game.renderer;
GameObject.call(this, scene, 'TileSprite');
/**
* The horizontal scroll position of the Tile Sprite.
* Internal tile position vector.
*
* @name Phaser.GameObjects.TileSprite#tilePositionX
* @type {number}
* @default 0
* @since 3.0.0
* @name Phaser.GameObjects.TileSprite#_tilePosition
* @type {Phaser.Math.Vector2}
* @private
* @since 3.12.0
*/
this.tilePositionX = 0;
this._tilePosition = new Vector2();
/**
* The vertical scroll position of the Tile Sprite.
* Internal tile scale vector.
*
* @name Phaser.GameObjects.TileSprite#tilePositionY
* @type {number}
* @default 0
* @since 3.0.0
* @name Phaser.GameObjects.TileSprite#_tileScale
* @type {Phaser.Math.Vector2}
* @private
* @since 3.12.0
*/
this.tilePositionY = 0;
/**
* The horizontal scale of the Tile Sprite texture.
*
* @name Phaser.GameObjects.TileSprite#tileScaleX
* @type {number}
* @default 1
* @since 3.11.0
*/
this.tileScaleX = 1;
/**
* The vertical scale of the Tile Sprite texture.
*
* @name Phaser.GameObjects.TileSprite#tileScaleY
* @type {number}
* @default 1
* @since 3.11.0
*/
this.tileScaleY = 1;
this._tileScale = new Vector2(1, 1);
/**
* Whether the Tile Sprite has changed in some way, requiring an re-render of its tile texture.
@ -143,21 +127,10 @@ var TileSprite = new Class({
*
* @name Phaser.GameObjects.TileSprite#dirty
* @type {boolean}
* @default true
* @default false
* @since 3.0.0
*/
this.dirty = true;
/**
* The texture that the Tile Sprite is rendered to, which is then rendered to a Scene.
* In WebGL this is a WebGLTexture. In Canvas it's a Canvas Fill Pattern.
*
* @name Phaser.GameObjects.TileSprite#tileTexture
* @type {?(WebGLTexture|CanvasPattern)}
* @default null
* @since 3.0.0
*/
this.tileTexture = null;
this.dirty = false;
/**
* The renderer in use by this Tile Sprite.
@ -168,67 +141,117 @@ var TileSprite = new Class({
*/
this.renderer = renderer;
this.setTexture(texture, frame);
/**
* The Canvas element that the TileSprite renders its fill pattern in to.
* Only used in Canvas mode.
*
* @name Phaser.GameObjects.TileSprite#canvas
* @type {?HTMLCanvasElement}
* @since 3.12.0
*/
this.canvas = CanvasPool.create(this, width, height);
/**
* The Context of the Canvas element that the TileSprite renders its fill pattern in to.
* Only used in Canvas mode.
*
* @name Phaser.GameObjects.TileSprite#context
* @type {CanvasRenderingContext2D}
* @since 3.12.0
*/
this.context = this.canvas.getContext('2d');
/**
* The Texture the TileSprite is using as its fill pattern.
*
* @name Phaser.GameObjects.TileSprite#displayTexture
* @type {Phaser.Textures.Texture|Phaser.Textures.CanvasTexture}
* @private
* @since 3.12.0
*/
this.displayTexture = null;
/**
* The Frame the TileSprite is using as its fill pattern.
*
* @name Phaser.GameObjects.TileSprite#displayFrame
* @type {Phaser.Textures.Frame}
* @private
* @since 3.12.0
*/
this.displayFrame = null;
/**
* The internal crop data object, as used by `setCrop` and passed to the `Frame.setCropUVs` method.
*
* @name Phaser.GameObjects.TileSprite#_crop
* @type {object}
* @private
* @since 3.12.0
*/
this._crop = this.resetCropObject();
// Create a Texture for this object
this.texture = scene.sys.textures.addCanvas(null, this.canvas, true);
// Get the frame
this.frame = this.texture.get();
this.setTexture(textureKey, frameKey);
this.setPosition(x, y);
this.setSize(width, height);
this.setOriginFromFrame();
this.initPipeline('TextureTintPipeline');
/**
* The next power of two value from the width of the Frame.
* The next power of two value from the width of the Fill Pattern frame.
*
* @name Phaser.GameObjects.TileSprite#potWidth
* @type {integer}
* @since 3.0.0
*/
this.potWidth = GetPowerOfTwo(this.frame.width);
this.potWidth = GetPowerOfTwo(this.displayFrame.width);
/**
* The next power of two value from the height of the Frame.
* The next power of two value from the height of the Fill Pattern frame.
*
* @name Phaser.GameObjects.TileSprite#potHeight
* @type {integer}
* @since 3.0.0
*/
this.potHeight = GetPowerOfTwo(this.frame.height);
this.potHeight = GetPowerOfTwo(this.displayFrame.height);
/**
* The Canvas Pattern used to repeat the TileSprite's texture.
* The Canvas that the TileSprites texture is rendered to.
* This is used to create a WebGL texture from.
*
* @name Phaser.GameObjects.TileSprite#canvasPattern
* @type {?CanvasPattern}
* @default null
* @since 3.0.0
*/
// this.canvasPattern = null;
/**
* The Canvas that the TileSprite's texture is rendered to.
*
* @name Phaser.GameObjects.TileSprite#canvasBuffer
* @name Phaser.GameObjects.TileSprite#fillCanvas
* @type {HTMLCanvasElement}
* @since 3.0.0
* @since 3.12.0
*/
this.canvasBuffer = CanvasPool.create2D(this, this.potWidth, this.potHeight);
this.fillCanvas = CanvasPool.create2D(this, this.potWidth, this.potHeight);
/**
* The Canvas Context used to render the TileSprite's texture.
* The Canvas Context used to render the TileSprites texture.
*
* @name Phaser.GameObjects.TileSprite#canvasBufferCtx
* @name Phaser.GameObjects.TileSprite#fillContext
* @type {CanvasRenderingContext2D}
* @since 3.0.0
* @since 3.12.0
*/
this.canvasBufferCtx = this.canvasBuffer.getContext('2d');
this.fillContext = this.fillCanvas.getContext('2d');
/**
* The previous Texture Frame being used.
* The texture that the Tile Sprite is rendered to, which is then rendered to a Scene.
* In WebGL this is a WebGLTexture. In Canvas it's a Canvas Fill Pattern.
*
* @name Phaser.GameObjects.Components.Texture#oldFrame
* @type {Phaser.Textures.Frame}
* @private
* @since 3.0.0
* @name Phaser.GameObjects.TileSprite#fillPattern
* @type {?(WebGLTexture|CanvasPattern)}
* @since 3.12.0
*/
this.oldFrame = null;
this.fillPattern = null;
// Update the fill pattern
this.dirty = true;
this.updateTileTexture();
@ -238,13 +261,90 @@ var TileSprite = new Class({
{
var gl = renderer.gl;
this.tileTexture = null;
this.dirty = true;
this.tileTexture = renderer.createTexture2D(0, gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT, gl.RGBA, this.canvasBuffer, this.potWidth, this.potHeight);
this.fillPattern = null;
this.fillPattern = renderer.createTexture2D(0, gl.LINEAR, gl.LINEAR, gl.REPEAT, gl.REPEAT, gl.RGBA, this.fillCanvas, this.potWidth, this.potHeight);
}, this);
}
},
/**
* Sets the texture and frame this Game Object will use to render with.
*
* Textures are referenced by their string-based keys, as stored in the Texture Manager.
*
* @method Phaser.GameObjects.TileSprite#setTexture
* @since 3.0.0
*
* @param {string} key - The key of the texture to be used, as stored in the Texture Manager.
* @param {(string|integer)} [frame] - The name or index of the frame within the Texture.
*
* @return {this} This Game Object instance.
*/
setTexture: function (key, frame)
{
this.displayTexture = this.scene.sys.textures.get(key);
return this.setFrame(frame);
},
/**
* Sets the frame this Game Object will use to render with.
*
* The Frame has to belong to the current Texture being used.
*
* It can be either a string or an index.
*
* Calling `setFrame` will modify the `width` and `height` properties of your Game Object.
* It will also change the `origin` if the Frame has a custom pivot point, as exported from packages like Texture Packer.
*
* @method Phaser.GameObjects.TileSprite#setFrame
* @since 3.0.0
*
* @param {(string|integer)} frame - The name or index of the frame within the Texture.
* @param {boolean} [updateSize=true] - Should this call adjust the size of the Game Object?
* @param {boolean} [updateOrigin=true] - Should this call adjust the origin of the Game Object?
*
* @return {this} This Game Object instance.
*/
setFrame: function (frame, updateSize, updateOrigin)
{
if (updateSize === undefined) { updateSize = true; }
if (updateOrigin === undefined) { updateOrigin = true; }
this.displayFrame = this.displayTexture.get(frame);
if (!this.displayFrame.cutWidth || !this.displayFrame.cutHeight)
{
this.renderFlags &= ~_FLAG;
}
else
{
this.renderFlags |= _FLAG;
}
if (this._sizeComponent && updateSize)
{
this.setSizeToFrame();
}
if (this._originComponent && updateOrigin)
{
if (this.displayFrame.customPivot)
{
this.setOrigin(this.displayFrame.pivotX, this.displayFrame.pivotY);
}
else
{
this.updateDisplayOrigin();
}
}
this.updateTileTexture();
return this;
},
/**
* Sets {@link Phaser.GameObjects.TileSprite#tilePositionX} and {@link Phaser.GameObjects.TileSprite#tilePositionY}.
*
@ -254,7 +354,7 @@ var TileSprite = new Class({
* @param {number} [x] - The x position of this sprite's tiling texture.
* @param {number} [y] - The y position of this sprite's tiling texture.
*
* @return {Phaser.GameObjects.TileSprite} This Tile Sprite instance.
* @return {this} This Tile Sprite instance.
*/
setTilePosition: function (x, y)
{
@ -271,25 +371,52 @@ var TileSprite = new Class({
return this;
},
/**
* Sets {@link Phaser.GameObjects.TileSprite#tileScaleX} and {@link Phaser.GameObjects.TileSprite#tileScaleY}.
*
* @method Phaser.GameObjects.TileSprite#setTileScale
* @since 3.12.0
*
* @param {number} [x] - The horizontal scale of the tiling texture.
* @param {number} [y] - The vertical scale of the tiling texture.
*
* @return {this} This Tile Sprite instance.
*/
setTileScale: function (x, y)
{
if (x !== undefined)
{
this.tilePositionX = x;
}
if (y !== undefined)
{
this.tilePositionY = y;
}
return this;
},
/**
* Render the tile texture if it is dirty, or if the frame has changed.
*
* @method Phaser.GameObjects.TileSprite#updateTileTexture
* @private
* @since 3.0.0
*/
updateTileTexture: function ()
{
var frame = this.frame;
if (!this.dirty && this.oldFrame === frame)
if (!this.dirty)
{
return;
}
this.oldFrame = frame;
// Draw the displayTexture to our fillCanvas
var ctx = this.canvasBufferCtx;
var canvas = this.canvasBuffer;
var frame = this.displayFrame;
var ctx = this.fillContext;
var canvas = this.fillCanvas;
var fw = this.potWidth;
var fh = this.potHeight;
@ -300,7 +427,7 @@ var TileSprite = new Class({
fh = frame.cutHeight;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.clearRect(0, 0, fw, fh);
canvas.width = fw;
canvas.height = fh;
@ -313,7 +440,66 @@ var TileSprite = new Class({
fw, fh
);
this.tileTexture = (this.renderer.gl) ? this.renderer.canvasToTexture(canvas, this.tileTexture) : ctx.createPattern(canvas, 'repeat');
if (this.renderer.gl)
{
this.fillPattern = this.renderer.canvasToTexture(canvas, this.fillPattern);
}
else
{
this.fillPattern = ctx.createPattern(canvas, 'repeat');
}
this.updateCanvas();
this.dirty = false;
},
/**
* Draw the fill pattern to the internal canvas.
*
* @method Phaser.GameObjects.TileSprite#updateCanvas
* @private
* @since 3.12.0
*/
updateCanvas: function ()
{
var canvas = this.canvas;
if (canvas.width !== this.width || canvas.height !== this.height)
{
canvas.width = this.width;
canvas.height = this.height;
this.frame.resize(this.width, this.height);
}
if (!this.dirty || this.renderer && this.renderer.gl)
{
this.dirty = false;
return;
}
var ctx = this.context;
var scaleX = this._tileScale.x;
var scaleY = this._tileScale.y;
var positionX = this._tilePosition.x;
var positionY = this._tilePosition.y;
ctx.clearRect(0, 0, this.width, this.height);
ctx.save();
ctx.scale(scaleX, scaleY);
ctx.translate(-positionX, -positionY);
ctx.fillStyle = this.fillPattern;
ctx.fillRect(positionX, positionY, this.width / scaleX, this.height / scaleY);
ctx.restore();
this.dirty = false;
},
@ -329,16 +515,114 @@ var TileSprite = new Class({
{
if (this.renderer && this.renderer.gl)
{
this.renderer.deleteTexture(this.tileTexture);
this.renderer.deleteTexture(this.fillPattern);
}
CanvasPool.remove(this.canvasBuffer);
CanvasPool.remove(this.canvas);
CanvasPool.remove(this.fillCanvas);
this.tileTexture = null;
this.canvasBufferCtx = null;
this.canvasBuffer = null;
this.fillPattern = null;
this.fillContext = null;
this.fillCanvas = null;
this.displayTexture = null;
this.displayFrame = null;
this.texture.destroy();
this.renderer = null;
},
/**
* The horizontal scroll position of the Tile Sprite.
*
* @name Phaser.GameObjects.TileSprite#tilePositionX
* @type {number}
* @default 0
* @since 3.0.0
*/
tilePositionX: {
get: function ()
{
return this._tilePosition.x;
},
set: function (value)
{
this._tilePosition.x = value;
this.dirty = true;
}
},
/**
* The vertical scroll position of the Tile Sprite.
*
* @name Phaser.GameObjects.TileSprite#tilePositionY
* @type {number}
* @default 0
* @since 3.0.0
*/
tilePositionY: {
get: function ()
{
return this._tilePosition.y;
},
set: function (value)
{
this._tilePosition.y = value;
this.dirty = true;
}
},
/**
* The horizontal scale of the Tile Sprite texture.
*
* @name Phaser.GameObjects.TileSprite#tileScaleX
* @type {number}
* @default 1
* @since 3.11.0
*/
tileScaleX: {
get: function ()
{
return this._tileScale.x;
},
set: function (value)
{
this._tileScale.x = value;
this.dirty = true;
}
},
/**
* The vertical scale of the Tile Sprite texture.
*
* @name Phaser.GameObjects.TileSprite#tileScaleY
* @type {number}
* @default 1
* @since 3.11.0
*/
tileScaleY: {
get: function ()
{
return this._tileScale.y;
},
set: function (value)
{
this._tileScale.y = value;
this.dirty = true;
}
}
});

View file

@ -23,14 +23,14 @@ var Utils = require('../../renderer/webgl/Utils');
*/
var TileSpriteWebGLRenderer = function (renderer, src, interpolationPercentage, camera, parentMatrix)
{
src.updateTileTexture();
src.updateCanvas();
var getTint = Utils.getTintAppendFloatAlpha;
this.pipeline.batchTexture(
src,
src.tileTexture,
src.frame.width * src.tileScaleX, src.frame.height * src.tileScaleY,
src.fillPattern,
src.displayFrame.width * src.tileScaleX, src.displayFrame.height * src.tileScaleY,
src.x, src.y,
src.width, src.height,
src.scaleX, src.scaleY,
@ -44,8 +44,8 @@ var TileSpriteWebGLRenderer = function (renderer, src, interpolationPercentage,
getTint(src._tintBL, camera.alpha * src._alphaBL),
getTint(src._tintBR, camera.alpha * src._alphaBR),
(src._isTinted && src.tintFill),
(src.tilePositionX % src.frame.width) / src.frame.width,
(src.tilePositionY % src.frame.height) / src.frame.height,
(src.tilePositionX % src.displayFrame.width) / src.displayFrame.width,
(src.tilePositionY % src.displayFrame.height) / src.displayFrame.height,
camera,
parentMatrix
);