diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java index 2dd1997d8..2edf1d5be 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java @@ -193,7 +193,7 @@ public class CurvesHelper extends AbstractBlenderHelper { if (nurbGeoms != null) {//setting the name and assigning materials for (Geometry nurbGeom : nurbGeoms) { if(materialContexts != null) { - materialContexts[nurbEntry.getKey().intValue()].applyMaterial(nurbGeom, curveStructure.getOldMemoryAddress(), false, null, blenderContext); + materialContexts[nurbEntry.getKey().intValue()].applyMaterial(nurbGeom, curveStructure.getOldMemoryAddress(), null, blenderContext); } else { nurbGeom.setMaterial(defaultMaterial); } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialContext.java b/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialContext.java index 9bc7fd988..1f1910e6e 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialContext.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialContext.java @@ -166,7 +166,7 @@ public final class MaterialContext { this.transparent = transparent; } - public void applyMaterial(Geometry geometry, Long geometriesOMA, boolean noTextures, List userDefinedUVCoordinates, BlenderContext blenderContext) { + public void applyMaterial(Geometry geometry, Long geometriesOMA, List userDefinedUVCoordinates, BlenderContext blenderContext) { Material material = null; if (shadeless) { material = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); @@ -195,45 +195,43 @@ public final class MaterialContext { } //applying textures - if(!noTextures) { - for(Entry entry : loadedTextures.entrySet()) { - CombinedTexture combinedTexture = entry.getValue(); - combinedTexture.flatten(geometry, geometriesOMA, userDefinedUVCoordinates, blenderContext); - VertexBuffer.Type uvCoordinatesType = null; - - switch(entry.getKey().intValue()) { - case MTEX_COL: - uvCoordinatesType = VertexBuffer.Type.TexCoord; - material.setTexture(shadeless ? MaterialHelper.TEXTURE_TYPE_COLOR : MaterialHelper.TEXTURE_TYPE_DIFFUSE, - combinedTexture.getResultTexture()); - break; - case MTEX_NOR: - uvCoordinatesType = VertexBuffer.Type.TexCoord2; - material.setTexture(MaterialHelper.TEXTURE_TYPE_NORMAL, combinedTexture.getResultTexture()); - break; - case MTEX_SPEC: - uvCoordinatesType = VertexBuffer.Type.TexCoord3; - material.setTexture(MaterialHelper.TEXTURE_TYPE_SPECULAR, combinedTexture.getResultTexture()); - break; - case MTEX_EMIT: - uvCoordinatesType = VertexBuffer.Type.TexCoord4; - material.setTexture(MaterialHelper.TEXTURE_TYPE_GLOW, combinedTexture.getResultTexture()); - break; - case MTEX_ALPHA: - uvCoordinatesType = VertexBuffer.Type.TexCoord5; - material.setTexture(MaterialHelper.TEXTURE_TYPE_ALPHA, combinedTexture.getResultTexture()); - break; - default: - LOGGER.severe("Unknown mapping type: " + entry.getKey().intValue()); - } - - //applying texture coordinates - if(uvCoordinatesType != null) { - VertexBuffer uvCoordsBuffer = new VertexBuffer(uvCoordinatesType); - uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, - BufferUtils.createFloatBuffer(combinedTexture.getResultUVS().toArray(new Vector2f[combinedTexture.getResultUVS().size()]))); - geometry.getMesh().setBuffer(uvCoordsBuffer); - } + for(Entry entry : loadedTextures.entrySet()) { + CombinedTexture combinedTexture = entry.getValue(); + combinedTexture.flatten(geometry, geometriesOMA, userDefinedUVCoordinates, blenderContext); + VertexBuffer.Type uvCoordinatesType = null; + + switch(entry.getKey().intValue()) { + case MTEX_COL: + uvCoordinatesType = VertexBuffer.Type.TexCoord; + material.setTexture(shadeless ? MaterialHelper.TEXTURE_TYPE_COLOR : MaterialHelper.TEXTURE_TYPE_DIFFUSE, + combinedTexture.getResultTexture()); + break; + case MTEX_NOR: + uvCoordinatesType = VertexBuffer.Type.TexCoord2; + material.setTexture(MaterialHelper.TEXTURE_TYPE_NORMAL, combinedTexture.getResultTexture()); + break; + case MTEX_SPEC: + uvCoordinatesType = VertexBuffer.Type.TexCoord3; + material.setTexture(MaterialHelper.TEXTURE_TYPE_SPECULAR, combinedTexture.getResultTexture()); + break; + case MTEX_EMIT: + uvCoordinatesType = VertexBuffer.Type.TexCoord4; + material.setTexture(MaterialHelper.TEXTURE_TYPE_GLOW, combinedTexture.getResultTexture()); + break; + case MTEX_ALPHA: + uvCoordinatesType = VertexBuffer.Type.TexCoord5; + material.setTexture(MaterialHelper.TEXTURE_TYPE_ALPHA, combinedTexture.getResultTexture()); + break; + default: + LOGGER.severe("Unknown mapping type: " + entry.getKey().intValue()); + } + + //applying texture coordinates + if(uvCoordinatesType != null) { + VertexBuffer uvCoordsBuffer = new VertexBuffer(uvCoordinatesType); + uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, + BufferUtils.createFloatBuffer(combinedTexture.getResultUVS().toArray(new Vector2f[combinedTexture.getResultUVS().size()]))); + geometry.getMesh().setBuffer(uvCoordsBuffer); } } 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 bb550e316..6ffcdf5d1 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 @@ -33,10 +33,8 @@ package com.jme3.scene.plugins.blender.meshes; import java.nio.ByteBuffer; import java.util.ArrayList; -import java.util.HashMap; import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.Map.Entry; import com.jme3.asset.BlenderKey.FeaturesToLoad; @@ -58,8 +56,6 @@ import com.jme3.scene.plugins.blender.file.Structure; import com.jme3.scene.plugins.blender.materials.MaterialContext; import com.jme3.scene.plugins.blender.materials.MaterialHelper; import com.jme3.scene.plugins.blender.objects.Properties; -import com.jme3.scene.plugins.blender.textures.TextureHelper; -import com.jme3.texture.Texture; import com.jme3.util.BufferUtils; /** @@ -68,7 +64,6 @@ import com.jme3.util.BufferUtils; * @author Marcin Roguski (Kaelthas) */ public class MeshHelper extends AbstractBlenderHelper { - /** * This constructor parses the given blender version and stores the result. Some functionalities may differ in different blender * versions. @@ -102,9 +97,6 @@ public class MeshHelper extends AbstractBlenderHelper { return copiedGeometries; } - // helpers - TextureHelper textureHelper = blenderContext.getHelper(TextureHelper.class); - // reading mesh data String name = structure.getName(); MeshContext meshContext = new MeshContext(); @@ -146,37 +138,22 @@ public class MeshHelper extends AbstractBlenderHelper { } // indicates if the material with the specified number should have a texture attached - Map materialNumberToTexture = new HashMap(); int vertexColorIndex = 0; Vector2f[] uvCoordinatesForFace = new Vector2f[3]; for (int i = 0; i < mFaces.size(); ++i) { Structure mFace = mFaces.get(i); - int matNr = ((Number) mFace.getFieldValue("mat_nr")).intValue(); + int materialNumber = ((Number) mFace.getFieldValue("mat_nr")).intValue(); boolean smooth = (((Number) mFace.getFieldValue("flag")).byteValue() & 0x01) != 0x00; DynamicArray uvs = null; - boolean materialWithoutTextures = false; - Pointer pImage = null; if (mtFaces != null) { Structure mtFace = mtFaces.get(i); - pImage = (Pointer) mtFace.getFieldValue("tpage"); - materialWithoutTextures = pImage.isNull(); // uvs always must be added wheater we have texture or not uvs = (DynamicArray) mtFace.getFieldValue("uv"); uvCoordinatesForFace[0] = new Vector2f(uvs.get(0, 0).floatValue(), uvs.get(0, 1).floatValue()); uvCoordinatesForFace[1] = new Vector2f(uvs.get(1, 0).floatValue(), uvs.get(1, 1).floatValue()); uvCoordinatesForFace[2] = new Vector2f(uvs.get(2, 0).floatValue(), uvs.get(2, 1).floatValue()); } - Integer materialNumber = Integer.valueOf(materialWithoutTextures ? -1 * matNr - 1 : matNr); - - // attaching image to texture (face can have UV's and image whlie its material may have no texture attached) - if (pImage != null && pImage.isNotNull() && !materialNumberToTexture.containsKey(materialNumber)) { - Texture texture = textureHelper.getTextureFromImage(pImage.fetchData(blenderContext.getInputStream()).get(0), - blenderContext); - if (texture != null) { - materialNumberToTexture.put(materialNumber, texture); - } - } int v1 = ((Number) mFace.getFieldValue("v1")).intValue(); int v2 = ((Number) mFace.getFieldValue("v2")).intValue(); @@ -290,29 +267,17 @@ public class MeshHelper extends AbstractBlenderHelper { if(materials != null) { for(Geometry geometry : geometries) { int materialNumber = meshContext.getMaterialIndex(geometry); - List uvCoordinates = meshBuilder.getUVCoordinates(materialNumber); - boolean noTextures = false; - if(materialNumber < 0) { - materialNumber = -1 * (materialNumber + 1); - noTextures = true; - } if(materials[materialNumber] != null) { + List uvCoordinates = meshBuilder.getUVCoordinates(materialNumber); MaterialContext materialContext = materials[materialNumber]; - materialContext.applyMaterial(geometry, structure.getOldMemoryAddress(), noTextures, uvCoordinates, blenderContext); - } else { - geometry.setMaterial(blenderContext.getDefaultMaterial()); - if(uvCoordinates != null) { - VertexBuffer uvCoordsBuffer = new VertexBuffer(Type.TexCoord); - uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, BufferUtils.createFloatBuffer(uvCoordinates.toArray(new Vector2f[uvCoordinates.size()]))); - geometry.getMesh().setBuffer(uvCoordsBuffer); - } + materialContext.applyMaterial(geometry, structure.getOldMemoryAddress(), uvCoordinates, blenderContext); } } } else { //add UV coordinates if they are defined even if the material is not applied to the model VertexBuffer uvCoordsBuffer = null; if(meshBuilder.hasUVCoordinates()) { - List uvs = meshBuilder.getUVCoordinates(-1); + List uvs = meshBuilder.getUVCoordinates(0); uvCoordsBuffer = new VertexBuffer(Type.TexCoord); uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, BufferUtils.createFloatBuffer(uvs.toArray(new Vector2f[uvs.size()]))); } diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/textures/CombinedTexture.java b/engine/src/blender/com/jme3/scene/plugins/blender/textures/CombinedTexture.java index 8107a47fd..b7b2a558b 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/textures/CombinedTexture.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/textures/CombinedTexture.java @@ -110,11 +110,11 @@ public class CombinedTexture { } else if (textureData.texture instanceof Texture2D) { resultTexture = textureData.texture; - if (userDefinedUVCoordinates == null || userDefinedUVCoordinates.size() == 0) { + if(textureData.uvCoordinatesType == UVCoordinatesType.TEXCO_UV && userDefinedUVCoordinates != null && userDefinedUVCoordinates.size() > 0) { + resultUVS = userDefinedUVCoordinates; + } else { List geometries = (List) blenderContext.getLoadedFeature(geometriesOMA, LoadedFeatureDataType.LOADED_FEATURE); resultUVS = UVCoordinatesGenerator.generateUVCoordinatesFor2DTexture(mesh, textureData.uvCoordinatesType, textureData.projectionType, geometries); - } else { - resultUVS = userDefinedUVCoordinates; } } this.blend(resultTexture, textureData.textureBlender, blenderContext); @@ -145,11 +145,11 @@ public class CombinedTexture { } // first triangulate the current texture List textureUVS = null; - if (textureData.uvCoordinatesType != UVCoordinatesType.TEXCO_UV) { + if(textureData.uvCoordinatesType == UVCoordinatesType.TEXCO_UV && userDefinedUVCoordinates != null && userDefinedUVCoordinates.size() > 0) { + textureUVS = userDefinedUVCoordinates; + } else { List geometries = (List) blenderContext.getLoadedFeature(geometriesOMA, LoadedFeatureDataType.LOADED_FEATURE); textureUVS = UVCoordinatesGenerator.generateUVCoordinatesFor2DTexture(mesh, textureData.uvCoordinatesType, textureData.projectionType, geometries); - } else { - textureUVS = userDefinedUVCoordinates; } TriangulatedTexture triangulatedTexture = new TriangulatedTexture((Texture2D) textureData.texture, textureUVS, blenderContext); // then move the texture to different UV's