diff --git a/src/deprecation.js b/src/deprecation.js index b9c73b3..b888ad5 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -918,6 +918,35 @@ }); /** + * @method + * @name PIXI.prepare.BasePrepare#register + * @see PIXI.prepare.BasePrepare#registerFindHook + * @deprecated since version 4.4.2 + * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ +prepare.BasePrepare.prototype.register = function register(addHook, uploadHook) +{ + warn('renderer.plugins.prepare.register is now deprecated, ' + + 'please use renderer.plugins.prepare.registerFindHook & renderer.plugins.prepare.registerUploadHook'); + + if (addHook) + { + this.registerFindHook(addHook); + } + + if (uploadHook) + { + this.registerUploadHook(uploadHook); + } + + return this; +}; + +/** * The number of graphics or textures to upload to the GPU. * * @name PIXI.prepare.canvas.UPLOADS_PER_FRAME diff --git a/src/deprecation.js b/src/deprecation.js index b9c73b3..b888ad5 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -918,6 +918,35 @@ }); /** + * @method + * @name PIXI.prepare.BasePrepare#register + * @see PIXI.prepare.BasePrepare#registerFindHook + * @deprecated since version 4.4.2 + * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ +prepare.BasePrepare.prototype.register = function register(addHook, uploadHook) +{ + warn('renderer.plugins.prepare.register is now deprecated, ' + + 'please use renderer.plugins.prepare.registerFindHook & renderer.plugins.prepare.registerUploadHook'); + + if (addHook) + { + this.registerFindHook(addHook); + } + + if (uploadHook) + { + this.registerUploadHook(uploadHook); + } + + return this; +}; + +/** * The number of graphics or textures to upload to the GPU. * * @name PIXI.prepare.canvas.UPLOADS_PER_FRAME diff --git a/src/prepare/BasePrepare.js b/src/prepare/BasePrepare.js index a4aae2f..5afed00 100644 --- a/src/prepare/BasePrepare.js +++ b/src/prepare/BasePrepare.js @@ -18,6 +18,18 @@ * basic queuing functionality and is extended by {@link PIXI.prepare.WebGLPrepare} and {@link PIXI.prepare.CanvasPrepare} * to provide preparation capabilities specific to their respective renderers. * + * @example + * // Create a sprite + * const sprite = new PIXI.Sprite.fromImage('something.png'); + * + * // Load object into GPU + * app.renderer.plugins.prepare.upload(sprite, () => { + * + * //Texture(s) has been uploaded to GPU + * app.stage.addChild(sprite); + * + * }) + * * @abstract * @class * @memberof PIXI.prepare @@ -100,8 +112,16 @@ this.prepareItems(); }; - this.register(findText, drawText); - this.register(findTextStyle, calculateTextStyle); + // hooks to find the correct texture + this.registerFindHook(findText); + this.registerFindHook(findTextStyle); + this.registerFindHook(findMultipleBaseTextures); + this.registerFindHook(findBaseTexture); + this.registerFindHook(findTexture); + + // upload hooks + this.registerUploadHook(drawText); + this.registerUploadHook(calculateTextStyle); } /** @@ -213,21 +233,31 @@ } /** - * Adds hooks for finding and uploading items. + * Adds hooks for finding items. * - * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` - function must return `true` if it was able to add item to the queue. - * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and - * function must return `true` if it was able to handle upload of item. - * @return {PIXI.CanvasPrepare} Instance of plugin for chaining. + * @param {Function} addHook - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. */ - register(addHook, uploadHook) + registerFindHook(addHook) { if (addHook) { this.addHooks.push(addHook); } + return this; + } + + /** + * Adds hooks for uploading items. + * + * @param {Function} uploadHook - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ + registerUploadHook(uploadHook) + { if (uploadHook) { this.uploadHooks.push(uploadHook); @@ -290,6 +320,88 @@ } /** + * Built-in hook to find multiple textures from objects like AnimatedSprites. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findMultipleBaseTextures(item, queue) +{ + let result = false; + + // Objects with mutliple textures + if (item && item._textures && item._textures.length) + { + for (let i = 0; i < item._textures.length; i++) + { + if (item._textures[i] instanceof core.Texture) + { + const baseTexture = item._textures[i].baseTexture; + + if (queue.indexOf(baseTexture) === -1) + { + queue.push(baseTexture); + result = true; + } + } + } + } + + return result; +} + +/** + * Built-in hook to find BaseTextures from Sprites. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findBaseTexture(item, queue) +{ + // Objects with textures, like Sprites/Text + if (item instanceof core.BaseTexture) + { + if (queue.indexOf(item) === -1) + { + queue.push(item); + } + + return true; + } + + return false; +} + +/** + * Built-in hook to find textures from objects. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findTexture(item, queue) +{ + if (item._texture && item._texture instanceof core.Texture) + { + const texture = item._texture.baseTexture; + + if (queue.indexOf(texture) === -1) + { + queue.push(texture); + } + + return true; + } + + return false; +} + +/** * Built-in hook to draw PIXI.Text to its texture. * * @private diff --git a/src/deprecation.js b/src/deprecation.js index b9c73b3..b888ad5 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -918,6 +918,35 @@ }); /** + * @method + * @name PIXI.prepare.BasePrepare#register + * @see PIXI.prepare.BasePrepare#registerFindHook + * @deprecated since version 4.4.2 + * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ +prepare.BasePrepare.prototype.register = function register(addHook, uploadHook) +{ + warn('renderer.plugins.prepare.register is now deprecated, ' + + 'please use renderer.plugins.prepare.registerFindHook & renderer.plugins.prepare.registerUploadHook'); + + if (addHook) + { + this.registerFindHook(addHook); + } + + if (uploadHook) + { + this.registerUploadHook(uploadHook); + } + + return this; +}; + +/** * The number of graphics or textures to upload to the GPU. * * @name PIXI.prepare.canvas.UPLOADS_PER_FRAME diff --git a/src/prepare/BasePrepare.js b/src/prepare/BasePrepare.js index a4aae2f..5afed00 100644 --- a/src/prepare/BasePrepare.js +++ b/src/prepare/BasePrepare.js @@ -18,6 +18,18 @@ * basic queuing functionality and is extended by {@link PIXI.prepare.WebGLPrepare} and {@link PIXI.prepare.CanvasPrepare} * to provide preparation capabilities specific to their respective renderers. * + * @example + * // Create a sprite + * const sprite = new PIXI.Sprite.fromImage('something.png'); + * + * // Load object into GPU + * app.renderer.plugins.prepare.upload(sprite, () => { + * + * //Texture(s) has been uploaded to GPU + * app.stage.addChild(sprite); + * + * }) + * * @abstract * @class * @memberof PIXI.prepare @@ -100,8 +112,16 @@ this.prepareItems(); }; - this.register(findText, drawText); - this.register(findTextStyle, calculateTextStyle); + // hooks to find the correct texture + this.registerFindHook(findText); + this.registerFindHook(findTextStyle); + this.registerFindHook(findMultipleBaseTextures); + this.registerFindHook(findBaseTexture); + this.registerFindHook(findTexture); + + // upload hooks + this.registerUploadHook(drawText); + this.registerUploadHook(calculateTextStyle); } /** @@ -213,21 +233,31 @@ } /** - * Adds hooks for finding and uploading items. + * Adds hooks for finding items. * - * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` - function must return `true` if it was able to add item to the queue. - * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and - * function must return `true` if it was able to handle upload of item. - * @return {PIXI.CanvasPrepare} Instance of plugin for chaining. + * @param {Function} addHook - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. */ - register(addHook, uploadHook) + registerFindHook(addHook) { if (addHook) { this.addHooks.push(addHook); } + return this; + } + + /** + * Adds hooks for uploading items. + * + * @param {Function} uploadHook - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ + registerUploadHook(uploadHook) + { if (uploadHook) { this.uploadHooks.push(uploadHook); @@ -290,6 +320,88 @@ } /** + * Built-in hook to find multiple textures from objects like AnimatedSprites. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findMultipleBaseTextures(item, queue) +{ + let result = false; + + // Objects with mutliple textures + if (item && item._textures && item._textures.length) + { + for (let i = 0; i < item._textures.length; i++) + { + if (item._textures[i] instanceof core.Texture) + { + const baseTexture = item._textures[i].baseTexture; + + if (queue.indexOf(baseTexture) === -1) + { + queue.push(baseTexture); + result = true; + } + } + } + } + + return result; +} + +/** + * Built-in hook to find BaseTextures from Sprites. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findBaseTexture(item, queue) +{ + // Objects with textures, like Sprites/Text + if (item instanceof core.BaseTexture) + { + if (queue.indexOf(item) === -1) + { + queue.push(item); + } + + return true; + } + + return false; +} + +/** + * Built-in hook to find textures from objects. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findTexture(item, queue) +{ + if (item._texture && item._texture instanceof core.Texture) + { + const texture = item._texture.baseTexture; + + if (queue.indexOf(texture) === -1) + { + queue.push(texture); + } + + return true; + } + + return false; +} + +/** * Built-in hook to draw PIXI.Text to its texture. * * @private diff --git a/src/prepare/canvas/CanvasPrepare.js b/src/prepare/canvas/CanvasPrepare.js index 0224bb5..09c207a 100644 --- a/src/prepare/canvas/CanvasPrepare.js +++ b/src/prepare/canvas/CanvasPrepare.js @@ -43,7 +43,7 @@ this.ctx = this.canvas.getContext('2d'); // Add textures to upload - this.register(findBaseTextures, uploadBaseTextures); + this.registerUploadHook(uploadBaseTextures); } /** @@ -89,39 +89,4 @@ return false; } -/** - * Built-in hook to find textures from Sprites. - * - * @private - * @param {PIXI.DisplayObject} item -Display object to check - * @param {Array<*>} queue - Collection of items to upload - * @return {boolean} if a PIXI.Texture object was found. - */ -function findBaseTextures(item, queue) -{ - // Objects with textures, like Sprites/Text - if (item instanceof core.BaseTexture) - { - if (queue.indexOf(item) === -1) - { - queue.push(item); - } - - return true; - } - else if (item._texture && item._texture instanceof core.Texture) - { - const texture = item._texture.baseTexture; - - if (queue.indexOf(texture) === -1) - { - queue.push(texture); - } - - return true; - } - - return false; -} - core.CanvasRenderer.registerPlugin('prepare', CanvasPrepare); diff --git a/src/deprecation.js b/src/deprecation.js index b9c73b3..b888ad5 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -918,6 +918,35 @@ }); /** + * @method + * @name PIXI.prepare.BasePrepare#register + * @see PIXI.prepare.BasePrepare#registerFindHook + * @deprecated since version 4.4.2 + * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ +prepare.BasePrepare.prototype.register = function register(addHook, uploadHook) +{ + warn('renderer.plugins.prepare.register is now deprecated, ' + + 'please use renderer.plugins.prepare.registerFindHook & renderer.plugins.prepare.registerUploadHook'); + + if (addHook) + { + this.registerFindHook(addHook); + } + + if (uploadHook) + { + this.registerUploadHook(uploadHook); + } + + return this; +}; + +/** * The number of graphics or textures to upload to the GPU. * * @name PIXI.prepare.canvas.UPLOADS_PER_FRAME diff --git a/src/prepare/BasePrepare.js b/src/prepare/BasePrepare.js index a4aae2f..5afed00 100644 --- a/src/prepare/BasePrepare.js +++ b/src/prepare/BasePrepare.js @@ -18,6 +18,18 @@ * basic queuing functionality and is extended by {@link PIXI.prepare.WebGLPrepare} and {@link PIXI.prepare.CanvasPrepare} * to provide preparation capabilities specific to their respective renderers. * + * @example + * // Create a sprite + * const sprite = new PIXI.Sprite.fromImage('something.png'); + * + * // Load object into GPU + * app.renderer.plugins.prepare.upload(sprite, () => { + * + * //Texture(s) has been uploaded to GPU + * app.stage.addChild(sprite); + * + * }) + * * @abstract * @class * @memberof PIXI.prepare @@ -100,8 +112,16 @@ this.prepareItems(); }; - this.register(findText, drawText); - this.register(findTextStyle, calculateTextStyle); + // hooks to find the correct texture + this.registerFindHook(findText); + this.registerFindHook(findTextStyle); + this.registerFindHook(findMultipleBaseTextures); + this.registerFindHook(findBaseTexture); + this.registerFindHook(findTexture); + + // upload hooks + this.registerUploadHook(drawText); + this.registerUploadHook(calculateTextStyle); } /** @@ -213,21 +233,31 @@ } /** - * Adds hooks for finding and uploading items. + * Adds hooks for finding items. * - * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` - function must return `true` if it was able to add item to the queue. - * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and - * function must return `true` if it was able to handle upload of item. - * @return {PIXI.CanvasPrepare} Instance of plugin for chaining. + * @param {Function} addHook - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. */ - register(addHook, uploadHook) + registerFindHook(addHook) { if (addHook) { this.addHooks.push(addHook); } + return this; + } + + /** + * Adds hooks for uploading items. + * + * @param {Function} uploadHook - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ + registerUploadHook(uploadHook) + { if (uploadHook) { this.uploadHooks.push(uploadHook); @@ -290,6 +320,88 @@ } /** + * Built-in hook to find multiple textures from objects like AnimatedSprites. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findMultipleBaseTextures(item, queue) +{ + let result = false; + + // Objects with mutliple textures + if (item && item._textures && item._textures.length) + { + for (let i = 0; i < item._textures.length; i++) + { + if (item._textures[i] instanceof core.Texture) + { + const baseTexture = item._textures[i].baseTexture; + + if (queue.indexOf(baseTexture) === -1) + { + queue.push(baseTexture); + result = true; + } + } + } + } + + return result; +} + +/** + * Built-in hook to find BaseTextures from Sprites. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findBaseTexture(item, queue) +{ + // Objects with textures, like Sprites/Text + if (item instanceof core.BaseTexture) + { + if (queue.indexOf(item) === -1) + { + queue.push(item); + } + + return true; + } + + return false; +} + +/** + * Built-in hook to find textures from objects. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findTexture(item, queue) +{ + if (item._texture && item._texture instanceof core.Texture) + { + const texture = item._texture.baseTexture; + + if (queue.indexOf(texture) === -1) + { + queue.push(texture); + } + + return true; + } + + return false; +} + +/** * Built-in hook to draw PIXI.Text to its texture. * * @private diff --git a/src/prepare/canvas/CanvasPrepare.js b/src/prepare/canvas/CanvasPrepare.js index 0224bb5..09c207a 100644 --- a/src/prepare/canvas/CanvasPrepare.js +++ b/src/prepare/canvas/CanvasPrepare.js @@ -43,7 +43,7 @@ this.ctx = this.canvas.getContext('2d'); // Add textures to upload - this.register(findBaseTextures, uploadBaseTextures); + this.registerUploadHook(uploadBaseTextures); } /** @@ -89,39 +89,4 @@ return false; } -/** - * Built-in hook to find textures from Sprites. - * - * @private - * @param {PIXI.DisplayObject} item -Display object to check - * @param {Array<*>} queue - Collection of items to upload - * @return {boolean} if a PIXI.Texture object was found. - */ -function findBaseTextures(item, queue) -{ - // Objects with textures, like Sprites/Text - if (item instanceof core.BaseTexture) - { - if (queue.indexOf(item) === -1) - { - queue.push(item); - } - - return true; - } - else if (item._texture && item._texture instanceof core.Texture) - { - const texture = item._texture.baseTexture; - - if (queue.indexOf(texture) === -1) - { - queue.push(texture); - } - - return true; - } - - return false; -} - core.CanvasRenderer.registerPlugin('prepare', CanvasPrepare); diff --git a/src/prepare/webgl/WebGLPrepare.js b/src/prepare/webgl/WebGLPrepare.js index eb7023f..9df3c3f 100644 --- a/src/prepare/webgl/WebGLPrepare.js +++ b/src/prepare/webgl/WebGLPrepare.js @@ -22,12 +22,11 @@ this.uploadHookHelper = this.renderer; // Add textures and graphics to upload - this.register(findBaseTextures, uploadBaseTextures) - .register(findGraphics, uploadGraphics); + this.registerFindHook(findGraphics); + this.registerUploadHook(uploadBaseTextures); + this.registerUploadHook(uploadGraphics); } - } - /** * Built-in hook to upload PIXI.Texture objects to the GPU. * @@ -80,41 +79,6 @@ } /** - * Built-in hook to find textures from Sprites. - * - * @private - * @param {PIXI.DisplayObject} item - Display object to check - * @param {Array<*>} queue - Collection of items to upload - * @return {boolean} if a PIXI.Texture object was found. - */ -function findBaseTextures(item, queue) -{ - // Objects with textures, like Sprites/Text - if (item instanceof core.BaseTexture) - { - if (queue.indexOf(item) === -1) - { - queue.push(item); - } - - return true; - } - else if (item._texture && item._texture instanceof core.Texture) - { - const texture = item._texture.baseTexture; - - if (queue.indexOf(texture) === -1) - { - queue.push(texture); - } - - return true; - } - - return false; -} - -/** * Built-in hook to find graphics. * * @private diff --git a/src/deprecation.js b/src/deprecation.js index b9c73b3..b888ad5 100644 --- a/src/deprecation.js +++ b/src/deprecation.js @@ -918,6 +918,35 @@ }); /** + * @method + * @name PIXI.prepare.BasePrepare#register + * @see PIXI.prepare.BasePrepare#registerFindHook + * @deprecated since version 4.4.2 + * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ +prepare.BasePrepare.prototype.register = function register(addHook, uploadHook) +{ + warn('renderer.plugins.prepare.register is now deprecated, ' + + 'please use renderer.plugins.prepare.registerFindHook & renderer.plugins.prepare.registerUploadHook'); + + if (addHook) + { + this.registerFindHook(addHook); + } + + if (uploadHook) + { + this.registerUploadHook(uploadHook); + } + + return this; +}; + +/** * The number of graphics or textures to upload to the GPU. * * @name PIXI.prepare.canvas.UPLOADS_PER_FRAME diff --git a/src/prepare/BasePrepare.js b/src/prepare/BasePrepare.js index a4aae2f..5afed00 100644 --- a/src/prepare/BasePrepare.js +++ b/src/prepare/BasePrepare.js @@ -18,6 +18,18 @@ * basic queuing functionality and is extended by {@link PIXI.prepare.WebGLPrepare} and {@link PIXI.prepare.CanvasPrepare} * to provide preparation capabilities specific to their respective renderers. * + * @example + * // Create a sprite + * const sprite = new PIXI.Sprite.fromImage('something.png'); + * + * // Load object into GPU + * app.renderer.plugins.prepare.upload(sprite, () => { + * + * //Texture(s) has been uploaded to GPU + * app.stage.addChild(sprite); + * + * }) + * * @abstract * @class * @memberof PIXI.prepare @@ -100,8 +112,16 @@ this.prepareItems(); }; - this.register(findText, drawText); - this.register(findTextStyle, calculateTextStyle); + // hooks to find the correct texture + this.registerFindHook(findText); + this.registerFindHook(findTextStyle); + this.registerFindHook(findMultipleBaseTextures); + this.registerFindHook(findBaseTexture); + this.registerFindHook(findTexture); + + // upload hooks + this.registerUploadHook(drawText); + this.registerUploadHook(calculateTextStyle); } /** @@ -213,21 +233,31 @@ } /** - * Adds hooks for finding and uploading items. + * Adds hooks for finding items. * - * @param {Function} [addHook] - Function call that takes two parameters: `item:*, queue:Array` - function must return `true` if it was able to add item to the queue. - * @param {Function} [uploadHook] - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and - * function must return `true` if it was able to handle upload of item. - * @return {PIXI.CanvasPrepare} Instance of plugin for chaining. + * @param {Function} addHook - Function call that takes two parameters: `item:*, queue:Array` + * function must return `true` if it was able to add item to the queue. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. */ - register(addHook, uploadHook) + registerFindHook(addHook) { if (addHook) { this.addHooks.push(addHook); } + return this; + } + + /** + * Adds hooks for uploading items. + * + * @param {Function} uploadHook - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and + * function must return `true` if it was able to handle upload of item. + * @return {PIXI.BasePrepare} Instance of plugin for chaining. + */ + registerUploadHook(uploadHook) + { if (uploadHook) { this.uploadHooks.push(uploadHook); @@ -290,6 +320,88 @@ } /** + * Built-in hook to find multiple textures from objects like AnimatedSprites. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findMultipleBaseTextures(item, queue) +{ + let result = false; + + // Objects with mutliple textures + if (item && item._textures && item._textures.length) + { + for (let i = 0; i < item._textures.length; i++) + { + if (item._textures[i] instanceof core.Texture) + { + const baseTexture = item._textures[i].baseTexture; + + if (queue.indexOf(baseTexture) === -1) + { + queue.push(baseTexture); + result = true; + } + } + } + } + + return result; +} + +/** + * Built-in hook to find BaseTextures from Sprites. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findBaseTexture(item, queue) +{ + // Objects with textures, like Sprites/Text + if (item instanceof core.BaseTexture) + { + if (queue.indexOf(item) === -1) + { + queue.push(item); + } + + return true; + } + + return false; +} + +/** + * Built-in hook to find textures from objects. + * + * @private + * @param {PIXI.DisplayObject} item - Display object to check + * @param {Array<*>} queue - Collection of items to upload + * @return {boolean} if a PIXI.Texture object was found. + */ +function findTexture(item, queue) +{ + if (item._texture && item._texture instanceof core.Texture) + { + const texture = item._texture.baseTexture; + + if (queue.indexOf(texture) === -1) + { + queue.push(texture); + } + + return true; + } + + return false; +} + +/** * Built-in hook to draw PIXI.Text to its texture. * * @private diff --git a/src/prepare/canvas/CanvasPrepare.js b/src/prepare/canvas/CanvasPrepare.js index 0224bb5..09c207a 100644 --- a/src/prepare/canvas/CanvasPrepare.js +++ b/src/prepare/canvas/CanvasPrepare.js @@ -43,7 +43,7 @@ this.ctx = this.canvas.getContext('2d'); // Add textures to upload - this.register(findBaseTextures, uploadBaseTextures); + this.registerUploadHook(uploadBaseTextures); } /** @@ -89,39 +89,4 @@ return false; } -/** - * Built-in hook to find textures from Sprites. - * - * @private - * @param {PIXI.DisplayObject} item -Display object to check - * @param {Array<*>} queue - Collection of items to upload - * @return {boolean} if a PIXI.Texture object was found. - */ -function findBaseTextures(item, queue) -{ - // Objects with textures, like Sprites/Text - if (item instanceof core.BaseTexture) - { - if (queue.indexOf(item) === -1) - { - queue.push(item); - } - - return true; - } - else if (item._texture && item._texture instanceof core.Texture) - { - const texture = item._texture.baseTexture; - - if (queue.indexOf(texture) === -1) - { - queue.push(texture); - } - - return true; - } - - return false; -} - core.CanvasRenderer.registerPlugin('prepare', CanvasPrepare); diff --git a/src/prepare/webgl/WebGLPrepare.js b/src/prepare/webgl/WebGLPrepare.js index eb7023f..9df3c3f 100644 --- a/src/prepare/webgl/WebGLPrepare.js +++ b/src/prepare/webgl/WebGLPrepare.js @@ -22,12 +22,11 @@ this.uploadHookHelper = this.renderer; // Add textures and graphics to upload - this.register(findBaseTextures, uploadBaseTextures) - .register(findGraphics, uploadGraphics); + this.registerFindHook(findGraphics); + this.registerUploadHook(uploadBaseTextures); + this.registerUploadHook(uploadGraphics); } - } - /** * Built-in hook to upload PIXI.Texture objects to the GPU. * @@ -80,41 +79,6 @@ } /** - * Built-in hook to find textures from Sprites. - * - * @private - * @param {PIXI.DisplayObject} item - Display object to check - * @param {Array<*>} queue - Collection of items to upload - * @return {boolean} if a PIXI.Texture object was found. - */ -function findBaseTextures(item, queue) -{ - // Objects with textures, like Sprites/Text - if (item instanceof core.BaseTexture) - { - if (queue.indexOf(item) === -1) - { - queue.push(item); - } - - return true; - } - else if (item._texture && item._texture instanceof core.Texture) - { - const texture = item._texture.baseTexture; - - if (queue.indexOf(texture) === -1) - { - queue.push(texture); - } - - return true; - } - - return false; -} - -/** * Built-in hook to find graphics. * * @private diff --git a/test/prepare/BasePrepare.js b/test/prepare/BasePrepare.js index 3cfa476..26dc8ad 100644 --- a/test/prepare/BasePrepare.js +++ b/test/prepare/BasePrepare.js @@ -10,7 +10,7 @@ expect(prep.renderer).to.equal(renderer); expect(prep.uploadHookHelper).to.be.null; expect(prep.queue).to.be.empty; - expect(prep.addHooks).to.have.lengthOf(2); + expect(prep.addHooks).to.have.lengthOf(5); expect(prep.uploadHooks).to.have.lengthOf(2); expect(prep.completes).to.be.empty; @@ -23,10 +23,11 @@ function uploadHook() { /* empty */ } const prep = new PIXI.prepare.BasePrepare(); - prep.register(addHook, uploadHook); + prep.registerFindHook(addHook); + prep.registerUploadHook(uploadHook); expect(prep.addHooks).to.contain(addHook); - expect(prep.addHooks).to.have.lengthOf(3); + expect(prep.addHooks).to.have.lengthOf(6); expect(prep.uploadHooks).to.contain(uploadHook); expect(prep.uploadHooks).to.have.lengthOf(3); @@ -58,7 +59,8 @@ }); const complete = sinon.spy(function () { /* empty */ }); - prep.register(addHook, uploadHook); + prep.registerFindHook(addHook); + prep.registerUploadHook(uploadHook); prep.upload(uploadItem, complete); expect(prep.queue).to.contain(uploadItem); @@ -82,7 +84,7 @@ } const complete = sinon.spy(function () { /* empty */ }); - prep.register(addHook); + prep.registerFindHook(addHook); prep.upload({}, complete); expect(complete.calledOnce).to.be.true; @@ -106,7 +108,8 @@ }); const complete = sinon.spy(function () { /* empty */ }); - prep.register(addHook, uploadHook); + prep.registerFindHook(addHook); + prep.registerUploadHook(uploadHook); prep.upload({}, complete); expect(prep.queue).to.have.lengthOf(1); @@ -137,7 +140,8 @@ }); const complete = sinon.spy(function () { /* empty */ }); - prep.register(addHook, uploadHook); + prep.registerFindHook(addHook); + prep.registerUploadHook(uploadHook); const item = {}; prep.upload(item, complete); @@ -181,7 +185,8 @@ done(); } - prep.register(addHook, uploadHook); + prep.registerFindHook(addHook); + prep.registerUploadHook(uploadHook); prep.upload({}, complete); expect(prep.queue).to.have.lengthOf(1);