1. A concise explanation of the problem you’re experiencing.
I need to swap a models textures at runtime with different sized textures.
2. A minimal code example. If you’ve found a bug, this helps us reproduce and repair it.
This is touched upon here:
https://github.com/CesiumGS/cesium/issues/5094
3. Context. Why do you need to do this? We might know a better way to accomplish your goal.
I have a number of 3D models which feature quite large textures. It is impossible to load all the models at once owing to the size of textures. However the models without textures load fine, or just one model with textures at a time. Therefore I would like to swap out the textures at run time when, for example, a user clicks a “show textures” button.
My thought was to replace all the textures with small 1x1 textures then on demand load in the correct textures and rebuild the GLTF.
The examples linked to above rely on the textures being the same dimensions which is not the case here…
I’ve been able to create new Texture objects but I don’t know how to force the model to rebuild itself with the updated Texture objects.
Something like…
var gl = viewer.canvas.getContext('webgl');
var tex = gl.createTexture();
tex.image = new Image();
tex.image.src = imagePath;
var oldTexture = textures[textureIndexToReplace];
var newTexture = new Texture(tex);
newTexture.generateMipmap();
oldTexture.destroy();
textures[textureIndexToReplace] = newTexture;
Help?
4. The Cesium version you’re using, your operating system and browser.
1.71
Windows
Chrome