* Blender loader now writes color as a normalized byte buffer, reduces mem usage by 4 times

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9015 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Sha..rd 13 years ago
parent 3e6d649c48
commit 3695f8f1fc
  1. 23
      engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java
  2. 25
      engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java

@ -38,6 +38,7 @@ import com.jme3.scene.plugins.blender.file.Pointer;
import com.jme3.scene.plugins.blender.file.Structure; import com.jme3.scene.plugins.blender.file.Structure;
import com.jme3.scene.plugins.blender.objects.Properties; import com.jme3.scene.plugins.blender.objects.Properties;
import com.jme3.util.BufferUtils; import com.jme3.util.BufferUtils;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.util.List; import java.util.List;
@ -94,6 +95,28 @@ public abstract class AbstractBlenderHelper {
return true; return true;
} }
/**
* Generate a new ByteBuffer using the given array of byte[4] objects. The ByteBuffer will be 4 * data.length
* long and contain the vector data as data[0][0], data[0][1], data[0][2], data[0][3], data[1][0]... etc.
* @param data
* list of byte[4] objects to place into a new ByteBuffer
*/
protected ByteBuffer createByteBuffer(List<byte[]> data) {
if (data == null) {
return null;
}
ByteBuffer buff = BufferUtils.createByteBuffer(4 * data.size());
for (byte[] v : data) {
if (v != null) {
buff.put(v[0]).put(v[1]).put(v[2]).put(v[3]);
} else {
buff.put((byte)0).put((byte)0).put((byte)0).put((byte)0);
}
}
buff.flip();
return buff;
}
/** /**
* Generate a new FloatBuffer using the given array of float[4] objects. The FloatBuffer will be 4 * data.length * Generate a new FloatBuffer using the given array of float[4] objects. The FloatBuffer will be 4 * data.length
* long and contain the vector data as data[0][0], data[0][1], data[0][2], data[0][3], data[1][0]... etc. * long and contain the vector data as data[0][0], data[0][1], data[0][2], data[0][3], data[1][0]... etc.

@ -57,6 +57,7 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper;
import com.jme3.scene.plugins.blender.textures.UVCoordinatesGenerator; import com.jme3.scene.plugins.blender.textures.UVCoordinatesGenerator;
import com.jme3.texture.Texture; import com.jme3.texture.Texture;
import com.jme3.util.BufferUtils; import com.jme3.util.BufferUtils;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
import java.util.*; import java.util.*;
import java.util.Map.Entry; import java.util.Map.Entry;
@ -113,7 +114,7 @@ public class MeshHelper extends AbstractBlenderHelper {
int verticesAmount = vertices.length; int verticesAmount = vertices.length;
// vertices Colors // vertices Colors
List<float[]> verticesColors = this.getVerticesColors(structure, blenderContext); List<byte[]> verticesColors = this.getVerticesColors(structure, blenderContext);
// reading faces // reading faces
// the following map sorts faces by material number (because in jme Mesh can have only one material) // the following map sorts faces by material number (because in jme Mesh can have only one material)
@ -297,7 +298,8 @@ public class MeshHelper extends AbstractBlenderHelper {
Properties properties = this.loadProperties(structure, blenderContext); Properties properties = this.loadProperties(structure, blenderContext);
// generating meshes // generating meshes
FloatBuffer verticesColorsBuffer = this.createFloatBuffer(verticesColors); //FloatBuffer verticesColorsBuffer = this.createFloatBuffer(verticesColors);
ByteBuffer verticesColorsBuffer = createByteBuffer(verticesColors);
for (Entry<Integer, List<Integer>> meshEntry : meshesMap.entrySet()) { for (Entry<Integer, List<Integer>> meshEntry : meshesMap.entrySet()) {
Mesh mesh = new Mesh(); Mesh mesh = new Mesh();
@ -323,6 +325,7 @@ public class MeshHelper extends AbstractBlenderHelper {
// setting vertices colors // setting vertices colors
if (verticesColorsBuffer != null) { if (verticesColorsBuffer != null) {
mesh.setBuffer(Type.Color, 4, verticesColorsBuffer); mesh.setBuffer(Type.Color, 4, verticesColorsBuffer);
mesh.getBuffer(Type.Color).setNormalized(true);
} }
// setting faces' normals // setting faces' normals
@ -462,7 +465,7 @@ public class MeshHelper extends AbstractBlenderHelper {
} }
/** /**
* This method returns the vertices colors. Each vertex is stored in float[4] array. * This method returns the vertices colors. Each vertex is stored in byte[4] array.
* *
* @param meshStructure * @param meshStructure
* the structure containing the mesh data * the structure containing the mesh data
@ -472,19 +475,19 @@ public class MeshHelper extends AbstractBlenderHelper {
* @throws BlenderFileException * @throws BlenderFileException
* this exception is thrown when the blend file structure is somehow invalid or corrupted * this exception is thrown when the blend file structure is somehow invalid or corrupted
*/ */
public List<float[]> getVerticesColors(Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException { public List<byte[]> getVerticesColors(Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException {
Pointer pMCol = (Pointer) meshStructure.getFieldValue("mcol"); Pointer pMCol = (Pointer) meshStructure.getFieldValue("mcol");
List<float[]> verticesColors = null; List<byte[]> verticesColors = null;
List<Structure> mCol = null; List<Structure> mCol = null;
if (pMCol.isNotNull()) { if (pMCol.isNotNull()) {
verticesColors = new LinkedList<float[]>(); verticesColors = new LinkedList<byte[]>();
mCol = pMCol.fetchData(blenderContext.getInputStream()); mCol = pMCol.fetchData(blenderContext.getInputStream());
for (Structure color : mCol) { for (Structure color : mCol) {
float r = (((Number)color.getFieldValue("r")).byteValue() & 0xFF) / 256.0f; byte r = ((Number)color.getFieldValue("r")).byteValue();
float g = (((Number)color.getFieldValue("g")).byteValue() & 0xFF) / 256.0f; byte g = ((Number)color.getFieldValue("g")).byteValue();
float b = (((Number)color.getFieldValue("b")).byteValue() & 0xFF) / 256.0f; byte b = ((Number)color.getFieldValue("b")).byteValue();
float a = (((Number)color.getFieldValue("a")).byteValue() & 0xFF) / 256.0f; byte a = ((Number)color.getFieldValue("a")).byteValue();
verticesColors.add(new float[]{b, g, r, a}); verticesColors.add(new byte[]{b, g, r, a});
} }
} }
return verticesColors; return verticesColors;

Loading…
Cancel
Save