Skip to content

Commit

Permalink
Added utils.upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Karl committed May 17, 2016
1 parent 3886f18 commit f25dd33
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 28 deletions.
69 changes: 41 additions & 28 deletions src/animate/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,65 +31,78 @@
/**
* Load the stage class and preload any assets
* @method load
* @param {Function} StageRef Reference to the stage class
* @param {Array} [StageRef.assets] Assets used to preload
* @param {PIXI.Container} parent The Container to auto-add the stage to.
* @param {Function} [complete] Function to call when complete
* @param {String} [assetBaseDir] Base root directory
* @param {Object} options Options for loading.
* @param {Function} options.stage Reference to the stage class
* @param {Object} [options.stage.assets] Assets used to preload
* @param {PIXI.Container} options.parent The Container to auto-add the stage to.
* @param {String} [options.basePath] Base root directory
*/
/**
* Load the stage class and preload any assets
* @method load
* @param {Function} StageRef Reference to the stage class
* @param {Array} [StageRef.assets] Assets used to preload
* @param {PIXI.Container} parent The Container to auto-add the stage to.
* @param {String} [assetBaseDir] Base root directory
* @param {Function} StageRef Reference to the stage class.
* @param {Object} [StageRef.assets] Assets used to preload.
* @param {Function} complete The callback function when complete.
*/
/**
* Load the stage class and preload any assets
* @method load
* @param {Function} StageRef Reference to the stage class
* @param {Array} [StageRef.assets] Assets used to preload
* @param {Function} complete The callback function when complete.
* @param {String} [assetBaseDir] Base root directory
* @param {Function} StageRef Reference to the stage class.
* @param {Object} [StageRef.assets] Assets used to preload.
* @param {PIXI.Container} parent The Container to auto-add the stage to.
*/
const load = function(StageRef, parent, complete, assetBaseDir) {
// Support arguments (ref, complete, assetBaseDir)
const load = function(options, parent, complete, basePath) {

// Support arguments (ref, complete, basePath)
if (typeof parent === "function") {
assetBaseDir = complete;
basePath = complete;
complete = parent;
parent = null;
} else {
if (typeof complete === "string") {
assetBaseDir = complete;
basePath = complete;
complete = null;
}
}

// Root load directory
assetBaseDir = assetBaseDir || "";
if (typeof options === "function") {
options = {
stage: options,
parent: parent,
basePath: basePath || "",
complete: complete
};
}

options = Object.assign({
stage: null,
parent: null,
basePath: '',
complete: null
}, options || {});

let assets = StageRef.assets || [];
const loader = new PIXI.loaders.Loader();

function done() {
let stage = new StageRef();
if (parent) {
parent.addChild(stage);
let instance = new options.stage();
if (options.parent) {
options.parent.addChild(instance);
}
if (complete) {
complete(stage);
if (options.complete) {
options.complete(instance);
}
}

// Check for assets to preload
let assets = options.stage.assets || {};
if (assets && Object.keys(assets).length) {
// assetBaseDir can accept either with trailing slash or not
if (assetBaseDir) {
assetBaseDir += "/";
let basePath = options.basePath;
if (basePath) {
basePath += "/";
}
for (let id in assets) {
loader.add(id, assetBaseDir + assets[id]);
loader.add(id, basePath + assets[id]);
}
loader.once('complete', done).load();
} else {
Expand Down
117 changes: 117 additions & 0 deletions src/animate/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
const SharedTicker = PIXI.ticker.shared;

// Number of assets to upload per frame
let _uploadsPerFrame = 4;

// Collectsion of graphics and textures
const _graphics = [];
const _textures = [];

/**
* @namespace PIXI.animate
* @class utils
Expand Down Expand Up @@ -184,4 +193,112 @@ export default class AnimateUtils {
}
}
}

/**
* The number of graphics or textures to upload to the GPU, if using
* utils.upload and WebGLRenderer
* @property {int} UPLOADS_PER_FRAME
* @static
* @default 4
*/
static set UPLOADS_PER_FRAME(value) {
_uploadsPerFrame = value;
}
static get UPLOADS_PER_FRAME() {
return _uploadsPerFrame;
}

/**
* Upload all the textures and graphics to the GPU.
* @method upload
* @static
* @param {PIXI.WebGLRenderer} renderer Render to upload to
* @param {PIXI.DisplayObject} clip MovieClip to upload
* @param {function} done When complete
*/
static upload(renderer, displayObject, done) {

// No need to upload if CanvasRenderer
if (!(renderer instanceof PIXI.WebGLRenderer)) {
return done();
}

// Get global properties
const textures = _textures;
const graphics = _graphics;

// Get the items for upload from the display
this.getUploadable(displayObject, textures, graphics);

let numLeft = this.UPLOADS_PER_FRAME;

const update = () => {

// Upload the graphics
while (graphics.length && numLeft) {
renderer.plugins.graphics.updateGraphics(graphics.pop());
numLeft--;
}

// Upload the textures
while (textures.length && numLeft) {
renderer.textureManager.updateTexture(textures.pop());
numLeft--;
}

// We're finished
if (textures.length || graphics.length) {
numLeft = this.UPLOADS_PER_FRAME;
} else {
SharedTicker.remove(update);
done();
}
};

// Listen to frame updates
SharedTicker.add(update);
}

/**
* Get the list of renderable items.
* @method getUploadable
* @static
* @private
* @param {PIXI.DisplayObject} displayObject
* @param {Array<PIXI.Texture>} textures Collection of textures
* @param {Array<PIXI.Graphics>} graphics Collection of graphics
*/
static getUploadable(displayObject, textures, graphics) {

// Objects with textures, like Sprites
if (displayObject._texture) {
let texture = displayObject._texture.baseTexture;
if (textures.indexOf(texture) == -1) {
textures.push(texture);
}
} else if (displayObject instanceof PIXI.Graphics) {
graphics.push(displayObject);
}

// Get timed childen
if (displayObject instanceof PIXI.animate.MovieClip) {
let children = displayObject.children.slice();
displayObject._timedChildTimelines.forEach((timeline) => {
this.getUploadable(timeline.target, textures, graphics);
const index = children.indexOf(timeline.target);
if (index > -1) {
children.splice(index, 1);
}
});
children.forEach((child) => {
this.getUploadable(child, textures, graphics);
});
}
// Recursively get textures
else if (displayObject instanceof PIXI.Container) {
displayObject.children.forEach((child) => {
this.getUploadable(child, textures, graphics);
});
}
}
}

0 comments on commit f25dd33

Please sign in to comment.