* Mesh.prepareForAnim() is now much smarter about what to do. It won't convert the buffers unless absolutely necessary and apply the proper usages to the buffers as needed.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10555 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
sha..RD 12 years ago
parent b6afd61ef0
commit 0f21f19735
  1. 98
      engine/src/core/com/jme3/scene/Mesh.java

@ -353,69 +353,71 @@ public class Mesh implements Savable, Cloneable {
*/ */
public void prepareForAnim(boolean forSoftwareAnim){ public void prepareForAnim(boolean forSoftwareAnim){
if (forSoftwareAnim) { if (forSoftwareAnim) {
// convert indices to ubytes on the heap or floats // convert indices to ubytes on the heap
VertexBuffer indices = getBuffer(Type.BoneIndex); VertexBuffer indices = getBuffer(Type.BoneIndex);
Buffer buffer = indices.getData(); if (!indices.getData().hasArray()) {
if (buffer instanceof ByteBuffer) { ByteBuffer originalIndex = (ByteBuffer) indices.getData();
ByteBuffer originalIndex = (ByteBuffer) buffer;
ByteBuffer arrayIndex = ByteBuffer.allocate(originalIndex.capacity()); ByteBuffer arrayIndex = ByteBuffer.allocate(originalIndex.capacity());
originalIndex.clear(); originalIndex.clear();
arrayIndex.put(originalIndex); arrayIndex.put(originalIndex);
indices.updateData(arrayIndex); indices.updateData(arrayIndex);
} else if (buffer instanceof FloatBuffer) {
//Floats back to bytes
FloatBuffer originalIndex = (FloatBuffer) buffer;
ByteBuffer arrayIndex = ByteBuffer.allocate(originalIndex.capacity());
originalIndex.clear();
for (int i = 0; i < originalIndex.capacity(); i++) {
arrayIndex.put((byte) originalIndex.get(i));
}
indices.updateData(arrayIndex);
} }
indices.setUsage(Usage.CpuOnly);
// convert weights on the heap // convert weights on the heap
VertexBuffer weights = getBuffer(Type.BoneWeight); VertexBuffer weights = getBuffer(Type.BoneWeight);
FloatBuffer originalWeight = (FloatBuffer) weights.getData(); if (!weights.getData().hasArray()) {
FloatBuffer arrayWeight = FloatBuffer.allocate(originalWeight.capacity()); FloatBuffer originalWeight = (FloatBuffer) weights.getData();
originalWeight.clear(); FloatBuffer arrayWeight = FloatBuffer.allocate(originalWeight.capacity());
arrayWeight.put(originalWeight); originalWeight.clear();
weights.updateData(arrayWeight); arrayWeight.put(originalWeight);
weights.updateData(arrayWeight);
}
weights.setUsage(Usage.CpuOnly);
// position, normal, and tanget buffers to be in "Stream" mode
VertexBuffer positions = getBuffer(Type.Position);
VertexBuffer normals = getBuffer(Type.Normal);
VertexBuffer tangents = getBuffer(Type.Tangent);
positions.setUsage(Usage.Stream);
if (normals != null) {
normals.setUsage(Usage.Stream);
}
if (tangents != null) {
tangents.setUsage(Usage.Stream);
}
} else { } else {
//BoneIndex must be 32 bit for attribute type constraints in shaders
VertexBuffer indices = getBuffer(Type.BoneIndex); VertexBuffer indices = getBuffer(Type.BoneIndex);
Buffer buffer = indices.getData(); if (!indices.getData().isDirect()) {
if (buffer instanceof ByteBuffer) { ByteBuffer originalIndex = (ByteBuffer) indices.getData();
ByteBuffer bIndex = (ByteBuffer) buffer; ByteBuffer directIndex = BufferUtils.createByteBuffer(originalIndex.capacity());
final float[] rval = new float[bIndex.capacity()]; originalIndex.clear();
for (int i = 0; i < rval.length; i++) { directIndex.put(originalIndex);
rval[i] = bIndex.get(i); indices.updateData(directIndex);
}
clearBuffer(Type.BoneIndex);
VertexBuffer ib = new VertexBuffer(Type.BoneIndex);
ib.setupData(Usage.Stream,
4,
Format.Float,
BufferUtils.createFloatBuffer(rval));
setBuffer(ib);
} else if (buffer instanceof FloatBuffer) {
//BoneWeights on DirectBuffer
FloatBuffer originalIndices = (FloatBuffer) buffer;
FloatBuffer arrayIndices = BufferUtils.createFloatBuffer(originalIndices.capacity());
originalIndices.clear();
arrayIndices.put(originalIndices);
indices.setUsage(Usage.Stream);
indices.updateData(arrayIndices);
} }
indices.setUsage(Usage.Static);
//BoneWeights on DirectBuffer
VertexBuffer weights = getBuffer(Type.BoneWeight); VertexBuffer weights = getBuffer(Type.BoneWeight);
FloatBuffer originalWeight = (FloatBuffer) weights.getData(); if (!weights.getData().isDirect()) {
FloatBuffer arrayWeight = BufferUtils.createFloatBuffer(originalWeight.capacity()); FloatBuffer originalWeight = (FloatBuffer) weights.getData();
originalWeight.clear(); FloatBuffer directWeight = BufferUtils.createFloatBuffer(originalWeight.capacity());
arrayWeight.put(originalWeight); originalWeight.clear();
directWeight.put(originalWeight);
weights.updateData(directWeight);
}
weights.setUsage(Usage.Static); weights.setUsage(Usage.Static);
weights.updateData(arrayWeight);
// position, normal, and tanget buffers to be in "Static" mode
VertexBuffer positions = getBuffer(Type.Position);
VertexBuffer normals = getBuffer(Type.Normal);
VertexBuffer tangents = getBuffer(Type.Tangent);
positions.setUsage(Usage.Static);
if (normals != null) {
normals.setUsage(Usage.Static);
}
if (tangents != null) {
tangents.setUsage(Usage.Static);
}
} }
} }

Loading…
Cancel
Save