Swapping Model Textures

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

Have you tried tiling your models as 3D Tiles? You can do this by uploading them to Cesium ion (https://cesium.com/ion/). That should allow you to load them pretty fast no matter how large or detailed the textures are.

Assuming your data is photogrammetry, you should select “3D Capture” when uploading it.

Hi Omar,

They’re not photogrammetry and sadly ION isn’t an option in this case. The systems they are deployed on are all strictly “off-line” and the models are generated by a 3rd party. We’re just trying to work around the limitations in the situation as best we can!

I think the best option for the moment may be to have two versions of the models, one with low resolution textures and the other with high resolution textures. We can then swap out/destroy the models as necessary. This won’t require any “hacking” of the Cesium source code either.

Many Thanks

Mark