From 30cc011ee997cb14cf80bebc9d6aee4da6372a33 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 10 Jul 2021 11:33:01 -0700 Subject: [PATCH] Lids everywhere - Ender chests and trapped chests, too - Chests orient themselves correctly - Lids do not position themselves correctly - Some math, will probably replace with JOML --- build.gradle | 2 + .../instancing/InstancedRenderRegistry.java | 2 +- .../flywheel/core/materials/OrientedData.java | 11 +++ .../com/jozufozu/flywheel/util/vec/Vec3.java | 77 +++++++++++++++ .../com/jozufozu/flywheel/util/vec/Vec4.java | 66 +++++++++++++ .../flywheel/vanilla/ChestInstance.java | 96 +++++++++++++++++-- .../flywheel/vanilla/VanillaInstances.java | 10 +- 7 files changed, 256 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/util/vec/Vec3.java create mode 100644 src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java diff --git a/build.gradle b/build.gradle index 5a9e3670f..72e97efaf 100644 --- a/build.gradle +++ b/build.gradle @@ -99,6 +99,8 @@ repositories { dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" + //implementation "org.joml:joml:1.10.1" + annotationProcessor 'org.spongepowered:mixin:0.8.2:processor' } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java index f30a2aae5..d6d107dfa 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java @@ -101,7 +101,7 @@ public class InstancedRenderRegistry { return this; } - public InstancedRenderRegistry build() { + public InstancedRenderRegistry register() { tiles.put(type, factory); InstancedRenderRegistry.this.skipRender.put(type, skipRender); diff --git a/src/main/java/com/jozufozu/flywheel/core/materials/OrientedData.java b/src/main/java/com/jozufozu/flywheel/core/materials/OrientedData.java index 1541b42b4..92e8f8a7c 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/OrientedData.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/OrientedData.java @@ -3,6 +3,8 @@ package com.jozufozu.flywheel.core.materials; import com.jozufozu.flywheel.backend.gl.buffer.MappedBuffer; import com.jozufozu.flywheel.backend.instancing.Instancer; +import com.jozufozu.flywheel.util.vec.Vec3; + import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.vector.Quaternion; import net.minecraft.util.math.vector.Vector3d; @@ -41,6 +43,11 @@ public class OrientedData extends BasicData { return this; } + + public OrientedData nudge(Vec3 pos) { + return nudge(pos.getX(), pos.getY(), pos.getZ()); + } + public OrientedData nudge(float x, float y, float z) { this.posX += x; this.posY += y; @@ -57,6 +64,10 @@ public class OrientedData extends BasicData { return setPosition((float) pos.getX(), (float) pos.getY(), (float) pos.getZ()); } + public OrientedData setPivot(Vec3 pos) { + return setPivot(pos.getX(), pos.getY(), pos.getZ()); + } + public OrientedData setPivot(float x, float y, float z) { this.pivotX = x; this.pivotY = y; diff --git a/src/main/java/com/jozufozu/flywheel/util/vec/Vec3.java b/src/main/java/com/jozufozu/flywheel/util/vec/Vec3.java new file mode 100644 index 000000000..2a7d1a8ae --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/vec/Vec3.java @@ -0,0 +1,77 @@ +package com.jozufozu.flywheel.util.vec; + +import net.minecraft.util.math.vector.Quaternion; +import net.minecraft.util.math.vector.Vector3f; + +public class Vec3 { + public static final Vec3 NEGATIVE_X = new Vec3(-1.0F, 0.0F, 0.0F); + public static final Vec3 POSITIVE_X = new Vec3(1.0F, 0.0F, 0.0F); + public static final Vec3 NEGATIVE_Y = new Vec3(0.0F, -1.0F, 0.0F); + public static final Vec3 POSITIVE_Y = new Vec3(0.0F, 1.0F, 0.0F); + public static final Vec3 NEGATIVE_Z = new Vec3(0.0F, 0.0F, -1.0F); + public static final Vec3 POSITIVE_Z = new Vec3(0.0F, 0.0F, 1.0F); + + private float x; + private float y; + private float z; + + public Vec3(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + + public float getX() { + return x; + } + + public float getY() { + return y; + } + + public float getZ() { + return z; + } + + public Vec3 multiply(Quaternion quat) { + Vec4 vec4 = new Vec4(this, 1f); + + vec4.multiply(quat); + + set(vec4.getX(), vec4.getY(), vec4.getZ()); + return this; + } + + public Vec3 copy() { + return new Vec3(x, y, z); + } + + public Vector3f convert() { + return new Vector3f(x, y, z); + } + + public void set(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + + public Vec3 add(Vec3 v) { + add(v.x, v.y, v.z); + return this; + } + + public Vec3 add(float x, float y, float z) { + this.x += x; + this.y += y; + this.z += z; + return this; + } + + public Vec3 sub(float x, float y, float z) { + this.x -= x; + this.y -= y; + this.z -= z; + return this; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java b/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java new file mode 100644 index 000000000..2fed35813 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java @@ -0,0 +1,66 @@ +package com.jozufozu.flywheel.util.vec; + +import net.minecraft.util.math.vector.Quaternion; +import net.minecraft.util.math.vector.Vector3f; + +public class Vec4 { + + private float x; + private float y; + private float z; + private float w; + + public Vec4(float x, float y, float z, float w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } + + public Vec4(Vec3 vec3) { + this(vec3, 0); + } + + public Vec4(Vec3 vec3, float w) { + this.x = vec3.getX(); + this.y = vec3.getY(); + this.z = vec3.getZ(); + this.w = w; + } + + public void multiply(Quaternion quat) { + Quaternion quaternion = new Quaternion(quat); + quaternion.multiply(new Quaternion(this.getX(), this.getY(), this.getZ(), 0.0F)); + Quaternion quaternion1 = new Quaternion(quat); + quaternion1.conjugate(); + quaternion.multiply(quaternion1); + this.set(quaternion.getX(), quaternion.getY(), quaternion.getZ(), this.getW()); + } + + public Vec3 xyz() { + return new Vec3(x, y, z); + } + + public float getX() { + return x; + } + + public float getY() { + return y; + } + + public float getZ() { + return z; + } + + public float getW() { + return w; + } + + public void set(float x, float y, float z, float w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java index 9a7831b0c..0e63f8f9d 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -11,6 +11,10 @@ import com.jozufozu.flywheel.core.materials.OrientedData; import com.jozufozu.flywheel.core.model.ModelPart; import com.jozufozu.flywheel.util.AnimationTickHolder; +import com.jozufozu.flywheel.util.vec.Vec3; +import com.jozufozu.flywheel.util.vec.Vec4; +import com.mojang.blaze3d.matrix.MatrixStack; + import it.unimi.dsi.fastutil.floats.Float2FloatFunction; import net.minecraft.block.Block; import net.minecraft.block.ChestBlock; @@ -18,26 +22,34 @@ import net.minecraft.client.renderer.Atlases; import net.minecraft.client.renderer.model.RenderMaterial; import net.minecraft.state.properties.ChestType; import net.minecraft.tileentity.ChestTileEntity; +import net.minecraft.tileentity.IChestLid; +import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntityMerger; import net.minecraft.util.math.vector.Quaternion; import net.minecraft.util.math.vector.Vector3f; +import net.minecraft.util.math.vector.Vector4f; + +import javax.annotation.Nonnull; import java.util.Calendar; -public class ChestInstance extends TileEntityInstance implements IDynamicInstance { +public class ChestInstance extends TileEntityInstance implements IDynamicInstance { private final OrientedData body; private final OrientedData lid; private final Float2FloatFunction lidProgress; private final RenderMaterial renderMaterial; + @Nonnull + private final ChestType chestType; + private final Quaternion baseRotation; - public ChestInstance(MaterialManager materialManager, ChestTileEntity tile) { + public ChestInstance(MaterialManager materialManager, T tile) { super(materialManager, tile); Block block = blockState.getBlock(); - ChestType chestType = blockState.contains(ChestBlock.TYPE) ? blockState.get(ChestBlock.TYPE) : ChestType.SINGLE; + chestType = blockState.contains(ChestBlock.TYPE) ? blockState.get(ChestBlock.TYPE) : ChestType.SINGLE; renderMaterial = Atlases.getChestTexture(tile, chestType, isChristmas()); body = baseInstance() @@ -48,6 +60,15 @@ public class ChestInstance extends TileEntityInstance implement if (block instanceof ChestBlock) { +// MatrixStack stack = new MatrixStack(); +// +// stack.push(); + float horizontalAngle = blockState.get(ChestBlock.FACING).getHorizontalAngle(); + + baseRotation = Vector3f.POSITIVE_Y.getDegreesQuaternion(-horizontalAngle); + + body.setRotation(baseRotation); + ChestBlock chestBlock = (ChestBlock) block; TileEntityMerger.ICallbackWrapper wrapper = chestBlock.getBlockEntitySource(blockState, world, getWorldPosition(), true); @@ -56,6 +77,7 @@ public class ChestInstance extends TileEntityInstance implement } else { + baseRotation = Quaternion.IDENTITY; lidProgress = $ -> 0f; } } @@ -69,10 +91,22 @@ public class ChestInstance extends TileEntityInstance implement float angleX = -(progress * ((float) Math.PI / 2F)); - Quaternion quaternion = new Quaternion(Vector3f.POSITIVE_X, angleX, false); + + Vec3 axis = Vec3.POSITIVE_X.copy(); + Vec3 pivot = new Vec3(0, 0, 1f / 16f); + pivot.add(0.5f, 0.5f, 0.5f) + .multiply(baseRotation) + .sub(0.5f, 0.5f, 0.5f); + axis.multiply(baseRotation); + + Quaternion quaternion = new Quaternion(axis.convert(), angleX, false); + + quaternion.multiply(baseRotation); lid.setRotation(quaternion) - .setPivot(0, 0, 1f / 16f); + .setPosition(getInstancePosition()) + .nudge(0, 9f/16f, 0) + .setPivot(pivot); } @@ -103,6 +137,27 @@ public class ChestInstance extends TileEntityInstance implement private BufferedModel getBaseModel() { + switch (chestType) { + case LEFT: + return ModelPart.builder(64, 64) + .sprite(renderMaterial.getSprite()) + .cuboid() + .textureOffset(0, 19) + .start(0, 0, 1) + .size(15, 10, 14) + .endCuboid() + .build(); + case RIGHT: + return ModelPart.builder(64, 64) + .sprite(renderMaterial.getSprite()) + .cuboid() + .textureOffset(0, 19) + .start(1, 0, 1) + .size(15, 10, 14) + .endCuboid() + .build(); + } + return ModelPart.builder(64, 64) .sprite(renderMaterial.getSprite()) .cuboid() @@ -115,12 +170,41 @@ public class ChestInstance extends TileEntityInstance implement private BufferedModel getLidModel() { + switch (chestType) { + case LEFT: + return ModelPart.builder(64, 64) + .sprite(renderMaterial.getSprite()) + .cuboid() + .textureOffset(0, 0) + .start(0, 0, 1) + .size(15, 5, 14) + .endCuboid() + .cuboid() + .start(0, -2, 15) + .size(1, 4, 1) + .endCuboid() + .build(); + case RIGHT: return ModelPart.builder(64, 64) .sprite(renderMaterial.getSprite()) .cuboid() .textureOffset(0, 0) .start(1, 0, 1) - .end(15, 5, 15) + .size(15, 5, 14) + .endCuboid() + .cuboid() + .start(15, -2, 15) + .size(1, 4, 1) + .endCuboid() + .build(); + } + + return ModelPart.builder(64, 64) + .sprite(renderMaterial.getSprite()) + .cuboid() + .textureOffset(0, 0) + .start(1, 0, 1) + .size(14, 5, 14) .endCuboid() .cuboid() .start(7, -2, 15) diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java b/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java index fd2aae00d..7b7628b2d 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java @@ -12,6 +12,14 @@ public class VanillaInstances { r.tile(TileEntityType.CHEST) .setSkipRender(true) .factory(ChestInstance::new) - .build(); + .register(); + r.tile(TileEntityType.ENDER_CHEST) + .setSkipRender(true) + .factory(ChestInstance::new) + .register(); + r.tile(TileEntityType.TRAPPED_CHEST) + .setSkipRender(true) + .factory(ChestInstance::new) + .register(); } }