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
3.0
sha..rd 13 years ago
parent d87e69d97f
commit 17b949fea2
  1. 38
      engine/src/blender/com/jme3/scene/plugins/blender/materials/MaterialHelper.java
  2. 11
      engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java
  3. 8
      engine/src/blender/com/jme3/scene/plugins/blender/objects/ObjectHelper.java
  4. 4
      engine/src/blender/com/jme3/scene/plugins/blender/textures/TextureHelper.java

@ -190,8 +190,9 @@ public class MaterialHelper extends AbstractBlenderHelper {
int mode = ((Number) structure.getFieldValue("mode")).intValue(); int mode = ((Number) structure.getFieldValue("mode")).intValue();
boolean shadeless = (mode & 0x4) != 0; boolean shadeless = (mode & 0x4) != 0;
boolean vertexColor = (mode & 0x16) != 0; boolean vertexColor = (mode & 0x80) != 0;
boolean transparent = (mode & 0x64) != 0; boolean transparent = (mode & 0x10000) != 0;
boolean vtangent = (mode & 0x4000000) != 0; // NOTE: Requires tangents
if (shadeless) { if (shadeless) {
result = new Material(dataRepository.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md"); result = new Material(dataRepository.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
@ -199,12 +200,18 @@ public class MaterialHelper extends AbstractBlenderHelper {
result = new Material(dataRepository.getAssetManager(), "Common/MatDefs/Light/Lighting.j3md"); 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); result.getAdditionalRenderState().setFaceCullMode(faceCullMode);
if (transparent) { if (transparent) {
result.setTransparent(true); result.setTransparent(true);
result.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); result.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
} }
String name = structure.getName(); String name = structure.getName();
LOGGER.log(Level.INFO, "Material's name: {0}", name); LOGGER.log(Level.INFO, "Material's name: {0}", name);
@ -216,6 +223,7 @@ public class MaterialHelper extends AbstractBlenderHelper {
ColorRGBA diffuseColor = null; ColorRGBA diffuseColor = null;
if (shadeless) { if (shadeless) {
// color of shadeless? doesn't seem to work in blender .. // color of shadeless? doesn't seem to work in blender ..
diffuseColor = ColorRGBA.White.clone();
} else { } else {
result.setBoolean("UseMaterialColors", Boolean.TRUE); result.setBoolean("UseMaterialColors", Boolean.TRUE);
@ -223,6 +231,9 @@ public class MaterialHelper extends AbstractBlenderHelper {
DiffuseShader diffuseShader = materialHelper.getDiffuseShader(structure); DiffuseShader diffuseShader = materialHelper.getDiffuseShader(structure);
result.setBoolean("Minnaert", diffuseShader == DiffuseShader.MINNAERT); result.setBoolean("Minnaert", diffuseShader == DiffuseShader.MINNAERT);
diffuseColor = materialHelper.getDiffuseColor(structure, diffuseShader); diffuseColor = materialHelper.getDiffuseColor(structure, diffuseShader);
if (!transparent){
diffuseColor.a = 1;
}
result.setColor("Diffuse", diffuseColor); result.setColor("Diffuse", diffuseColor);
SpecularShader specularShader = materialHelper.getSpecularShader(structure); SpecularShader specularShader = materialHelper.getSpecularShader(structure);
@ -231,6 +242,8 @@ public class MaterialHelper extends AbstractBlenderHelper {
result.setColor("Ambient", materialHelper.getAmbientColor(structure)); result.setColor("Ambient", materialHelper.getAmbientColor(structure));
result.setFloat("Shininess", materialHelper.getShininess(structure)); result.setFloat("Shininess", materialHelper.getShininess(structure));
} }
// texture // texture
@ -262,40 +275,43 @@ public class MaterialHelper extends AbstractBlenderHelper {
if ((mapto & 0x01) != 0) {// Col if ((mapto & 0x01) != 0) {// Col
// Map to COLOR channel or DIFFUSE // Map to COLOR channel or DIFFUSE
// Set diffuse to white so it doesn't get multiplied by texture // 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); //result.setBoolean("UseMaterialColors", Boolean.FALSE);
// blending the texture with material color and texture's defined color // blending the texture with material color and texture's defined color
int blendType = ((Number) textureLink.getFieldValue("blendtype")).intValue(); 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[] 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(); float colfac = ((Number) textureLink.getFieldValue("colfac")).floatValue();
texture = textureHelper.blendTexture(diffuseColor.getColorArray(), texture, color, colfac, blendType, negateTexture, dataRepository); 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) { if (shadeless) {
result.setTexture(TEXTURE_TYPE_COLOR, texture); result.setTexture(TEXTURE_TYPE_COLOR, texture);
} else { } else {
result.setTexture(TEXTURE_TYPE_DIFFUSE, texture); result.setTexture(TEXTURE_TYPE_DIFFUSE, texture);
} }
} }
if ((mapto & 0x02) != 0) {// Nor if ((mapto & 0x02) != 0 && !shadeless) {// Nor
Texture normalMapTexture; Texture normalMapTexture;
if(texture.getKey() instanceof GeneratedTextureKey) { if(texture.getKey() instanceof GeneratedTextureKey) {
normalMapTexture = textureHelper.convertToNormalMapTexture(texture, ((Number)textureLink.getFieldValue("norfac")).floatValue()); normalMapTexture = textureHelper.convertToNormalMapTexture(texture, ((Number)textureLink.getFieldValue("norfac")).floatValue());
} else { normalMapTexture.setMinFilter(MinFilter.Trilinear);
} else {
normalMapTexture = texture; normalMapTexture = texture;
} }
result.setTexture(TEXTURE_TYPE_NORMAL, normalMapTexture); result.setTexture(TEXTURE_TYPE_NORMAL, normalMapTexture);
if (vertexColor) { if (vertexColor) {
result.setBoolean(shadeless ? "VertexColor" : "UseVertexColor", false); result.setBoolean(shadeless ? "VertexColor" : "UseVertexColor", false);
} }
} }
if ((mapto & 0x04) != 0) {// Spec if ((mapto & 0x04) != 0 && !shadeless) {// Spec
// Map to SPECULAR // Map to SPECULAR
result.setTexture(TEXTURE_TYPE_SPECULAR, texture); result.setTexture(TEXTURE_TYPE_SPECULAR, texture);
} }
if ((mapto & 0x40) != 0) {// Emit if ((mapto & 0x40) != 0) {// Emit
result.setTexture(TEXTURE_TYPE_GLOW, texture); result.setTexture(TEXTURE_TYPE_GLOW, texture);
} }
if ((mapto & 0x80) != 0) {// Alpha if ((mapto & 0x80) != 0 && !shadeless) {// Alpha
result.setTexture(TEXTURE_TYPE_ALPHA, texture); result.setTexture(TEXTURE_TYPE_ALPHA, texture);
} }
} else { } else {

@ -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) // the following map sorts faces by material number (because in jme Mesh can have only one material)
Map<Integer, List<Integer>> meshesMap = new HashMap<Integer, List<Integer>>(); Map<Integer, List<Integer>> meshesMap = new HashMap<Integer, List<Integer>>();
Pointer pMFace = (Pointer) structure.getFieldValue("mface"); Pointer pMFace = (Pointer) structure.getFieldValue("mface");
List<Structure> mFaces = pMFace.fetchData(dataRepository.getInputStream()); List<Structure> mFaces = null;
if (pMFace.isNotNull()){
mFaces = pMFace.fetchData(dataRepository.getInputStream());
}
Pointer pMTFace = (Pointer) structure.getFieldValue("mtface"); Pointer pMTFace = (Pointer) structure.getFieldValue("mtface");
List<Vector2f> uvCoordinates = null; List<Vector2f> uvCoordinates = null;
@ -153,6 +156,9 @@ public class MeshHelper extends AbstractBlenderHelper {
// positions (it simply tells which vertex is referenced where in the result list) // positions (it simply tells which vertex is referenced where in the result list)
Map<Integer, List<Integer>> vertexReferenceMap = new HashMap<Integer, List<Integer>>(verticesAmount); Map<Integer, List<Integer>> vertexReferenceMap = new HashMap<Integer, List<Integer>>(verticesAmount);
int vertexColorIndex = 0; int vertexColorIndex = 0;
if (mFaces == null){
return null;
}
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);
boolean smooth = (((Number) mFace.getFieldValue("flag")).byteValue() & 0x01) != 0x00; 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 { public Vector3f[] getVertices(Structure meshStructure, DataRepository dataRepository) throws BlenderFileException {
int verticesAmount = ((Number) meshStructure.getFieldValue("totvert")).intValue(); int verticesAmount = ((Number) meshStructure.getFieldValue("totvert")).intValue();
Vector3f[] vertices = new Vector3f[verticesAmount]; Vector3f[] vertices = new Vector3f[verticesAmount];
if (verticesAmount == 0)
return vertices;
Pointer pMVert = (Pointer) meshStructure.getFieldValue("mvert"); Pointer pMVert = (Pointer) meshStructure.getFieldValue("mvert");
List<Structure> mVerts = pMVert.fetchData(dataRepository.getInputStream()); List<Structure> mVerts = pMVert.fetchData(dataRepository.getInputStream());
for (int i = 0; i < verticesAmount; ++i) { for (int i = 0; i < verticesAmount; ++i) {

@ -184,9 +184,11 @@ public class ObjectHelper extends AbstractBlenderHelper {
Pointer pMesh = (Pointer)objectStructure.getFieldValue("data"); Pointer pMesh = (Pointer)objectStructure.getFieldValue("data");
List<Structure> meshesArray = pMesh.fetchData(dataRepository.getInputStream()); List<Structure> meshesArray = pMesh.fetchData(dataRepository.getInputStream());
List<Geometry> geometries = meshHelper.toMesh(meshesArray.get(0), dataRepository); List<Geometry> geometries = meshHelper.toMesh(meshesArray.get(0), dataRepository);
for(Geometry geometry : geometries) { if (geometries != null){
node.attachChild(geometry); for(Geometry geometry : geometries) {
} node.attachChild(geometry);
}
}
node.setLocalTransform(t); node.setLocalTransform(t);
//reading and applying all modifiers //reading and applying all modifiers

@ -935,12 +935,12 @@ public class TextureHelper extends AbstractBlenderHelper {
name = name.substring(1); name = name.substring(1);
} }
TextureKey texKey = new TextureKey(name, false); TextureKey texKey = new TextureKey(name, true);
try { try {
Texture tex = dataRepository.getAssetManager().loadTexture(texKey); Texture tex = dataRepository.getAssetManager().loadTexture(texKey);
image = tex.getImage(); image = tex.getImage();
} catch (AssetNotFoundException e) { } catch (AssetNotFoundException e) {
LOGGER.warning("Asset not found: " + e.getLocalizedMessage()); LOGGER.log(Level.WARNING, "Asset not found: {0}", e.getLocalizedMessage());
} }
} }

Loading…
Cancel
Save