Skip to content

Commit

Permalink
Correctly handle colorization on NeoForge
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 14, 2024
1 parent b758900 commit 152f499
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.caffeinemc.mods.sodium.api.util.ColorU8;
import net.caffeinemc.mods.sodium.api.vertex.buffer.VertexBufferWriter;
import net.caffeinemc.mods.sodium.api.vertex.format.common.EntityVertex;
import net.caffeinemc.mods.sodium.client.render.frapi.helper.ColorHelper;
import net.caffeinemc.mods.sodium.client.services.PlatformRuntimeInformation;
import org.joml.Matrix3f;
import org.joml.Matrix4f;
Expand All @@ -23,7 +24,7 @@ private static int mergeLighting(int stored, int calculated) {

private static final boolean MULTIPLY_ALPHA = PlatformRuntimeInformation.getInstance().usesAlphaMultiplication();

public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose matrices, ModelQuadView quad, int color, int light, int overlay) {
public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose matrices, ModelQuadView quad, int color, int light, int overlay, boolean colorize) {
Matrix3f matNormal = matrices.normal();
Matrix4f matPosition = matrices.pose();

Expand All @@ -39,6 +40,12 @@ public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose m

int newLight = mergeLighting(quad.getLight(i), light);

int newColor = color;

if (colorize) {
newColor = ColorHelper.multiplyColor(newColor, quad.getColor(i));
}

// The packed transformed normal vector
int normal = MatrixHelper.transformNormal(matNormal, matrices.trustedNormals, quad.getAccurateNormal(i));

Expand All @@ -47,7 +54,7 @@ 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);

EntityVertex.write(ptr, xt, yt, zt, color, quad.getTexU(i), quad.getTexV(i), overlay, newLight, normal);
EntityVertex.write(ptr, xt, yt, zt, newColor, quad.getTexU(i), quad.getTexV(i), overlay, newLight, normal);
ptr += EntityVertex.STRIDE;
}

Expand Down Expand Up @@ -116,4 +123,8 @@ public static void writeQuadVertices(VertexBufferWriter writer, PoseStack.Pose m
writer.push(stack, buffer, 4, EntityVertex.FORMAT);
}
}

public static boolean shouldMultiplyAlpha() {
return MULTIPLY_ALPHA;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void putBulkData(PoseStack.Pose matrices, BakedQuad bakedQuad, float r, f
ModelQuadView quad = (ModelQuadView) bakedQuad;

int color = ColorABGR.pack(r, g, b, a);
BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay);
BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay, false);

SpriteUtil.markSpriteActive(quad.getSprite());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void renderBakedItemQuads(PoseStack.Pose matrices, VertexBufferWriter wr
color = ColorARGB.toABGR((colorProvider.getColor(itemStack, quad.getColorIndex())));
}

BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay);
BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay, BakedModelEncoder.shouldMultiplyAlpha());

SpriteUtil.markSpriteActive(quad.getSprite());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static void renderQuads(PoseStack.Pose matrices, VertexBufferWriter writ

int color = quad.hasColor() ? defaultColor : 0xFFFFFFFF;

BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay);
BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay, false);

SpriteUtil.markSpriteActive(quad.getSprite());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private static void renderQuads(PoseStack.Pose matrices, VertexBufferWriter writ

int color = quad.hasColor() ? defaultColor : 0xFFFFFFFF;

BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay);
BakedModelEncoder.writeQuadVertices(writer, matrices, quad, color, light, overlay, false);

SpriteUtil.markSpriteActive(quad.getSprite());
}
Expand Down

0 comments on commit 152f499

Please sign in to comment.