Change texture data during runtime

Hello there,

I have hooked up berkelium (chrome based offscreen renderer of web pages) into my jme3 project and I’d like to update a texture incrementally.

As of now I’ve always 1) created a new Image instance with a byte buffer I get from berkelium, 2) passed that to a Texture2D constructor and 3) then set the texture on a material (which is bound to a quad) using setTexture.

So far that worked nicely, but since berkelium send incremential/partial updates to the image, I’d like to be able to modify the old texture instead of creating a new one.

I’ve tried retrieving the old texture using mat.getTextureParam("").getTextureValue().getImage() and setting the image data through its setData method, but I don’t see any change. Is there something I have to do to propagate the change to the jme3 state? Or this there an alternate way to do this?

Kind regards,


Make sure to call setRefreshNeeded() on the image

[snippet id=“14”]

Well this is pretty much what I am doing and it does not work unless I 1) use a new Texture2D object in the setTexture call and 2) use a new Image object in the call to PaintableTexture.getTexture.

That does not work either. In fact, setUpdateNeeded is already being called on the image on every setData call.

I’ve now found out, that the problem seems to lie within getData or setData. If I try creating a copy of a working image (it works, because upon setting it per mat.setTexture it is being displayed correctly) by using the data returned by img.getData() in a new Image - then it does not display correctly. In code:

/// this works with workingImage being manually generated from a ByteBuffer called myBytes.

Image workingImage = new Image(Format.RGBA8, width, height, myBytes);

mat.setTexture(“ColorMap”, new Texture2D(workingImage));

/// this does not work, even though workingImage is after all being generated from a ByteBuffer

mat.setTexture(“ColorMap”, new Texture2D(new Image(Format.RGBA8, width, height, workingImage.getData(0))));

Is there possibly some transformation of the image data being done at some point?

Uff, turns out its all my fault and it does work correctly in fact. I used the wrong initialization parameters (i.e. smaller values for width and height) for the new image which resulted in only part of the correct image being copied.

So basically one can very easily change texture data by simply calling setData on a bound (i.e. bound to a texture) Image.

Sorry about the noise & many thanks!