Skip to content

Commit

Permalink
updated quad api stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed Jul 23, 2022
1 parent 155f820 commit c162506
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 169 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.0.5
mod_version = 6.1.0
maven_group = net.ludocrypt
archives_base_name = limlib
17 changes: 0 additions & 17 deletions src/main/java/net/ludocrypt/limlib/access/BakedModelAccess.java

This file was deleted.

15 changes: 15 additions & 0 deletions src/main/java/net/ludocrypt/limlib/access/ModelAccess.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.ludocrypt.limlib.access;

import java.util.Optional;

import org.jetbrains.annotations.Nullable;

import net.minecraft.util.Identifier;

public interface ModelAccess {

public Optional<Identifier> getLiminalQuadRenderer();

public void setLiminalQuadRenderer(@Nullable Identifier id);

}
18 changes: 0 additions & 18 deletions src/main/java/net/ludocrypt/limlib/access/UnbakedModelAccess.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import org.jetbrains.annotations.Nullable;
import org.lwjgl.system.MemoryStack;

import com.google.common.collect.Lists;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.datafixers.util.Pair;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -24,6 +23,7 @@
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormat.DrawMode;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
Expand All @@ -32,6 +32,7 @@
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.math.Vec3f;
import net.minecraft.util.math.Vector4f;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;

@Environment(EnvType.CLIENT)
Expand All @@ -41,31 +42,31 @@ public abstract class LiminalQuadRenderer {

public List<Runnable> itemRenderQueue = Lists.newArrayList();

public abstract void renderQuad(BakedQuad quad, BufferBuilder bufferBuilder, Matrix4f matrix, Camera camera, World world, MatrixStack matrices, List<Pair<BakedQuad, Optional<Direction>>> quads);
public abstract void renderQuad(BakedQuad quad, BufferBuilder bufferBuilder, Matrix4f matrix, Camera camera, World world, MatrixStack matrices, BakedModel model, @Nullable BlockState state, @Nullable Direction dir, Random random);

public void renderQuads(List<Pair<BakedQuad, Optional<Direction>>> quads, World world, BlockPos pos, BlockState state, MatrixStack matrices, Camera camera) {
public void renderModel(BakedModel model, World world, BlockPos pos, BlockState state, MatrixStack matrices, Camera camera) {
Matrix4f matrix = setupMatrix(matrices, camera, true);
BufferBuilder bufferBuilder = setupRenderer(matrices, camera);

for (Pair<BakedQuad, Optional<Direction>> quadPair : quads) {
BakedQuad quad = quadPair.getFirst();
if (quadPair.getSecond().isPresent()) {
if (Block.shouldDrawSide(state, world, pos, quadPair.getSecond().get(), pos.offset(quadPair.getSecond().get()))) {
this.renderQuad(quad, bufferBuilder, matrix, camera, world, matrices, quads);
}
} else {
this.renderQuad(quad, bufferBuilder, matrix, camera, world, matrices, quads);
}
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, null, random).forEach((quad) -> this.renderQuad(quad, bufferBuilder, matrix, camera, world, matrices, model, state, null, random));

this.endRenderer(bufferBuilder, matrices, camera);
}

public void renderItemQuads(List<Pair<BakedQuad, Optional<Direction>>> quads, World world, ItemStack stack, MatrixStack matrices, Camera camera, boolean inGui) {
public void renderItemModel(BakedModel model, World world, ItemStack stack, MatrixStack matrices, Camera camera, boolean inGui) {
Matrix4f matrix = setupMatrix(matrices, camera, !inGui);
BufferBuilder bufferBuilder = setupRenderer(matrices, camera);

quads.forEach((pair) -> this.renderQuad(pair.getFirst(), bufferBuilder, matrix, camera, world, matrices, quads));
Random random = Random.create(42L);
for (Direction dir : Direction.values()) {
model.getQuads(null, dir, random).forEach((quad) -> this.renderQuad(quad, bufferBuilder, matrix, camera, world, matrices, model, null, dir, random));
}
model.getQuads(null, null, random).forEach((quad) -> this.renderQuad(quad, bufferBuilder, matrix, camera, world, matrices, model, null, null, random));

this.endRenderer(bufferBuilder, matrices, camera);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.fabricmc.loader.api.FabricLoader;
import net.ludocrypt.limlib.access.BakedModelAccess;
import net.ludocrypt.limlib.access.DimensionTypeAccess;
import net.ludocrypt.limlib.access.ModelAccess;
import net.ludocrypt.limlib.access.RenderDataAccess;
import net.ludocrypt.limlib.access.WorldRendererAccess;
import net.ludocrypt.limlib.api.LiminalEffects;
Expand All @@ -35,6 +35,7 @@
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.render.WorldRenderer.ChunkInfo;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -177,7 +178,8 @@ 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)).getSubQuads().forEach((id, quads) -> LimlibRendering.LIMINAL_QUAD_RENDERER.get(id).renderQuads(quads, world, pos, state, matrices, camera));
BakedModel model = MinecraftClient.getInstance().getBlockRenderManager().getModel(state);
LimlibRendering.LIMINAL_QUAD_RENDERER.get(((ModelAccess) model).getLiminalQuadRenderer().get()).renderModel(model, world, pos, state, matrices, camera);

matrices.pop();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import net.ludocrypt.limlib.access.BakedModelAccess;
import net.ludocrypt.limlib.access.ModelAccess;
import net.ludocrypt.limlib.access.RenderDataAccess;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.RenderLayer;
Expand All @@ -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)).getSubQuads().isEmpty()) {
if (((ModelAccess) blockRenderManager.getModel(blockState)).getLiminalQuadRenderer().isPresent()) {
((RenderDataAccess) (Object) renderData).getCustomQuadData().put(blockPos3.toImmutable(), blockState);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import me.jellysquid.mods.sodium.client.render.pipeline.context.ChunkRenderCacheLocal;
import me.jellysquid.mods.sodium.client.util.task.CancellationSource;
import me.jellysquid.mods.sodium.client.world.WorldSlice;
import net.ludocrypt.limlib.access.BakedModelAccess;
import net.ludocrypt.limlib.access.ModelAccess;
import net.ludocrypt.limlib.access.RenderDataAccess;
import net.minecraft.block.BlockState;
import net.minecraft.client.render.chunk.ChunkOcclusionDataBuilder;
Expand All @@ -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)).getSubQuads().isEmpty()) {
if (((ModelAccess) cache.getBlockModels().getModel(blockState)).getLiminalQuadRenderer().isPresent()) {
((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)).getSubQuads().isEmpty()) {
if (((ModelAccess) cache.getBlockModels().getModel(blockState)).getLiminalQuadRenderer().isPresent()) {
((RenderDataAccess) (Object) renderData).getCustomQuadData().put(blockPos.toImmutable(), blockState);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import com.google.common.collect.Lists;

import net.fabricmc.loader.api.FabricLoader;
import net.ludocrypt.limlib.access.BakedModelAccess;
import net.ludocrypt.limlib.access.IrisClientAccess;
import net.ludocrypt.limlib.access.ItemRendererAccess;
import net.ludocrypt.limlib.access.ModelAccess;
import net.ludocrypt.limlib.access.WorldRendererAccess;
import net.ludocrypt.limlib.impl.LimlibRendering;
import net.minecraft.client.MinecraftClient;
Expand All @@ -40,7 +40,7 @@ public class ItemRendererMixin implements ItemRendererAccess {
MatrixStack matrixStack = new MatrixStack();
matrixStack.multiplyPositionMatrix(matrices.peek().getPositionMatrix().copy());
List<Runnable> immediateRenderer = Lists.newArrayList();
((BakedModelAccess) model).getSubQuads().forEach((id, quads) -> (isHandRendering ? LimlibRendering.LIMINAL_QUAD_RENDERER.get(id).heldItemRenderQueue : isItemRendering ? LimlibRendering.LIMINAL_QUAD_RENDERER.get(id).itemRenderQueue : immediateRenderer).add(() -> LimlibRendering.LIMINAL_QUAD_RENDERER.get(id).renderItemQuads(quads, client.world, stack.copy(), matrixStack, client.gameRenderer.getCamera(), inGui)));
LimlibRendering.LIMINAL_QUAD_RENDERER.get(((ModelAccess) model).getLiminalQuadRenderer().get()).renderItemModel(model, 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,26 +1,24 @@
package net.ludocrypt.limlib.mixin.client.render.model;

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

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

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

import net.ludocrypt.limlib.access.BakedModelAccess;
import net.ludocrypt.limlib.access.ModelAccess;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;

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

@Override
default Optional<Identifier> getLiminalQuadRenderer() {
return Optional.empty();
}

@Override
default Map<Identifier, List<Pair<BakedQuad, Optional<Direction>>>> getSubQuads() {
return Maps.newHashMap();
default void setLiminalQuadRenderer(@Nullable Identifier id) {
}

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

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

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

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

import net.ludocrypt.limlib.access.BakedModelAccess;
import net.minecraft.client.render.model.BakedQuad;
import net.ludocrypt.limlib.access.ModelAccess;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;

@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" })
public class GeneralBakedModelMixin implements BakedModelAccess {
public class GeneralBakedModelMixin implements ModelAccess {

@Unique
private Map<Identifier, List<Pair<BakedQuad, Optional<Direction>>>> subQuads = Maps.newHashMap();
private Optional<Identifier> liminalQuadRenderer = Optional.empty();

@Override
public Optional<Identifier> getLiminalQuadRenderer() {
return liminalQuadRenderer;
}

@Override
public Map<Identifier, List<Pair<BakedQuad, Optional<Direction>>>> getSubQuads() {
return subQuads;
public void setLiminalQuadRenderer(Identifier id) {
liminalQuadRenderer = Optional.ofNullable(id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import com.google.common.collect.Maps;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import net.ludocrypt.limlib.access.UnbakedModelAccess;
import net.ludocrypt.limlib.access.ModelAccess;
import net.ludocrypt.limlib.impl.LimlibRendering;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.render.model.json.ModelElement;
Expand All @@ -26,16 +24,12 @@ 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, List<ModelElement>> map = Maps.newHashMap();
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("limlib_extra")) {
LimlibRendering.LIMINAL_QUAD_RENDERER.getEntrySet().forEach((entry) -> {
if (jsonObject.get("limlib_extra").getAsJsonObject().has(entry.getKey().getValue().toString())) {
map.put(entry.getKey().getValue(), elementsFromJson(jsonDeserializationContext, jsonObject.get("limlib_extra").getAsJsonObject().get(entry.getKey().getValue().toString()).getAsJsonObject()));
}
});
if (jsonObject.has("limlib_renderer")) {
if (LimlibRendering.LIMINAL_QUAD_RENDERER.containsId(new Identifier(jsonObject.get("limlib_renderer").getAsString()))) {
((ModelAccess) ci.getReturnValue()).setLiminalQuadRenderer(new Identifier(jsonObject.get("limlib_renderer").getAsString()));
}
}
((UnbakedModelAccess) ci.getReturnValue()).putSubElements(map);
}

@Shadow
Expand Down
Loading

0 comments on commit c162506

Please sign in to comment.