Very broken test of affine-transforming quads

This commit is contained in:
quat1024 2023-06-30 04:27:35 -04:00
parent 5c980183e3
commit e1408c5a7a
3 changed files with 68 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -8,6 +8,7 @@ import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.texture.Sprite; import net.minecraft.client.texture.Sprite;
import net.minecraft.client.util.SpriteIdentifier; import net.minecraft.client.util.SpriteIdentifier;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AffineTransformation;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random; import net.minecraft.util.math.random.Random;
import net.minecraft.world.BlockRenderView; import net.minecraft.world.BlockRenderView;
@ -16,14 +17,16 @@ import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
public final class SlopeBakedModel extends ForwardingBakedModel { public final class SlopeBakedModel extends ForwardingBakedModel {
public SlopeBakedModel(BakedModel baseModel, BlockState slopeState, Function<SpriteIdentifier, Sprite> spriteLookup) { public SlopeBakedModel(BakedModel baseModel, BlockState slopeState, AffineTransformation aff, Function<SpriteIdentifier, Sprite> spriteLookup) {
this.wrapped = baseModel; this.wrapped = baseModel;
this.preparer = new SlopeMeshTransformPreparer(spriteLookup); 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 TemplateQuadTransformPreparer preparer;
private final RenderContext.QuadTransform affineTransformer;
private final Mesh baseMesh; private final Mesh baseMesh;
@Override @Override
@ -33,15 +36,19 @@ public final class SlopeBakedModel extends ForwardingBakedModel {
@Override @Override
public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) { public void emitBlockQuads(BlockRenderView blockView, BlockState state, BlockPos pos, Supplier<Random> randomSupplier, RenderContext context) {
context.pushTransform(affineTransformer);
context.pushTransform(preparer.blockTransformer(blockView, state, pos, randomSupplier)); context.pushTransform(preparer.blockTransformer(blockView, state, pos, randomSupplier));
context.meshConsumer().accept(baseMesh); context.meshConsumer().accept(baseMesh);
context.popTransform(); context.popTransform();
context.popTransform();
} }
@Override @Override
public void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context) { public void emitItemQuads(ItemStack stack, Supplier<Random> randomSupplier, RenderContext context) {
context.pushTransform(affineTransformer);
context.pushTransform(preparer.itemTransformer(stack, randomSupplier)); context.pushTransform(preparer.itemTransformer(stack, randomSupplier));
context.meshConsumer().accept(baseMesh); context.meshConsumer().accept(baseMesh);
context.popTransform(); context.popTransform();
context.popTransform();
} }
} }

View File

@ -31,6 +31,7 @@ public record SlopeUnbakedModel(BlockState slopeState) implements UnbakedModel {
//TODO: weird, should use my own model instead //TODO: weird, should use my own model instead
BakedModel baseModel = baker.bake(BlockModels.getModelId(Blocks.SANDSTONE_STAIRS.getDefaultState()), modelBakeSettings); 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);
} }
} }