From 17b949fea20207c4122d4bcf95497c58b02f20f9 Mon Sep 17 00:00:00 2001 From: "sha..rd" Date: Sat, 13 Aug 2011 19:01:52 +0000 Subject: [PATCH] Blender Loader * Enable mipmaps for packed and generated textures * Better handling of material flags for 2.58 * Allow loading of meshes with no vertices instead of crashing git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8005 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../blender/materials/MaterialHelper.java | 42 +++++++++++++------ .../plugins/blender/meshes/MeshHelper.java | 11 ++++- .../plugins/blender/objects/ObjectHelper.java | 8 ++-- .../blender/textures/TextureHelper.java | 4 +- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java index aad4aebdd..42e96747e 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java @@ -190,32 +190,40 @@ public class MaterialHelper extends AbstractBlenderHelper { int mode = ((Number) structure.getFieldValue("mode")).intValue(); boolean shadeless = (mode & 0x4) != 0; - boolean vertexColor = (mode & 0x16) != 0; - boolean transparent = (mode & 0x64) != 0; - + boolean vertexColor = (mode & 0x80) != 0; + boolean transparent = (mode & 0x10000) != 0; + boolean vtangent = (mode & 0x4000000) != 0; // NOTE: Requires tangents + if (shadeless) { result = new Material(dataRepository.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); } else { result = new Material(dataRepository.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md"); } + //System.out.println("Mode: \n" + + // "Shadeless: " + shadeless + "\n" + + // "VColor: " + vertexColor + "\n" + + // "ZTrans: " + transparent + "\n" + + // "VTangent: " + vtangent); + result.getAdditionalRenderState().setFaceCullMode(faceCullMode); if (transparent) { - result.setTransparent(true); + result.setTransparent(true); result.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); - } + } String name = structure.getName(); LOGGER.log(Level.INFO, "Material's name: {0}", name); if (vertexColor) { result.setBoolean(shadeless ? "VertexColor" : "UseVertexColor", true); } - + MaterialHelper materialHelper = dataRepository.getHelper(MaterialHelper.class); ColorRGBA diffuseColor = null; if (shadeless) { // color of shadeless? doesn't seem to work in blender .. + diffuseColor = ColorRGBA.White.clone(); } else { result.setBoolean("UseMaterialColors", Boolean.TRUE); @@ -223,6 +231,9 @@ public class MaterialHelper extends AbstractBlenderHelper { DiffuseShader diffuseShader = materialHelper.getDiffuseShader(structure); result.setBoolean("Minnaert", diffuseShader == DiffuseShader.MINNAERT); diffuseColor = materialHelper.getDiffuseColor(structure, diffuseShader); + if (!transparent){ + diffuseColor.a = 1; + } result.setColor("Diffuse", diffuseColor); SpecularShader specularShader = materialHelper.getSpecularShader(structure); @@ -231,6 +242,8 @@ public class MaterialHelper extends AbstractBlenderHelper { result.setColor("Ambient", materialHelper.getAmbientColor(structure)); result.setFloat("Shininess", materialHelper.getShininess(structure)); + + } // texture @@ -262,40 +275,43 @@ public class MaterialHelper extends AbstractBlenderHelper { if ((mapto & 0x01) != 0) {// Col // Map to COLOR channel or DIFFUSE // Set diffuse to white so it doesn't get multiplied by texture - result.setColor("Diffuse", ColorRGBA.White); + diffuseColor.r = diffuseColor.g = diffuseColor.b = 1.0f; + result.setColor(shadeless ? "Color" : "Diffuse", diffuseColor); //result.setBoolean("UseMaterialColors", Boolean.FALSE); // blending the texture with material color and texture's defined color int blendType = ((Number) textureLink.getFieldValue("blendtype")).intValue(); float[] color = new float[] { ((Number) textureLink.getFieldValue("r")).floatValue(), ((Number) textureLink.getFieldValue("g")).floatValue(), ((Number) textureLink.getFieldValue("b")).floatValue() }; float colfac = ((Number) textureLink.getFieldValue("colfac")).floatValue(); texture = textureHelper.blendTexture(diffuseColor.getColorArray(), texture, color, colfac, blendType, negateTexture, dataRepository); - texture.setWrap(WrapMode.Repeat); + texture.setMinFilter(MinFilter.Trilinear); + texture.setWrap(WrapMode.Repeat); if (shadeless) { result.setTexture(TEXTURE_TYPE_COLOR, texture); } else { result.setTexture(TEXTURE_TYPE_DIFFUSE, texture); } } - if ((mapto & 0x02) != 0) {// Nor + if ((mapto & 0x02) != 0 && !shadeless) {// Nor Texture normalMapTexture; if(texture.getKey() instanceof GeneratedTextureKey) { normalMapTexture = textureHelper.convertToNormalMapTexture(texture, ((Number)textureLink.getFieldValue("norfac")).floatValue()); - } else { + normalMapTexture.setMinFilter(MinFilter.Trilinear); + } else { normalMapTexture = texture; } - result.setTexture(TEXTURE_TYPE_NORMAL, normalMapTexture); + result.setTexture(TEXTURE_TYPE_NORMAL, normalMapTexture); if (vertexColor) { result.setBoolean(shadeless ? "VertexColor" : "UseVertexColor", false); } } - if ((mapto & 0x04) != 0) {// Spec + if ((mapto & 0x04) != 0 && !shadeless) {// Spec // Map to SPECULAR result.setTexture(TEXTURE_TYPE_SPECULAR, texture); } if ((mapto & 0x40) != 0) {// Emit result.setTexture(TEXTURE_TYPE_GLOW, texture); } - if ((mapto & 0x80) != 0) {// Alpha + if ((mapto & 0x80) != 0 && !shadeless) {// Alpha result.setTexture(TEXTURE_TYPE_ALPHA, texture); } } else { 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 46ce3dd9c..317e44edd 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 @@ -127,7 +127,10 @@ public class MeshHelper extends AbstractBlenderHelper { // the following map sorts faces by material number (because in jme Mesh can have only one material) Map> meshesMap = new HashMap>(); Pointer pMFace = (Pointer) structure.getFieldValue("mface"); - List mFaces = pMFace.fetchData(dataRepository.getInputStream()); + List mFaces = null; + if (pMFace.isNotNull()){ + mFaces = pMFace.fetchData(dataRepository.getInputStream()); + } Pointer pMTFace = (Pointer) structure.getFieldValue("mtface"); List uvCoordinates = null; @@ -153,6 +156,9 @@ public class MeshHelper extends AbstractBlenderHelper { // positions (it simply tells which vertex is referenced where in the result list) Map> vertexReferenceMap = new HashMap>(verticesAmount); int vertexColorIndex = 0; + if (mFaces == null){ + return null; + } for (int i = 0; i < mFaces.size(); ++i) { Structure mFace = mFaces.get(i); boolean smooth = (((Number) mFace.getFieldValue("flag")).byteValue() & 0x01) != 0x00; @@ -544,6 +550,9 @@ public class MeshHelper extends AbstractBlenderHelper { public Vector3f[] getVertices(Structure meshStructure, DataRepository dataRepository) throws BlenderFileException { int verticesAmount = ((Number) meshStructure.getFieldValue("totvert")).intValue(); Vector3f[] vertices = new Vector3f[verticesAmount]; + if (verticesAmount == 0) + return vertices; + Pointer pMVert = (Pointer) meshStructure.getFieldValue("mvert"); List mVerts = pMVert.fetchData(dataRepository.getInputStream()); for (int i = 0; i < verticesAmount; ++i) { diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java index 5d9f916a1..4baa7a3ba 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java @@ -184,9 +184,11 @@ public class ObjectHelper extends AbstractBlenderHelper { Pointer pMesh = (Pointer)objectStructure.getFieldValue("data"); List meshesArray = pMesh.fetchData(dataRepository.getInputStream()); List geometries = meshHelper.toMesh(meshesArray.get(0), dataRepository); - for(Geometry geometry : geometries) { - node.attachChild(geometry); - } + if (geometries != null){ + for(Geometry geometry : geometries) { + node.attachChild(geometry); + } + } node.setLocalTransform(t); //reading and applying all modifiers diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java index 545277236..94d65de36 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java @@ -935,12 +935,12 @@ public class TextureHelper extends AbstractBlenderHelper { name = name.substring(1); } - TextureKey texKey = new TextureKey(name, false); + TextureKey texKey = new TextureKey(name, true); try { Texture tex = dataRepository.getAssetManager().loadTexture(texKey); image = tex.getImage(); } catch (AssetNotFoundException e) { - LOGGER.warning("Asset not found: " + e.getLocalizedMessage()); + LOGGER.log(Level.WARNING, "Asset not found: {0}", e.getLocalizedMessage()); } }