Skip to content

Commit

Permalink
Actually fixed model stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed Jul 25, 2022
1 parent c2133b8 commit ce9f4a8
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 46 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ satin_version=1.8.0
sodium_version=mc1.19-0.4.2
iris_version=1.19.x-v1.2.5

mod_version = 6.2.1
mod_version = 6.3.0
maven_group = net.ludocrypt
archives_base_name = limlib
11 changes: 9 additions & 2 deletions src/main/java/net/ludocrypt/limlib/access/BakedModelAccess.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package net.ludocrypt.limlib.access;

import java.util.Map;
import java.util.List;

import org.jetbrains.annotations.Nullable;

import com.mojang.datafixers.util.Pair;

import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.Identifier;

public interface BakedModelAccess {

public Map<Identifier, BakedModel> getSubModels();
public List<Pair<Identifier, BakedModel>> getModels(@Nullable BlockState state);

public void addModel(Identifier rendererId, @Nullable BlockState state, BakedModel model);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import java.util.Map;

import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.Identifier;

public interface UnbakedModelAccess {

public Map<Identifier, ModelIdentifier> getSubModels();
public Map<Identifier, Identifier> getSubModels();

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void renderModel(BakedModel model, World world, BlockPos pos, BlockState
Random random = Random.create(state.getRenderingSeed(pos));

for (Direction dir : Direction.values()) {
model.getQuads(state, dir, random).stream().filter((quad) -> Block.shouldDrawSide(state, world, pos, dir, pos)).forEach((quad) -> this.renderQuad(quad, bufferBuilder, matrix, camera, world, matrices, model, state, dir, random));
model.getQuads(state, dir, random).stream().filter((quad) -> Block.shouldDrawSide(state, world, pos, dir, pos.offset(dir))).forEach((quad) -> this.renderQuad(quad, bufferBuilder, matrix, camera, world, matrices, model, state, dir, random));
}
model.getQuads(state, null, random).forEach((quad) -> this.renderQuad(quad, bufferBuilder, matrix, camera, world, matrices, model, state, null, random));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void renderBlocks(Framebuffer framebuffer, float tickDelta, MatrixStack m
matrices.push();
matrices.translate(pos.getX() - camera.getPos().getX(), pos.getY() - camera.getPos().getY(), pos.getZ() - camera.getPos().getZ());

((BakedModelAccess) MinecraftClient.getInstance().getBlockRenderManager().getModel(state)).getSubModels().forEach((id, quads) -> LimlibRendering.LIMINAL_QUAD_RENDERER.get(id).renderModel(quads, world, pos, state, matrices, camera));
((BakedModelAccess) MinecraftClient.getInstance().getBlockRenderManager().getModel(state)).getModels(state).forEach((renderPair) -> LimlibRendering.LIMINAL_QUAD_RENDERER.get(renderPair.getFirst()).renderModel(renderPair.getSecond(), world, pos, state, matrices, camera));

matrices.pop();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class ChunkBuilderBuiltChunkRebuildTaskMixin {
@Inject(method = "Lnet/minecraft/client/render/chunk/ChunkBuilder$BuiltChunk$RebuildTask;render(FFFLnet/minecraft/client/render/chunk/BlockBufferBuilderStorage;)Lnet/minecraft/client/render/chunk/ChunkBuilder$BuiltChunk$RebuildTask$RenderData;", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/chunk/ChunkRendererRegion;getBlockState(Lnet/minecraft/util/math/BlockPos;)Lnet/minecraft/block/BlockState;", ordinal = 0, shift = Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILHARD)
private void limlib$render(float cameraX, float cameraY, float cameraZ, BlockBufferBuilderStorage blockBufferBuilderStorage, CallbackInfoReturnable<RenderData> ci, RenderData renderData, int i, BlockPos blockPos, BlockPos blockPos2, ChunkOcclusionDataBuilder chunkOcclusionDataBuilder, ChunkRendererRegion chunkRendererRegion, MatrixStack matrixStack, Set<RenderLayer> set, Random random, BlockRenderManager blockRenderManager, Iterator<BlockPos> var15, BlockPos blockPos3) {
BlockState blockState = chunkRendererRegion.getBlockState(blockPos3);
if (!((BakedModelAccess) blockRenderManager.getModel(blockState)).getSubModels().isEmpty()) {
if (!((BakedModelAccess) blockRenderManager.getModel(blockState)).getModels(blockState).isEmpty()) {
((RenderDataAccess) (Object) renderData).getCustomQuadData().put(blockPos3.toImmutable(), blockState);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ChunkRenderRebuildTaskMixin {
@Inject(method = "Lme/jellysquid/mods/sodium/client/render/chunk/tasks/ChunkRenderRebuildTask;performBuild(Lme/jellysquid/mods/sodium/client/gl/compile/ChunkBuildContext;Lme/jellysquid/mods/sodium/client/util/task/CancellationSource;)Lme/jellysquid/mods/sodium/client/render/chunk/compile/ChunkBuildResult;", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/BlockPos$Mutable;set(III)Lnet/minecraft/util/math/BlockPos$Mutable;", ordinal = 1, shift = Shift.AFTER, remap = false), locals = LocalCapture.CAPTURE_FAILHARD, remap = false, require = 0)
private void limlib$performBuild$mapped(ChunkBuildContext buildContext, CancellationSource cancellationSource, CallbackInfoReturnable<ChunkBuildResult> ci, ChunkRenderData.Builder renderData, ChunkOcclusionDataBuilder occluder, ChunkRenderBounds.Builder bounds, ChunkBuildBuffers buffers, ChunkRenderCacheLocal cache, WorldSlice slice, int minX, int minY, int minZ, int maxX, int maxY, int maxZ, Mutable blockPos, Mutable offset) {
BlockState blockState = slice.getBlockState(blockPos);
if (!((BakedModelAccess) cache.getBlockModels().getModel(blockState)).getSubModels().isEmpty()) {
if (!((BakedModelAccess) cache.getBlockModels().getModel(blockState)).getModels(blockState).isEmpty()) {
((RenderDataAccess) (Object) renderData).getCustomQuadData().put(blockPos.toImmutable(), blockState);
}
}
Expand All @@ -41,7 +41,7 @@ public class ChunkRenderRebuildTaskMixin {
@Inject(method = "Lme/jellysquid/mods/sodium/client/render/chunk/tasks/ChunkRenderRebuildTask;performBuild(Lme/jellysquid/mods/sodium/client/gl/compile/ChunkBuildContext;Lme/jellysquid/mods/sodium/client/util/task/CancellationSource;)Lme/jellysquid/mods/sodium/client/render/chunk/compile/ChunkBuildResult;", at = @At(value = "INVOKE", target = "Lnet/minecraft/class_2338$class_2339;method_10103(III)Lnet/minecraft/class_2338$class_2339;", ordinal = 1, shift = Shift.AFTER, remap = false), locals = LocalCapture.CAPTURE_FAILHARD, remap = false, require = 0)
private void limlib$performBuild$unmapped(ChunkBuildContext buildContext, CancellationSource cancellationSource, CallbackInfoReturnable<ChunkBuildResult> ci, ChunkRenderData.Builder renderData, ChunkOcclusionDataBuilder occluder, ChunkRenderBounds.Builder bounds, ChunkBuildBuffers buffers, ChunkRenderCacheLocal cache, WorldSlice slice, int minX, int minY, int minZ, int maxX, int maxY, int maxZ, Mutable blockPos, Mutable offset) {
BlockState blockState = slice.getBlockState(blockPos);
if (!((BakedModelAccess) cache.getBlockModels().getModel(blockState)).getSubModels().isEmpty()) {
if (!((BakedModelAccess) cache.getBlockModels().getModel(blockState)).getModels(blockState).isEmpty()) {
((RenderDataAccess) (Object) renderData).getCustomQuadData().put(blockPos.toImmutable(), blockState);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public class ItemRendererMixin implements ItemRendererAccess {
MatrixStack matrixStack = new MatrixStack();
matrixStack.multiplyPositionMatrix(matrices.peek().getPositionMatrix().copy());
List<Runnable> immediateRenderer = Lists.newArrayList();
((BakedModelAccess) model).getSubModels().forEach((id, subModel) -> (isHandRendering ? LimlibRendering.LIMINAL_QUAD_RENDERER.get(id).heldItemRenderQueue : isItemRendering ? LimlibRendering.LIMINAL_QUAD_RENDERER.get(id).itemRenderQueue : immediateRenderer).add(() -> LimlibRendering.LIMINAL_QUAD_RENDERER.get(id).renderItemModel(subModel, client.world, stack.copy(), matrixStack, client.gameRenderer.getCamera(), inGui)));

((BakedModelAccess) model).getModels(null).forEach((renderPair) -> (isHandRendering ? LimlibRendering.LIMINAL_QUAD_RENDERER.get(renderPair.getFirst()).heldItemRenderQueue : isItemRendering ? LimlibRendering.LIMINAL_QUAD_RENDERER.get(renderPair.getFirst()).itemRenderQueue : immediateRenderer).add(() -> LimlibRendering.LIMINAL_QUAD_RENDERER.get(renderPair.getFirst()).renderItemModel(renderPair.getSecond(), client.world, stack.copy(), matrixStack, client.gameRenderer.getCamera(), inGui)));

immediateRenderer.forEach(Runnable::run);
immediateRenderer.clear();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package net.ludocrypt.limlib.mixin.client.render.model;

import java.util.Map;
import java.util.List;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;

import com.google.common.collect.Maps;
import com.google.common.collect.Lists;
import com.mojang.datafixers.util.Pair;

import net.ludocrypt.limlib.access.BakedModelAccess;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.Identifier;

@Mixin(BakedModel.class)
public interface BakedModelMixin extends BakedModelAccess {

@Override
default Map<Identifier, BakedModel> getSubModels() {
return Maps.newHashMap();
default List<Pair<Identifier, BakedModel>> getModels(@Nullable BlockState state) {
return Lists.newArrayList();
}

@Override
default void addModel(Identifier rendererId, @Nullable BlockState state, BakedModel model) {
}

}
Original file line number Diff line number Diff line change
@@ -1,25 +1,44 @@
package net.ludocrypt.limlib.mixin.client.render.model;

import java.util.List;
import java.util.Map;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mojang.datafixers.util.Pair;

import net.ludocrypt.limlib.access.BakedModelAccess;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.util.Identifier;

@Mixin(targets = { "net/minecraft/client/render/model/BasicBakedModel", "net/minecraft/client/render/model/BuiltinBakedModel", "net/minecraft/client/render/model/MultipartBakedModel", "net/minecraft/client/render/model/WeightedBakedModel", "net/fabricmc/fabric/api/renderer/v1/model/ForwardingBakedModel" })
@Mixin(targets = { "net/minecraft/client/render/model/BasicBakedModel", "net/minecraft/client/render/model/BuiltinBakedModel", "net/minecraft/client/render/model/WeightedBakedModel", "net/fabricmc/fabric/api/renderer/v1/model/ForwardingBakedModel" })
public class GeneralBakedModelMixin implements BakedModelAccess {

@Unique
private Map<Identifier, BakedModel> subQuads = Maps.newHashMap();
private final Map<BlockState, List<Pair<Identifier, BakedModel>>> modelsMap = Maps.newHashMap();
private List<Pair<Identifier, BakedModel>> defaultModels = Lists.newArrayList();

@Override
public Map<Identifier, BakedModel> getSubModels() {
return subQuads;
public List<Pair<Identifier, BakedModel>> getModels(@Nullable BlockState state) {
return modelsMap.getOrDefault(state, defaultModels);
}

@Override
public void addModel(Identifier rendererId, @Nullable BlockState state, BakedModel model) {
if (state == null) {
defaultModels.add(Pair.of(rendererId, model));
} else {
List<Pair<Identifier, BakedModel>> list = modelsMap.get(state);
if (list == null) {
list = Lists.newArrayList();
modelsMap.put(state, list);
}

list.add(Pair.of(rendererId, model));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@
import net.ludocrypt.limlib.access.UnbakedModelAccess;
import net.ludocrypt.limlib.impl.LimlibRendering;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.Identifier;

@Mixin(JsonUnbakedModel.Deserializer.class)
public abstract class JsonUnbakedModelDeserializerMixin {

@Inject(method = "Lnet/minecraft/client/render/model/json/JsonUnbakedModel$Deserializer;deserialize(Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcom/google/gson/JsonDeserializationContext;)Lnet/minecraft/client/render/model/json/JsonUnbakedModel;", at = @At("RETURN"), cancellable = true)
private void limlib$deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext, CallbackInfoReturnable<JsonUnbakedModel> ci) {
Map<Identifier, ModelIdentifier> map = Maps.newHashMap();
Map<Identifier, Identifier> map = Maps.newHashMap();
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("limlib_extra")) {
LimlibRendering.LIMINAL_QUAD_RENDERER.getEntrySet().forEach((entry) -> {
JsonObject limlibExtra = jsonObject.get("limlib_extra").getAsJsonObject();
if (limlibExtra.has(entry.getKey().getValue().toString())) {
map.put(entry.getKey().getValue(), new ModelIdentifier(limlibExtra.get(entry.getKey().getValue().toString()).getAsString()));
map.put(entry.getKey().getValue(), new Identifier(limlibExtra.get(entry.getKey().getValue().toString()).getAsString()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.ludocrypt.limlib.mixin.client.render.model;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand All @@ -19,7 +18,6 @@
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.mojang.datafixers.util.Pair;

import net.ludocrypt.limlib.access.BakedModelAccess;
Expand All @@ -30,7 +28,6 @@
import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.client.util.SpriteIdentifier;
import net.minecraft.util.Identifier;

Expand All @@ -42,21 +39,7 @@ public abstract class JsonUnbakedModelMixin implements UnbakedModelAccess {
private static Logger LOGGER;

@Unique
private Map<Identifier, ModelIdentifier> subModels = Maps.newHashMap();

@Shadow
public String id;

@Shadow
protected JsonUnbakedModel parent;

@Inject(method = "Lnet/minecraft/client/render/model/json/JsonUnbakedModel;getModelDependencies()Ljava/util/Collection;", at = @At("RETURN"), cancellable = true)
private void limlib$getModelDependencies(CallbackInfoReturnable<Collection<Identifier>> ci) {
HashSet<Identifier> set = Sets.newHashSet();
set.addAll(ci.getReturnValue());
set.addAll(subModels.values());
ci.setReturnValue(set);
}
private Map<Identifier, Identifier> subModels = Maps.newHashMap();

@Inject(method = "Lnet/minecraft/client/render/model/json/JsonUnbakedModel;getTextureDependencies(Ljava/util/function/Function;Ljava/util/Set;)Ljava/util/Collection;", at = @At(value = "INVOKE", target = "Ljava/util/List;forEach(Ljava/util/function/Consumer;)V", ordinal = 0, shift = Shift.BEFORE), locals = LocalCapture.CAPTURE_FAILHARD)
private void limlib$getTextureDependencies(Function<Identifier, UnbakedModel> unbakedModelGetter, Set<Pair<String, String>> unresolvedTextureReferences, CallbackInfoReturnable<Collection<SpriteIdentifier>> ci, Set<JsonUnbakedModel> set, JsonUnbakedModel jsonUnbakedModel, Set<SpriteIdentifier> set2) {
Expand All @@ -70,13 +53,17 @@ public abstract class JsonUnbakedModelMixin implements UnbakedModelAccess {

@Inject(method = "Lnet/minecraft/client/render/model/json/JsonUnbakedModel;bake(Lnet/minecraft/client/render/model/ModelLoader;Lnet/minecraft/client/render/model/json/JsonUnbakedModel;Ljava/util/function/Function;Lnet/minecraft/client/render/model/ModelBakeSettings;Lnet/minecraft/util/Identifier;Z)Lnet/minecraft/client/render/model/BakedModel;", at = @At("RETURN"), cancellable = true)
private void limlib$bake(ModelLoader loader, JsonUnbakedModel parent, Function<SpriteIdentifier, Sprite> textureGetter, ModelBakeSettings settings, Identifier id, boolean hasDepth, CallbackInfoReturnable<BakedModel> ci) {
Map<Identifier, BakedModel> subModels = Maps.newHashMap();
this.getSubModels().forEach((rendererId, modelId) -> subModels.put(rendererId, loader.getOrLoadModel(modelId).bake(loader, textureGetter, settings, modelId)));
((BakedModelAccess) ci.getReturnValue()).getSubModels().putAll(subModels);
this.getSubModels().forEach((rendererId, modelId) -> {
if (!modelId.equals(id)) {
((BakedModelAccess) ci.getReturnValue()).addModel(rendererId, null, loader.bake(modelId, settings));
} else {
LOGGER.warn("Model '{}' caught in chain! Renderer '{}' caught model '{}'", id, rendererId, modelId);
}
});
}

@Override
public Map<Identifier, ModelIdentifier> getSubModels() {
public Map<Identifier, Identifier> getSubModels() {
return subModels;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package net.ludocrypt.limlib.mixin.client.render.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import com.google.common.collect.Lists;
import com.mojang.datafixers.util.Pair;

import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap;
import net.ludocrypt.limlib.access.BakedModelAccess;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.MultipartBakedModel;
import net.minecraft.util.Identifier;

@Mixin(MultipartBakedModel.class)
public class MultipartBakedModelMixin implements BakedModelAccess {

@Shadow
@Final
private List<org.apache.commons.lang3.tuple.Pair<Predicate<BlockState>, BakedModel>> components;

@Unique
private final Map<BlockState, List<Pair<Identifier, BakedModel>>> subModelCache = new Reference2ReferenceOpenHashMap<>();

@Override
public List<Pair<Identifier, BakedModel>> getModels(@Nullable BlockState state) {
if (state == null) {
return Lists.newArrayList();
}

List<Pair<Identifier, BakedModel>> models;
synchronized (this.subModelCache) {
models = this.subModelCache.get(state);

if (models == null) {
models = new ArrayList<>(this.components.size());

for (org.apache.commons.lang3.tuple.Pair<Predicate<BlockState>, BakedModel> pair : this.components) {
if ((pair.getLeft()).test(state)) {
models.addAll(((BakedModelAccess) pair.getRight()).getModels(state));
}
}

this.subModelCache.put(state, models);
}
}

return models;
}

@Override
public void addModel(Identifier rendererId, @Nullable BlockState state, BakedModel model) {

}

}
1 change: 1 addition & 0 deletions src/main/resources/limlib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"client.render.model.GeneralBakedModelMixin",
"client.render.model.JsonUnbakedModelDeserializerMixin",
"client.render.model.JsonUnbakedModelMixin",
"client.render.model.MultipartBakedModelMixin",
"client.sound.SoundSystemMixin",
"client.sound.SourceAccessor"
],
Expand Down

0 comments on commit ce9f4a8

Please sign in to comment.