Skip to content

Commit

Permalink
Fix bug caused by NaN projection matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralhalo committed May 28, 2023
1 parent 837599a commit ea5a9b7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/grondag/canvas/shader/data/MatrixData.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,23 @@
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;

import grondag.canvas.CanvasMod;
import grondag.canvas.mixinterface.GameRendererExt;

public final class MatrixData {
private MatrixData() { }

private static boolean sanitizedOnce = false;

private static void fixVanillaBug(Matrix4f matrix) {
// this only happens in the first frame, when the loading screen is still visible
// remove this once it stops spitting out the warning
if (Float.isNaN(matrix.determinant())) {
CanvasMod.LOG.warn("Switching out projection matrix with identity because it's NaN");
matrix.identity();
}
}

static void update(PoseStack.Pose view, Matrix4f projectionMatrix, Camera camera, float tickDelta) {
// write values for prior frame before updating
viewMatrix.get(VIEW_LAST * 16, MATRIX_DATA);
Expand All @@ -44,6 +56,11 @@ static void update(PoseStack.Pose view, Matrix4f projectionMatrix, Camera camera
cleanProjMatrix.get(CLEAN_PROJ_LAST * 16, MATRIX_DATA);
cleanViewProjMatrix.get(CLEAN_VP_LAST * 16, MATRIX_DATA);

if (!sanitizedOnce) {
fixVanillaBug(projectionMatrix);
sanitizedOnce = true;
}

viewNormalMatrix.set(view.normal());

viewMatrix.set(view.pose());
Expand Down

0 comments on commit ea5a9b7

Please sign in to comment.