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. 76
      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
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);
}

@ -166,7 +166,7 @@ public final class MaterialContext {
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;
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<Number, CombinedTexture> 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());
}
for(Entry<Number, CombinedTexture> 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);
}
//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);
}
}

@ -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<Integer, Texture> materialNumberToTexture = new HashMap<Integer, Texture>();
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<Number> 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<Number>) 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<Vector2f> uvCoordinates = meshBuilder.getUVCoordinates(materialNumber);
boolean noTextures = false;
if(materialNumber < 0) {
materialNumber = -1 * (materialNumber + 1);
noTextures = true;
}
if(materials[materialNumber] != null) {
List<Vector2f> 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<Vector2f> uvs = meshBuilder.getUVCoordinates(-1);
List<Vector2f> uvs = meshBuilder.getUVCoordinates(0);
uvCoordsBuffer = new VertexBuffer(Type.TexCoord);
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) {
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);
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<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);
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

Loading…
Cancel
Save