From 3767390c9672224b61efeaee567b02d3532a6945 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Thu, 16 Dec 2021 00:11:47 -0800 Subject: [PATCH] Better SBB params - Transform interface for grouping traits and combined behavior - Move transforms to params, impl Transform - Pass Params object to BatchingTransformers instead of SBB --- .../api/struct/BatchingTransformer.java | 2 +- .../instancing/batching/CPUInstancer.java | 10 +- .../core/materials/model/ModelData.java | 16 +- .../flywheel/core/model/SuperByteBuffer.java | 340 ++++++++---------- .../flywheel/util/transform/Transform.java | 37 ++ 5 files changed, 213 insertions(+), 192 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/util/transform/Transform.java diff --git a/src/main/java/com/jozufozu/flywheel/api/struct/BatchingTransformer.java b/src/main/java/com/jozufozu/flywheel/api/struct/BatchingTransformer.java index 4b7e9f5d7..53e0dedb0 100644 --- a/src/main/java/com/jozufozu/flywheel/api/struct/BatchingTransformer.java +++ b/src/main/java/com/jozufozu/flywheel/api/struct/BatchingTransformer.java @@ -5,5 +5,5 @@ import com.jozufozu.flywheel.core.model.SuperByteBuffer; @FunctionalInterface public interface BatchingTransformer { - void transform(S s, SuperByteBuffer b); + void transform(S s, SuperByteBuffer.Params b); } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java index 38c5a7510..307f3315b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/batching/CPUInstancer.java @@ -36,10 +36,14 @@ public class CPUInstancer extends AbstractInstancer { renderSetup(); - for (D d : data) { - if (context.usesOverlay()) sbb.entityMode(); + if (context.usesOverlay()) { + sbb.getDefaultParams().entityMode(); + } - transform.transform(d, sbb); + sbb.reset(); + + for (D d : data) { + transform.transform(d, sbb.getParams()); sbb.renderInto(stack, buffer); } diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelData.java b/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelData.java index 0b5b2590a..f8ca670bd 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelData.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/model/ModelData.java @@ -1,8 +1,10 @@ package com.jozufozu.flywheel.core.materials.model; import com.jozufozu.flywheel.core.materials.BasicData; +import com.jozufozu.flywheel.core.model.SuperByteBuffer; import com.jozufozu.flywheel.util.transform.Rotate; import com.jozufozu.flywheel.util.transform.Scale; +import com.jozufozu.flywheel.util.transform.Transform; import com.jozufozu.flywheel.util.transform.Translate; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.math.Matrix3f; @@ -11,7 +13,7 @@ import com.mojang.math.Quaternion; import net.minecraft.util.Mth; -public class ModelData extends BasicData implements Translate, Rotate, Scale { +public class ModelData extends BasicData implements Transform { public final Matrix4f model = new Matrix4f(); public final Matrix3f normal = new Matrix3f(); @@ -83,4 +85,16 @@ public class ModelData extends BasicData implements Translate, Rotate model.multiplyWithTranslation((float) x, (float) y, (float) z); return this; } + + @Override + public ModelData mulPose(Matrix4f pose) { + this.model.multiply(pose); + return this; + } + + @Override + public ModelData mulNormal(Matrix3f normal) { + this.normal.mul(normal); + return this; + } } diff --git a/src/main/java/com/jozufozu/flywheel/core/model/SuperByteBuffer.java b/src/main/java/com/jozufozu/flywheel/core/model/SuperByteBuffer.java index 4d3aa6561..5135885b5 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/SuperByteBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/SuperByteBuffer.java @@ -1,10 +1,7 @@ package com.jozufozu.flywheel.core.model; import com.jozufozu.flywheel.util.ModelReader; -import com.jozufozu.flywheel.util.transform.Rotate; -import com.jozufozu.flywheel.util.transform.Scale; -import com.jozufozu.flywheel.util.transform.TStack; -import com.jozufozu.flywheel.util.transform.Translate; +import com.jozufozu.flywheel.util.transform.Transform; import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.VertexConsumer; import com.mojang.math.*; @@ -15,24 +12,27 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.LevelRenderer; import net.minecraft.client.renderer.LightTexture; import net.minecraft.client.renderer.texture.OverlayTexture; -import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.core.BlockPos; -import net.minecraft.core.Direction; import net.minecraft.util.Mth; import net.minecraft.world.level.Level; import net.minecraftforge.client.model.pipeline.LightUtil; -public class SuperByteBuffer implements Scale, Translate, Rotate, TStack { +public class SuperByteBuffer { private final Model model; private final ModelReader template; - // Vertex Position - private final PoseStack transforms; - private final Params defaultParams = Params.defaultParams(); private final Params params = defaultParams.copy(); + public Params getDefaultParams() { + return defaultParams; + } + + public Params getParams() { + return params; + } + // Temporary private static final Long2IntMap WORLD_LIGHT_CACHE = new Long2IntOpenHashMap(); private final Vector4f pos = new Vector4f(); @@ -42,9 +42,6 @@ public class SuperByteBuffer implements Scale, Translate, Translate, Translate, Translate> 16) & 0xFF); - params.g = ((color >> 8) & 0xFF); - params.b = (color & 0xFF); - params.a = 255; - return this; - } - - public SuperByteBuffer shiftUV(SpriteShiftFunc entry) { - params.spriteShiftFunc = entry; - return this; - } - - public SuperByteBuffer overlay() { - params.hasOverlay = true; - return this; - } - - public SuperByteBuffer overlay(int overlay) { - params.hasOverlay = true; - params.overlay = overlay; - return this; - } - - /** - * Transforms normals not only by the local matrix stack, but also by the passed matrix stack. - */ - public SuperByteBuffer entityMode() { - params.hasOverlay = true; - params.fullNormalTransform = true; - params.diffuseMode = DiffuseMode.NONE; - params.colorMode = ColorMode.RECOLOR; - return this; - } - - public SuperByteBuffer light() { - params.useWorldLight = true; - return this; - } - - public SuperByteBuffer light(Matrix4f lightTransform) { - params.useWorldLight = true; - params.lightTransform = lightTransform; - return this; - } - - public SuperByteBuffer light(int packedLightCoords) { - params.hasCustomLight = true; - params.packedLightCoords = packedLightCoords; - return this; - } - - public SuperByteBuffer light(Matrix4f lightTransform, int packedLightCoords) { - light(lightTransform); - light(packedLightCoords); - return this; - } - - /** - * Uses max light from calculated light (world light or custom light) and vertex light for the final light value. - * Ineffective if any other light method was not called. - */ - public SuperByteBuffer hybridLight() { - params.hybridLight = true; - return this; - } - public boolean isEmpty() { return template.isEmpty(); } - @Override - public SuperByteBuffer scale(float factorX, float factorY, float factorZ) { - transforms.scale(factorX, factorY, factorZ); - return this; - } - - @Override - public SuperByteBuffer pushPose() { - transforms.pushPose(); - return this; - } - - @Override - public SuperByteBuffer popPose() { - transforms.popPose(); - return this; - } - @Override public String toString() { return "SuperByteBuffer[" + model + ']'; @@ -375,7 +223,11 @@ public class SuperByteBuffer implements Scale, Translate { + // Vertex Position + public final Matrix4f model = new Matrix4f(); + public final Matrix3f normal = new Matrix3f(); + // Vertex Coloring public ColorMode colorMode = ColorMode.DIFFUSE_ONLY; public DiffuseMode diffuseMode = DiffuseMode.INSTANCE; @@ -402,6 +254,8 @@ public class SuperByteBuffer implements Scale, Translate, Translate> 16) & 0xFF); + this.g = ((color >> 8) & 0xFF); + this.b = (color & 0xFF); + this.a = 255; + return this; + } + + public Params shiftUV(SpriteShiftFunc entry) { + this.spriteShiftFunc = entry; + return this; + } + + public Params overlay() { + this.hasOverlay = true; + return this; + } + + public Params overlay(int overlay) { + this.hasOverlay = true; + this.overlay = overlay; + return this; + } + + /** + * Transforms normals not only by the local matrix stack, but also by the passed matrix stack. + */ + public Params entityMode() { + this.hasOverlay = true; + this.fullNormalTransform = true; + this.diffuseMode = DiffuseMode.NONE; + this.colorMode = ColorMode.RECOLOR; + return this; + } + + public Params light() { + this.useWorldLight = true; + return this; + } + + public Params light(Matrix4f lightTransform) { + this.useWorldLight = true; + this.lightTransform = lightTransform; + return this; + } + + public Params light(int packedLightCoords) { + this.hasCustomLight = true; + this.packedLightCoords = packedLightCoords; + return this; + } + + public Params light(Matrix4f lightTransform, int packedLightCoords) { + light(lightTransform); + light(packedLightCoords); + return this; + } + + /** + * Uses max light from calculated light (world light or custom light) and vertex light for the final light value. + * Ineffective if any other light method was not called. + */ + public Params hybridLight() { + hybridLight = true; + return this; + } + + @Override + public Params multiply(Quaternion quaternion) { + model.multiply(quaternion); + return this; + } + + @Override + public Params scale(float pX, float pY, float pZ) { + model.multiply(Matrix4f.createScaleMatrix(pX, pY, pZ)); + if (pX == pY && pY == pZ) { + if (pX > 0.0F) { + return this; + } + + normal.mul(-1.0F); + } + + float f = 1.0F / pX; + float f1 = 1.0F / pY; + float f2 = 1.0F / pZ; + float f3 = Mth.fastInvCubeRoot(f * f1 * f2); + normal.mul(Matrix3f.createScaleMatrix(f3 * f, f3 * f1, f3 * f2)); + return this; + } + + @Override + public Params translate(double x, double y, double z) { + model.multiplyWithTranslation((float) x, (float) y, (float) z); + + return this; + } + + @Override + public Params mulPose(Matrix4f pose) { + this.model.multiply(pose); + return this; + } + + @Override + public Params mulNormal(Matrix3f normal) { + this.normal.mul(normal); + return this; + } + public static Params defaultParams() { Params out = new Params(); + out.model.setIdentity(); + out.normal.setIdentity(); out.colorMode = ColorMode.DIFFUSE_ONLY; out.diffuseMode = DiffuseMode.INSTANCE; out.r = 0xFF; @@ -444,25 +430,5 @@ public class SuperByteBuffer implements Scale, Translate> extends Translate, Rotate, Scale { + Self mulPose(Matrix4f pose); + + Self mulNormal(Matrix3f normal); + + default Self transform(Matrix4f pose, Matrix3f normal) { + mulPose(pose); + return mulNormal(normal); + } + + default Self transform(PoseStack stack) { + PoseStack.Pose last = stack.last(); + return transform(last.pose(), last.normal()); + } + + default Self rotateCentered(Direction axis, float radians) { + translate(.5f, .5f, .5f).rotate(axis, radians) + .translate(-.5f, -.5f, -.5f); + return (Self) this; + } + + default Self rotateCentered(Quaternion q) { + translate(.5f, .5f, .5f).multiply(q) + .translate(-.5f, -.5f, -.5f); + return (Self) this; + } +}