Fix issues with QuadBakingVertexConsumer and Forge's OBJ loader (#10065)

This commit is contained in:
Martijn van den Brand authored and GitHub committed 2024-08-22 17:26:44 -07:00
1 parent 93c72d2d92
commit 26d0f7282f
8 files changed
+140 -10

No files matched your search

@@ -490,7 +490,7 @@ public class ObjModel extends SimpleUnbakedGeometry<ObjModel>
}
}
return Pair.of(quadBaker.getQuad(), cull);
return Pair.of(quadBaker.build(), cull);
}
public CompositeRenderable bakeRenderable(IGeometryBakingContext configuration)
@@ -33,11 +33,12 @@ public class QuadBakingVertexConsumer implements VertexConsumer
for (var element : DefaultVertexFormat.BLOCK.getElements())
map.put(element, DefaultVertexFormat.BLOCK.getOffset(i++) / 4); // Int offset
});
private static final int QUAD_DATA_SIZE = IQuadTransformer.STRIDE * 4;
private static final int MAX_VERTICES = 4;
private static final int QUAD_DATA_SIZE = IQuadTransformer.STRIDE * MAX_VERTICES;
private final Consumer<BakedQuad> quadConsumer;
int vertexIndex = 0;
int vertexIndex = -1;
private int[] quadData = new int[QUAD_DATA_SIZE];
private int tintIndex;
@@ -54,7 +55,8 @@ public class QuadBakingVertexConsumer implements VertexConsumer
@Override
public VertexConsumer addVertex(float x, float y, float z)
{
endVertex();
if (vertexIndex++ == MAX_VERTICES)
build();
int offset = vertexIndex * IQuadTransformer.STRIDE + IQuadTransformer.POSITION;
quadData[offset] = Float.floatToRawIntBits((float) x);
@@ -124,16 +126,14 @@ public class QuadBakingVertexConsumer implements VertexConsumer
return this;
}
private void endVertex()
public BakedQuad build()
{
if (vertexIndex != 4) {
++vertexIndex;
return;
}
BakedQuad quad = new BakedQuad(quadData, tintIndex, direction, sprite, shade, hasAmbientOcclusion);
// We have a full quad, pass it to the consumer and reset
quadConsumer.accept(new BakedQuad(quadData, tintIndex, direction, sprite, shade, hasAmbientOcclusion));
quadConsumer.accept(quad);
vertexIndex = 0;
quadData = new int[QUAD_DATA_SIZE];
return quad;
}
public void setTintIndex(int tintIndex)
@@ -0,0 +1,74 @@
package net.minecraftforge.debug.client;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.ModelEvent;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.client.model.data.ModelData;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
/**
* Tests loading and rendering an obj-model using the using Forge's {@link net.minecraftforge.client.model.obj.ObjLoader}.
* When enabled, this will render a pyramid loaded from an obj model at coordinates 0,0,0.
*/
@Mod(ObjModelTest.MODID)
public class ObjModelTest {
private static final boolean ENABLED = true;
public static final String MODID = "obj_model_test";
private static final PoseStack POSE_STACK = new PoseStack();
private static final ModelResourceLocation PYRAMID_MODEL_LOCATION = new ModelResourceLocation(ResourceLocation.fromNamespaceAndPath(MODID, "pyramid"), "");
private static BakedModel pyramidModel;
public ObjModelTest() {
if (ENABLED) {
FMLJavaModLoadingContext.get().getModEventBus().addListener(ObjModelTest::registerObjModel);
FMLJavaModLoadingContext.get().getModEventBus().addListener(ObjModelTest::collectObjModel);
MinecraftForge.EVENT_BUS.addListener(ObjModelTest::renderModel);
}
}
public static void registerObjModel(ModelEvent.RegisterAdditional e) {
e.register(PYRAMID_MODEL_LOCATION);
}
public static void collectObjModel(ModelEvent.BakingCompleted e) {
pyramidModel = e.getModels().get(PYRAMID_MODEL_LOCATION);
}
public static void renderModel(RenderLevelStageEvent e) {
if (e.getStage() == RenderLevelStageEvent.Stage.AFTER_SOLID_BLOCKS) {
POSE_STACK.setIdentity();
Vec3 camera = e.getCamera().getPosition();
//noinspection removal
POSE_STACK.mulPose(e.getPoseStack());
POSE_STACK.translate(0.5 - camera.x, 0.5 - camera.y, 0.5 - camera.z);
MultiBufferSource.BufferSource bufferSource = Minecraft.getInstance().renderBuffers().bufferSource();
// Render the obj model
Minecraft.getInstance().getBlockRenderer().getModelRenderer().renderModel(
POSE_STACK.last(),
bufferSource.getBuffer(RenderType.solid()),
null,
pyramidModel,
1,
1,
1,
LightTexture.FULL_BRIGHT,
OverlayTexture.NO_OVERLAY,
ModelData.EMPTY,
RenderType.solid()
);
bufferSource.endBatch();
}
}
}
@@ -0,0 +1,8 @@
{
"sources": [
{
"type": "single",
"resource": "obj_model_test:pyramid"
}
]
}
@@ -0,0 +1,8 @@
{
"loader": "forge:obj",
"model": "obj_model_test:models/pyramid.obj",
"flip-v": true,
"detectCullableFaces": false,
"diffuseLighting": false,
"ambientToFullbright": true
}
@@ -0,0 +1,4 @@
# Made in Blockbench 4.9.4
newmtl m_d61f9499-1bf7-4e99-8524-c1ff74261694
map_Kd obj_model_test:pyramid
newmtl none
@@ -0,0 +1,36 @@
# Made in Blockbench 4.9.4
mtllib pyramid.mtl
o pyramid
v 0 1 0
v 0.5 0 0.5
v 0.5 0 -0.5
v -0.5 0 0.5
v -0.5 0 -0.5
vt 0.6875 0.25
vt 0.6875 0
vt 0.4375 0
vt 0.4375 0.25
vt 0.3780786804403148 0.5791233118566227
vt 0.6062963195596852 0.6811855928167345
vt 0.6062963195596851 0.375
vt 0.003078680440314799 0.2041233118566227
vt 0.23129631955968522 0.3061855928167345
vt 0.23129631955968505 0
vt 0.19057868044031478 0.3916233118566227
vt 0.4187963195596852 0.4936855928167345
vt 0.41879631955968505 0.1875
vt 0.003078680440314799 0.6416233118566227
vt 0.23129631955968522 0.7436855928167345
vt 0.23129631955968505 0.4375
vn 0 -1 0
vn 0.8944271909999161 0.44721359549995804 0
vn 0 0.44721359549995804 0.8944271909999161
vn 0 0.44721359549995804 -0.8944271909999161
vn -0.8944271909999161 0.44721359549995804 0
usemtl m_d61f9499-1bf7-4e99-8524-c1ff74261694
f 3/1/1 2/2/1 4/3/1 5/4/1
f 2/5/2 3/6/2 1/7/2
f 4/8/3 2/9/3 1/10/3
f 3/11/4 5/12/4 1/13/4
f 5/14/5 4/15/5 1/16/5
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB