* Made MTLLoader more compatible

* Fixed bug where MTLLoader would skip lines
 * Fixed exception in Mesh.setInterleaved

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7463 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
sha..rd 14 years ago
parent 7938b776de
commit 5bf644ff90
  1. 126
      engine/src/core-plugins/com/jme3/scene/plugins/MTLLoader.java
  2. 1
      engine/src/core/com/jme3/material/Material.java
  3. 6
      engine/src/core/com/jme3/scene/Mesh.java

@ -40,7 +40,6 @@ import com.jme3.material.MatParam;
import com.jme3.material.Material;
import com.jme3.material.MaterialList;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA;
import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture;
@ -58,7 +57,7 @@ public class MTLLoader implements AssetLoader {
protected Material material;
protected AssetManager assetManager;
protected String folderName;
public void reset(){
scan = null;
matList = null;
@ -86,107 +85,130 @@ public class MTLLoader implements AssetLoader {
material.setFloat("Shininess", 16f); // prevents "premature culling" bug
matList.put(name, material);
}
protected Texture loadTexture(String path){
String name = new File(path).getName();
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
if (texture != null){
texture.setWrap(WrapMode.Repeat);
}
return texture;
}
protected boolean readLine(){
if (!scan.hasNext()){
return false;
}
String cmd = scan.next();
String cmd = scan.next().toLowerCase();
if (cmd.startsWith("#")){
// skip entire comment until next line
nextStatement();
}else if (cmd.equals("newmtl")){
String name = scan.next();
startMaterial(name);
}else if (cmd.equals("Ka") || cmd.equals("Ke") || cmd.equals("Ni") || cmd.equals("illum")){
// ignore it for now
}else if (cmd.equals("Kd")){
}else if (cmd.equals("ka")){
material.setColor("Ambient", readColor());
}else if (cmd.equals("kd")){
ColorRGBA color = readColor();
MatParam param = material.getParam("Diffuse");
if (param != null){
color.a = ((ColorRGBA) param.getValue()).getAlpha();
}
material.setColor("Diffuse", color);
}else if (cmd.equals("Ks")){
}else if (cmd.equals("ks")){
material.setColor("Specular", readColor());
}else if (cmd.equals("Ns")){
}else if (cmd.equals("ns")){
material.setFloat("Shininess", scan.nextFloat() /* (128f / 1000f)*/ );
}else if (cmd.equals("d")){
}else if (cmd.equals("d") || cmd.equals("tr")){
float alpha = scan.nextFloat();
// if (alpha < 1f){
// MatParam param = material.getParam("Diffuse");
// ColorRGBA color;
// if (param != null)
// color = (ColorRGBA) param.getValue();
// else
// color = new ColorRGBA(ColorRGBA.White);
//
// color.a = scan.nextFloat();
// material.setColor("Diffuse", color);
// material.setBoolean("UseAlpha", true);
// material.setTransparent(true);
// material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
// }
}else if (cmd.equals("map_Ka")){
if (alpha < 1f){
MatParam param = material.getParam("Diffuse");
ColorRGBA color;
if (param != null)
color = (ColorRGBA) param.getValue();
else
color = new ColorRGBA(ColorRGBA.White);
color.a = scan.nextFloat();
material.setColor("Diffuse", color);
material.setTransparent(true);
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
}
}else if (cmd.equals("map_ka")){
// ignore it for now
}else if (cmd.equals("map_Kd")){
}else if (cmd.equals("map_kd")){
String path = scan.next();
String name = new File(path).getName();
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
if (texture != null){
texture.setWrap(WrapMode.Repeat);
material.setTexture("DiffuseMap", texture);
}
material.setTexture("DiffuseMap", loadTexture(path));
}else if (cmd.equals("map_bump") || cmd.equals("bump")){
if (material.getParam("NormalMap") == null){
String path = scan.next();
String name = new File(path).getName();
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
Texture texture = loadTexture(path);
if (texture != null){
texture.setWrap(WrapMode.Repeat);
material.setTexture("NormalMap", texture);
if (texture.getImage().getFormat() == Format.LATC){
material.setBoolean("LATC", true);
}
}
}
}else if (cmd.equals("map_Ks")){
}else if (cmd.equals("map_ks")){
String path = scan.next();
String name = new File(path).getName();
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
Texture texture = loadTexture(path);
if (texture != null){
texture.setWrap(WrapMode.Repeat);
material.setTexture("SpecularMap", texture);
// NOTE: since specular color is modulated with specmap
// make sure we have it set
material.setColor("Specular", ColorRGBA.White);
MatParam specParam = material.getParam("Specular");
if (specParam == null){
material.setColor("Specular", ColorRGBA.White);
}else{
ColorRGBA spec = (ColorRGBA) specParam.getValue();
if (spec.equals(ColorRGBA.Black)){
material.setColor("Specular", ColorRGBA.White);
}
}
}
}else if (cmd.equals("map_d")){
String path = scan.next();
String name = new File(path).getName();
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
Texture texture = loadTexture(path);
if (texture != null){
texture.setWrap(WrapMode.Repeat);
material.setTexture("AlphaMap", texture);
material.setTransparent(true);
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
material.getAdditionalRenderState().setAlphaTest(true);
material.getAdditionalRenderState().setAlphaFallOff(0.01f);
}
}else if (cmd.equals("illum")){
int mode = scan.nextInt();
switch (mode){
case 0:
// no ambient
material.setColor("Ambient", ColorRGBA.Black);
break;
case 4:
case 6:
case 7:
case 9:
// Enable transparency
material.setTransparent(true);
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
// Works best if diffuse map has an alpha channel
break;
}
}else if (cmd.equals("ke") || cmd.equals("ni")){
// Ni: index of refraction - unsupported in jME
// Ke: emission color
nextStatement();
}else{
System.out.println("Unknown statement in MTL! "+cmd);
nextStatement();
}
nextStatement();
return true;
}

@ -279,7 +279,6 @@ public class Material implements Cloneable, Savable, Comparable<Material> {
if (param instanceof MatParam) {
return (MatParam) param;
}
return null;
}

@ -336,7 +336,7 @@ public class Mesh implements Savable, Cloneable {
VertexBuffer allData = new VertexBuffer(Type.InterleavedData);
ByteBuffer dataBuf = BufferUtils.createByteBuffer(stride * getVertexCount());
allData.setupData(Usage.Static, -1, Format.UnsignedByte, dataBuf);
allData.setupData(Usage.Static, 1, Format.UnsignedByte, dataBuf);
// adding buffer directly so that no update counts is forced
buffers.put(Type.InterleavedData.ordinal(), allData);
@ -387,8 +387,8 @@ public class Mesh implements Savable, Cloneable {
vb.setOffset(offset);
vb.setStride(stride);
// discard old buffer
vb.setupData(vb.usage, vb.components, vb.format, null);
vb.updateData(null);
//vb.setupData(vb.usage, vb.components, vb.format, null);
offset += vb.componentsLength;
}
}

Loading…
Cancel
Save