* Fix MTL issue where "Tr 0" could cause model to be invisible even though illum index specifies that it shouldn't use transparency

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8327 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
sha..rd 14 years ago
parent d38b469de6
commit 97a9b4ee00
  1. 38
      engine/src/core-plugins/com/jme3/scene/plugins/MTLLoader.java
  2. 19
      engine/src/core-plugins/com/jme3/scene/plugins/OBJLoader.java

@ -46,6 +46,7 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Locale; import java.util.Locale;
import java.util.NoSuchElementException;
import java.util.Scanner; import java.util.Scanner;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -69,6 +70,9 @@ public class MTLLoader implements AssetLoader {
protected String matName; protected String matName;
protected float alpha = 1; protected float alpha = 1;
protected boolean transparent = false; protected boolean transparent = false;
protected boolean disallowTransparency = false;
protected boolean disallowAmbient = false;
protected boolean disallowSpecular = false;
public void reset(){ public void reset(){
scan = null; scan = null;
@ -91,8 +95,14 @@ public class MTLLoader implements AssetLoader {
return result; return result;
} }
protected void skipLine(){ protected boolean skipLine(){
try {
scan.skip(".*\r{0,1}\n"); scan.skip(".*\r{0,1}\n");
return true;
} catch (NoSuchElementException ex){
// EOF
return false;
}
} }
protected void resetMaterial(){ protected void resetMaterial(){
@ -100,6 +110,9 @@ public class MTLLoader implements AssetLoader {
diffuse.set(ColorRGBA.LightGray); diffuse.set(ColorRGBA.LightGray);
specular.set(ColorRGBA.Black); specular.set(ColorRGBA.Black);
shininess = 16; shininess = 16;
disallowTransparency = false;
disallowAmbient = false;
disallowSpecular = false;
shadeless = false; shadeless = false;
transparent = false; transparent = false;
matName = null; matName = null;
@ -113,7 +126,7 @@ public class MTLLoader implements AssetLoader {
protected void createMaterial(){ protected void createMaterial(){
Material material; Material material;
if (alpha < 1f){ if (alpha < 1f && transparent && !disallowTransparency){
diffuse.a = alpha; diffuse.a = alpha;
} }
@ -136,7 +149,7 @@ public class MTLLoader implements AssetLoader {
if (alphaMap != null) material.setTexture("AlphaMap", alphaMap); if (alphaMap != null) material.setTexture("AlphaMap", alphaMap);
} }
if (transparent){ if (transparent && !disallowTransparency){
material.setTransparent(true); material.setTransparent(true);
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
material.getAdditionalRenderState().setAlphaTest(true); material.getAdditionalRenderState().setAlphaTest(true);
@ -181,7 +194,7 @@ public class MTLLoader implements AssetLoader {
String cmd = scan.next().toLowerCase(); String cmd = scan.next().toLowerCase();
if (cmd.startsWith("#")){ if (cmd.startsWith("#")){
// skip entire comment until next line // skip entire comment until next line
skipLine(); return skipLine();
}else if (cmd.equals("newmtl")){ }else if (cmd.equals("newmtl")){
String name = scan.next(); String name = scan.next();
startMaterial(name); startMaterial(name);
@ -201,7 +214,7 @@ public class MTLLoader implements AssetLoader {
transparent = true; transparent = true;
}else if (cmd.equals("map_ka")){ }else if (cmd.equals("map_ka")){
// ignore it for now // ignore it for now
skipLine(); return skipLine();
}else if (cmd.equals("map_kd")){ }else if (cmd.equals("map_kd")){
String path = nextStatement(); String path = nextStatement();
diffuseMap = loadTexture(path); diffuseMap = loadTexture(path);
@ -231,6 +244,17 @@ public class MTLLoader implements AssetLoader {
case 0: case 0:
// no lighting // no lighting
shadeless = true; shadeless = true;
disallowTransparency = true;
break;
case 1:
disallowSpecular = true;
disallowTransparency = true;
break;
case 2:
case 3:
case 5:
case 8:
disallowTransparency = true;
break; break;
case 4: case 4:
case 6: case 6:
@ -244,10 +268,10 @@ public class MTLLoader implements AssetLoader {
}else if (cmd.equals("ke") || cmd.equals("ni")){ }else if (cmd.equals("ke") || cmd.equals("ni")){
// Ni: index of refraction - unsupported in jME // Ni: index of refraction - unsupported in jME
// Ke: emission color // Ke: emission color
skipLine(); return skipLine();
}else{ }else{
logger.log(Level.WARNING, "Unknown statement in MTL! {0}", cmd); logger.log(Level.WARNING, "Unknown statement in MTL! {0}", cmd);
skipLine(); return skipLine();
} }
return true; return true;

@ -59,6 +59,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Scanner; import java.util.Scanner;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -323,7 +324,11 @@ public final class OBJLoader implements AssetLoader {
// NOTE: Cut off any relative/absolute paths // NOTE: Cut off any relative/absolute paths
name = new File(name).getName(); name = new File(name).getName();
try {
matList = (MaterialList) assetManager.loadAsset(key.getFolder() + name); matList = (MaterialList) assetManager.loadAsset(key.getFolder() + name);
} catch (AssetNotFoundException ex){
throw new AssetNotFoundException("Cannot find material " + name + " for model " + key.getName());
}
if (matList != null){ if (matList != null){
// create face lists for every material // create face lists for every material
@ -336,8 +341,14 @@ public final class OBJLoader implements AssetLoader {
} }
} }
protected void nextStatement(){ protected boolean nextStatement(){
try {
scan.skip(".*\r{0,1}\n"); scan.skip(".*\r{0,1}\n");
return true;
} catch (NoSuchElementException ex){
// EOF
return false;
}
} }
protected boolean readLine() throws IOException{ protected boolean readLine() throws IOException{
@ -348,7 +359,7 @@ public final class OBJLoader implements AssetLoader {
String cmd = scan.next(); String cmd = scan.next();
if (cmd.startsWith("#")){ if (cmd.startsWith("#")){
// skip entire comment until next line // skip entire comment until next line
nextStatement(); return nextStatement();
}else if (cmd.equals("v")){ }else if (cmd.equals("v")){
// vertex position // vertex position
verts.add(readVector3()); verts.add(readVector3());
@ -372,11 +383,11 @@ public final class OBJLoader implements AssetLoader {
String mtllib = scan.nextLine().trim(); String mtllib = scan.nextLine().trim();
loadMtlLib(mtllib); loadMtlLib(mtllib);
}else if (cmd.equals("s") || cmd.equals("g")){ }else if (cmd.equals("s") || cmd.equals("g")){
nextStatement(); return nextStatement();
}else{ }else{
// skip entire command until next line // skip entire command until next line
logger.log(Level.WARNING, "Unknown statement in OBJ! {0}", cmd); logger.log(Level.WARNING, "Unknown statement in OBJ! {0}", cmd);
nextStatement(); return nextStatement();
} }
return true; return true;

Loading…
Cancel
Save