Skip to content

Commit

Permalink
Revert "Revert "Apply Embeddium's patches for packed quad data to ove…
Browse files Browse the repository at this point in the history
…rwritten""

This reverts commit 9a4f55c.
  • Loading branch information
dima-dencep committed Dec 21, 2023
1 parent cbcf03d commit f027e2d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.mojang.blaze3d.vertex.PoseStack;
import me.jellysquid.mods.sodium.client.model.quad.ModelQuadView;
import me.jellysquid.mods.sodium.client.util.ModelQuadUtil;
import net.caffeinemc.mods.sodium.api.math.MatrixHelper;
import net.caffeinemc.mods.sodium.api.util.ColorARGB;
import net.caffeinemc.mods.sodium.api.util.NormI8;
import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter;
import net.caffeinemc.mods.sodium.api.vertex.format.VertexFormatDescription;
Expand Down Expand Up @@ -86,6 +88,22 @@ public static void write2(long ptr,

}

// TODO: Publicize this method in Embeddium so it doesn't need to be copied
private static int multARGBInts(int colorA, int colorB) {
// Most common case: Either quad coloring or tint-based coloring, but not both
if (colorA == -1) {
return colorB;
} else if (colorB == -1) {
return colorA;
}
// General case (rare): Both colorings, actually perform the multiplication
int a = (int)((ColorARGB.unpackAlpha(colorA)/255.0f) * (ColorARGB.unpackAlpha(colorB)/255.0f) * 255.0f);
int b = (int)((ColorARGB.unpackBlue(colorA)/255.0f) * (ColorARGB.unpackBlue(colorB)/255.0f) * 255.0f);
int g = (int)((ColorARGB.unpackGreen(colorA)/255.0f) * (ColorARGB.unpackGreen(colorB)/255.0f) * 255.0f);
int r = (int)((ColorARGB.unpackRed(colorA)/255.0f) * (ColorARGB.unpackRed(colorB)/255.0f) * 255.0f);
return ColorARGB.pack(r, g, b, a);
}

public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose matrices, ModelQuadView quad, int light, int overlay, int color) {
Matrix3f matNormal = matrices.normal();
Matrix4f matPosition = matrices.pose();
Expand All @@ -94,25 +112,11 @@ public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose m
long buffer = stack.nmalloc(4 * STRIDE);
long ptr = buffer;

// The packed normal vector
var n = quad.getNormal();

// The normal vector
float nx = NormI8.unpackX(n);
float ny = NormI8.unpackY(n);
float nz = NormI8.unpackZ(n);
float nxt = 0, nyt = 0, nzt = 0;

float midU = ((quad.getTexU(0) + quad.getTexU(1) + quad.getTexU(2) + quad.getTexU(3)) * 0.25f);
float midV = ((quad.getTexV(0) + quad.getTexV(1) + quad.getTexV(2) + quad.getTexV(3)) * 0.25f);

// The transformed normal vector
float nxt = MatrixHelper.transformNormalX(matNormal, nx, ny, nz);
float nyt = MatrixHelper.transformNormalY(matNormal, nx, ny, nz);
float nzt = MatrixHelper.transformNormalZ(matNormal, nx, ny, nz);

// The packed transformed normal vector
var nt = NormI8.pack(nxt, nyt, nzt);

for (int i = 0; i < 4; i++) {
// The position vector
float x = quad.getX(i);
Expand All @@ -124,7 +128,27 @@ public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose m
float yt = MatrixHelper.transformPositionY(matPosition, x, y, z);
float zt = MatrixHelper.transformPositionZ(matPosition, x, y, z);

write2(ptr, xt, yt, zt, color, quad.getTexU(i), quad.getTexV(i), midU, midV, light, overlay, nt);
// The packed normal vector
var n = quad.getForgeNormal(i);

if((n & 0xFFFFFF) == 0) {
n = quad.getNormal();
}

// The normal vector
float nx = NormI8.unpackX(n);
float ny = NormI8.unpackY(n);
float nz = NormI8.unpackZ(n);

// The transformed normal vector
nxt = MatrixHelper.transformNormalX(matNormal, nx, ny, nz);
nyt = MatrixHelper.transformNormalY(matNormal, nx, ny, nz);
nzt = MatrixHelper.transformNormalZ(matNormal, nx, ny, nz);

// The packed transformed normal vector
var nt = NormI8.pack(nxt, nyt, nzt);

write2(ptr, xt, yt, zt, multARGBInts(quad.getColor(i), color), quad.getTexU(i), quad.getTexV(i), midU, midV, ModelQuadUtil.mergeBakedLight(quad.getLight(i), light), overlay, nt);
ptr += STRIDE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.blaze3d.vertex.PoseStack;
import me.jellysquid.mods.sodium.client.model.quad.ModelQuadView;
import me.jellysquid.mods.sodium.client.render.immediate.model.BakedModelEncoder;
import me.jellysquid.mods.sodium.client.util.ModelQuadUtil;
import net.caffeinemc.mods.sodium.api.math.MatrixHelper;
import net.caffeinemc.mods.sodium.api.util.ColorABGR;
import net.caffeinemc.mods.sodium.api.util.ColorU8;
Expand All @@ -24,6 +25,10 @@

@Mixin(BakedModelEncoder.class)
public class MixinModelVertex {
@Shadow private static int mergeNormalAndMult(int packed, int calc, Matrix3f matNormal) {
throw new AssertionError();
}

@Inject(method = "writeQuadVertices(Lnet/caffeinemc/mods/sodium/api/vertex/buffer/VertexBufferWriter;Lcom/mojang/blaze3d/vertex/PoseStack$Pose;Lme/jellysquid/mods/sodium/client/model/quad/ModelQuadView;III)V", at = @At("HEAD"), cancellable = true, remap = false)
private static void redirect2(VertexBufferWriter writer, PoseStack.Pose matrices, ModelQuadView quad, int color, int light, int overlay, CallbackInfo ci) {
if (shouldBeExtended()) {
Expand Down Expand Up @@ -74,7 +79,7 @@ public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose m
}

color = ColorABGR.pack(fR, fG, fB, 1.0F);
ModelVertex.write(ptr, xt, yt, zt, color, quad.getTexU(i), quad.getTexV(i), overlay, light[i], normal);
ModelVertex.write(ptr, xt, yt, zt, color, quad.getTexU(i), quad.getTexV(i), overlay, ModelQuadUtil.mergeBakedLight(quad.getLight(i), light[i]), mergeNormalAndMult(quad.getForgeNormal(i), normal, matNormal));
ptr += 36L;
}

Expand Down

0 comments on commit f027e2d

Please sign in to comment.