PIXI Uploading to GPU
by Brendon
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/pixi.js/4.4.1/pixi.js"></script>
<p>
Toggle the `workAround` value to see the difference in performance on the first play back of this animation<br/>
Click the button play animation.
</p>
<button id="button" disabled="disabled">
loading into GPU...
</button>
</button>
CSS
*{
font-family: arial;
}
button{
padding:10px;
font-size:16px;
display:block;
cursor: pointer;
margin-bottom: 10px;
border-radius:3px;
background:#eee;
border: 1px solid #aaa;
}
JavaScript
/* README
Toggle the `workAround` value to see the difference in performance on the first play back of this animation
Click the button play animation.
*/
const workAround = false
var app = new PIXI.Application(400, 600, {backgroundColor : 0x1099bb});
document.body.appendChild(app.view);
var sprite;
// lets start shit off
function main(){
PIXI.loader.defaultQueryString = 'v=' + '34453ggfg';
PIXI.loader.add('sprite0','https://s3.eu-central-1.amazonaws.com/scr-stg/assets/sprite/confetti-0.json')
PIXI.loader.add('sprite1','https://s3.eu-central-1.amazonaws.com/scr-stg/assets/sprite/confetti-1.json')
PIXI.loader.load(afterLoad)
}
// load the texture we need
function afterLoad(loader, resources){
// merge frames
let textures = Object.assign(resources.sprite0.textures, resources.sprite1.textures);
// order frames into array
let array = []
Object.keys(textures).sort().forEach(function(key) {
array.push(textures[key])
});
// make animated sprite
sprite = new PIXI.extras.AnimatedSprite(array);
sprite.x = 10
sprite.y = 10
sprite.animationSpeed = 0.4
sprite.loop = false
// upload to GPU the proper way, by simply adding the animatedSprite
app.renderer.plugins.prepare.upload(sprite,function(){
console.log('uploaded animated sprite')
enableButton()
})
// try uploading the individual texture images
app.renderer.plugins.prepare.upload(new PIXI.Texture.fromImage('sprite0_image'),function(){
console.log('uploaded sprite 0')
})
app.renderer.plugins.prepare.upload(new PIXI.Texture.fromImage('sprite1_image'),function(){
console.log('uploaded sprite 1')
})
if(workAround){
// upload to GPU my way
app.renderer.bindTexture(new PIXI.Texture.fromImage('sprite0_image'))
app.renderer.bindTexture(new PIXI.Texture.fromImage('sprite1_image'))
}
};
function enableButton(){
var button = document.getElementById('button')
button.disabled...