I’ve spent the day attempting to get the average color of a texture, but the buffers were always filled with nothing but 0 values. Some more testing revealed that this didn’t happen for jpg or png textures, but did for ktx2 textures, which my application uses for practically everything.
In my project, these textures are loaded and visible on the models long before I try to get their average colors (for a manually triggered IFC export), so I was certain they were all present. However, while making a repro of this behaviour, I noticed that materials with ktx2 textures never trigger onCompiled().
Might there be some part of the process when loading ktx2 textures that causes early termination, also preventing the buffers from being filled?
In the repro I first try to color the ground in the onCompiled() call, which doesn’t trigger for the two ktx2 files. And for illustration purposes I also try again after 2 seconds to showcase that the buffer values are 0.
KTX2 textures are GPU-compressed textures. That’s the key difference from JPG/PNG.
JPG/PNG → decoded on CPU → pixel data available.
KTX2 → stays compressed → uploaded directly to GPU → no CPU-side pixel buffer.
That’s why your buffer is all 0,0,0.
onCompiled() callback is tied to shader compilation, not texture readiness.
With KTX2 the shader may compile before the texture is fully transcoded/uploaded, or it may already be compiled and reused → so onCompiled never fires again.
The possible solution is to render the KTX2 texture to a small RenderTargetTexture and then read pixels from that.