Fix parent and texture-variable baking

This commit is contained in:
Lukas Rieger (Blue) 2022-05-29 02:34:26 +02:00
parent 7389cb1a16
commit f98376fe27
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
10 changed files with 131 additions and 23 deletions

@ -1 +1 @@
Subproject commit 0776e69c88ed24e2d2f0141e616d0512a5482ac9
Subproject commit be1c4b4939d88d8a8da166219c1968ec290e40ac

View File

@ -327,9 +327,6 @@ public synchronized ResourcePack getResourcePack() throws ConfigurationException
resourcePack.loadResources(defaultResourceFile);
resourcePack.bake();
StateDumper.global().dump(Path.of("dump.json"));
} catch (IOException | RuntimeException e) {
throw new ConfigurationException("Failed to parse resources!\n" +
"Is one of your resource-packs corrupted?", e);

View File

@ -117,8 +117,8 @@ public HttpResponse handle(HttpRequest request) {
// provide meta-data
Matcher metaMatcher = META_PATTERN.matcher(path);
if (metaMatcher.matches()) {
String mapId = tileMatcher.group(1);
String metaFilePath = tileMatcher.group(2);
String mapId = metaMatcher.group(1);
String metaFilePath = metaMatcher.group(2);
Storage storage = mapStorageProvider.apply(mapId);
if (storage != null) {
@ -191,7 +191,6 @@ private static long stringToTimestamp(String timeString) throws IllegalArgumentE
int month = Calendar.JANUARY;
switch (timeString.substring(8, 11)){
case "Jan" : month = Calendar.JANUARY; break;
case "Feb" : month = Calendar.FEBRUARY; break;
case "Mar" : month = Calendar.MARCH; break;
case "Apr" : month = Calendar.APRIL; break;

View File

@ -6,6 +6,7 @@
import de.bluecolored.bluemap.core.resources.adapter.ResourcesGson;
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.resources.resourcepack.texture.Texture;
import org.jetbrains.annotations.Nullable;
import java.io.*;
import java.util.HashMap;
@ -22,7 +23,8 @@ public TextureGallery() {
this.nextId = 0;
}
public int get(ResourcePath<Texture> textureResourcePath) {
public int get(@Nullable ResourcePath<Texture> textureResourcePath) {
if (textureResourcePath == null) textureResourcePath = ResourcePack.MISSING_TEXTURE;
Integer ordinal = ordinalMap.get(textureResourcePath);
return ordinal != null ? ordinal : 0;
}

View File

@ -41,7 +41,9 @@ public class ResourcePack {
private final Map<String, ResourcePath<BlockState>> blockStatePaths;
private final Map<ResourcePath<BlockState>, BlockState> blockStates;
private final Map<String, ResourcePath<BlockModel>> blockModelPaths;
private final Map<ResourcePath<BlockModel>, BlockModel> blockModels;
private final Map<String, ResourcePath<Texture>> texturePaths;
private final Map<ResourcePath<Texture>, Texture> textures;
private final Map<ResourcePath<BufferedImage>, BufferedImage> colormaps;
@ -54,7 +56,9 @@ public class ResourcePack {
public ResourcePack() {
this.blockStatePaths = new HashMap<>();
this.blockStates = new HashMap<>();
this.blockModelPaths = new HashMap<>();
this.blockModels = new HashMap<>();
this.texturePaths = new HashMap<>();
this.textures = new HashMap<>();
this.colormaps = new HashMap<>();
@ -68,6 +72,11 @@ public ResourcePack() {
.build(this::loadBlockProperties);
}
@Nullable
public ResourcePath<BlockState> getBlockStatePath(String formatted) {
return blockStatePaths.get(formatted);
}
@Nullable
public BlockState getBlockState(de.bluecolored.bluemap.core.world.BlockState blockState) {
ResourcePath<BlockState> path = blockStatePaths.get(blockState.getFormatted());
@ -84,6 +93,11 @@ public Map<ResourcePath<BlockState>, BlockState> getBlockStates() {
return blockStates;
}
@Nullable
public ResourcePath<BlockModel> getBlockModelPath(String formatted) {
return blockModelPaths.get(formatted);
}
@Nullable
public BlockModel getBlockModel(ResourcePath<BlockModel> path) {
BlockModel blockModel = blockModels.get(path);
@ -94,6 +108,11 @@ public Map<ResourcePath<BlockModel>, BlockModel> getBlockModels() {
return blockModels;
}
@Nullable
public ResourcePath<Texture> getTexturePath(String formatted) {
return texturePaths.get(formatted);
}
@Nullable
public Texture getTexture(ResourcePath<Texture> path) {
Texture texture = textures.get(path);
@ -279,12 +298,23 @@ public synchronized void loadResources(Path root) throws IOException {
public synchronized void bake() throws IOException {
Logger.global.logInfo("Baking resources...");
for (ResourcePath<BlockState> path : blockStates.keySet()) {
blockStatePaths.put(path.getFormatted(), path);
// fill path maps
blockStates.keySet().forEach(path -> blockStatePaths.put(path.getFormatted(), path));
blockModels.keySet().forEach(path -> blockModelPaths.put(path.getFormatted(), path));
textures.keySet().forEach(path -> texturePaths.put(path.getFormatted(), path));
// optimize references
for (BlockModel model : blockModels.values()) {
model.optimize(this);
}
// apply model parents
for (BlockModel model : blockModels.values()) {
model.applyParent(this);
}
// calculate model properties
for (BlockModel model : blockModels.values()) {
model.calculateProperties(this);
}

View File

@ -7,9 +7,7 @@
import de.bluecolored.bluemap.core.util.Direction;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
@SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"})
@DebugDump
@ -56,6 +54,18 @@ public boolean isOccluding() {
return occluding;
}
public synchronized void optimize(ResourcePack resourcePack) {
for (var variable : this.textures.values()) {
variable.optimize(resourcePack);
}
if (this.elements != null) {
for (var element : elements) {
element.optimize(resourcePack);
}
}
}
public synchronized void applyParent(ResourcePack resourcePack) {
if (this.parent == null) return;
@ -72,8 +82,17 @@ public synchronized void applyParent(ResourcePack resourcePack) {
if (parent != null) {
parent.applyParent(resourcePack);
parent.textures.forEach(this.textures::putIfAbsent);
if (this.elements == null) this.elements = parent.elements;
parent.textures.forEach(this::applyTextureVariable);
if (this.elements == null && parent.elements != null) {
this.elements = new ArrayList<>();
parent.elements.forEach(element -> this.elements.add(element.copy()));
}
}
}
private synchronized void applyTextureVariable(String key, TextureVariable value) {
if (!this.textures.containsKey(key)) {
this.textures.put(key, value.copy());
}
}

View File

@ -8,6 +8,7 @@
import com.google.gson.stream.JsonReader;
import de.bluecolored.bluemap.core.debug.DebugDump;
import de.bluecolored.bluemap.core.resources.AbstractTypeAdapterFactory;
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.util.Direction;
import java.io.IOException;
@ -25,7 +26,17 @@ public class Element {
private boolean shade = true;
private EnumMap<Direction, Face> faces = new EnumMap<>(Direction.class);
private Element(){}
@SuppressWarnings("unused")
private Element() {}
private Element(Element copyFrom) {
this.from = copyFrom.from;
this.to = copyFrom.to;
this.rotation = copyFrom.rotation;
this.shade = copyFrom.shade;
copyFrom.faces.forEach((direction, face) -> this.faces.put(direction, face.copy()));
}
private void init() {
faces.forEach((direction, face) -> face.init(direction, this::calculateDefaultUV));
@ -84,6 +95,10 @@ public EnumMap<Direction, Face> getFaces() {
return faces;
}
public Element copy() {
return new Element(this);
}
boolean isFullCube() {
if (!(FULL_BLOCK_MIN.equals(from) && FULL_BLOCK_MAX.equals(to))) return false;
for (Direction dir : Direction.values()) {
@ -92,6 +107,12 @@ boolean isFullCube() {
return true;
}
public void optimize(ResourcePack resourcePack) {
for (var face : faces.values()) {
face.optimize(resourcePack);
}
}
static class Adapter extends AbstractTypeAdapterFactory<Element> {
public Adapter() {

View File

@ -2,8 +2,7 @@
import com.flowpowered.math.vector.Vector4f;
import de.bluecolored.bluemap.core.debug.DebugDump;
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.resources.resourcepack.texture.Texture;
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.util.Direction;
import java.util.function.Function;
@ -12,7 +11,7 @@
@DebugDump
public class Face {
private static final TextureVariable DEFAULT_TEXTURE = new TextureVariable((ResourcePath<Texture>) null);
private static final TextureVariable DEFAULT_TEXTURE = new TextureVariable(ResourcePack.MISSING_TEXTURE);
private Vector4f uv;
private TextureVariable texture = DEFAULT_TEXTURE;
@ -20,7 +19,16 @@ public class Face {
private int rotation = 0;
private int tintindex = -1;
private Face(){}
@SuppressWarnings("unused")
private Face() {}
private Face(Face copyFrom) {
this.uv = copyFrom.uv;
this.texture = copyFrom.texture.copy();
this.cullface = copyFrom.cullface;
this.rotation = copyFrom.rotation;
this.tintindex = copyFrom.tintindex;
}
void init(Direction direction, Function<Direction, Vector4f> defaultUvCalculator) {
if (cullface == null) cullface = direction;
@ -47,4 +55,11 @@ public int getTintindex() {
return tintindex;
}
public Face copy() {
return new Face(this);
}
public void optimize(ResourcePack resourcePack) {
this.texture.optimize(resourcePack);
}
}

View File

@ -6,10 +6,12 @@
import com.google.gson.stream.JsonWriter;
import de.bluecolored.bluemap.core.debug.DebugDump;
import de.bluecolored.bluemap.core.resources.ResourcePath;
import de.bluecolored.bluemap.core.resources.resourcepack.ResourcePack;
import de.bluecolored.bluemap.core.resources.resourcepack.texture.Texture;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.Objects;
import java.util.function.Function;
@DebugDump
@ -21,8 +23,15 @@ public class TextureVariable {
private transient volatile boolean isReference, isResolving;
private TextureVariable(TextureVariable copyFrom) {
this.referenceName = copyFrom.referenceName;
this.texturePath = copyFrom.texturePath;
this.isReference = copyFrom.isReference;
this.isResolving = copyFrom.isResolving;
}
public TextureVariable(String referenceName) {
this.referenceName = referenceName;
this.referenceName = Objects.requireNonNull(referenceName);
this.texturePath = null;
this.isReference = true;
this.isResolving = false;
@ -30,7 +39,7 @@ public TextureVariable(String referenceName) {
public TextureVariable(ResourcePath<Texture> texturePath) {
this.referenceName = null;
this.texturePath = texturePath;
this.texturePath = Objects.requireNonNull(texturePath);
this.isReference = false;
this.isResolving = false;
}
@ -68,6 +77,7 @@ private ResourcePath<Texture> resolveTexturePath(Function<String, TextureVariabl
}
this.isReference = false;
this.isResolving = false;
}
return this.texturePath;
}
@ -81,6 +91,20 @@ public boolean isReference() {
return isReference;
}
public TextureVariable copy() {
synchronized (TextureVariable.class) {
return new TextureVariable(this);
}
}
public void optimize(ResourcePack resourcePack) {
synchronized (TextureVariable.class) {
if (texturePath != null) {
texturePath = resourcePack.getTexturePath(texturePath.getFormatted());
}
}
}
static class Adapter extends TypeAdapter<TextureVariable> {
@Override

View File

@ -44,12 +44,13 @@ public String getFormatted() {
return formatted;
}
@SuppressWarnings("StringEquality")
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ResourcePath<?> that = (ResourcePath<?>) o;
return getFormatted().equals(that.getFormatted());
return getFormatted() == that.getFormatted();
}
@Override