Fixing bugs with applying UV maps.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9515 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
Kae..pl 13 years ago
parent 440217fbd2
commit 332fe4d3ac
  1. 2
      engine/src/blender/com/jme3/scene/plugins/blender/curves/CurvesHelper.java
  2. 4
      engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialContext.java
  3. 43
      engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java
  4. 12
      engine/src/blender/com/jme3/scene/plugins/blender/textures/CombinedTexture.java

@ -193,7 +193,7 @@ public class CurvesHelper extends AbstractBlenderHelper {
if (nurbGeoms != null) {//setting the name and assigning materials if (nurbGeoms != null) {//setting the name and assigning materials
for (Geometry nurbGeom : nurbGeoms) { for (Geometry nurbGeom : nurbGeoms) {
if(materialContexts != null) { 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 { } else {
nurbGeom.setMaterial(defaultMaterial); nurbGeom.setMaterial(defaultMaterial);
} }

@ -166,7 +166,7 @@ public final class MaterialContext {
this.transparent = transparent; this.transparent = transparent;
} }
public void applyMaterial(Geometry geometry, Long geometriesOMA, boolean noTextures, List<Vector2f> userDefinedUVCoordinates, BlenderContext blenderContext) { public void applyMaterial(Geometry geometry, Long geometriesOMA, List<Vector2f> userDefinedUVCoordinates, BlenderContext blenderContext) {
Material material = null; Material material = null;
if (shadeless) { if (shadeless) {
material = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); material = new Material(blenderContext.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
@ -195,7 +195,6 @@ public final class MaterialContext {
} }
//applying textures //applying textures
if(!noTextures) {
for(Entry<Number, CombinedTexture> entry : loadedTextures.entrySet()) { for(Entry<Number, CombinedTexture> entry : loadedTextures.entrySet()) {
CombinedTexture combinedTexture = entry.getValue(); CombinedTexture combinedTexture = entry.getValue();
combinedTexture.flatten(geometry, geometriesOMA, userDefinedUVCoordinates, blenderContext); combinedTexture.flatten(geometry, geometriesOMA, userDefinedUVCoordinates, blenderContext);
@ -235,7 +234,6 @@ public final class MaterialContext {
geometry.getMesh().setBuffer(uvCoordsBuffer); geometry.getMesh().setBuffer(uvCoordsBuffer);
} }
} }
}
//applying additional data //applying additional data
material.setName(name); material.setName(name);

@ -33,10 +33,8 @@ package com.jme3.scene.plugins.blender.meshes;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import com.jme3.asset.BlenderKey.FeaturesToLoad; 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.MaterialContext;
import com.jme3.scene.plugins.blender.materials.MaterialHelper; import com.jme3.scene.plugins.blender.materials.MaterialHelper;
import com.jme3.scene.plugins.blender.objects.Properties; 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; import com.jme3.util.BufferUtils;
/** /**
@ -68,7 +64,6 @@ import com.jme3.util.BufferUtils;
* @author Marcin Roguski (Kaelthas) * @author Marcin Roguski (Kaelthas)
*/ */
public class MeshHelper extends AbstractBlenderHelper { public class MeshHelper extends AbstractBlenderHelper {
/** /**
* This constructor parses the given blender version and stores the result. Some functionalities may differ in different blender * This constructor parses the given blender version and stores the result. Some functionalities may differ in different blender
* versions. * versions.
@ -102,9 +97,6 @@ public class MeshHelper extends AbstractBlenderHelper {
return copiedGeometries; return copiedGeometries;
} }
// helpers
TextureHelper textureHelper = blenderContext.getHelper(TextureHelper.class);
// reading mesh data // reading mesh data
String name = structure.getName(); String name = structure.getName();
MeshContext meshContext = new MeshContext(); 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 // indicates if the material with the specified number should have a texture attached
Map<Integer, Texture> materialNumberToTexture = new HashMap<Integer, Texture>();
int vertexColorIndex = 0; int vertexColorIndex = 0;
Vector2f[] uvCoordinatesForFace = new Vector2f[3]; Vector2f[] uvCoordinatesForFace = new Vector2f[3];
for (int i = 0; i < mFaces.size(); ++i) { for (int i = 0; i < mFaces.size(); ++i) {
Structure mFace = mFaces.get(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; boolean smooth = (((Number) mFace.getFieldValue("flag")).byteValue() & 0x01) != 0x00;
DynamicArray<Number> uvs = null; DynamicArray<Number> uvs = null;
boolean materialWithoutTextures = false;
Pointer pImage = null;
if (mtFaces != null) { if (mtFaces != null) {
Structure mtFace = mtFaces.get(i); 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 always must be added wheater we have texture or not
uvs = (DynamicArray<Number>) mtFace.getFieldValue("uv"); uvs = (DynamicArray<Number>) mtFace.getFieldValue("uv");
uvCoordinatesForFace[0] = new Vector2f(uvs.get(0, 0).floatValue(), uvs.get(0, 1).floatValue()); 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[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()); 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 v1 = ((Number) mFace.getFieldValue("v1")).intValue();
int v2 = ((Number) mFace.getFieldValue("v2")).intValue(); int v2 = ((Number) mFace.getFieldValue("v2")).intValue();
@ -290,29 +267,17 @@ public class MeshHelper extends AbstractBlenderHelper {
if(materials != null) { if(materials != null) {
for(Geometry geometry : geometries) { for(Geometry geometry : geometries) {
int materialNumber = meshContext.getMaterialIndex(geometry); int materialNumber = meshContext.getMaterialIndex(geometry);
List<Vector2f> uvCoordinates = meshBuilder.getUVCoordinates(materialNumber);
boolean noTextures = false;
if(materialNumber < 0) {
materialNumber = -1 * (materialNumber + 1);
noTextures = true;
}
if(materials[materialNumber] != null) { if(materials[materialNumber] != null) {
List<Vector2f> uvCoordinates = meshBuilder.getUVCoordinates(materialNumber);
MaterialContext materialContext = materials[materialNumber]; MaterialContext materialContext = materials[materialNumber];
materialContext.applyMaterial(geometry, structure.getOldMemoryAddress(), noTextures, uvCoordinates, blenderContext); materialContext.applyMaterial(geometry, structure.getOldMemoryAddress(), 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);
}
} }
} }
} else { } else {
//add UV coordinates if they are defined even if the material is not applied to the model //add UV coordinates if they are defined even if the material is not applied to the model
VertexBuffer uvCoordsBuffer = null; VertexBuffer uvCoordsBuffer = null;
if(meshBuilder.hasUVCoordinates()) { if(meshBuilder.hasUVCoordinates()) {
List<Vector2f> uvs = meshBuilder.getUVCoordinates(-1); List<Vector2f> uvs = meshBuilder.getUVCoordinates(0);
uvCoordsBuffer = new VertexBuffer(Type.TexCoord); uvCoordsBuffer = new VertexBuffer(Type.TexCoord);
uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, BufferUtils.createFloatBuffer(uvs.toArray(new Vector2f[uvs.size()]))); uvCoordsBuffer.setupData(Usage.Static, 2, Format.Float, BufferUtils.createFloatBuffer(uvs.toArray(new Vector2f[uvs.size()])));
} }

@ -110,11 +110,11 @@ public class CombinedTexture {
} else if (textureData.texture instanceof Texture2D) { } else if (textureData.texture instanceof Texture2D) {
resultTexture = textureData.texture; resultTexture = textureData.texture;
if (userDefinedUVCoordinates == null || userDefinedUVCoordinates.size() == 0) { if(textureData.uvCoordinatesType == UVCoordinatesType.TEXCO_UV && userDefinedUVCoordinates != null && userDefinedUVCoordinates.size() > 0) {
resultUVS = userDefinedUVCoordinates;
} else {
List<Geometry> geometries = (List<Geometry>) blenderContext.getLoadedFeature(geometriesOMA, LoadedFeatureDataType.LOADED_FEATURE); List<Geometry> geometries = (List<Geometry>) blenderContext.getLoadedFeature(geometriesOMA, LoadedFeatureDataType.LOADED_FEATURE);
resultUVS = UVCoordinatesGenerator.generateUVCoordinatesFor2DTexture(mesh, textureData.uvCoordinatesType, textureData.projectionType, geometries); resultUVS = UVCoordinatesGenerator.generateUVCoordinatesFor2DTexture(mesh, textureData.uvCoordinatesType, textureData.projectionType, geometries);
} else {
resultUVS = userDefinedUVCoordinates;
} }
} }
this.blend(resultTexture, textureData.textureBlender, blenderContext); this.blend(resultTexture, textureData.textureBlender, blenderContext);
@ -145,11 +145,11 @@ public class CombinedTexture {
} }
// first triangulate the current texture // first triangulate the current texture
List<Vector2f> textureUVS = null; List<Vector2f> textureUVS = null;
if (textureData.uvCoordinatesType != UVCoordinatesType.TEXCO_UV) { if(textureData.uvCoordinatesType == UVCoordinatesType.TEXCO_UV && userDefinedUVCoordinates != null && userDefinedUVCoordinates.size() > 0) {
textureUVS = userDefinedUVCoordinates;
} else {
List<Geometry> geometries = (List<Geometry>) blenderContext.getLoadedFeature(geometriesOMA, LoadedFeatureDataType.LOADED_FEATURE); List<Geometry> geometries = (List<Geometry>) blenderContext.getLoadedFeature(geometriesOMA, LoadedFeatureDataType.LOADED_FEATURE);
textureUVS = UVCoordinatesGenerator.generateUVCoordinatesFor2DTexture(mesh, textureData.uvCoordinatesType, textureData.projectionType, geometries); textureUVS = UVCoordinatesGenerator.generateUVCoordinatesFor2DTexture(mesh, textureData.uvCoordinatesType, textureData.projectionType, geometries);
} else {
textureUVS = userDefinedUVCoordinates;
} }
TriangulatedTexture triangulatedTexture = new TriangulatedTexture((Texture2D) textureData.texture, textureUVS, blenderContext); TriangulatedTexture triangulatedTexture = new TriangulatedTexture((Texture2D) textureData.texture, textureUVS, blenderContext);
// then move the texture to different UV's // then move the texture to different UV's

Loading…
Cancel
Save