I guess I don’t get what is meant by a mesh being dynamic, as I am unable to hand it in new vertex data after the first time.
I am trying to create a dynamic little patch of seawater, and use this code to try to update the Y values of the vertices:
FloatBuffer mVertices;
float[][] mHeightMap;
Mesh mMesh; // has been setDynamic()
void updateHeights() {
int index = 1;
for (int s = 0; s < pointCountX; s++) {
for (int t = 0; t < pointCountZ; t++) {
mVertices.put(index, mHeightMap[t]);
index += 3;
}
}
// setBuffer() throws exception shown below the second time it is used
mMesh.setBuffer(VertexBuffer.Type.Position, 3, mVertices);
mMesh.updateBound();
}
The exception is:
java.lang.UnsupportedOperationException: Data has already been sent. Cannot setupData again.
Shall I create a whole new Mesh each update?
tone
You should update the mesh, instead of creating a new one. Use this:
http://hub.jmonkeyengine.org/javadoc/com/jme3/scene/VertexBuffer.html#updateData(java.nio.Buffer)
Heres an example where I have used it before:
[java] private void modifyUV(Geometry g, float amount) {
VertexBuffer uv = g.getMesh().getBuffer(Type.TexCoord);
float[] uvArray = BufferUtils.getFloatArray((FloatBuffer) uv.getData());
for (int j = 0; j < uvArray.length; j++) {
if (j % 2 != 0) {
uvArray[j] += amount;
}
}
uv.updateData(BufferUtils.createFloatBuffer(uvArray));
}[/java]
You don’t want to send in a new buffer each time for the mesh. You just have to get the existing buffer and modify it.
Get the buffer from your mesh:
VertexBuffer vb = mesh.getBuffer(Type.Position);
Modify the data in it:
BufferUtils.setInBuffer(myValue, (FloatBuffer)vb.getData(), index);
Update it:
vb.setUpdateNeeded();
@wezrule’s method will work too
Thanks both!
tone