Skip to content

Commit

Permalink
finalize quad api
Browse files Browse the repository at this point in the history
  • Loading branch information
LudoCrypt committed Jun 26, 2022
1 parent bdb6fc1 commit 79f1047
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 307 deletions.
51 changes: 0 additions & 51 deletions src/main/java/net/ludocrypt/limlib/LimlibClient.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
package net.ludocrypt.limlib;

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

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.datafixers.util.Pair;

import ladysnake.satin.api.event.ShaderEffectRenderCallback;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.ludocrypt.limlib.access.DimensionEffectsAccess;
import net.ludocrypt.limlib.api.render.LiminalCoreShader;
import net.ludocrypt.limlib.api.render.LiminalQuadRenderer;
import net.ludocrypt.limlib.api.render.LiminalShaderApplier;
import net.ludocrypt.limlib.api.sound.SoundEmitter;
import net.ludocrypt.limlib.impl.LimlibRegistries;
import net.minecraft.block.BlockState;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.Camera;
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.BakedQuad;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Matrix4f;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;

public class LimlibClient implements ClientModInitializer {

Expand Down Expand Up @@ -59,38 +40,6 @@ public void onInitializeClient() {
}
}
});

LiminalCoreShader core = Registry.register(LimlibRegistries.LIMINAL_CORE_SHADER, new Identifier("limlib", "skybox_clone"), new LiminalCoreShader(VertexFormats.POSITION));

Registry.register(LimlibRegistries.LIMINAL_QUAD_RENDERER, new Identifier("limlib", "black"), new LiminalQuadRenderer() {

@Override
public void renderQuad(BakedQuad quad, BufferBuilder bufferBuilder, Matrix4f matrix, Camera camera, World world, MatrixStack matrices, List<Pair<BakedQuad, Optional<Direction>>> quads) {
RenderSystem.setShader(core::getShader);

for (int i = 0; i < 6; i++) {
RenderSystem.setShaderTexture(i, new Identifier(quad.getSprite().getId().getNamespace(), "textures/" + quad.getSprite().getId().getPath() + "_" + i + ".png"));
}

LiminalQuadRenderer.quad((vec3f) -> bufferBuilder.vertex(vec3f.getX(), vec3f.getY(), vec3f.getZ()).next(), matrix, quad);
}

@Override
public boolean renderBehind() {
return true;
}

@Override
public VertexFormat vertexFormat() {
return VertexFormats.POSITION;
}

@Override
public DrawMode drawMode() {
return DrawMode.QUADS;
}
});

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.ludocrypt.limlib.access;

public interface ItemRendererAccess {

public boolean isInGui();

public void setInGui(boolean in);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mojang.datafixers.util.Pair;

import net.minecraft.block.BlockState;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.render.Camera;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.BlockPos;
Expand All @@ -16,6 +17,12 @@ public interface WorldRendererAccess {

public void renderQuads(float tickDelta, MatrixStack matrices, Camera camera);

public void renderHands(Framebuffer framebuffer, float tickDelta, MatrixStack matrices, Camera camera);

public void renderItems(Framebuffer framebuffer, float tickDelta, MatrixStack matrices, Camera camera);

public void renderBlocks(Framebuffer framebuffer, float tickDelta, MatrixStack matrices, Camera camera);

public List<Pair<BlockPos, BlockState>> getQuadRenderData();

public boolean isRenderingHands();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public abstract class LiminalQuadRenderer {
public abstract void renderQuad(BakedQuad quad, BufferBuilder bufferBuilder, Matrix4f matrix, Camera camera, World world, MatrixStack matrices, List<Pair<BakedQuad, Optional<Direction>>> quads);

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

for (Pair<BakedQuad, Optional<Direction>> quadPair : quads) {
Expand All @@ -61,8 +61,8 @@ public void renderQuads(List<Pair<BakedQuad, Optional<Direction>>> quads, World
this.endRenderer(bufferBuilder, matrices, camera);
}

public void renderItemQuads(List<Pair<BakedQuad, Optional<Direction>>> quads, World world, ItemStack stack, MatrixStack matrices, Camera camera) {
Matrix4f matrix = setupMatrix(matrices, camera);
public void renderItemQuads(List<Pair<BakedQuad, Optional<Direction>>> quads, 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));
Expand All @@ -84,13 +84,15 @@ public BufferBuilder setupRenderer(MatrixStack matrices, Camera camera) {
return bufferBuilder;
}

public Matrix4f setupMatrix(MatrixStack matrices, Camera camera) {
public Matrix4f setupMatrix(MatrixStack matrices, Camera camera, boolean stationize) {
Matrix4f matrix = new MatrixStack().peek().getPositionMatrix().copy();

// Stationize
matrix.multiply(Vec3f.NEGATIVE_Y.getDegreesQuaternion(180));
matrix.multiply(Vec3f.NEGATIVE_Y.getDegreesQuaternion(camera.getYaw()));
matrix.multiply(Vec3f.NEGATIVE_X.getDegreesQuaternion(camera.getPitch()));
if (stationize) {
matrix.multiply(Vec3f.NEGATIVE_Y.getDegreesQuaternion(180));
matrix.multiply(Vec3f.NEGATIVE_Y.getDegreesQuaternion(camera.getYaw()));
matrix.multiply(Vec3f.NEGATIVE_X.getDegreesQuaternion(camera.getPitch()));
}

matrix.multiply(matrices.peek().getPositionMatrix().copy());
return matrix;
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/net/ludocrypt/limlib/impl/LimlibRegistries.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

import com.mojang.serialization.Codec;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import net.ludocrypt.limlib.api.LiminalWorld;
import net.ludocrypt.limlib.api.render.LiminalBaseEffects;
import net.ludocrypt.limlib.api.render.LiminalCoreShader;
import net.ludocrypt.limlib.api.render.LiminalQuadRenderer;
import net.ludocrypt.limlib.api.render.LiminalShaderApplier;
import net.ludocrypt.limlib.api.render.LiminalSkyRenderer;
import net.ludocrypt.limlib.api.world.maze.MazeComponent;
Expand All @@ -27,12 +23,6 @@ public class LimlibRegistries {
public static final SimpleRegistry<Codec<? extends LiminalShaderApplier>> LIMINAL_SHADER_APPLIER = (SimpleRegistry<Codec<? extends LiminalShaderApplier>>) (Object) FabricRegistryBuilder.createSimple(Codec.class, new Identifier("limlib", "limlib_shader_applier")).attribute(RegistryAttribute.SYNCED).buildAndRegister();
public static final SimpleRegistry<Codec<? extends LiminalBaseEffects>> LIMINAL_BASE_EFFECTS = (SimpleRegistry<Codec<? extends LiminalBaseEffects>>) (Object) FabricRegistryBuilder.createSimple(Codec.class, new Identifier("limlib", "limlib_base_effects")).attribute(RegistryAttribute.SYNCED).buildAndRegister();

@Environment(EnvType.CLIENT)
public static final SimpleRegistry<LiminalQuadRenderer> LIMINAL_QUAD_RENDERER = FabricRegistryBuilder.createSimple(LiminalQuadRenderer.class, new Identifier("limlib", "limlib_quad_renderer")).attribute(RegistryAttribute.SYNCED).buildAndRegister();

@Environment(EnvType.CLIENT)
public static final SimpleRegistry<LiminalCoreShader> LIMINAL_CORE_SHADER = FabricRegistryBuilder.createSimple(LiminalCoreShader.class, new Identifier("limlib", "limlib_core_shader")).attribute(RegistryAttribute.SYNCED).buildAndRegister();

public static final SimpleRegistry<Codec<? extends MazeGenerator<? extends ChunkGenerator, ? extends MazeComponent>>> LIMINAL_MAZE_GENERATOR = (SimpleRegistry<Codec<? extends MazeGenerator<? extends ChunkGenerator, ? extends MazeComponent>>>) (Object) FabricRegistryBuilder.createSimple(Codec.class, new Identifier("limlib", "limlib_maze_generator")).attribute(RegistryAttribute.SYNCED).buildAndRegister();

}
16 changes: 16 additions & 0 deletions src/main/java/net/ludocrypt/limlib/impl/LimlibRendering.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.ludocrypt.limlib.impl;

import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import net.ludocrypt.limlib.api.render.LiminalCoreShader;
import net.ludocrypt.limlib.api.render.LiminalQuadRenderer;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.SimpleRegistry;

public class LimlibRendering {

public static final SimpleRegistry<LiminalQuadRenderer> LIMINAL_QUAD_RENDERER = FabricRegistryBuilder.createSimple(LiminalQuadRenderer.class, new Identifier("limlib", "limlib_quad_renderer")).attribute(RegistryAttribute.SYNCED).buildAndRegister();

public static final SimpleRegistry<LiminalCoreShader> LIMINAL_CORE_SHADER = FabricRegistryBuilder.createSimple(LiminalCoreShader.class, new Identifier("limlib", "limlib_core_shader")).attribute(RegistryAttribute.SYNCED).buildAndRegister();

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import com.mojang.datafixers.util.Pair;

import net.ludocrypt.limlib.impl.LimlibRegistries;
import net.ludocrypt.limlib.impl.LimlibRendering;
import net.minecraft.client.gl.Program;
import net.minecraft.client.render.GameRenderer;
import net.minecraft.client.render.Shader;
Expand All @@ -25,9 +25,9 @@ public class GameRendererMixin {

@Inject(method = "loadShaders", at = @At(value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z", ordinal = 53, shift = Shift.AFTER), locals = LocalCapture.CAPTURE_FAILHARD)
private void limlib$loadShaders(ResourceManager manager, CallbackInfo ci, List<Program> list, List<Pair<Shader, Consumer<Shader>>> list2) {
LimlibRegistries.LIMINAL_CORE_SHADER.forEach((core) -> {
LimlibRendering.LIMINAL_CORE_SHADER.forEach((core) -> {
try {
Identifier id = LimlibRegistries.LIMINAL_CORE_SHADER.getId(core);
Identifier id = LimlibRendering.LIMINAL_CORE_SHADER.getId(core);
list2.add(Pair.of(new Shader(manager, "rendertype_" + id.getNamespace() + "_" + id.getPath(), core.getVertexFormat()), core::setShader));
} catch (IOException e) {
list2.forEach((pair) -> {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/net/ludocrypt/limlib/mixin/InventoryScreenMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.ludocrypt.limlib.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.ludocrypt.limlib.access.ItemRendererAccess;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.entity.LivingEntity;

@Mixin(InventoryScreen.class)
public class InventoryScreenMixin {

@Unique
private static final MinecraftClient CLIENT = MinecraftClient.getInstance();

@Inject(method = "Lnet/minecraft/client/gui/screen/ingame/InventoryScreen;drawEntity(IIIFFLnet/minecraft/entity/LivingEntity;)V", at = @At("HEAD"))
private static void limlib$drawEntity$head(int x, int y, int size, float mouseX, float mouseY, LivingEntity entity, CallbackInfo ci) {
((ItemRendererAccess) CLIENT.getItemRenderer()).setInGui(true);
}

@Inject(method = "Lnet/minecraft/client/gui/screen/ingame/InventoryScreen;drawEntity(IIIFFLnet/minecraft/entity/LivingEntity;)V", at = @At("TAIL"))
private static void limlib$drawEntity$tail(int x, int y, int size, float mouseX, float mouseY, LivingEntity entity, CallbackInfo ci) {
((ItemRendererAccess) CLIENT.getItemRenderer()).setInGui(false);
}

}
41 changes: 36 additions & 5 deletions src/main/java/net/ludocrypt/limlib/mixin/ItemRendererMixin.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package net.ludocrypt.limlib.mixin;

import java.util.List;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

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.WorldRendererAccess;
import net.ludocrypt.limlib.impl.LimlibRegistries;
import net.ludocrypt.limlib.impl.LimlibRendering;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.item.ItemRenderer;
Expand All @@ -18,7 +24,10 @@
import net.minecraft.item.ItemStack;

@Mixin(ItemRenderer.class)
public class ItemRendererMixin {
public class ItemRendererMixin implements ItemRendererAccess {

@Unique
private boolean inGui = false;

@Inject(method = "Lnet/minecraft/client/render/item/ItemRenderer;renderBakedItemModel(Lnet/minecraft/client/render/model/BakedModel;Lnet/minecraft/item/ItemStack;IILnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumer;)V", at = @At("HEAD"))
private void limlib$renderBakedItemModel(BakedModel model, ItemStack stack, int light, int overlay, MatrixStack matrices, VertexConsumer vertices, CallbackInfo ci) {
Expand All @@ -27,11 +36,33 @@ public class ItemRendererMixin {
boolean isHandRendering = ((WorldRendererAccess) client.worldRenderer).isRenderingHands() || (FabricLoader.getInstance().isModLoaded("iris") && ((IrisClientAccess) client).isHandRenderingActive());
boolean isItemRendering = ((WorldRendererAccess) client.worldRenderer).isRenderingItems();

if (isHandRendering || isItemRendering) {
if (isHandRendering || isItemRendering || inGui) {
MatrixStack matrixStack = new MatrixStack();
matrixStack.multiplyPositionMatrix(matrices.peek().getPositionMatrix().copy());

((BakedModelAccess) model).getSubQuads().forEach((id, quads) -> (isHandRendering ? LimlibRegistries.LIMINAL_QUAD_RENDERER.get(id).heldItemRenderQueue : LimlibRegistries.LIMINAL_QUAD_RENDERER.get(id).itemRenderQueue).add(() -> LimlibRegistries.LIMINAL_QUAD_RENDERER.get(id).renderItemQuads(quads, client.world, stack.copy(), matrixStack, client.gameRenderer.getCamera())));
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)));
immediateRenderer.forEach(Runnable::run);
immediateRenderer.clear();
}
}

@Inject(method = "Lnet/minecraft/client/render/item/ItemRenderer;renderGuiItemModel(Lnet/minecraft/item/ItemStack;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("HEAD"))
private void limlib$renderGuiItemModel$head(ItemStack stack, int x, int y, BakedModel model, CallbackInfo ci) {
inGui = true;
}

@Inject(method = "Lnet/minecraft/client/render/item/ItemRenderer;renderGuiItemModel(Lnet/minecraft/item/ItemStack;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At("TAIL"))
private void limlib$renderGuiItemModel$tail(ItemStack stack, int x, int y, BakedModel model, CallbackInfo ci) {
inGui = false;
}

@Override
public boolean isInGui() {
return inGui;
}

@Override
public void setInGui(boolean in) {
inGui = in;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.google.gson.JsonObject;

import net.ludocrypt.limlib.access.UnbakedModelAccess;
import net.ludocrypt.limlib.impl.LimlibRegistries;
import net.ludocrypt.limlib.impl.LimlibRendering;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.render.model.json.ModelElement;
import net.minecraft.util.Identifier;
Expand All @@ -29,7 +29,7 @@ public abstract class JsonUnbakedModelDeserializerMixin {
Map<Identifier, List<ModelElement>> map = Maps.newHashMap();
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("limlib_extra")) {
LimlibRegistries.LIMINAL_QUAD_RENDERER.getEntrySet().forEach((entry) -> {
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()));
}
Expand Down
Loading

0 comments on commit 79f1047

Please sign in to comment.