Hi,
If I wanted to update just one channel of a texture, say because it is an ORM texture and I just want to update the metalness part of the texture, what would be the best approach?
I could use DynamicTexture, but as I am targeting iOS I am concerned about the amount of memory that will be used.
Alternatively I could use readpixels → apply my changes to a single channel → update the texture somehow (maybe using the updateTextureData method?)
Before I started trying random things I was hoping someone may be able to point me in a recommended direction?
Yes, I think using readpixels to get the data once, change the channel you want and use updateTextureData to update the texture is a good way to do it.
For some reasons it does not work with a .jpg file, but it works with a .png…
Try:
grass.png is a 512x512 image, so the buffer is 1048576 bytes and it works as expected (I add some randomly colored pixels before updating the texture, to easily see if the update works).
Now, using fire.jpg instead:
This is also a 512x512 texture, so buffer is also 1048576 but this time it does not work.
However, I checked that the webgl texSubImage2D call uses the exact same parameters in both cases!
So, I don’t understand why it does not work (note that it works in WebGPU)…
@Evgeni_Popov - it looks like the issue is that jpg textures are incorrectly thought to be RGBA by babylon when in fact they are RGB.
Forcing the texture type and format for both the texture and Internal texture allows the code to run.
However, as the read pixels are RGBA and we are writing to RGB additional work needs to be done to ensure the data written to the texture are the actual values required.