Skip to content

Commit

Permalink
Fix crash with Flywheel's vertex consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Oct 27, 2023
1 parent 02f3bb4 commit 837512f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
import org.joml.Vector3f;
import org.joml.Vector4f;

import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.model.BakedQuad;
import net.minecraft.client.util.math.MatrixStack;

import net.fabricmc.fabric.api.renderer.v1.mesh.Mesh;
import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView;
Expand Down Expand Up @@ -112,24 +115,43 @@ protected void bufferQuad(MutableQuadViewImpl quad, VertexConsumer vertexConsume
normalVec.mul(normalMatrix);
}

for (int i = 0; i < 4; i++) {
posVec.set(quad.x(i), quad.y(i), quad.z(i), 1.0f);
posVec.mul(matrix);
vertexConsumer.vertex(posVec.x(), posVec.y(), posVec.z());

final int color = quad.color(i);
vertexConsumer.color((color >>> 16) & 0xFF, (color >>> 8) & 0xFF, color & 0xFF, (color >>> 24) & 0xFF);
vertexConsumer.texture(quad.u(i), quad.v(i));
vertexConsumer.overlay(overlay);
vertexConsumer.light(quad.lightmap(i));

if (useNormals) {
quad.copyNormal(i, normalVec);
normalVec.mul(normalMatrix);
// Use fast track for the default vanilla implementation
if (vertexConsumer instanceof BufferBuilder) {
for (int i = 0; i < 4; i++) {
posVec.set(quad.x(i), quad.y(i), quad.z(i), 1.0f);
posVec.mul(matrix);
vertexConsumer.vertex(posVec.x(), posVec.y(), posVec.z());

final int color = quad.color(i);
vertexConsumer.color((color >>> 16) & 0xFF, (color >>> 8) & 0xFF, color & 0xFF, (color >>> 24) & 0xFF);
vertexConsumer.texture(quad.u(i), quad.v(i));
vertexConsumer.overlay(overlay);
vertexConsumer.light(quad.lightmap(i));

if (useNormals) {
quad.copyNormal(i, normalVec);
normalVec.mul(normalMatrix);
}

vertexConsumer.normal(normalVec.x(), normalVec.y(), normalVec.z());
vertexConsumer.next();
}

vertexConsumer.normal(normalVec.x(), normalVec.y(), normalVec.z());
vertexConsumer.next();
}
// Other implementations (namely Flywheel's ShadeSeparatingVertexConsumer) only support calling putBulkData
// However, this results in a few additional operations made, so we only use it when necessary
else {
MatrixStack.Entry entry = new MatrixStack().peek();
entry.getPositionMatrix().set(matrix);
entry.getNormalMatrix().set(normalMatrix);
BakedQuad bakedQuad = quad.toBakedQuad(null);
vertexConsumer.putBulkData(
entry,
bakedQuad,
1.0F, 1.0F, 1.0F, 1.0F,
0,
overlay,
true
);
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ fabric-client-tags-api-v1-version=1.1.1
loom.platform=forge
forge_version=1.20.1-47.1.3
pack_format=15
forgified_version=1.9.28
forgified_version=1.9.29
forge_fabric_loader_version=2.3.4+0.14.21+1.20.1

0 comments on commit 837512f

Please sign in to comment.