diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java index 8f8ba891b..8c4980411 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/AbstractBlenderHelper.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.objects.Properties; import com.jme3.util.BufferUtils; +import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.util.List; @@ -94,6 +95,28 @@ public abstract class AbstractBlenderHelper { 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 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 * long and contain the vector data as data[0][0], data[0][1], data[0][2], data[0][3], data[1][0]... etc. diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java index ea3bec534..484e942ec 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java @@ -57,6 +57,7 @@ import com.jme3.scene.plugins.blender.textures.TextureHelper; import com.jme3.scene.plugins.blender.textures.UVCoordinatesGenerator; import com.jme3.texture.Texture; import com.jme3.util.BufferUtils; +import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.util.*; import java.util.Map.Entry; @@ -113,7 +114,7 @@ public class MeshHelper extends AbstractBlenderHelper { int verticesAmount = vertices.length; // vertices Colors - List verticesColors = this.getVerticesColors(structure, blenderContext); + List verticesColors = this.getVerticesColors(structure, blenderContext); // reading faces // 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); // generating meshes - FloatBuffer verticesColorsBuffer = this.createFloatBuffer(verticesColors); + //FloatBuffer verticesColorsBuffer = this.createFloatBuffer(verticesColors); + ByteBuffer verticesColorsBuffer = createByteBuffer(verticesColors); for (Entry> meshEntry : meshesMap.entrySet()) { Mesh mesh = new Mesh(); @@ -323,6 +325,7 @@ public class MeshHelper extends AbstractBlenderHelper { // setting vertices colors if (verticesColorsBuffer != null) { mesh.setBuffer(Type.Color, 4, verticesColorsBuffer); + mesh.getBuffer(Type.Color).setNormalized(true); } // 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 * the structure containing the mesh data @@ -472,19 +475,19 @@ public class MeshHelper extends AbstractBlenderHelper { * @throws BlenderFileException * this exception is thrown when the blend file structure is somehow invalid or corrupted */ - public List getVerticesColors(Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException { + public List getVerticesColors(Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException { Pointer pMCol = (Pointer) meshStructure.getFieldValue("mcol"); - List verticesColors = null; + List verticesColors = null; List mCol = null; if (pMCol.isNotNull()) { - verticesColors = new LinkedList(); + verticesColors = new LinkedList(); mCol = pMCol.fetchData(blenderContext.getInputStream()); for (Structure color : mCol) { - float r = (((Number)color.getFieldValue("r")).byteValue() & 0xFF) / 256.0f; - float g = (((Number)color.getFieldValue("g")).byteValue() & 0xFF) / 256.0f; - float b = (((Number)color.getFieldValue("b")).byteValue() & 0xFF) / 256.0f; - float a = (((Number)color.getFieldValue("a")).byteValue() & 0xFF) / 256.0f; - verticesColors.add(new float[]{b, g, r, a}); + byte r = ((Number)color.getFieldValue("r")).byteValue(); + byte g = ((Number)color.getFieldValue("g")).byteValue(); + byte b = ((Number)color.getFieldValue("b")).byteValue(); + byte a = ((Number)color.getFieldValue("a")).byteValue(); + verticesColors.add(new byte[]{b, g, r, a}); } } return verticesColors;