From 9d2314c31ac43852da34af52ed948c11a1c6d2ef Mon Sep 17 00:00:00 2001 From: Ocelot Date: Thu, 16 May 2024 01:16:00 -0600 Subject: [PATCH] Improved performance and memory --- .../veil/example/VeilExampleModClient.java | 2 +- .../client/render/DebugFrustumRenderer.java | 120 ------------------ .../render/MirrorBlockEntityRenderer.java | 29 +++-- .../pinwheel/framebuffers/mirror.json | 2 - 4 files changed, 18 insertions(+), 135 deletions(-) delete mode 100644 src/main/java/foundry/veil/example/client/render/DebugFrustumRenderer.java diff --git a/src/main/java/foundry/veil/example/VeilExampleModClient.java b/src/main/java/foundry/veil/example/VeilExampleModClient.java index f6406df..4a28c48 100644 --- a/src/main/java/foundry/veil/example/VeilExampleModClient.java +++ b/src/main/java/foundry/veil/example/VeilExampleModClient.java @@ -27,7 +27,7 @@ public void onInitializeClient() { FabricVeilRenderLevelStageEvent.EVENT.register((stage, levelRenderer, bufferSource, poseStack, projectionMatrix, renderTick, partialTicks, camera, frustum) -> { if (stage == VeilRenderLevelStageEvent.Stage.AFTER_LEVEL) { - MirrorBlockEntityRenderer.renderLevel(Minecraft.getInstance().level, bufferSource, poseStack, projectionMatrix, partialTicks, VeilRenderBridge.create(frustum), camera); + MirrorBlockEntityRenderer.renderLevel(Minecraft.getInstance().level, projectionMatrix, partialTicks, VeilRenderBridge.create(frustum), camera); } }); } diff --git a/src/main/java/foundry/veil/example/client/render/DebugFrustumRenderer.java b/src/main/java/foundry/veil/example/client/render/DebugFrustumRenderer.java deleted file mode 100644 index 6555a45..0000000 --- a/src/main/java/foundry/veil/example/client/render/DebugFrustumRenderer.java +++ /dev/null @@ -1,120 +0,0 @@ -package foundry.veil.example.client.render; - -import com.mojang.blaze3d.vertex.PoseStack; -import com.mojang.blaze3d.vertex.VertexConsumer; -import net.minecraft.client.renderer.MultiBufferSource; -import net.minecraft.client.renderer.RenderType; -import org.jetbrains.annotations.NotNull; -import org.joml.Matrix4f; -import org.joml.Matrix4fc; -import org.joml.Vector3f; -import org.joml.Vector3fc; - -/** - * Draws the sun shadow frustums, but can draw any arbitrary view frustum. - * - * @author Ocelot - */ -public class DebugFrustumRenderer { - - private static final Vector3f[] POSITIONS = new Vector3f[8]; - private static final Matrix4f MODELVIEW = new Matrix4f(); - private static final Matrix4f PROJECTION = new Matrix4f(); - - static { - for (int i = 0; i < POSITIONS.length; i++) { - POSITIONS[i] = new Vector3f(); - } - } - - private static int getIndex(int x, int y, int z) { - return (z * 2 + y) * 2 + x; - } - - private static void putLine(@NotNull VertexConsumer consumer, - int x0, - int y0, - int z0, - int x1, - int y1, - int z1, - float red, - float green, - float blue, - float alpha) { - Vector3fc pos0 = POSITIONS[getIndex(x0, y0, z0)]; - Vector3fc pos1 = POSITIONS[getIndex(x1, y1, z1)]; - Vector3fc norm = pos1.sub(pos0, new Vector3f()).normalize(); - - consumer.vertex(pos0.x(), pos0.y(), pos0.z()) - .color(red, green, blue, alpha) - .normal(norm.x(), norm.y(), norm.z()) - .endVertex(); - consumer.vertex(pos1.x(), pos1.y(), pos1.z()) - .color(red, green, blue, alpha) - .normal(norm.x(), norm.y(), norm.z()) - .endVertex(); - } - - public static void renderFrustum(@NotNull MultiBufferSource source, - @NotNull PoseStack stack, - @NotNull Matrix4fc pose, - @NotNull Matrix4fc projection, - float red, - float green, - float blue, - float alpha) { - stack.pushPose(); - Matrix4f modelView = stack.last().pose(); - modelView.mul(pose.invert(MODELVIEW)); - modelView.mul(projection.invert(PROJECTION)); - - for (int z = 0; z < 2; z++) { - for (int x = 0; x < 2; x++) { - for (int y = 0; y < 2; y++) { - modelView.transformProject(x * 2 - 1, - y * 2 - 1, - z * 2 - 1, - POSITIONS[getIndex(x, y, z)]); - } - } - } - - VertexConsumer consumer = source.getBuffer(RenderType.lines()); - - // Near - putLine(consumer, 0, 0, 0, 0, 1, 0, red, green, blue, alpha); - putLine(consumer, 0, 0, 1, 0, 1, 1, red, green, blue, alpha); - putLine(consumer, 0, 0, 0, 0, 0, 1, red, green, blue, alpha); - - putLine(consumer, 0, 1, 0, 1, 1, 0, red, green, blue, alpha); - putLine(consumer, 0, 1, 1, 1, 1, 1, red, green, blue, alpha); - putLine(consumer, 0, 1, 0, 0, 1, 1, red, green, blue, alpha); - - putLine(consumer, 1, 1, 0, 1, 0, 0, red, green, blue, alpha); - putLine(consumer, 1, 1, 1, 1, 0, 1, red, green, blue, alpha); - putLine(consumer, 1, 1, 0, 1, 1, 1, red, green, blue, alpha); - - putLine(consumer, 1, 0, 0, 0, 0, 0, red, green, blue, alpha); - putLine(consumer, 1, 0, 1, 0, 0, 1, red, green, blue, alpha); - putLine(consumer, 1, 0, 0, 1, 0, 1, red, green, blue, alpha); - - stack.popPose(); - } - -// @Override -// public void render(@NotNull ClientLevel level, -// @NotNull MatrixStack stack, -// @NotNull MultiBufferSource source, -// @NotNull StarfallCamera camera) { -// int layer = StarfallClient.getInstance().getDebugFlags().getShadowLayer(); -// ShadowCamera shadowCamera = this.atlas.getCamera(layer); -// this.renderShadowCameraFrustum(stack, source, shadowCamera); -// this.renderShadowCameraFrustum(stack, source, camera); -// } -// -// @Override -// public boolean shouldRender() { -// return StarfallClient.getInstance().getDebugFlags().getShadowLayer() >= 0; -// } -} diff --git a/src/main/java/foundry/veil/example/client/render/MirrorBlockEntityRenderer.java b/src/main/java/foundry/veil/example/client/render/MirrorBlockEntityRenderer.java index 2c9e277..5b882fb 100644 --- a/src/main/java/foundry/veil/example/client/render/MirrorBlockEntityRenderer.java +++ b/src/main/java/foundry/veil/example/client/render/MirrorBlockEntityRenderer.java @@ -36,7 +36,6 @@ import net.minecraft.core.Vec3i; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.resources.ResourceManager; -import net.minecraft.util.Mth; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; @@ -47,7 +46,6 @@ import java.lang.Math; import java.nio.IntBuffer; import java.util.Map; -import java.util.stream.IntStream; import static org.lwjgl.opengl.GL11C.*; import static org.lwjgl.opengl.GL30C.glGenerateMipmap; @@ -59,6 +57,8 @@ public class MirrorBlockEntityRenderer implements BlockEntityRenderer RENDER_POSITIONS = new ObjectArraySet<>(); private static final Long2ObjectMap TEXTURES = new Long2ObjectArrayMap<>(); @@ -74,6 +74,10 @@ private static long getKey(BlockPos pos, Direction face) { @Override public void render(MirrorBlockEntity blockEntity, float f, PoseStack poseStack, MultiBufferSource multiBufferSource, int i, int j) { + if (VeilLevelPerspectiveRenderer.isRenderingPerspective()) { + return; + } + BlockPos pos = blockEntity.getBlockPos(); Direction facing = blockEntity.getBlockState().getValue(MirrorBlock.FACING); @@ -139,7 +143,7 @@ private static float dot(BlockPos pos, Vec3i normal, double x, double y, double return (float) ((pos.getX() + 0.5 - normal.getX() * 0.5 - x) * normal.getX() + (pos.getY() + 0.5 - normal.getY() * 0.5 - y) * normal.getY() + (pos.getZ() + 0.5 - normal.getZ() * 0.5 - z) * normal.getZ()); } - public static void renderLevel(ClientLevel level, MultiBufferSource.BufferSource bufferSource, PoseStack poseStack, Matrix4fc projection, float partialTicks, CullFrustum frustum, Camera camera) { + public static void renderLevel(ClientLevel level, Matrix4fc projection, float partialTicks, CullFrustum frustum, Camera camera) { if (VeilLevelPerspectiveRenderer.isRenderingPerspective() || !projection.isFinite()) { return; } @@ -183,9 +187,9 @@ public static void renderLevel(ClientLevel level, MultiBufferSource.BufferSource new Quaternionf().lookAlong(dir, up).transform(plane); - CalculateObliqueMatrix(RENDER_PROJECTION, plane); + calculateObliqueMatrix(RENDER_PROJECTION, plane, RENDER_PROJECTION); - VeilLevelPerspectiveRenderer.render(fbo, RENDER_MODELVIEW.identity(), RENDER_PROJECTION, renderPos, new Quaternionf().lookAlong(dir, up), RENDER_DISTANCE, partialTicks); + VeilLevelPerspectiveRenderer.render(fbo, RENDER_MODELVIEW, RENDER_PROJECTION, renderPos, VIEW.lookAlong(dir, up), RENDER_DISTANCE, partialTicks); mirror.copy(fbo); mirror.setRendered(true); } @@ -205,14 +209,15 @@ public static void renderLevel(ClientLevel level, MultiBufferSource.BufferSource RENDER_POSITIONS.clear(); } - public static void CalculateObliqueMatrix(Matrix4f projection, Vector4fc clipPlane) { - Vector4f q = projection.invert(new Matrix4f()).transform(new Vector4f( - Math.signum(clipPlane.x()), - Math.signum(clipPlane.y()), + public static void calculateObliqueMatrix(Matrix4fc projection, Vector4fc c, Matrix4f store) { + Vector4f q = projection.invert(new Matrix4f()).transform( + Math.signum(c.x()), + Math.signum(c.y()), + 1.0f, 1.0f, - 1.0f)); - Vector4f c = clipPlane.mul(2.0F / (clipPlane.dot(q)), new Vector4f()); - projection.m02(c.x - projection.m03()).m12(c.y - projection.m13()).m22(c.z - projection.m23()).m32(c.w - projection.m33()); + OBLIQUE_PLANE); + float dot = c.dot(q); + store.m02(c.x() * 2.0F / dot - projection.m03()).m12(c.y() * 2.0F / dot - projection.m13()).m22(c.z() * 2.0F / dot - projection.m23()).m32(c.w() * 2.0F / dot - projection.m33()); } @Override diff --git a/src/main/resources/assets/veil-example-mod/pinwheel/framebuffers/mirror.json b/src/main/resources/assets/veil-example-mod/pinwheel/framebuffers/mirror.json index 9c1ea2d..d09cfe6 100644 --- a/src/main/resources/assets/veil-example-mod/pinwheel/framebuffers/mirror.json +++ b/src/main/resources/assets/veil-example-mod/pinwheel/framebuffers/mirror.json @@ -1,7 +1,5 @@ { "depth": true, - "width": 2048, - "height": 2048, "format": "RGB8", "autoClear": false } \ No newline at end of file