Newer
Older
pixi.js / src / extras / TilingSprite.js
@Steffen Bär Steffen Bär on 24 Sep 2016 13 KB Prefer const (#3002)
import core from '../core';
import Texture from '../core/textures/Texture';
import CanvasTinter from '../core/sprites/canvas/CanvasTinter';
import TilingShader from './webgl/TilingShader';

const tempArray = new Float32Array(4);
const tempPoint = new core.Point();
/**
 * A tiling sprite is a fast way of rendering a tiling image
 *
 * @class
 * @extends PIXI.Sprite
 * @memberof PIXI.extras
 * @param texture {PIXI.Texture} the texture of the tiling sprite
 * @param [width=100] {number}  the width of the tiling sprite
 * @param [height=100] {number} the height of the tiling sprite
 */
class TilingSprite extends core.Sprite
{
    constructor(texture, width=100, height=100)
    {
        super(texture);

        /**
         * The scaling of the image that is being tiled
         *
         * @member {PIXI.Point}
         */
        this.tileScale = new core.Point(1,1);


        /**
         * The offset position of the image that is being tiled
         *
         * @member {PIXI.Point}
         */
        this.tilePosition = new core.Point(0,0);

        ///// private

        /**
         * The with of the tiling sprite
         *
         * @member {number}
         * @private
         */
        this._width = width;

        /**
         * The height of the tiling sprite
         *
         * @member {number}
         * @private
         */
        this._height = height;

        /**
         * An internal WebGL UV cache.
         *
         * @member {PIXI.TextureUvs}
         * @private
         */
        this._uvs = new core.TextureUvs();

        this._canvasPattern = null;

        this._glDatas = [];
    }

    _onTextureUpdate()
    {
        return;
    }


    /**
     * Renders the object using the WebGL renderer
     *
     * @param renderer {PIXI.WebGLRenderer} The renderer
     * @private
     */
    _renderWebGL(renderer)
    {

        // tweak our texture temporarily..
        const texture = this._texture;

        if(!texture || !texture._uvs)
        {
            return;
        }

         // get rid of any thing that may be batching.
        renderer.flush();

        const gl = renderer.gl;
        let glData = this._glDatas[renderer.CONTEXT_UID];

        if(!glData)
        {
            glData = {
                shader:new TilingShader(gl),
                quad:new core.Quad(gl)
            };

            this._glDatas[renderer.CONTEXT_UID] = glData;

            glData.quad.initVao(glData.shader);
        }

        // if the sprite is trimmed and is not a tilingsprite then we need to add the extra space before transforming the sprite coords..
        const vertices = glData.quad.vertices;

        vertices[0] = vertices[6] = ( this._width ) * -this.anchor.x;
        vertices[1] = vertices[3] = this._height * -this.anchor.y;

        vertices[2] = vertices[4] = ( this._width ) * (1-this.anchor.x);
        vertices[5] = vertices[7] = this._height * (1-this.anchor.y);

        glData.quad.upload();

        renderer.bindShader(glData.shader);

        const textureUvs = texture._uvs,
            textureWidth = texture._frame.width,
            textureHeight = texture._frame.height,
            textureBaseWidth = texture.baseTexture.width,
            textureBaseHeight = texture.baseTexture.height;

        const uPixelSize = glData.shader.uniforms.uPixelSize;
        uPixelSize[0] = 1.0/textureBaseWidth;
        uPixelSize[1] = 1.0/textureBaseHeight;
        glData.shader.uniforms.uPixelSize = uPixelSize;

        const uFrame = glData.shader.uniforms.uFrame;
        uFrame[0] = textureUvs.x0;
        uFrame[1] = textureUvs.y0;
        uFrame[2] = textureUvs.x1 - textureUvs.x0;
        uFrame[3] = textureUvs.y2 - textureUvs.y0;
        glData.shader.uniforms.uFrame = uFrame;

        const uTransform = glData.shader.uniforms.uTransform;
        uTransform[0] = (this.tilePosition.x % (textureWidth * this.tileScale.x)) / this._width;
        uTransform[1] = (this.tilePosition.y % (textureHeight * this.tileScale.y)) / this._height;
        uTransform[2] = ( textureBaseWidth / this._width ) * this.tileScale.x;
        uTransform[3] = ( textureBaseHeight / this._height ) * this.tileScale.y;
        glData.shader.uniforms.uTransform = uTransform;

        glData.shader.uniforms.translationMatrix = this.worldTransform.toArray(true);

        const color = tempArray;

        core.utils.hex2rgb(this.tint, color);
        color[3] = this.worldAlpha;

        glData.shader.uniforms.uColor = color;

        renderer.bindTexture(this._texture, 0);

        renderer.state.setBlendMode( this.blendMode );
        glData.quad.draw();
    }

    /**
     * Renders the object using the Canvas renderer
     *
     * @param renderer {PIXI.CanvasRenderer} a reference to the canvas renderer
     * @private
     */
    _renderCanvas(renderer)
    {
        const texture = this._texture;

        if (!texture.baseTexture.hasLoaded)
        {
          return;
        }

        const context = renderer.context,
            transform = this.worldTransform,
            resolution = renderer.resolution,
            baseTexture = texture.baseTexture,
            modX = (this.tilePosition.x / this.tileScale.x) % texture._frame.width,
            modY = (this.tilePosition.y / this.tileScale.y) % texture._frame.height;

        // create a nice shiny pattern!
        // TODO this needs to be refreshed if texture changes..
        if(!this._canvasPattern)
        {
            // cut an object from a spritesheet..
            const tempCanvas = new core.CanvasRenderTarget(texture._frame.width, texture._frame.height);

            // Tint the tiling sprite
            if (this.tint !== 0xFFFFFF)
            {
                if (this.cachedTint !== this.tint)
                {
                    this.cachedTint = this.tint;

                    this.tintedTexture = CanvasTinter.getTintedTexture(this, this.tint);
                }
                tempCanvas.context.drawImage(this.tintedTexture, 0, 0);
            }
            else
            {
                tempCanvas.context.drawImage(baseTexture.source, -texture._frame.x, -texture._frame.y);
            }
            this._canvasPattern = tempCanvas.context.createPattern( tempCanvas.canvas, 'repeat' );
        }

        // set context state..
        context.globalAlpha = this.worldAlpha;
        context.setTransform(transform.a * resolution,
                           transform.b * resolution,
                           transform.c * resolution,
                           transform.d * resolution,
                           transform.tx * resolution,
                           transform.ty * resolution);

        // TODO - this should be rolled into the setTransform above..
        context.scale(this.tileScale.x,this.tileScale.y);

        context.translate(modX + (this.anchor.x * -this._width ),
                          modY + (this.anchor.y * -this._height));

        // check blend mode
        const compositeOperation = renderer.blendModes[this.blendMode];
        if (compositeOperation !== renderer.context.globalCompositeOperation)
        {
            context.globalCompositeOperation = compositeOperation;
        }

        // fill the pattern!
        context.fillStyle = this._canvasPattern;
        context.fillRect(-modX,
                         -modY,
                         this._width / this.tileScale.x,
                         this._height / this.tileScale.y);


        //TODO - pretty sure this can be deleted...
        //context.translate(-this.tilePosition.x + (this.anchor.x * this._width), -this.tilePosition.y + (this.anchor.y * this._height));
        //context.scale(1 / this.tileScale.x, 1 / this.tileScale.y);
    }


    /**
     * Returns the framing rectangle of the sprite as a Rectangle object
    *
     * @return {PIXI.Rectangle} the framing rectangle
     */
    getBounds()
    {
        const width = this._width;
        const height = this._height;

        const w0 = width * (1-this.anchor.x);
        const w1 = width * -this.anchor.x;

        const h0 = height * (1-this.anchor.y);
        const h1 = height * -this.anchor.y;

        const worldTransform = this.worldTransform;

        const a = worldTransform.a;
        const b = worldTransform.b;
        const c = worldTransform.c;
        const d = worldTransform.d;
        const tx = worldTransform.tx;
        const ty = worldTransform.ty;

        const x1 = a * w1 + c * h1 + tx;
        const y1 = d * h1 + b * w1 + ty;

        const x2 = a * w0 + c * h1 + tx;
        const y2 = d * h1 + b * w0 + ty;

        const x3 = a * w0 + c * h0 + tx;
        const y3 = d * h0 + b * w0 + ty;

        const x4 =  a * w1 + c * h0 + tx;
        const y4 =  d * h0 + b * w1 + ty;

        let minX,
            maxX,
            minY,
            maxY;

        minX = x1;
        minX = x2 < minX ? x2 : minX;
        minX = x3 < minX ? x3 : minX;
        minX = x4 < minX ? x4 : minX;

        minY = y1;
        minY = y2 < minY ? y2 : minY;
        minY = y3 < minY ? y3 : minY;
        minY = y4 < minY ? y4 : minY;

        maxX = x1;
        maxX = x2 > maxX ? x2 : maxX;
        maxX = x3 > maxX ? x3 : maxX;
        maxX = x4 > maxX ? x4 : maxX;

        maxY = y1;
        maxY = y2 > maxY ? y2 : maxY;
        maxY = y3 > maxY ? y3 : maxY;
        maxY = y4 > maxY ? y4 : maxY;

        const bounds = this._bounds;

        bounds.x = minX;
        bounds.width = maxX - minX;

        bounds.y = minY;
        bounds.height = maxY - minY;

        // store a reference so that if this function gets called again in the render cycle we do not have to recalculate
        this._currentBounds = bounds;

        return bounds;
    }

    /**
     * Checks if a point is inside this tiling sprite
     * @param point {PIXI.Point} the point to check
     */
    containsPoint( point )
    {
        this.worldTransform.applyInverse(point,  tempPoint);

        const width = this._width;
        const height = this._height;
        const x1 = -width * this.anchor.x;

        if ( tempPoint.x > x1 && tempPoint.x < x1 + width )
        {
            const y1 = -height * this.anchor.y;

            if ( tempPoint.y > y1 && tempPoint.y < y1 + height )
            {
                return true;
            }
        }

        return false;
    }

    /**
     * Destroys this tiling sprite
     *
     */
    destroy()
    {
        super.destroy();

        this.tileScale = null;
        this._tileScaleOffset = null;
        this.tilePosition = null;

        this._uvs = null;
    }

    /**
     * Helper function that creates a new tiling sprite based on the source you provide.
     * The source can be - frame id, image url, video url, canvas element, video element, base texture
     *
     * @static
     * @param {number|string|PIXI.BaseTexture|HTMLCanvasElement|HTMLVideoElement} source Source to create texture from
    * @param width {number}  the width of the tiling sprite
     * @param height {number} the height of the tiling sprite
     * @return {PIXI.Texture} The newly created texture
     */
    static from(source,width,height)
    {
        return new TilingSprite(Texture.from(source),width,height);
    }


    /**
     * Helper function that creates a tiling sprite that will use a texture from the TextureCache based on the frameId
     * The frame ids are created when a Texture packer file has been loaded
     *
     * @static
     * @param frameId {string} The frame Id of the texture in the cache
     * @return {PIXI.extras.TilingSprite} A new TilingSprite using a texture from the texture cache matching the frameId
     * @param width {number}  the width of the tiling sprite
     * @param height {number} the height of the tiling sprite
     * @return {PIXI.extras.TilingSprite} A new TilingSprite
     */
    static fromFrame(frameId,width,height)
    {
        const texture = core.utils.TextureCache[frameId];

        if (!texture)
        {
            throw new Error('The frameId "' + frameId + '" does not exist in the texture cache ' + this);
        }

        return new TilingSprite(texture,width,height);
    }

    /**
     * Helper function that creates a sprite that will contain a texture based on an image url
     * If the image is not in the texture cache it will be loaded
     *
     * @static
     * @param imageId {string} The image url of the texture
     * @param width {number}  the width of the tiling sprite
     * @param height {number} the height of the tiling sprite
     * @param [crossorigin=(auto)] {boolean} if you want to specify the cross-origin parameter
     * @param [scaleMode=PIXI.SCALE_MODES.DEFAULT] {number} if you want to specify the scale mode, see {@link PIXI.SCALE_MODES} for possible values
     * @return {PIXI.extras.TilingSprite} A new TilingSprite using a texture from the texture cache matching the image id
     */
    static fromImage(imageId, width, height, crossorigin, scaleMode)
    {
        return new TilingSprite(core.Texture.fromImage(imageId, crossorigin, scaleMode),width,height);
    }

    /**
     * The width of the sprite, setting this will actually modify the scale to achieve the value set
     *
     * @member {number}
     * @memberof PIXI.extras.TilingSprite#
     */
    get width()
    {
        return this._width;
    }
    set width(value)
    {
        this._width = value;
    }

    /**
     * The height of the TilingSprite, setting this will actually modify the scale to achieve the value set
     *
     * @member {number}
     * @memberof PIXI.extras.TilingSprite#
     */
    get height()
    {
        return this._height;
    }
    set height(value)
    {
        this._height = value;
    }
}

export default TilingSprite;