I’ve made use of the render texture code, as is in the tutorial. I have a question…
Is there anything I should be doing to clean-up when destroyed, due to changing scenes?
The reason I ask is because I noticed that the videos stop playing if I swap scenes a few times.
Hi @Cain_Quigley,
Good question, what you need to be doing is unload the texture created from VRAM (video memory) as soon as you don’t need it anymore:
this.videoTexture.destroy();
https://developer.playcanvas.com/en/api/pc.Texture.html#destroy
1 Like
Ah, thanks much! That seems to be improving performance greatly (after swapping scenes a few times).
I also did the following in the ‘initialization’ function, with regard to clean-up in the tutorial code (I’ve already added the texture ‘destroy’ call):
var onCanPlay = function () {
videoTexture.setSource(video);
video.play();
};
video.addEventListener('canplay', onCanPlay);
this.on('destroy', function () {
video.removeEventListener('canplay',onCanPlay);
document.body.removeChild(video);
videoTexture.destroy();
});
I’ve created a local, named function for ‘onCanPlay’, so that I can remove the listener. I also call ‘removeChild’ on the document. I hope I got this right. I suppose the garbage collector can free resources for the ‘video’, then, without any additional code?
I’m new to JavaScript, but I have a background in C++ and Lua. So, thanks for the guidance!
1 Like
That’s great @Cain_Quigley, I think that should be enough. I wouldn’t be doing anything in addition myself.
Thanks for sharing your code as well, someone may find this quite useful. Javascript is quite forgiving with garbage collection, but if you don’t take care it can be a problem eventually.