diff --git a/src/main/java/io/github/cottonmc/templates/model/AffineQuadTransformer.java b/src/main/java/io/github/cottonmc/templates/model/AffineQuadTransformer.java new file mode 100644 index 0000000..705e72e --- /dev/null +++ b/src/main/java/io/github/cottonmc/templates/model/AffineQuadTransformer.java @@ -0,0 +1,57 @@ +package io.github.cottonmc.templates.model; + +import net.fabricmc.fabric.api.renderer.v1.mesh.MutableQuadView; +import net.fabricmc.fabric.api.renderer.v1.render.RenderContext; +import net.minecraft.util.math.AffineTransformation; +import net.minecraft.util.math.AffineTransformations; +import net.minecraft.util.math.Direction; +import org.joml.Matrix4f; +import org.joml.Vector3f; +import org.joml.Vector4f; + +public record AffineQuadTransformer(Matrix4f affineMatrix) implements RenderContext.QuadTransform { + public AffineQuadTransformer(AffineTransformation aff) { + this(aff.getMatrix()); + } + + @Override + public boolean transform(MutableQuadView quad) { + Matrix4f mat = affineMatrix; + //TODO: Hard Coded for DEBUGGING + mat = ( + AffineTransformations.DIRECTION_ROTATIONS.get(Direction.NORTH) + ).getMatrix(); + + Vector3f pos3 = new Vector3f(); + //Vector3f norm3 = new Vector3f(); + + //ugh + Vector4f pos4 = new Vector4f(); + //Vector4f norm4 = new Vector4f(); + + for(int i = 0; i < 4; i++) { + //Copy quad data into vec3's + quad.copyPos(i, pos3); + //quad.copyNormal(i, norm3); + + pos3.add(-0.5f, -0.5f, -0.5f); //TODO HACK + + //Initialize the x/y/z components of vec4s using that data + //W component of normal vector is set to 1, normal vectors transform differently from points :) + pos4.set(pos3, 0); + //norm4.set(norm3, 1); + + //Compute the matrix-vector product. This function mutates the vec4 in-place. + //Note that `transformAffine` has the same purpose as `transform`; the difference is it + //assumes (without checking) that the last row of the matrix is 0,0,0,1, as an optimization + mat.transformAffine(pos4); + //mat.transformAffine(norm4); + + //Manually copy the data back onto the vertex + quad.pos(i, pos4.x + 0.5f, pos4.y + 0.5f, pos4.z + 0.5f); + //quad.normal(i, norm4.x, norm4.y + 1, norm4.z); + } + + return true; + } +} diff --git a/src/main/java/io/github/cottonmc/templates/model/SlopeBakedModel.java b/src/main/java/io/github/cottonmc/templates/model/SlopeBakedModel.java index dccbab5..7359bf2 100644 --- a/src/main/java/io/github/cottonmc/templates/model/SlopeBakedModel.java +++ b/src/main/java/io/github/cottonmc/templates/model/SlopeBakedModel.java @@ -8,6 +8,7 @@ import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.texture.Sprite; import net.minecraft.client.util.SpriteIdentifier; import net.minecraft.item.ItemStack; +import net.minecraft.util.math.AffineTransformation; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.random.Random; import net.minecraft.world.BlockRenderView; @@ -16,14 +17,16 @@ import java.util.function.Function; import java.util.function.Supplier; public final class SlopeBakedModel extends ForwardingBakedModel { - public SlopeBakedModel(BakedModel baseModel, BlockState slopeState, Function spriteLookup) { + public SlopeBakedModel(BakedModel baseModel, BlockState slopeState, AffineTransformation aff, Function spriteLookup) { this.wrapped = baseModel; this.preparer = new SlopeMeshTransformPreparer(spriteLookup); - this.baseMesh = SlopeBaseMesh.make(slopeState); + this.affineTransformer = new AffineQuadTransformer(aff); + this.baseMesh = SlopeBaseMesh.make(slopeState.getBlock().getDefaultState()); //TODO } private final TemplateQuadTransformPreparer preparer; + private final RenderContext.QuadTransform affineTransformer; private final Mesh baseMesh; @Override @@ -33,15 +36,19 @@ public final class SlopeBakedModel extends ForwardingBakedModel { @Override public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier randomSupplier, RenderContext context) { + context.pushTransform(affineTransformer); context.pushTransform(preparer.blockTransformer(blockView, state, pos, randomSupplier)); context.meshConsumer().accept(baseMesh); context.popTransform(); + context.popTransform(); } @Override public void emitItemQuads(ItemStack stack, Supplier randomSupplier, RenderContext context) { + context.pushTransform(affineTransformer); context.pushTransform(preparer.itemTransformer(stack, randomSupplier)); context.meshConsumer().accept(baseMesh); context.popTransform(); + context.popTransform(); } } diff --git a/src/main/java/io/github/cottonmc/templates/model/SlopeUnbakedModel.java b/src/main/java/io/github/cottonmc/templates/model/SlopeUnbakedModel.java index edc7489..bcea86c 100644 --- a/src/main/java/io/github/cottonmc/templates/model/SlopeUnbakedModel.java +++ b/src/main/java/io/github/cottonmc/templates/model/SlopeUnbakedModel.java @@ -31,6 +31,7 @@ public record SlopeUnbakedModel(BlockState slopeState) implements UnbakedModel { //TODO: weird, should use my own model instead BakedModel baseModel = baker.bake(BlockModels.getModelId(Blocks.SANDSTONE_STAIRS.getDefaultState()), modelBakeSettings); - return new SlopeBakedModel(baseModel, slopeState, function); + //TODO: ModelBakeSettings.getRotation is always the identity transform atm + return new SlopeBakedModel(baseModel, slopeState, modelBakeSettings.getRotation(), function); } }