Update texture in game loop

hello

i try to update a texture as the game is running but it has no effect even if i re set the texture to the material

how do i tell the engine to refresh the material/texture/shader state ?

[java]
@Override
public void simpleUpdate(float tpf)
{
if(texture!=null)
{
Image image = texture.getImage();
ByteBuffer data=image.getData(0);
data.rewind();
for(int y=0;y<areaHeight+1;y++)
for(int x=0;x<areaWidth+1;x++)
{
data.put((byte)255);
data.put((byte)(Math.random()*255));
data.put((byte)(Math.random()*255));
data.put((byte)(Math.random()*255));
}
texture.setImage(image);
mat.setTexture(“DiffuseMap”, texture);
}
[/java]

thx

for those interested : image.setUpdateNeeded();

Recompile shader with new parameters is slow way to make it.
Why dont you just write shader for that? i think shader is what you really want.

<cite>@oxplay2 said:</cite> Recompile shader with new parameters is slow way to make it. Why dont you just write shader for that? i think shader is what you really want.

hi oxplay2,

actualy, the code provided is for an update test, not the actual code
i do uv mouse painting in the end

i found this update trick in an android application from google that was also doing uv mouse painting

it is actualy working super fast and i dont see why i should lead to shader beeing recompiled everytime (?)
i mean in that case , there must be a way to update a sampler2d memory data and have an effect on the rendering process
usualy, it is basic opengl shader stuff

any idea how to do this without using image.setUpdateNeeded() ?

thx

Use ImageRaster and/or ImagePainter and the flag is set for you automatically.

<cite>@zarch said:</cite> Use ImageRaster and/or ImagePainter and the flag is set for you automatically.

ok, but will it recompile the shader as oxplay2 explained above ?

@curtisnewton said: ok, but will it recompile the shader as oxplay2 explained above ?
no, your code is fine. you don't need to call mat.setTexture(“DiffuseMap”, texture); on each loop, the texture reference has not change, only the data, so you can do that once in the simpleInit. Though it's not what's gonna kill performance. I guess what oxplay2 means is that you you are generating the texture on the CPU. I don't know what's your goal in the end, but for example if you are trying to animate a texture, there is probably a way to do it a lot more efficiently in a shader on the GPU.

The cost is that the image is being resent to the GPU each frame if it has changed - which can get expensive if you are changing it a lot.

This approach is fine for making occasional changes or for small images but something like a sprite sheet (which could be generated at run time once using this technique) or a custom shader will perform better.

the goal is to have an rgb map of the size of a grid mesh
for instance, a grid mesh of 10x10 (composed of quads made of 2 triangles) would lead to a 11x11 bitmap
i would use r componnent for moisture, g componnent for temperature …
as i paint the grid, a shader would render different textures for different type of soils
as the colors are linearly interpolated on the mesh, i would linearliy interpolate transitions between soils types

on the game map, there would be many of those as the player would create these “gardens” and the occasionaly, he would change the nature of the soil (one garden at a time)
so it is only when the user change a garden soil property, that it would modify the texture

is there any shader that does that ? terrain shader perhaps ?

i thought that shader is recompiled, when material parameters are updated, but sorry, if i was wrong.
anyway still shader is faster for that kind of texture manipulation.

edit:

is there any shader that does that ? terrain shader perhaps ?

Terrain shader gives possibility to paint, but it’s better to make new shader that will use textures, like you wish.
i wrote similar shader based on “tiles”, but last time i had no time for programming, so dont know how much JME shader system. changed.

Actually what you describe does sound like a good use for image raster/image painter.

<cite>@zarch said:</cite> Actually what you describe does sound like a good use for image raster/image painter.

ok i’ll check it out :slight_smile: