Shaun Dreclin Posted January 15, 2016 Share Posted January 15, 2016 So I'm looking at https://pixijs.github.io/docs/PIXI.Sprite.html#destroy and I don't quite understand the difference. I'm getting an error when I try to destroy the texture (possibly because I use the same texture in multiple sprites), and nothing seems to happen when I try to destroy the baseTexture (Mind you, I don't know what should be happening or what to look for). Could somebody explain it to me? If it helps, here is my sprite adding function: function addSprite(texture, position) { var sprite = new PIXI.Sprite(PIXI.Texture.fromImage("https://domain.com/img/" + texture + ".png")); sprite.position.set(position[0], position[1]); sprite.anchor.set(0.5, 1); stage.addChild(sprite); return sprite; } It gets called several times with the same texture parameter. And my deleting function: function removeSprite(sprite) { stage.removeChild(sprite); sprite.destroy(); } Edit: when I call sprite.destroy(true) in the removeSprite function I get this error: TypeError: t.baseTexture is null pixi.min.js:5:18132 [60]</<.texture.set() pixi.min.js:5 i() pixi.min.js:5 addSprite() _display:62 logicLoop() _display:325 Quote Link to comment Share on other sites More sharing options...
bubamara Posted January 15, 2016 Share Posted January 15, 2016 PIXI.BaseTexture A texture stores the information that represents an image. All textures have a base texture. PIXI.Texture A texture stores the information that represents an image or part of an image. It cannot be added to the display list directly. Instead use it as the texture for a Sprite. If no frame is provided then the whole image is used. PIXI.Texture is reference to PIXI.BaseTexture sprite.destroy(); - will destroy sprite, leaving PIXI.Texture and PIXI.BaseTexture untouched sprite.destroy(true); - will destroy sprite and PIXI.Texture; PIXI.BaseTexture remains untouched sprite.destroy(true, true); - will destroy sprite, PIXI.Texture and PIXI.BaseTexture Generally you don't want to create PIXI.Texture every single time addSprite() is called and destroying it in removeSprite(). Even when this is handled in Pixi internally and no duplicate textures are created. Create your textures ahead, store them into array and pick one when it is needed. Destroy PIXI.Texture when no other sprite will be using it and destroy PIXI.BaseTexture when you are completely done. EDIT: corrected sprite.destroy() Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted January 15, 2016 Share Posted January 15, 2016 Destroying PIXI.Texture does not free memory, it just makes texture not valid and removes it from image cache, so other sprites wont be able to use it. I really dont know cases when you have to call it Destroying PIXI.BaseTexture frees WebGL objects that are bound to it. Call it for dynamic texture that you use or some statics that arent needed anymore. Quote Link to comment Share on other sites More sharing options...
Shaun Dreclin Posted January 15, 2016 Author Share Posted January 15, 2016 Okay cause I read answers on this forum saying that I should always use sprite.destroy(true) to free up the memory. I guess that was wrong haha So, I should change my addSprite() function to check to see if the texture has already been created, if not create it and store it in an array/object of textures, if it has been use the one from that texture bank? And for texture/basetexture, The basetexture is the image itself and the texture is the portion of the image you're displaying, like if I was using a sprite sheet? Is that correct? Also if pixi handles it internally and doesn't create duplicate sprites, why isn't it good to just make a new texture any time I need one? Is it bad on performance? Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted January 15, 2016 Share Posted January 15, 2016 You can create new texture every time you create sprite, just be sure its using existing baseTexture. Either you specify baseTexture in the constructor parameters, either you use fromImage(). BaseTexture.fromImage is checking cache, then trying to load image if it wasnt found. Texture.fromImage is checking cache for both images and atlas regions, if not found then it uses BaseTexture.fromImage. Quote Link to comment Share on other sites More sharing options...
xerver Posted January 15, 2016 Share Posted January 15, 2016 Calling destroy on any object you plan to stop using is a good idea. We null out internal references, and if you don't do it you can end up with circular references and other problems that may make pixi keep objects alive unnecessarily. The API contract is that if you call destroy, we guarantee that pixi will not hold any more references to that object and it will be cleaned up (assuming you do the same in your code). If you don't we may keep references around that prevent that object from ever been garbage collected. Any pixi object you create (Sprite, Texture, BaseTexture, Text, anything) should have its destroy method called when you are disposing it. Part of the disposal process can sometimes mean cleaning up WebGL memory, but most of the time it just means preparing that object and its resources to be garbage collectible. Quote Link to comment Share on other sites More sharing options...
Shaun Dreclin Posted January 15, 2016 Author Share Posted January 15, 2016 Hmm, is there a way to destroy textures that haven't been used in a while? Or would I have to keep track of that myself? I'm thinking I'll just keep loading new textures in from the web server as needed, since many of them will be reused through the whole game session. If the browser starts using too much memory though I should probably have a way to throw out old stuff. Quote Link to comment Share on other sites More sharing options...
xerver Posted January 16, 2016 Share Posted January 16, 2016 You have to track it yourself, if you render with a texture it sits in gpu memory until you tell us to take it out (rendering is your way of telling us to put it in). Quote Link to comment Share on other sites More sharing options...
Shaun Dreclin Posted January 16, 2016 Author Share Posted January 16, 2016 Hmm I guess I could stick a timestamp on every texture any time it gets used to create or update a sprite, then every once in a while go through and destroy ones that haven't been used in like 30 minutes. Will I have to destroy the texture and the base texture? Or will destroying the base also destroy any textures that use that base? Quote Link to comment Share on other sites More sharing options...
xerver Posted January 16, 2016 Share Posted January 16, 2016 Every object that you no longer need has to have it's destroy method call. If you look at the docs you can see that objects that own other objects can call destroy for you. Like bubamara posted the destroy methods sometimes have params that if set to true also destroy owned objects, like Texture.destroy(true) will destroy the texture and call destroy on the underlying base texture as well. I recommend being smarter about texture management than just "how old is it". Usually you should be able to know what textures are in use, and which aren't just because of how your app functions. Rarely do you need generalized texture garbage collection. Quote Link to comment Share on other sites More sharing options...
Shaun Dreclin Posted January 16, 2016 Author Share Posted January 16, 2016 Well I'd rather have textures stored for the entire session just to ease load on my server. No point downloading the same texture more than once per session if you can avoid it. As for management, I can tell what textures are currently being used but a texture that is no longer currently in use might be used again in a few seconds or never. That's why I was thinking of clearing them based on time since last use. (User-generated world here, so I have to expect that any texture can show up at any time.) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.