Allow model jsons to override the material library used by OBJ models.

Fix model loader test mod resources.
Update licenses.
This commit is contained in:
David Quintana 2020-01-01 17:15:49 +01:00
parent 53747b0cb5
commit ecb56054bc
5 changed files with 75 additions and 23 deletions

View File

@ -26,11 +26,17 @@ import net.minecraft.client.renderer.Vector4f;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.NoSuchElementException;
public class MaterialLibrary
{
public static final MaterialLibrary EMPTY = new MaterialLibrary();
final Map<String, Material> materials = Maps.newHashMap();
private MaterialLibrary()
{
}
public MaterialLibrary(OBJLoader.LineReader reader) throws IOException
{
Material currentMaterial = null;
@ -97,10 +103,12 @@ public class MaterialLibrary
public Material getMaterial(String mat)
{
if (!materials.containsKey(mat))
throw new NoSuchElementException("The material was not found in the library: " + mat);
return materials.get(mat);
}
public class Material
public static class Material
{
public final String name;
public Vector4f ambientColor = new Vector4f(0,0,0,1);

View File

@ -67,17 +67,19 @@ public class OBJLoader implements IModelLoader<OBJModel>
boolean diffuseLighting = JSONUtils.getBoolean(modelContents, "diffuseLighting", false);
boolean flipV = JSONUtils.getBoolean(modelContents, "flip-v", false);
boolean ambientToFullbright = JSONUtils.getBoolean(modelContents, "ambientToFullbright", true);
@Nullable
String materialLibraryOverrideLocation = modelContents.has("materialLibraryOverride") ? JSONUtils.getString(modelContents, "materialLibraryOverride") : null;
return loadModel(new ResourceLocation(modelLocation), detectCullableFaces, diffuseLighting, flipV, ambientToFullbright);
return loadModel(new ModelSettings(new ResourceLocation(modelLocation), detectCullableFaces, diffuseLighting, flipV, ambientToFullbright, materialLibraryOverrideLocation));
}
public OBJModel loadModel(ResourceLocation modelLocation, boolean detectCullableFaces, boolean diffuseLighting, boolean flipV, boolean ambientToFullbright)
public OBJModel loadModel(ModelSettings settings)
{
return modelCache.computeIfAbsent(new ModelSettings(modelLocation, detectCullableFaces, diffuseLighting, flipV, ambientToFullbright), (data) -> {
return modelCache.computeIfAbsent(settings, (data) -> {
IResource resource;
try
{
resource = manager.getResource(modelLocation);
resource = manager.getResource(settings.modelLocation);
}
catch (IOException e)
{
@ -86,7 +88,7 @@ public class OBJLoader implements IModelLoader<OBJModel>
try(LineReader rdr = new LineReader(resource))
{
return new OBJModel(modelLocation, rdr, detectCullableFaces, diffuseLighting, flipV, ambientToFullbright);
return new OBJModel(rdr, settings);
}
catch (Exception e)
{
@ -133,6 +135,7 @@ public class OBJLoader implements IModelLoader<OBJModel>
@Nullable
public String[] readAndSplitLine(boolean ignoreEmptyLines) throws IOException
{
//noinspection LoopConditionNotUpdatedInsideLoop
do
{
String currentLine = lineReader.readLine();
@ -170,7 +173,7 @@ public class OBJLoader implements IModelLoader<OBJModel>
if (lineParts.size() > 0)
return lineParts.toArray(new String[0]);
}
while (ignoreEmptyLines); // conditional expression is not updated, yes, I know.
while (ignoreEmptyLines);
return new String[0];
}
@ -183,22 +186,26 @@ public class OBJLoader implements IModelLoader<OBJModel>
}
}
private class ModelSettings
public static class ModelSettings
{
@Nonnull
private final ResourceLocation modelLocation;
private final boolean detectCullableFaces;
private final boolean diffuseLighting;
private final boolean flipV;
private final boolean ambientToFullbright;
public final ResourceLocation modelLocation;
public final boolean detectCullableFaces;
public final boolean diffuseLighting;
public final boolean flipV;
public final boolean ambientToFullbright;
@Nullable
public final String materialLibraryOverrideLocation;
public ModelSettings(@Nonnull ResourceLocation modelLocation, boolean detectCullableFaces, boolean diffuseLighting, boolean flipV, boolean ambientToFullbright)
public ModelSettings(@Nonnull ResourceLocation modelLocation, boolean detectCullableFaces, boolean diffuseLighting, boolean flipV, boolean ambientToFullbright,
@Nullable String materialLibraryOverrideLocation)
{
this.modelLocation = modelLocation;
this.detectCullableFaces = detectCullableFaces;
this.diffuseLighting = diffuseLighting;
this.flipV = flipV;
this.ambientToFullbright = ambientToFullbright;
this.materialLibraryOverrideLocation = materialLibraryOverrideLocation;
}
@Override
@ -216,13 +223,14 @@ public class OBJLoader implements IModelLoader<OBJModel>
diffuseLighting == that.diffuseLighting &&
flipV == that.flipV &&
ambientToFullbright == that.ambientToFullbright &&
modelLocation.equals(that.modelLocation);
modelLocation.equals(that.modelLocation) &&
Objects.equals(materialLibraryOverrideLocation, that.materialLibraryOverrideLocation);
}
@Override
public int hashCode()
{
return Objects.hash(modelLocation, detectCullableFaces, diffuseLighting, flipV, ambientToFullbright);
return Objects.hash(modelLocation, detectCullableFaces, diffuseLighting, flipV, ambientToFullbright, materialLibraryOverrideLocation);
}
}
}

View File

@ -73,13 +73,18 @@ public class OBJModel implements IMultipartModelGeometry<OBJModel>
public final ResourceLocation modelLocation;
OBJModel(ResourceLocation modelLocation, OBJLoader.LineReader reader, boolean detectCullableFaces, boolean diffuseLighting, boolean flipV, boolean ambientToFullbright) throws IOException
@Nullable
public final String materialLibraryOverrideLocation;
OBJModel(OBJLoader.LineReader reader, OBJLoader.ModelSettings settings) throws IOException
{
this.modelLocation = modelLocation;
this.detectCullableFaces = detectCullableFaces;
this.diffuseLighting = diffuseLighting;
this.flipV = flipV;
this.ambientToFullbright = ambientToFullbright;
this.modelLocation = settings.modelLocation;
this.detectCullableFaces = settings.detectCullableFaces;
this.diffuseLighting = settings.diffuseLighting;
this.flipV = settings.flipV;
this.ambientToFullbright = settings.ambientToFullbright;
this.materialLibraryOverrideLocation = settings.materialLibraryOverrideLocation;
// for relative references to material libraries
String modelDomain = modelLocation.getNamespace();
@ -90,7 +95,7 @@ public class OBJModel implements IMultipartModelGeometry<OBJModel>
else
modelPath = "";
MaterialLibrary mtllib = null;
MaterialLibrary mtllib = MaterialLibrary.EMPTY;
MaterialLibrary.Material currentMat = null;
String currentSmoothingGroup = null;
ModelGroup currentGroup = null;
@ -99,6 +104,15 @@ public class OBJModel implements IMultipartModelGeometry<OBJModel>
boolean objAboveGroup = false;
if (materialLibraryOverrideLocation != null)
{
String lib = materialLibraryOverrideLocation;
if (lib.contains(":"))
mtllib = OBJLoader.INSTANCE.loadMaterialLibrary(new ResourceLocation(lib));
else
mtllib = OBJLoader.INSTANCE.loadMaterialLibrary(new ResourceLocation(modelDomain, modelPath + lib));
}
String[] line;
while((line = reader.readAndSplitLine(true)) != null)
{
@ -106,6 +120,9 @@ public class OBJModel implements IMultipartModelGeometry<OBJModel>
{
case "mtllib": // Loads material library
{
if (materialLibraryOverrideLocation != null)
break;
String lib = line[1];
if (lib.contains(":"))
mtllib = OBJLoader.INSTANCE.loadMaterialLibrary(new ResourceLocation(lib));

View File

@ -1,3 +1,22 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.debug.client.model;
import com.google.common.collect.ImmutableList;