OBJ loader: reworked texture resolution: keys now have to start with #, like every other model loader; models without explicit library now work, remapping is possible by using the key "#OBJModel.Default.Texture.Name"; in addition to remapping by material name, remapping by texture name works too, like in other model formats.

This commit is contained in:
RainWarrior 2015-11-07 20:36:57 +03:00
parent 3d03461a52
commit 8a11ad7a7a
2 changed files with 21 additions and 3 deletions

View file

@ -212,6 +212,7 @@ public class OBJModel implements IRetexturableModel, IModelCustomData
{
String currentLine = "";
Material material = new Material();
material.setName(Material.DEFAULT_NAME);
int usemtlCounter = 0;
// float[] minUVBounds = new float[] {0.0f, 0.0f};
@ -431,6 +432,9 @@ public class OBJModel implements IRetexturableModel, IModelCustomData
public MaterialLibrary()
{
this.groups.put(Group.DEFAULT_NAME, new Group(Group.DEFAULT_NAME, null));
Material def = new Material();
def.setName(Material.DEFAULT_NAME);
this.materials.put(Material.DEFAULT_NAME, def);
}
public MaterialLibrary makeLibWithReplacements(ImmutableMap<String, String> replacements)
@ -438,10 +442,24 @@ public class OBJModel implements IRetexturableModel, IModelCustomData
Map<String, Material> mats = new HashMap<String, Material>();
for (Map.Entry<String, Material> e : this.materials.entrySet())
{
if (replacements.containsKey(e.getKey()) || replacements.containsKey("all"))
// key for the material name, with # added if missing
String keyMat = e.getKey();
if(!keyMat.startsWith("#")) keyMat = "#" + keyMat;
// key for the texture name, with ".png" stripped and # added if missing
String keyTex = e.getValue().getTexture().getPath();
if(keyTex.endsWith(".png")) keyTex = keyTex.substring(0, keyTex.length() - ".png".length());
if(!keyTex.startsWith("#")) keyTex = "#" + keyTex;
if (replacements.containsKey(keyMat))
{
Texture currentTexture = e.getValue().texture;
Texture replacementTexture = new Texture(replacements.get(e.getKey()), currentTexture.position, currentTexture.scale, currentTexture.rotation);
Texture replacementTexture = new Texture(replacements.get(keyMat), currentTexture.position, currentTexture.scale, currentTexture.rotation);
Material replacementMaterial = new Material(e.getValue().color, replacementTexture, e.getValue().name);
mats.put(e.getKey(), replacementMaterial);
}
else if (replacements.containsKey(keyTex))
{
Texture currentTexture = e.getValue().texture;
Texture replacementTexture = new Texture(replacements.get(keyTex), currentTexture.position, currentTexture.scale, currentTexture.rotation);
Material replacementMaterial = new Material(e.getValue().color, replacementTexture, e.getValue().name);
mats.put(e.getKey(), replacementMaterial);
}

View file

@ -2,7 +2,7 @@
"forge_marker": 1,
"defaults": {
"textures": {
// "lambert7SG": "forgedebugmodelloaderregistry:texture" //the identifier must be a name of a material defined by the "model" obj's .mtl file
// "#lambert7SG": "forgedebugmodelloaderregistry:texture" //the identifier must be a name of a material defined by the "model" obj's .mtl file
},
"model": "forgedebugmodelloaderregistry:tesseract.obj"
},