Skip to content

Commit

Permalink
merge 1.20.3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gliscowo committed May 7, 2024
2 parents f646afb + e81aec1 commit b8e8d7c
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 50 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.10

# Mod Properties
mod_version = 0.1.7
mod_version = 0.1.8
maven_group = io.wispforest
archives_base_name = lavender

Expand All @@ -21,7 +21,7 @@ org.gradle.jvmargs=-Xmx1G
rei_version=15.0.728

# https://maven.wispforest.io/io/wispforest/owo-lib/
owo_version=0.12.7+1.20.5
owo_version=0.12.8+1.20.5

# https://maven.terraformersmc.com/releases/com/terraformersmc/modmenu
modmenu_version=10.0.0-beta.1
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/wispforest/lavender/client/BlitCutoutProgram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.wispforest.lavender.client;

import io.wispforest.lavender.Lavender;
import io.wispforest.owo.shader.GlProgram;
import net.minecraft.client.gl.ShaderProgram;
import net.minecraft.client.render.VertexFormats;

public class BlitCutoutProgram extends GlProgram {

public BlitCutoutProgram() {
super(Lavender.id("blit_cutout"), VertexFormats.BLIT_SCREEN);
}

public ShaderProgram program() {
return this.backingProgram;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
@Environment(EnvType.CLIENT)
public class LavenderClient implements ClientModInitializer {

public static final BlitCutoutProgram BLIT_CUTOUT_PROGRAM = new BlitCutoutProgram();

private static final Int2ObjectMap<Size> TEXTURE_SIZES = new Int2ObjectOpenHashMap<>();
private static final Identifier ENTRY_HUD_ID = Lavender.id("entry_hud");

Expand Down
138 changes: 93 additions & 45 deletions src/main/java/io/wispforest/lavender/client/OffhandBookRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@

import com.google.common.base.Suppliers;
import com.mojang.blaze3d.systems.RenderSystem;
import io.wispforest.lavender.Lavender;
import io.wispforest.lavender.book.Book;
import io.wispforest.lavender.pond.LavenderFramebufferExtension;
import io.wispforest.owo.ui.event.WindowResizeCallback;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.gl.SimpleFramebuffer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.render.*;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.texture.AbstractTexture;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Arm;
import net.minecraft.util.math.RotationAxis;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

public class OffhandBookRenderer {

private static final Supplier<Framebuffer> FRAMEBUFFER = Suppliers.memoize(() -> {
public static boolean rendering = false;

private static final Supplier<Framebuffer> BACK_BUFFER = Suppliers.memoize(() -> {
var window = MinecraftClient.getInstance().getWindow();

var framebuffer = new SimpleFramebuffer(window.getFramebufferWidth(), window.getFramebufferHeight(), true, MinecraftClient.IS_SYSTEM_MAC);
((LavenderFramebufferExtension)framebuffer).lavender$setUseCutoutBlit();
framebuffer.setClearColor(0f, 0f, 0f, 0f);
return framebuffer;
});

private static final Supplier<Framebuffer> DISPLAY_BUFFER = Suppliers.memoize(() -> {
var window = MinecraftClient.getInstance().getWindow();

var framebuffer = new SimpleFramebuffer(window.getFramebufferWidth(), window.getFramebufferHeight(), true, MinecraftClient.IS_SYSTEM_MAC);
Expand All @@ -30,77 +46,109 @@ public class OffhandBookRenderer {

public static void initialize() {
WindowResizeCallback.EVENT.register((client, window) -> {
FRAMEBUFFER.get().resize(window.getFramebufferWidth(), window.getFramebufferHeight(), MinecraftClient.IS_SYSTEM_MAC);
DISPLAY_BUFFER.get().resize(window.getFramebufferWidth(), window.getFramebufferHeight(), MinecraftClient.IS_SYSTEM_MAC);
BACK_BUFFER.get().resize(window.getFramebufferWidth(), window.getFramebufferHeight(), MinecraftClient.IS_SYSTEM_MAC);
cachedScreen = null;
});
}

public static void beginFrame() {
public static void beginFrame(@Nullable Book book) {
cacheExpired = true;
}

public static void render(MatrixStack matrices, Book book) {
final var client = MinecraftClient.getInstance();
client.getBufferBuilders().getEntityVertexConsumers().draw();
if (book == null) return;
var client = MinecraftClient.getInstance();

cacheExpired = false;
rendering = true;
var backBuffer = BACK_BUFFER.get();

// --- render book screen to separate framebuffer ---
try {
// --- render book screen to separate framebuffer ---

var screen = cachedScreen;
if (screen == null || screen.book != book) {
cachedScreen = screen = new LavenderBookScreen(book, true);
screen.init(client, client.getWindow().getScaledWidth(), client.getWindow().getScaledHeight());
var screen = cachedScreen;
if (screen == null || screen.book != book) {
cachedScreen = screen = new LavenderBookScreen(book, true);
screen.init(client, client.getWindow().getScaledWidth(), client.getWindow().getScaledHeight());

// we dispose the ui adapter here to
// stop it from messing with and/or
// leaking GLFW cursor objects
screen.adapter().dispose();
}
// we dispose the ui adapter here to
// stop it from messing with and/or
// leaking GLFW cursor objects
screen.adapter().dispose();
}

var modelView = RenderSystem.getModelViewStack();
modelView.pushMatrix();
modelView.identity();
modelView.translate(0, 0, -2000);
RenderSystem.applyModelViewMatrix();
var modelView = RenderSystem.getModelViewStack();
modelView.pushMatrix();
modelView.identity();
modelView.translate(0, 0, -2000);
RenderSystem.applyModelViewMatrix();

var framebuffer = FRAMEBUFFER.get();
framebuffer.clear(MinecraftClient.IS_SYSTEM_MAC);
framebuffer.beginWrite(false);
backBuffer.clear(MinecraftClient.IS_SYSTEM_MAC);
backBuffer.beginWrite(false);

screen.render(new DrawContext(client, client.getBufferBuilders().getEntityVertexConsumers()), -69, -69, 0);
RenderSystem.disableDepthTest();
screen.render(new DrawContext(client, client.getBufferBuilders().getEntityVertexConsumers()), -69, -69, 0);
RenderSystem.disableDepthTest();

client.getFramebuffer().beginWrite(false);
modelView.popMatrix();
RenderSystem.applyModelViewMatrix();

modelView.popMatrix();
RenderSystem.applyModelViewMatrix();
var displayBuffer = DISPLAY_BUFFER.get();
displayBuffer.clear(MinecraftClient.IS_SYSTEM_MAC);
displayBuffer.beginWrite(false);

backBuffer.draw(backBuffer.textureWidth, backBuffer.textureHeight, false);

client.getFramebuffer().beginWrite(false);
} finally {
rendering = false;
}
}

public static void render(MatrixStack matrices, int light) {
cacheExpired = false;
var client = MinecraftClient.getInstance();

// --- draw color attachment in place of map texture ---

var framebuffer = DISPLAY_BUFFER.get();

var texture = new FramebufferTexture(framebuffer.getColorAttachment());
client.getTextureManager().registerTexture(Lavender.id("offhand_book_framebuffer"), texture);

var rightHanded = client.player.getMainArm() == Arm.RIGHT;

matrices.push();
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(client.player.getMainArm() == Arm.RIGHT ? 15 : -15));
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(rightHanded ? 15 : -15));
matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(-10));

matrices.scale(1.3f * (framebuffer.textureWidth / (float) framebuffer.textureHeight), 1.35f, 1.35f);
matrices.translate(-.5f, -.4f, -.5f);

RenderSystem.setShader(GameRenderer::getPositionTexProgram);
RenderSystem.setShaderTexture(0, framebuffer.getColorAttachment());
matrices.scale(1 * (framebuffer.textureWidth / (float) framebuffer.textureHeight), 1f, 1f);
matrices.translate(rightHanded ? -.4f : -.6f, -.35f, -.165f);

var buffer = client.getBufferBuilders().getEntityVertexConsumers().getBuffer(RenderLayer.getText(Lavender.id("offhand_book_framebuffer")));
var matrix = matrices.peek().getPositionMatrix();
var buffer = Tessellator.getInstance().getBuffer();
buffer.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE);
buffer.vertex(matrix, 0, 1, 0).texture(0, 1).next();
buffer.vertex(matrix, 0, 0, 0).texture(0, 0).next();
buffer.vertex(matrix, 1, 0, 0).texture(1, 0).next();
buffer.vertex(matrix, 1, 1, 0).texture(1, 1).next();
BufferRenderer.drawWithGlobalProgram(buffer.end());

buffer.vertex(matrix, 0, 1, 0).color(1f, 1f, 1f, 1f).texture(0, 1).light(light).next();
buffer.vertex(matrix, 0, 0, 0).color(1f, 1f, 1f, 1f).texture(0, 0).light(light).next();
buffer.vertex(matrix, 1, 0, 0).color(1f, 1f, 1f, 1f).texture(1, 0).light(light).next();
buffer.vertex(matrix, 1, 1, 0).color(1f, 1f, 1f, 1f).texture(1, 1).light(light).next();

client.getBufferBuilders().getEntityVertexConsumers().draw();

matrices.pop();
}

public static void endFrame() {
if (cacheExpired) cachedScreen = null;
}

private static class FramebufferTexture extends AbstractTexture {

private FramebufferTexture(int textureId) {
this.glId = textureId;
}

@Override
public void load(ResourceManager manager) {}

@Override
public void clearGlId() {}
}
}
31 changes: 31 additions & 0 deletions src/main/java/io/wispforest/lavender/mixin/FramebufferMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.wispforest.lavender.mixin;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.mojang.blaze3d.platform.GlStateManager;
import io.wispforest.lavender.client.LavenderClient;
import io.wispforest.lavender.pond.LavenderFramebufferExtension;
import net.minecraft.client.gl.Framebuffer;
import net.minecraft.client.gl.ShaderProgram;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

import java.util.function.Supplier;

@Mixin(Framebuffer.class)
public class FramebufferMixin implements LavenderFramebufferExtension {

private boolean useCutoutBlit = false;

@Override
public void lavender$setUseCutoutBlit() {
this.useCutoutBlit = true;
}

@ModifyExpressionValue(method = "drawInternal", at = @At(value = "FIELD", target = "Lnet/minecraft/client/render/GameRenderer;blitScreenProgram:Lnet/minecraft/client/gl/ShaderProgram;"))
private ShaderProgram applyBlitProgram(ShaderProgram original) {
if (!this.useCutoutBlit) return original;

GlStateManager._colorMask(true, true, true, true);
return LavenderClient.BLIT_CUTOUT_PROGRAM.program();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ private boolean injectMap(boolean original, AbstractClientPlayerEntity player, f
}

@Inject(method = "renderFirstPersonMap", at = @At("HEAD"), cancellable = true)
private void injectBook(MatrixStack matrices, VertexConsumerProvider vertexConsumers, int swingProgress, ItemStack stack, CallbackInfo ci) {
private void injectBook(MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, ItemStack stack, CallbackInfo ci) {
if (!(stack.getItem() instanceof LavenderBookItem)) return;
ci.cancel();

OffhandBookRenderer.render(matrices, LavenderBookItem.bookOf(stack));
OffhandBookRenderer.render(matrices, light);
}

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,46 @@
package io.wispforest.lavender.mixin;

import io.wispforest.lavender.book.Book;
import io.wispforest.lavender.book.LavenderBookItem;
import io.wispforest.lavender.client.LavenderBookScreen;
import io.wispforest.lavender.client.OffhandBookRenderer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.network.ClientPlayerEntity;
import org.jetbrains.annotations.Nullable;
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.CallbackInfo;

@Mixin(MinecraftClient.class)
public class MinecraftClientMixin {

@Shadow
@Nullable
public ClientPlayerEntity player;

@Shadow
@Nullable
public Screen currentScreen;

@Inject(method = "render", at = @At("HEAD"))
private void onFrameStart(boolean tick, CallbackInfo ci) {
OffhandBookRenderer.beginFrame();
if (this.player == null) return;

Book bookToRender = null;
var offhandStack = this.player.getOffHandStack();
if (offhandStack.getItem() instanceof LavenderBookItem && LavenderBookItem.bookOf(offhandStack) != null && !(this.currentScreen instanceof LavenderBookScreen)) {
bookToRender = LavenderBookItem.bookOf(offhandStack);
}

OffhandBookRenderer.beginFrame(bookToRender);
}

@Inject(method = "render", at = @At("TAIL"))
private void onFrameEnd(boolean tick, CallbackInfo ci) {
if (this.player == null) return;
OffhandBookRenderer.endFrame();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.wispforest.lavender.pond;

public interface LavenderFramebufferExtension {
void lavender$setUseCutoutBlit();
}
18 changes: 18 additions & 0 deletions src/main/resources/assets/lavender/shaders/core/blit_cutout.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 150

uniform sampler2D DiffuseSampler;

uniform vec4 ColorModulator;

in vec2 texCoord;
in vec4 vertexColor;

out vec4 fragColor;

void main() {
vec4 color = texture(DiffuseSampler, texCoord) * vertexColor;
color.a = min(1.0f, color.a * 1e8f);

// blit final output of compositor into displayed back buffer
fragColor = color * ColorModulator;
}
22 changes: 22 additions & 0 deletions src/main/resources/assets/lavender/shaders/core/blit_cutout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"blend": {
"func": "add",
"srcrgb": "srcalpha",
"dstrgb": "1-srcalpha"
},
"vertex": "blit_screen",
"fragment": "lavender:blit_cutout",
"attributes": [
"Position",
"UV",
"Color"
],
"samplers": [
{ "name": "DiffuseSampler" }
],
"uniforms": [
{ "name": "ModelViewMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ProjMat", "type": "matrix4x4", "count": 16, "values": [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ] },
{ "name": "ColorModulator", "type": "float", "count": 4, "values": [ 1.0, 1.0, 1.0, 1.0 ] }
]
}
1 change: 1 addition & 0 deletions src/main/resources/lavender.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"ClientAdvancementManagerAccessor",
"CreativeInventoryScreenMixin",
"DrawContextMixin",
"FramebufferMixin",
"HeldItemRendererMixin",
"MinecraftClientMixin",
"MouseMixin",
Expand Down

0 comments on commit b8e8d7c

Please sign in to comment.