I have been working on a tree.js project, with a simple model.
I try to change the colors by changing the map texture.
on mobile phones, especially iPhones, the WebGL crashes after 3-4 colors (map texture) changes.
on the PC browser, it works well (although if you change the texture many times fast enough it will also crash).
the only error I get after a crash is: (on the PC)
webgl: context_lost_webgl: losecontext: context lost
the model build by 6 objects that all has the same map (each object knows what part of the map to use).
I am changing the texture of only one of the objects by using TextureLoader:
//g_Body declare on scene load
var g_Body = scene.getObjectByName("Body");
...
var loader = new THREE.TextureLoader();
// load a resource
loader.load(
// resource URL
`static/Color/${color}/Combie_Combie_Diffuse.png`,
// Function when resource is loaded
function ( texture ) {
// apply the new texture to the model body map
g_Body.material.map = texture;
g_Body.material.needsUpdate = true;
},
// Function called when download progresses
function ( xhr ) {
console.log( (xhr.loaded / xhr.total * 100) + '% loaded' );
},
// Function called when download errors
function ( xhr ) {
console.log( 'An error happened' );
}
);
I noticed that the memory used by the GPU process in Chrome is increasing every time I change the color. So, I’m guessing it’s a memory management issue of some kind?
Yes I think so as well,
but I can’t find a way to clear the last texture after (or before) I change color (texture).
any suggestion on how to handle the memory?
I would suggest trying to narrow it down to which lines are actually causing the memory to increase. For example, does the memory still increase if you comment out these lines?:
thanks for the input bill =)
the crashes are definitely caused by texture changes. I think that the GPU get memory overflow.
I recently added the lines that you refer to:
g_Body.material.map.dispose();
which helped a lot with most mobile crashes.
but still, I have some rare crashes on iPhones with older versions, on specifics devices (tested on 2 similar iPhone 6+ with same ios version and on was working perfect and one crashing after texture change…)
any ideas on how to debug or solve this?
the texture is made by a .png file and is attached to specifics objects in the model. each object knows which part of the texture to use
I load it with THREE.TextureLoader() (as you can see in the code in my first post). I load ALL the texture when the app is loading and then only change the texture like this: