From 60d7ca6e916b06f129ac752526931f178ec1a3fc Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Fri, 9 Jul 2021 13:24:26 -0700 Subject: [PATCH 01/12] 4d chest - Refactor InstancedRenderRegistry to allow for 3rd party TEs to have instances - Add ChestInstance, incomplete but there - Add basic api for building vanilla formatted models - Better instance factory registration --- .../com/jozufozu/flywheel/FlywheelClient.java | 4 + .../backend/instancing/InstanceManager.java | 18 +- .../instancing/InstancedRenderRegistry.java | 91 +++++++- .../entity/EntityInstanceManager.java | 6 + .../instancing/tile/TileInstanceManager.java | 5 + .../flywheel/core/materials/OrientedData.java | 2 +- .../flywheel/core/model/ModelPart.java | 8 + .../flywheel/core/model/PartBuilder.java | 199 ++++++++++++++++++ .../flywheel/core/model/Readable.java | 118 +++++++++++ .../mixin/CancelEntityRenderMixin.java | 11 +- .../mixin/CancelTileEntityRenderMixin.java | 6 +- .../jozufozu/flywheel/util/RenderMath.java | 8 + .../transform/QuaternionTransformStack.java | 97 +++++++++ .../util/transform/TransformStack.java | 80 +++++++ .../flywheel/vanilla/ChestInstance.java | 136 ++++++++++++ .../flywheel/vanilla/VanillaInstances.java | 17 ++ 16 files changed, 788 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java create mode 100644 src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java create mode 100644 src/main/java/com/jozufozu/flywheel/core/model/Readable.java create mode 100644 src/main/java/com/jozufozu/flywheel/util/RenderMath.java create mode 100644 src/main/java/com/jozufozu/flywheel/util/transform/QuaternionTransformStack.java create mode 100644 src/main/java/com/jozufozu/flywheel/util/transform/TransformStack.java create mode 100644 src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java create mode 100644 src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java diff --git a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java index 068f6ea31..98ecf0473 100644 --- a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java +++ b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java @@ -5,6 +5,8 @@ import com.jozufozu.flywheel.core.AtlasStitcher; import com.jozufozu.flywheel.core.Contexts; import com.jozufozu.flywheel.core.Materials; +import com.jozufozu.flywheel.vanilla.VanillaInstances; + import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @@ -20,5 +22,7 @@ public class FlywheelClient { modEventBus.addListener(Contexts::flwInit); modEventBus.addListener(Materials::flwInit); + + VanillaInstances.init(); } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java index 2c6a24f14..984bfaf37 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java @@ -40,6 +40,12 @@ public abstract class InstanceManager implements MaterialManager.OriginShiftL materialManager.addListener(this); } + protected abstract boolean canInstance(T obj); + + protected abstract IInstance createRaw(T obj); + + protected abstract boolean canCreateInstance(T entity); + public void tick(double cameraX, double cameraY, double cameraZ) { tick++; @@ -100,7 +106,7 @@ public abstract class InstanceManager implements MaterialManager.OriginShiftL if (!Backend.getInstance() .canUseInstancing()) return; - if (obj instanceof IInstanceRendered) { + if (canInstance(obj)) { addInternal(obj); } } @@ -116,7 +122,7 @@ public abstract class InstanceManager implements MaterialManager.OriginShiftL if (!Backend.getInstance() .canUseInstancing()) return; - if (obj instanceof IInstanceRendered) { + if (canInstance(obj)) { IInstance instance = getInstance(obj, false); if (instance != null) { @@ -143,7 +149,7 @@ public abstract class InstanceManager implements MaterialManager.OriginShiftL if (!Backend.getInstance() .canUseInstancing()) return; - if (obj instanceof IInstanceRendered) { + if (canInstance(obj)) { IInstance instance = getInstance(obj, false); if (instance != null) instance.updateLight(); @@ -154,7 +160,7 @@ public abstract class InstanceManager implements MaterialManager.OriginShiftL if (!Backend.getInstance() .canUseInstancing()) return; - if (obj instanceof IInstanceRendered) { + if (canInstance(obj)) { IInstance instance = getInstance(obj, false); if (instance != null) removeInternal(obj, instance); } @@ -241,8 +247,4 @@ public abstract class InstanceManager implements MaterialManager.OriginShiftL invalidate(); instancedTiles.forEach(this::add); } - - protected abstract IInstance createRaw(T obj); - - protected abstract boolean canCreateInstance(T entity); } 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 31f2b5611..f30a2aae5 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java @@ -10,6 +10,8 @@ import com.jozufozu.flywheel.backend.instancing.entity.IEntityInstanceFactory; import com.jozufozu.flywheel.backend.instancing.tile.ITileInstanceFactory; import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; +import it.unimi.dsi.fastutil.objects.Object2BooleanLinkedOpenHashMap; +import it.unimi.dsi.fastutil.objects.Object2BooleanMap; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityType; import net.minecraft.tileentity.TileEntity; @@ -22,15 +24,36 @@ public class InstancedRenderRegistry { return INSTANCE; } + private final Object2BooleanMap skipRender = new Object2BooleanLinkedOpenHashMap<>(); private final Map, ITileInstanceFactory> tiles = Maps.newHashMap(); private final Map, IEntityInstanceFactory> entities = Maps.newHashMap(); - public void register(TileEntityType type, ITileInstanceFactory rendererFactory) { - this.tiles.put(type, rendererFactory); + protected InstancedRenderRegistry() { + skipRender.defaultReturnValue(false); } - public void register(EntityType type, IEntityInstanceFactory rendererFactory) { - this.entities.put(type, rendererFactory); + public boolean shouldSkipRender(T type) { + return _skipRender(type.getType()) || ((type instanceof IInstanceRendered) && !((IInstanceRendered) type).shouldRenderNormally()); + } + + public boolean shouldSkipRender(T type) { + return _skipRender(type.getType()) || ((type instanceof IInstanceRendered) && !((IInstanceRendered) type).shouldRenderNormally()); + } + + public boolean canInstance(TileEntityType type) { + return tiles.containsKey(type); + } + + public boolean canInstance(EntityType type) { + return entities.containsKey(type); + } + + public TileRegistrater tile(TileEntityType type) { + return new TileRegistrater<>(type); + } + + public EntityRegistrater entity(EntityType type) { + return new EntityRegistrater<>(type); } @SuppressWarnings("unchecked") @@ -54,4 +77,64 @@ public class InstancedRenderRegistry { else return factory.create(manager, tile); } + private boolean _skipRender(Object o) { + return skipRender.getBoolean(o); + } + + public class TileRegistrater { + + private final TileEntityType type; + private ITileInstanceFactory factory; + private boolean skipRender = false; + + public TileRegistrater(TileEntityType type) { + this.type = type; + } + + public TileRegistrater factory(ITileInstanceFactory rendererFactory) { + factory = rendererFactory; + return this; + } + + public TileRegistrater setSkipRender(boolean skipRender) { + this.skipRender = skipRender; + return this; + } + + public InstancedRenderRegistry build() { + tiles.put(type, factory); + InstancedRenderRegistry.this.skipRender.put(type, skipRender); + + return InstancedRenderRegistry.this; + } + } + + public class EntityRegistrater { + + private final EntityType type; + private IEntityInstanceFactory factory; + private boolean skipRender = false; + + public EntityRegistrater(EntityType type) { + this.type = type; + } + + public EntityRegistrater factory(IEntityInstanceFactory rendererFactory) { + factory = rendererFactory; + return this; + } + + public EntityRegistrater setSkipRender(boolean skipRender) { + this.skipRender = skipRender; + return this; + } + + public InstancedRenderRegistry build() { + entities.put(type, factory); + InstancedRenderRegistry.this.skipRender.put(type, skipRender); + + return InstancedRenderRegistry.this; + } + } + } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java index 55d52e66e..a2b5bc186 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java @@ -7,6 +7,7 @@ import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import com.jozufozu.flywheel.backend.instancing.MaterialManager; import net.minecraft.entity.Entity; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockReader; import net.minecraft.world.World; @@ -17,6 +18,11 @@ public class EntityInstanceManager extends InstanceManager { super(materialManager); } + @Override + protected boolean canInstance(Entity obj) { + return obj != null && InstancedRenderRegistry.getInstance().canInstance(obj.getType()); + } + @Override protected IInstance createRaw(Entity obj) { return InstancedRenderRegistry.getInstance() diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/tile/TileInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/tile/TileInstanceManager.java index 4c8f3079a..07b957ac7 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/tile/TileInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/tile/TileInstanceManager.java @@ -17,6 +17,11 @@ public class TileInstanceManager extends InstanceManager { super(materialManager); } + @Override + protected boolean canInstance(TileEntity obj) { + return obj != null && InstancedRenderRegistry.getInstance().canInstance(obj.getType()); + } + @Override protected IInstance createRaw(TileEntity obj) { return InstancedRenderRegistry.getInstance() 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 2d9cca280..1541b42b4 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/OrientedData.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/OrientedData.java @@ -19,7 +19,7 @@ public class OrientedData extends BasicData { private float qX; private float qY; private float qZ; - private float qW; + private float qW = 1; public OrientedData(Instancer owner) { super(owner); diff --git a/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java b/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java new file mode 100644 index 000000000..05ec25cdf --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/model/ModelPart.java @@ -0,0 +1,8 @@ +package com.jozufozu.flywheel.core.model; + +public class ModelPart { + + public static PartBuilder builder(int sizeU, int sizeV) { + return new PartBuilder(sizeU, sizeV); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java new file mode 100644 index 000000000..178b34f5f --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java @@ -0,0 +1,199 @@ +package com.jozufozu.flywheel.core.model; + +import static com.jozufozu.flywheel.util.RenderMath.*; + +import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; +import com.jozufozu.flywheel.backend.model.BufferedModel; + +import com.jozufozu.flywheel.backend.model.IndexedModel; + +import com.jozufozu.flywheel.core.Formats; + +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.util.Direction; +import net.minecraft.util.math.vector.Vector3f; + +import java.util.ArrayList; +import java.util.EnumSet; +import java.util.List; +import java.util.Set; + +public class PartBuilder { + + private float sizeU = 64.0F; + private float sizeV = 32.0F; + + private TextureAtlasSprite sprite; + + private final List cuboids = new ArrayList<>(); + + public PartBuilder() { } + + public PartBuilder(int sizeU, int sizeV) { + this.setTextureSize(sizeU, sizeV); + } + + public PartBuilder setTextureSize(int textureWidth, int textureHeight) { + this.sizeU = (float)textureWidth; + this.sizeV = (float)textureHeight; + return this; + } + + public PartBuilder sprite(TextureAtlasSprite sprite) { + this.sprite = sprite; + return this; + } + + public CuboidBuilder cuboid() { + return new CuboidBuilder(this); + } + + public BufferedModel build() { + int vertices = 0; + + for (CuboidBuilder cuboid : cuboids) { + vertices += cuboid.vertices(); + } + + VecBuffer buffer = VecBuffer.allocate(vertices * Formats.UNLIT_MODEL.getStride()); + + for (CuboidBuilder cuboid : cuboids) { + cuboid.buffer(buffer); + } + + buffer.rewind(); + + return IndexedModel.fromSequentialQuads(Formats.UNLIT_MODEL, buffer.unwrap(), vertices); + } + + private PartBuilder addCuboid(CuboidBuilder builder) { + cuboids.add(builder); + return this; + } + + public static class CuboidBuilder { + + TextureAtlasSprite sprite; + + Set visibleFaces = EnumSet.allOf(Direction.class); + int textureOffsetU; + int textureOffsetV; + + float posX1; + float posY1; + float posZ1; + float posX2; + float posY2; + float posZ2; + + final PartBuilder partBuilder; + + CuboidBuilder(PartBuilder partBuilder) { + this.partBuilder = partBuilder; + this.sprite = partBuilder.sprite; + } + + public CuboidBuilder textureOffset(int u, int v) { + this.textureOffsetU = u; + this.textureOffsetV = v; + return this; + } + + public CuboidBuilder start(float x, float y, float z) { + this.posX1 = x; + this.posY1 = y; + this.posZ1 = z; + return this; + } + + public CuboidBuilder end(float x, float y, float z) { + this.posX2 = x; + this.posY2 = y; + this.posZ2 = z; + return this; + } + + public CuboidBuilder size(float x, float y, float z) { + this.posX2 = posX1 + x; + this.posY2 = posY1 + y; + this.posZ2 = posZ1 + z; + return this; + } + + public CuboidBuilder sprite(TextureAtlasSprite sprite) { + this.sprite = sprite; + return this; + } + + public PartBuilder endCuboid() { + return partBuilder.addCuboid(this); + } + + public int vertices() { + return visibleFaces.size() * 4; + } + + public void buffer(VecBuffer buffer) { + + float sizeX = posX2 - posX1; + float sizeY = posY2 - posY1; + float sizeZ = posZ2 - posZ1; + + Vector3f lll = new Vector3f(posX1 / 16f, posY1 / 16f, posZ1 / 16f); + Vector3f hll = new Vector3f(posX2 / 16f, posY1 / 16f, posZ1 / 16f); + Vector3f hhl = new Vector3f(posX2 / 16f, posY2 / 16f, posZ1 / 16f); + Vector3f lhl = new Vector3f(posX1 / 16f, posY2 / 16f, posZ1 / 16f); + Vector3f llh = new Vector3f(posX1 / 16f, posY1 / 16f, posZ2 / 16f); + Vector3f hlh = new Vector3f(posX2 / 16f, posY1 / 16f, posZ2 / 16f); + Vector3f hhh = new Vector3f(posX2 / 16f, posY2 / 16f, posZ2 / 16f); + Vector3f lhh = new Vector3f(posX1 / 16f, posY2 / 16f, posZ2 / 16f); + float f4 = getU((float)textureOffsetU); + float f5 = getU((float)textureOffsetU + sizeZ); + float f6 = getU((float)textureOffsetU + sizeZ + sizeX); + float f7 = getU((float)textureOffsetU + sizeZ + sizeX + sizeX); + float f8 = getU((float)textureOffsetU + sizeZ + sizeX + sizeZ); + float f9 = getU((float)textureOffsetU + sizeZ + sizeX + sizeZ + sizeX); + float f10 = getV((float)textureOffsetV); + float f11 = getV((float)textureOffsetV + sizeZ); + float f12 = getV((float)textureOffsetV + sizeZ + sizeY); + + + float textureWidth = partBuilder.sizeU; + float textureHeight = partBuilder.sizeV; + quad(buffer, new Vector3f[]{hlh, llh, lll, hll}, f5, f10, f6, f11, textureWidth, textureHeight, Direction.DOWN); + quad(buffer, new Vector3f[]{hhl, lhl, lhh, hhh}, f6, f11, f7, f10, textureWidth, textureHeight, Direction.UP); + quad(buffer, new Vector3f[]{lll, llh, lhh, lhl}, f4, f11, f5, f12, textureWidth, textureHeight, Direction.WEST); + quad(buffer, new Vector3f[]{hll, lll, lhl, hhl}, f5, f11, f6, f12, textureWidth, textureHeight, Direction.NORTH); + quad(buffer, new Vector3f[]{hlh, hll, hhl, hhh}, f6, f11, f8, f12, textureWidth, textureHeight, Direction.EAST); + quad(buffer, new Vector3f[]{llh, hlh, hhh, lhh}, f8, f11, f9, f12, textureWidth, textureHeight, Direction.SOUTH); + } + + + public void quad(VecBuffer buffer, Vector3f[] vertices, float minU, float minV, float maxU, float maxV, float texWidth, float texHeight, Direction dir) { + + Vector3f normal = dir.getUnitVector(); + + buffer.putVec3(vertices[0].getX(), vertices[0].getY(), vertices[0].getZ()).putVec3(nb(normal.getX()), nb(normal.getY()), nb(normal.getZ())).putVec2(maxU, minV); + buffer.putVec3(vertices[1].getX(), vertices[1].getY(), vertices[1].getZ()).putVec3(nb(normal.getX()), nb(normal.getY()), nb(normal.getZ())).putVec2(minU, minV); + buffer.putVec3(vertices[2].getX(), vertices[2].getY(), vertices[2].getZ()).putVec3(nb(normal.getX()), nb(normal.getY()), nb(normal.getZ())).putVec2(minU, maxV); + buffer.putVec3(vertices[3].getX(), vertices[3].getY(), vertices[3].getZ()).putVec3(nb(normal.getX()), nb(normal.getY()), nb(normal.getZ())).putVec2(maxU, maxV); + + } + + public float getU(float u) { + if (sprite != null) + return sprite.getInterpolatedU(u / 4.); + else + return u; + } + + public float getV(float v) { + if (sprite != null) + return sprite.getInterpolatedV(v / 4.); + else + return v; + } + } + + +} diff --git a/src/main/java/com/jozufozu/flywheel/core/model/Readable.java b/src/main/java/com/jozufozu/flywheel/core/model/Readable.java new file mode 100644 index 000000000..6d6d569b1 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/model/Readable.java @@ -0,0 +1,118 @@ +package com.jozufozu.flywheel.core.model; + +import net.minecraft.util.Direction; +import net.minecraft.util.math.vector.Vector3f; + +public class Readable { + public static class ModelBox { + private final TexturedQuad[] quads; + public final float posX1; + public final float posY1; + public final float posZ1; + public final float posX2; + public final float posY2; + public final float posZ2; + + public ModelBox(int texOffU, int texOffV, float posX1, float posY1, float posZ1, float sizeX, float sizeY, float sizeZ, float growX, float growY, float growZ, boolean mirror, float texWidth, float texHeight) { + this.posX1 = posX1; + this.posY1 = posY1; + this.posZ1 = posZ1; + this.posX2 = posX1 + sizeX; + this.posY2 = posY1 + sizeY; + this.posZ2 = posZ1 + sizeZ; + this.quads = new TexturedQuad[6]; + float posX2 = posX1 + sizeX; + float posY2 = posY1 + sizeY; + float posZ2 = posZ1 + sizeZ; + posX1 = posX1 - growX; + posY1 = posY1 - growY; + posZ1 = posZ1 - growZ; + posX2 = posX2 + growX; + posY2 = posY2 + growY; + posZ2 = posZ2 + growZ; + if (mirror) { + float tmp = posX2; + posX2 = posX1; + posX1 = tmp; + } + + PositionTextureVertex lll = new PositionTextureVertex(posX1, posY1, posZ1, 0.0F, 0.0F); + PositionTextureVertex hll = new PositionTextureVertex(posX2, posY1, posZ1, 0.0F, 8.0F); + PositionTextureVertex hhl = new PositionTextureVertex(posX2, posY2, posZ1, 8.0F, 8.0F); + PositionTextureVertex lhl = new PositionTextureVertex(posX1, posY2, posZ1, 8.0F, 0.0F); + PositionTextureVertex llh = new PositionTextureVertex(posX1, posY1, posZ2, 0.0F, 0.0F); + PositionTextureVertex hlh = new PositionTextureVertex(posX2, posY1, posZ2, 0.0F, 8.0F); + PositionTextureVertex hhh = new PositionTextureVertex(posX2, posY2, posZ2, 8.0F, 8.0F); + PositionTextureVertex lhh = new PositionTextureVertex(posX1, posY2, posZ2, 8.0F, 0.0F); + float f4 = (float)texOffU; + float f5 = (float)texOffU + sizeZ; + float f6 = (float)texOffU + sizeZ + sizeX; + float f7 = (float)texOffU + sizeZ + sizeX + sizeX; + float f8 = (float)texOffU + sizeZ + sizeX + sizeZ; + float f9 = (float)texOffU + sizeZ + sizeX + sizeZ + sizeX; + float f10 = (float)texOffV; + float f11 = (float)texOffV + sizeZ; + float f12 = (float)texOffV + sizeZ + sizeY; + this.quads[2] = new TexturedQuad(new PositionTextureVertex[]{hlh, llh, lll, hll}, f5, f10, f6, f11, texWidth, texHeight, mirror, Direction.DOWN); + this.quads[3] = new TexturedQuad(new PositionTextureVertex[]{hhl, lhl, lhh, hhh}, f6, f11, f7, f10, texWidth, texHeight, mirror, Direction.UP); + this.quads[1] = new TexturedQuad(new PositionTextureVertex[]{lll, llh, lhh, lhl}, f4, f11, f5, f12, texWidth, texHeight, mirror, Direction.WEST); + this.quads[4] = new TexturedQuad(new PositionTextureVertex[]{hll, lll, lhl, hhl}, f5, f11, f6, f12, texWidth, texHeight, mirror, Direction.NORTH); + this.quads[0] = new TexturedQuad(new PositionTextureVertex[]{hlh, hll, hhl, hhh}, f6, f11, f8, f12, texWidth, texHeight, mirror, Direction.EAST); + this.quads[5] = new TexturedQuad(new PositionTextureVertex[]{llh, hlh, hhh, lhh}, f8, f11, f9, f12, texWidth, texHeight, mirror, Direction.SOUTH); + } + } + + public static class PositionTextureVertex { + public final float x; + public final float y; + public final float z; + public final float u; + public final float v; + + public PositionTextureVertex(float x, float y, float z) { + this(x, y, z, 0, 0); + } + + public PositionTextureVertex(float x, float y, float z, float u, float v) { + this.x = x; + this.y = y; + this.z = z; + this.u = u; + this.v = v; + } + + public PositionTextureVertex setTexturePosition(float u, float v) { + return new PositionTextureVertex(x, y, z, u, v); + } + } + + public static class TexturedQuad { + public final PositionTextureVertex[] vertices; + public final Vector3f normal; + + public TexturedQuad(PositionTextureVertex[] vertices, float minU, float minV, float maxU, float maxV, float texWidth, float texHeight, boolean p_i225951_8_, Direction p_i225951_9_) { + this.vertices = vertices; + float w = 0.0F / texWidth; + float h = 0.0F / texHeight; + vertices[0] = vertices[0].setTexturePosition(maxU / texWidth - w, minV / texHeight + h); + vertices[1] = vertices[1].setTexturePosition(minU / texWidth + w, minV / texHeight + h); + vertices[2] = vertices[2].setTexturePosition(minU / texWidth + w, maxV / texHeight - h); + vertices[3] = vertices[3].setTexturePosition(maxU / texWidth - w, maxV / texHeight - h); + if (p_i225951_8_) { + int i = vertices.length; + + for(int j = 0; j < i / 2; ++j) { + PositionTextureVertex modelrenderer$positiontexturevertex = vertices[j]; + vertices[j] = vertices[i - 1 - j]; + vertices[i - 1 - j] = modelrenderer$positiontexturevertex; + } + } + + this.normal = p_i225951_9_.getUnitVector(); + if (p_i225951_8_) { + this.normal.multiplyComponentwise(-1.0F, 1.0F, 1.0F); + } + + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java index f87155d22..1042fccc7 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java @@ -3,6 +3,8 @@ package com.jozufozu.flywheel.mixin; import java.util.ArrayList; import java.util.Iterator; +import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; + import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Group; @@ -10,7 +12,6 @@ import org.spongepowered.asm.mixin.injection.Redirect; import com.google.common.collect.Lists; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.instancing.IInstanceRendered; import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.world.ClientWorld; @@ -28,7 +29,9 @@ public class CancelEntityRenderMixin { .canUseInstancing()) { ArrayList filtered = Lists.newArrayList(entities); - filtered.removeIf(entity -> entity instanceof IInstanceRendered && !((IInstanceRendered) entity).shouldRenderNormally()); + + InstancedRenderRegistry r = InstancedRenderRegistry.getInstance(); + filtered.removeIf(r::shouldSkipRender); return filtered; } @@ -42,7 +45,9 @@ public class CancelEntityRenderMixin { .canUseInstancing()) { ArrayList filtered = Lists.newArrayList(classInheritanceMultiMap); - filtered.removeIf(entity -> entity instanceof IInstanceRendered && !((IInstanceRendered) entity).shouldRenderNormally()); + + InstancedRenderRegistry r = InstancedRenderRegistry.getInstance(); + filtered.removeIf(r::shouldSkipRender); return filtered.iterator(); } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java index 88247741b..7a73e73ee 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java @@ -2,13 +2,14 @@ package com.jozufozu.flywheel.mixin; import java.util.List; +import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; + import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.jozufozu.flywheel.backend.Backend; -import com.jozufozu.flywheel.backend.instancing.IInstanceRendered; import net.minecraft.client.renderer.chunk.ChunkRenderDispatcher; import net.minecraft.tileentity.TileEntity; @@ -31,7 +32,8 @@ public class CancelTileEntityRenderMixin { .canUseInstancing()) { List tiles = cir.getReturnValue(); - tiles.removeIf(tile -> tile instanceof IInstanceRendered && !((IInstanceRendered) tile).shouldRenderNormally()); + InstancedRenderRegistry r = InstancedRenderRegistry.getInstance(); + tiles.removeIf(r::shouldSkipRender); } } } diff --git a/src/main/java/com/jozufozu/flywheel/util/RenderMath.java b/src/main/java/com/jozufozu/flywheel/util/RenderMath.java new file mode 100644 index 000000000..79028b953 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/RenderMath.java @@ -0,0 +1,8 @@ +package com.jozufozu.flywheel.util; + +public class RenderMath { + + public static byte nb(float f) { + return (byte) (f * 127); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/util/transform/QuaternionTransformStack.java b/src/main/java/com/jozufozu/flywheel/util/transform/QuaternionTransformStack.java new file mode 100644 index 000000000..7e4fb84b0 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/transform/QuaternionTransformStack.java @@ -0,0 +1,97 @@ +package com.jozufozu.flywheel.util.transform; + +import com.google.common.collect.Lists; + +import net.minecraft.util.math.vector.Quaternion; +import net.minecraft.util.math.vector.Vector3d; +import net.minecraft.util.math.vector.Vector3f; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.Stack; +import java.util.Vector; + +public class QuaternionTransformStack implements TransformStack { + + private final Deque stack; + + public QuaternionTransformStack() { + stack = new ArrayDeque<>(); + stack.add(new Transform()); + } + + @Override + public TransformStack translate(double x, double y, double z) { + + Transform peek = stack.peek(); + + double qx = peek.qx; + double qy = peek.qy; + double qz = peek.qz; + double qw = peek.qw; + peek.x += qw * x + qy * z - qz * y; + peek.y += qw * y - qx * z + qz * x; + peek.z += qw * z + qx * y - qy * x; + + return this; + } + + @Override + public TransformStack multiply(Quaternion quaternion) { + return this; + } + + @Override + public TransformStack push() { + stack.push(stack.peek().copy()); + return this; + } + + @Override + public TransformStack pop() { + + if (stack.size() == 1) { + stack.peek().loadIdentity(); + } else { + stack.pop(); + } + + return this; + } + + private static class Transform { + public double qx; + public double qy; + public double qz; + public double qw; + public double x; + public double y; + public double z; + + public Transform() { + qw = 1.0; + } + + public void loadIdentity() { + x = y = z = 0.0; + + qx = qy = qz = 0.0; + qw = 1.0; + } + + public Transform copy() { + Transform transform = new Transform(); + + transform.qx = this.qx; + transform.qy = this.qy; + transform.qz = this.qz; + transform.qw = this.qw; + transform.x = this.x; + transform.y = this.y; + transform.z = this.z; + + return transform; + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/util/transform/TransformStack.java b/src/main/java/com/jozufozu/flywheel/util/transform/TransformStack.java new file mode 100644 index 000000000..547b07c70 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/transform/TransformStack.java @@ -0,0 +1,80 @@ +package com.jozufozu.flywheel.util.transform; + +import net.minecraft.util.Direction; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.vector.Quaternion; +import net.minecraft.util.math.vector.Vector3d; +import net.minecraft.util.math.vector.Vector3f; +import net.minecraft.util.math.vector.Vector3i; + +public interface TransformStack { + public static final Vector3d center = new Vector3d(0.5, 0.5, 0.5); + + TransformStack translate(double x, double y, double z); + + TransformStack multiply(Quaternion quaternion); + + TransformStack push(); + + TransformStack pop(); + + default TransformStack rotate(Direction axis, float radians) { + if (radians == 0) + return this; + return multiply(axis.getUnitVector() + .getRadialQuaternion(radians)); + } + + default TransformStack rotate(double angle, Direction.Axis axis) { + Vector3f vec = + axis == Direction.Axis.X ? Vector3f.POSITIVE_X : axis == Direction.Axis.Y ? Vector3f.POSITIVE_Y : Vector3f.POSITIVE_Z; + return multiply(vec, angle); + } + + default TransformStack rotateX(double angle) { + return multiply(Vector3f.POSITIVE_X, angle); + } + + default TransformStack rotateY(double angle) { + return multiply(Vector3f.POSITIVE_Y, angle); + } + + default TransformStack rotateZ(double angle) { + return multiply(Vector3f.POSITIVE_Z, angle); + } + + default TransformStack centre() { + return translate(center); + } + + default TransformStack unCentre() { + return translateBack(center); + } + + default TransformStack translate(Vector3i vec) { + return translate(vec.getX(), vec.getY(), vec.getZ()); + } + + default TransformStack translate(Vector3d vec) { + return translate(vec.x, vec.y, vec.z); + } + + default TransformStack translateBack(Vector3d vec) { + return translate(-vec.x, -vec.y, -vec.z); + } + + default TransformStack nudge(int id) { + long randomBits = (long) id * 31L * 493286711L; + randomBits = randomBits * randomBits * 4392167121L + randomBits * 98761L; + float xNudge = (((float) (randomBits >> 16 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F; + float yNudge = (((float) (randomBits >> 20 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F; + float zNudge = (((float) (randomBits >> 24 & 7L) + 0.5F) / 8.0F - 0.5F) * 0.004F; + return translate(xNudge, yNudge, zNudge); + } + + default TransformStack multiply(Vector3f axis, double angle) { + if (angle == 0) + return this; + return multiply(axis.getDegreesQuaternion((float) angle)); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java new file mode 100644 index 000000000..9a7831b0c --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -0,0 +1,136 @@ +package com.jozufozu.flywheel.vanilla; + +import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; +import com.jozufozu.flywheel.backend.instancing.MaterialManager; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; + +import com.jozufozu.flywheel.backend.model.BufferedModel; +import com.jozufozu.flywheel.core.Materials; +import com.jozufozu.flywheel.core.materials.OrientedData; + +import com.jozufozu.flywheel.core.model.ModelPart; +import com.jozufozu.flywheel.util.AnimationTickHolder; + +import it.unimi.dsi.fastutil.floats.Float2FloatFunction; +import net.minecraft.block.Block; +import net.minecraft.block.ChestBlock; +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.TileEntityMerger; +import net.minecraft.util.math.vector.Quaternion; +import net.minecraft.util.math.vector.Vector3f; + +import java.util.Calendar; + +public class ChestInstance extends TileEntityInstance implements IDynamicInstance { + + private final OrientedData body; + private final OrientedData lid; + + private final Float2FloatFunction lidProgress; + private final RenderMaterial renderMaterial; + + public ChestInstance(MaterialManager materialManager, ChestTileEntity tile) { + super(materialManager, tile); + + Block block = blockState.getBlock(); + + ChestType chestType = blockState.contains(ChestBlock.TYPE) ? blockState.get(ChestBlock.TYPE) : ChestType.SINGLE; + renderMaterial = Atlases.getChestTexture(tile, chestType, isChristmas()); + + body = baseInstance() + .setPosition(getInstancePosition()); + lid = lidInstance() + .setPosition(getInstancePosition()) + .nudge(0, 9f/16f, 0); + + if (block instanceof ChestBlock) { + + ChestBlock chestBlock = (ChestBlock) block; + + TileEntityMerger.ICallbackWrapper wrapper = chestBlock.getBlockEntitySource(blockState, world, getWorldPosition(), true); + + this.lidProgress = wrapper.apply(ChestBlock.getAnimationProgressRetriever(tile)); + + + } else { + lidProgress = $ -> 0f; + } + } + + @Override + public void beginFrame() { + float progress = lidProgress.get(AnimationTickHolder.getPartialTicks()); + + progress = 1.0F - progress; + progress = 1.0F - progress * progress * progress; + + float angleX = -(progress * ((float) Math.PI / 2F)); + + Quaternion quaternion = new Quaternion(Vector3f.POSITIVE_X, angleX, false); + + lid.setRotation(quaternion) + .setPivot(0, 0, 1f / 16f); + + } + + @Override + public void updateLight() { + relight(getWorldPosition(), body, lid); + } + + @Override + public void remove() { + body.delete(); + lid.delete(); + } + + private OrientedData baseInstance() { + + return materialManager.getMaterial(Materials.ORIENTED, renderMaterial.getAtlasId()) + .get("base_" + renderMaterial.getTextureId(), this::getBaseModel) + .createInstance(); + } + + private OrientedData lidInstance() { + + return materialManager.getMaterial(Materials.ORIENTED, renderMaterial.getAtlasId()) + .get("lid_" + renderMaterial.getTextureId(), this::getLidModel) + .createInstance(); + } + + private BufferedModel getBaseModel() { + + return ModelPart.builder(64, 64) + .sprite(renderMaterial.getSprite()) + .cuboid() + .textureOffset(0, 19) + .start(1, 0, 1) + .end(15, 10, 15) + .endCuboid() + .build(); + } + + private BufferedModel getLidModel() { + + return ModelPart.builder(64, 64) + .sprite(renderMaterial.getSprite()) + .cuboid() + .textureOffset(0, 0) + .start(1, 0, 1) + .end(15, 5, 15) + .endCuboid() + .cuboid() + .start(7, -2, 15) + .size(2, 4, 1) + .endCuboid() + .build(); + } + + public static boolean isChristmas() { + Calendar calendar = Calendar.getInstance(); + return calendar.get(Calendar.MONTH) + 1 == 12 && calendar.get(Calendar.DATE) >= 24 && calendar.get(Calendar.DATE) <= 26; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java b/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java new file mode 100644 index 000000000..fd2aae00d --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.vanilla; + +import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; + +import net.minecraft.tileentity.TileEntityType; + +public class VanillaInstances { + + public static void init() { + InstancedRenderRegistry r = InstancedRenderRegistry.getInstance(); + + r.tile(TileEntityType.CHEST) + .setSkipRender(true) + .factory(ChestInstance::new) + .build(); + } +} From 18848b14e1e104164125fc5e61148e50adf1472f Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Fri, 9 Jul 2021 13:39:56 -0700 Subject: [PATCH 02/12] PartialModel gets called from the right place --- src/main/java/com/jozufozu/flywheel/FlywheelClient.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java index 068f6ea31..f38ca7b6b 100644 --- a/src/main/java/com/jozufozu/flywheel/FlywheelClient.java +++ b/src/main/java/com/jozufozu/flywheel/FlywheelClient.java @@ -5,6 +5,8 @@ import com.jozufozu.flywheel.core.AtlasStitcher; import com.jozufozu.flywheel.core.Contexts; import com.jozufozu.flywheel.core.Materials; +import com.jozufozu.flywheel.core.PartialModel; + import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; @@ -20,5 +22,7 @@ public class FlywheelClient { modEventBus.addListener(Contexts::flwInit); modEventBus.addListener(Materials::flwInit); + modEventBus.addListener(PartialModel::onModelRegistry); + modEventBus.addListener(PartialModel::onModelBake); } } From 80e851a1f1ccba5337eafd5e3b697ee948d183ca Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 10 Jul 2021 11:33:01 -0700 Subject: [PATCH 03/12] 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(); } } From fd610d2b626b1a21346dd3c5828b84702b1bdba1 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 10 Jul 2021 11:36:30 -0700 Subject: [PATCH 04/12] Rename builders - *Registrater -> *Config - build -> register --- .../instancing/InstancedRenderRegistry.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) 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 d6d107dfa..2be5996ac 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java @@ -48,12 +48,12 @@ public class InstancedRenderRegistry { return entities.containsKey(type); } - public TileRegistrater tile(TileEntityType type) { - return new TileRegistrater<>(type); + public TileConfig tile(TileEntityType type) { + return new TileConfig<>(type); } - public EntityRegistrater entity(EntityType type) { - return new EntityRegistrater<>(type); + public EntityConfig entity(EntityType type) { + return new EntityConfig<>(type); } @SuppressWarnings("unchecked") @@ -81,22 +81,22 @@ public class InstancedRenderRegistry { return skipRender.getBoolean(o); } - public class TileRegistrater { + public class TileConfig { private final TileEntityType type; private ITileInstanceFactory factory; private boolean skipRender = false; - public TileRegistrater(TileEntityType type) { + public TileConfig(TileEntityType type) { this.type = type; } - public TileRegistrater factory(ITileInstanceFactory rendererFactory) { + public TileConfig factory(ITileInstanceFactory rendererFactory) { factory = rendererFactory; return this; } - public TileRegistrater setSkipRender(boolean skipRender) { + public TileConfig setSkipRender(boolean skipRender) { this.skipRender = skipRender; return this; } @@ -109,27 +109,27 @@ public class InstancedRenderRegistry { } } - public class EntityRegistrater { + public class EntityConfig { private final EntityType type; private IEntityInstanceFactory factory; private boolean skipRender = false; - public EntityRegistrater(EntityType type) { + public EntityConfig(EntityType type) { this.type = type; } - public EntityRegistrater factory(IEntityInstanceFactory rendererFactory) { + public EntityConfig factory(IEntityInstanceFactory rendererFactory) { factory = rendererFactory; return this; } - public EntityRegistrater setSkipRender(boolean skipRender) { + public EntityConfig setSkipRender(boolean skipRender) { this.skipRender = skipRender; return this; } - public InstancedRenderRegistry build() { + public InstancedRenderRegistry register() { entities.put(type, factory); InstancedRenderRegistry.this.skipRender.put(type, skipRender); From 261571de97bb1096d283ac57c126db65f5e5d009 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 10 Jul 2021 13:06:18 -0700 Subject: [PATCH 05/12] Bump version - 0.1.0 --- changelog.txt | 7 +++++++ gradle.properties | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/changelog.txt b/changelog.txt index 51c60fb62..e1c78e5fa 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,3 +1,10 @@ +0.1.0: +Fixes + - Render layer events are always dispatched, even when the backend is disabled + - Fix a crash when light updates occur close to the world height + - Fix issue with optifine shaders + - Fix PartialModels not being initialized + 0.0.3: New - Include the logo in the mod diff --git a/gradle.properties b/gradle.properties index da9c12f49..ca26875c7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ org.gradle.jvmargs=-Xmx3G org.gradle.daemon=false # mod version info -mod_version=0.0.3 +mod_version=0.1.0 mc_update_version=1.16 minecraft_version=1.16.5 forge_version=36.0.42 From 0a463da72470f25c1b32f5409ec8d4df5626c758 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sat, 10 Jul 2021 18:42:15 -0700 Subject: [PATCH 06/12] Actually backwards compat is important - Reintroduce InstancedRenderRegistry#register methods - Deprecate the methods --- .../instancing/InstancedRenderRegistry.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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 2be5996ac..78f8f2c4b 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java @@ -56,6 +56,26 @@ public class InstancedRenderRegistry { return new EntityConfig<>(type); } + /** + * @deprecated will be removed in 0.2.0, use {@link #tile} + */ + @Deprecated + public void register(TileEntityType type, ITileInstanceFactory rendererFactory) { + this.tile(type) + .factory(rendererFactory) + .register(); + } + + /** + * @deprecated will be removed in 0.2.0, use {@link #entity} + */ + @Deprecated + public void register(EntityType type, IEntityInstanceFactory rendererFactory) { + this.entity(type) + .factory(rendererFactory) + .register(); + } + @SuppressWarnings("unchecked") @Nullable public TileEntityInstance create(MaterialManager manager, T tile) { From 4a27fbd4381ee80cfc97558a546c67c13af65557 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Sun, 11 Jul 2021 15:54:51 -0700 Subject: [PATCH 07/12] Correctly render chests - Give up on quaternions - Lids lazily update transform matrices - MatrixTransformStack --- .../jozufozu/flywheel/core/PartialModel.java | 7 +-- .../util/transform/MatrixTransformStack.java | 36 +++++++++++++++ .../util/transform/TransformStack.java | 7 ++- .../com/jozufozu/flywheel/util/vec/Vec3.java | 13 ++++-- .../com/jozufozu/flywheel/util/vec/Vec4.java | 7 +-- .../flywheel/vanilla/ChestInstance.java | 46 ++++++++++--------- 6 files changed, 80 insertions(+), 36 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/util/transform/MatrixTransformStack.java diff --git a/src/main/java/com/jozufozu/flywheel/core/PartialModel.java b/src/main/java/com/jozufozu/flywheel/core/PartialModel.java index f769f542b..198c8c8ba 100644 --- a/src/main/java/com/jozufozu/flywheel/core/PartialModel.java +++ b/src/main/java/com/jozufozu/flywheel/core/PartialModel.java @@ -12,12 +12,13 @@ import net.minecraftforge.client.model.ModelLoader; /** * A helper class for loading and accessing json models. - *

+ *
* Creating a PartialModel will make the associated modelLocation automatically load. - * As such, PartialModels must be initialized at or before {@link ModelRegistryEvent}. + * PartialModels must be initialized during {@link net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent FMLClientSetupEvent}. + *
* Once {@link ModelBakeEvent} finishes, all PartialModels (with valid modelLocations) * will have their bakedModel fields populated. - *

+ *
* Attempting to create a PartialModel after ModelRegistryEvent will cause an error. */ public class PartialModel { diff --git a/src/main/java/com/jozufozu/flywheel/util/transform/MatrixTransformStack.java b/src/main/java/com/jozufozu/flywheel/util/transform/MatrixTransformStack.java new file mode 100644 index 000000000..0ae2e605b --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/util/transform/MatrixTransformStack.java @@ -0,0 +1,36 @@ +package com.jozufozu.flywheel.util.transform; + +import com.mojang.blaze3d.matrix.MatrixStack; + +import net.minecraft.util.math.vector.Quaternion; + +public class MatrixTransformStack implements TransformStack { + + private final MatrixStack internal = new MatrixStack(); + + public MatrixStack unwrap() { + return internal; + } + + @Override + public TransformStack translate(double x, double y, double z) { + internal.translate(x, y, z); + return this; + } + + @Override + public TransformStack multiply(Quaternion quaternion) { + internal.multiply(quaternion); + return this; + } + + @Override + public TransformStack push() { + return this; + } + + @Override + public TransformStack pop() { + return this; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/util/transform/TransformStack.java b/src/main/java/com/jozufozu/flywheel/util/transform/TransformStack.java index 547b07c70..f50571a43 100644 --- a/src/main/java/com/jozufozu/flywheel/util/transform/TransformStack.java +++ b/src/main/java/com/jozufozu/flywheel/util/transform/TransformStack.java @@ -1,14 +1,13 @@ package com.jozufozu.flywheel.util.transform; import net.minecraft.util.Direction; -import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.vector.Quaternion; import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3f; import net.minecraft.util.math.vector.Vector3i; public interface TransformStack { - public static final Vector3d center = new Vector3d(0.5, 0.5, 0.5); + Vector3d CENTER = new Vector3d(0.5, 0.5, 0.5); TransformStack translate(double x, double y, double z); @@ -44,11 +43,11 @@ public interface TransformStack { } default TransformStack centre() { - return translate(center); + return translate(CENTER); } default TransformStack unCentre() { - return translateBack(center); + return translateBack(CENTER); } default TransformStack translate(Vector3i vec) { diff --git a/src/main/java/com/jozufozu/flywheel/util/vec/Vec3.java b/src/main/java/com/jozufozu/flywheel/util/vec/Vec3.java index 2a7d1a8ae..d9f6f3414 100644 --- a/src/main/java/com/jozufozu/flywheel/util/vec/Vec3.java +++ b/src/main/java/com/jozufozu/flywheel/util/vec/Vec3.java @@ -38,8 +38,7 @@ public class Vec3 { vec4.multiply(quat); - set(vec4.getX(), vec4.getY(), vec4.getZ()); - return this; + return set(vec4.getX(), vec4.getY(), vec4.getZ()); } public Vec3 copy() { @@ -50,15 +49,15 @@ public class Vec3 { return new Vector3f(x, y, z); } - public void set(float x, float y, float z) { + public Vec3 set(float x, float y, float z) { this.x = x; this.y = y; this.z = z; + return this; } public Vec3 add(Vec3 v) { - add(v.x, v.y, v.z); - return this; + return add(v.x, v.y, v.z); } public Vec3 add(float x, float y, float z) { @@ -68,6 +67,10 @@ public class Vec3 { return this; } + public Vec3 sub(Vec3 v) { + return sub(v.x, v.y, v.z); + } + public Vec3 sub(float x, float y, float z) { this.x -= x; this.y -= y; diff --git a/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java b/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java index 2fed35813..668751fa9 100644 --- a/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java +++ b/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java @@ -28,13 +28,13 @@ public class Vec4 { this.w = w; } - public void multiply(Quaternion quat) { + public Vec4 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()); + return set(quaternion.getX(), quaternion.getY(), quaternion.getZ(), this.getW()); } public Vec3 xyz() { @@ -57,10 +57,11 @@ public class Vec4 { return w; } - public void set(float x, float y, float z, float w) { + public Vec4 set(float x, float y, float z, float w) { this.x = x; this.y = y; this.z = z; this.w = w; + return this; } } diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java index 0e63f8f9d..f8108ebdc 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -6,16 +6,19 @@ import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; import com.jozufozu.flywheel.backend.model.BufferedModel; import com.jozufozu.flywheel.core.Materials; +import com.jozufozu.flywheel.core.materials.ModelData; 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.transform.MatrixTransformStack; 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.AbstractChestBlock; import net.minecraft.block.Block; import net.minecraft.block.ChestBlock; import net.minecraft.client.renderer.Atlases; @@ -26,6 +29,7 @@ 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.Vector3d; import net.minecraft.util.math.vector.Vector3f; import net.minecraft.util.math.vector.Vector4f; @@ -36,7 +40,7 @@ import java.util.Calendar; public class ChestInstance extends TileEntityInstance implements IDynamicInstance { private final OrientedData body; - private final OrientedData lid; + private final ModelData lid; private final Float2FloatFunction lidProgress; private final RenderMaterial renderMaterial; @@ -44,6 +48,8 @@ public class ChestInstance extends TileEntityI private final ChestType chestType; private final Quaternion baseRotation; + private float lastProgress = Float.NaN; + public ChestInstance(MaterialManager materialManager, T tile) { super(materialManager, tile); @@ -54,11 +60,9 @@ public class ChestInstance extends TileEntityI body = baseInstance() .setPosition(getInstancePosition()); - lid = lidInstance() - .setPosition(getInstancePosition()) - .nudge(0, 9f/16f, 0); + lid = lidInstance(); - if (block instanceof ChestBlock) { + if (block instanceof AbstractChestBlock) { // MatrixStack stack = new MatrixStack(); // @@ -69,7 +73,7 @@ public class ChestInstance extends TileEntityI body.setRotation(baseRotation); - ChestBlock chestBlock = (ChestBlock) block; + AbstractChestBlock chestBlock = (AbstractChestBlock) block; TileEntityMerger.ICallbackWrapper wrapper = chestBlock.getBlockEntitySource(blockState, world, getWorldPosition(), true); @@ -86,27 +90,27 @@ public class ChestInstance extends TileEntityI public void beginFrame() { float progress = lidProgress.get(AnimationTickHolder.getPartialTicks()); + if (lastProgress == progress) return; + + lastProgress = progress; + progress = 1.0F - progress; progress = 1.0F - progress * progress * progress; float angleX = -(progress * ((float) Math.PI / 2F)); + MatrixTransformStack stack = new MatrixTransformStack(); - Vec3 axis = Vec3.POSITIVE_X.copy(); - Vec3 pivot = new Vec3(0, 0, 1f / 16f); - pivot.add(0.5f, 0.5f, 0.5f) + stack.translate(getInstancePosition()) + .translate(0, 9f/16f, 0) + .centre() .multiply(baseRotation) - .sub(0.5f, 0.5f, 0.5f); - axis.multiply(baseRotation); + .unCentre() + .translate(0, 0, 1f / 16f) + .multiply(Vector3f.POSITIVE_X.getRadialQuaternion(angleX)) + .translate(0, 0, -1f / 16f); - Quaternion quaternion = new Quaternion(axis.convert(), angleX, false); - - quaternion.multiply(baseRotation); - - lid.setRotation(quaternion) - .setPosition(getInstancePosition()) - .nudge(0, 9f/16f, 0) - .setPivot(pivot); + lid.setTransform(stack.unwrap()); } @@ -128,9 +132,9 @@ public class ChestInstance extends TileEntityI .createInstance(); } - private OrientedData lidInstance() { + private ModelData lidInstance() { - return materialManager.getMaterial(Materials.ORIENTED, renderMaterial.getAtlasId()) + return materialManager.getMaterial(Materials.TRANSFORMED, renderMaterial.getAtlasId()) .get("lid_" + renderMaterial.getTextureId(), this::getLidModel) .createInstance(); } From 2a0757e867fb12fb993e8fff0b84409dc258542a Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 12 Jul 2021 14:52:54 -0700 Subject: [PATCH 08/12] Atlases, bells, breaking - Bell instance - Fix some inconsistencies with PartBuilder - Store information on texture atlasses - Crumbling overlay fixes --- .../backend/instancing/InstanceManager.java | 1 + .../instancing/InstancedRenderDispatcher.java | 6 +- .../instancing/InstancedRenderRegistry.java | 31 ++------ .../backend/instancing/MaterialManager.java | 2 +- .../com/jozufozu/flywheel/core/Contexts.java | 1 + .../flywheel/core/CrumblingProgram.java | 31 -------- .../flywheel/core/atlas/AtlasInfo.java | 43 ++++++++++ .../flywheel/core/atlas/SheetData.java | 17 ++++ .../CrumblingInstanceManager.java | 6 +- .../crumbling/CrumblingMaterialManager.java | 68 ++++++++++++++++ .../core/crumbling/CrumblingProgram.java | 63 +++++++++++++++ .../flywheel/core/model/PartBuilder.java | 23 +++--- .../flywheel/mixin/atlas/AtlasDataMixin.java | 33 ++++++++ .../mixin/atlas/SheetDataAccessor.java | 17 ++++ .../flywheel/vanilla/BellInstance.java | 78 +++++++++++++++++++ .../flywheel/vanilla/VanillaInstances.java | 34 ++++++-- src/main/resources/flywheel.mixins.json | 2 + 17 files changed, 376 insertions(+), 80 deletions(-) delete mode 100644 src/main/java/com/jozufozu/flywheel/core/CrumblingProgram.java create mode 100644 src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java create mode 100644 src/main/java/com/jozufozu/flywheel/core/atlas/SheetData.java rename src/main/java/com/jozufozu/flywheel/core/{ => crumbling}/CrumblingInstanceManager.java (77%) create mode 100644 src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java create mode 100644 src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingProgram.java create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/atlas/AtlasDataMixin.java create mode 100644 src/main/java/com/jozufozu/flywheel/mixin/atlas/SheetDataAccessor.java create mode 100644 src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java index 984bfaf37..41159f061 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceManager.java @@ -167,6 +167,7 @@ public abstract class InstanceManager implements MaterialManager.OriginShiftL } public void invalidate() { + instances.values().forEach(IInstance::remove); instances.clear(); dynamicInstances.clear(); tickableInstances.clear(); diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java index 883fbd228..6df479ddc 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java @@ -16,7 +16,8 @@ import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.instancing.entity.EntityInstanceManager; import com.jozufozu.flywheel.backend.instancing.tile.TileInstanceManager; import com.jozufozu.flywheel.core.Contexts; -import com.jozufozu.flywheel.core.CrumblingInstanceManager; +import com.jozufozu.flywheel.core.crumbling.CrumblingInstanceManager; +import com.jozufozu.flywheel.core.crumbling.CrumblingProgram; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; @@ -163,8 +164,6 @@ public class InstancedRenderDispatcher { glBindTexture(GL_TEXTURE_2D, textureManager.getTexture(PlayerContainer.BLOCK_ATLAS_TEXTURE) .getGlTextureId()); - glActiveTexture(GL_TEXTURE4); - crumblingLayer.startDrawing(); bitSet.stream() .forEach(i -> { @@ -173,6 +172,7 @@ public class InstancedRenderDispatcher { renderer.beginFrame(info); if (breaking != null) { + glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, breaking.getGlTextureId()); renderer.materialManager.render(RenderType.getCutoutMipped(), viewProjection, cameraX, cameraY, cameraZ); } 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 78f8f2c4b..ee0c37e07 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java @@ -62,8 +62,7 @@ public class InstancedRenderRegistry { @Deprecated public void register(TileEntityType type, ITileInstanceFactory rendererFactory) { this.tile(type) - .factory(rendererFactory) - .register(); + .factory(rendererFactory); } /** @@ -72,8 +71,7 @@ public class InstancedRenderRegistry { @Deprecated public void register(EntityType type, IEntityInstanceFactory rendererFactory) { this.entity(type) - .factory(rendererFactory) - .register(); + .factory(rendererFactory); } @SuppressWarnings("unchecked") @@ -104,56 +102,39 @@ public class InstancedRenderRegistry { public class TileConfig { private final TileEntityType type; - private ITileInstanceFactory factory; - private boolean skipRender = false; public TileConfig(TileEntityType type) { this.type = type; } public TileConfig factory(ITileInstanceFactory rendererFactory) { - factory = rendererFactory; + tiles.put(type, rendererFactory); return this; } public TileConfig setSkipRender(boolean skipRender) { - this.skipRender = skipRender; - return this; - } - - public InstancedRenderRegistry register() { - tiles.put(type, factory); InstancedRenderRegistry.this.skipRender.put(type, skipRender); - - return InstancedRenderRegistry.this; + return this; } } public class EntityConfig { private final EntityType type; - private IEntityInstanceFactory factory; - private boolean skipRender = false; public EntityConfig(EntityType type) { this.type = type; } public EntityConfig factory(IEntityInstanceFactory rendererFactory) { - factory = rendererFactory; + entities.put(type, rendererFactory); return this; } public EntityConfig setSkipRender(boolean skipRender) { - this.skipRender = skipRender; - return this; - } - - public InstancedRenderRegistry register() { - entities.put(type, factory); InstancedRenderRegistry.this.skipRender.put(type, skipRender); - return InstancedRenderRegistry.this; + return this; } } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialManager.java index dc912edc7..af338c604 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/MaterialManager.java @@ -34,7 +34,7 @@ public class MaterialManager

{ protected final Map>> renderers; protected final Map, InstanceMaterial>> materials; - private BlockPos originCoordinate = BlockPos.ZERO; + protected BlockPos originCoordinate = BlockPos.ZERO; private final WeakHashSet listeners; diff --git a/src/main/java/com/jozufozu/flywheel/core/Contexts.java b/src/main/java/com/jozufozu/flywheel/core/Contexts.java index f4d0a3c40..1752bba54 100644 --- a/src/main/java/com/jozufozu/flywheel/core/Contexts.java +++ b/src/main/java/com/jozufozu/flywheel/core/Contexts.java @@ -4,6 +4,7 @@ import com.jozufozu.flywheel.Flywheel; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.SpecMetaRegistry; import com.jozufozu.flywheel.backend.gl.shader.ShaderType; +import com.jozufozu.flywheel.core.crumbling.CrumblingProgram; import com.jozufozu.flywheel.core.shader.WorldFog; import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.core.shader.gamestate.FogStateProvider; diff --git a/src/main/java/com/jozufozu/flywheel/core/CrumblingProgram.java b/src/main/java/com/jozufozu/flywheel/core/CrumblingProgram.java deleted file mode 100644 index dff507f42..000000000 --- a/src/main/java/com/jozufozu/flywheel/core/CrumblingProgram.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.jozufozu.flywheel.core; - -import static org.lwjgl.opengl.GL20.glUniform2f; - -import java.util.List; - -import com.jozufozu.flywheel.backend.loading.Program; -import com.jozufozu.flywheel.core.shader.WorldProgram; -import com.jozufozu.flywheel.core.shader.extension.IProgramExtension; - -public class CrumblingProgram extends WorldProgram { - protected final int uTextureScale; - protected int uCrumbling; - - public CrumblingProgram(Program program, List extensions) { - super(program, extensions); - - uTextureScale = getUniformLocation("uTextureScale"); - } - - @Override - protected void registerSamplers() { - super.registerSamplers(); - uCrumbling = setSamplerBinding("uCrumbling", 4); - } - - public void setTextureScale(float x, float y) { - glUniform2f(uTextureScale, x, y); - } - -} diff --git a/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java b/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java new file mode 100644 index 000000000..91333f875 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java @@ -0,0 +1,43 @@ +package com.jozufozu.flywheel.core.atlas; + +import com.jozufozu.flywheel.mixin.atlas.SheetDataAccessor; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.texture.AtlasTexture; +import net.minecraft.client.renderer.texture.Texture; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.util.ResourceLocation; + +import java.util.HashMap; +import java.util.Map; + +public class AtlasInfo { + + private static final Map sheetData = new HashMap<>(); + + public static AtlasTexture getAtlas(ResourceLocation name) { + Texture texture = Minecraft.getInstance().textureManager.getTexture(name); + + if (texture instanceof AtlasTexture) + return (AtlasTexture) texture; + else + return null; + } + + public static SheetData getAtlasData(TextureAtlasSprite texture) { + return getAtlasData(texture.getAtlas()); + } + + public static SheetData getAtlasData(AtlasTexture atlas) { + return getAtlasData(atlas.getId()); + } + + public static SheetData getAtlasData(ResourceLocation loc) { + return sheetData.get(loc); + } + + public static void setAtlasData(ResourceLocation atlas, SheetDataAccessor accessor) { + sheetData.put(atlas, new SheetData(accessor.getWidth(), accessor.getHeight())); + } + +} diff --git a/src/main/java/com/jozufozu/flywheel/core/atlas/SheetData.java b/src/main/java/com/jozufozu/flywheel/core/atlas/SheetData.java new file mode 100644 index 000000000..9841e6899 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/atlas/SheetData.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.core.atlas; + +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.util.ResourceLocation; + +import java.util.List; +import java.util.Set; + +public class SheetData { + public final int width; + public final int height; + + public SheetData(int width, int height) { + this.width = width; + this.height = height; + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/CrumblingInstanceManager.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java similarity index 77% rename from src/main/java/com/jozufozu/flywheel/core/CrumblingInstanceManager.java rename to src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java index bbea00ada..8d2cbc8eb 100644 --- a/src/main/java/com/jozufozu/flywheel/core/CrumblingInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java @@ -1,13 +1,15 @@ -package com.jozufozu.flywheel.core; +package com.jozufozu.flywheel.core.crumbling; import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.tile.TileInstanceManager; +import com.jozufozu.flywheel.core.Contexts; + import net.minecraft.util.math.BlockPos; public class CrumblingInstanceManager extends TileInstanceManager { public CrumblingInstanceManager() { - super(new MaterialManager<>(Contexts.CRUMBLING)); + super(new CrumblingMaterialManager()); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java new file mode 100644 index 000000000..2d8a6a8cb --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java @@ -0,0 +1,68 @@ +package com.jozufozu.flywheel.core.crumbling; + +import com.jozufozu.flywheel.backend.instancing.MaterialManager; +import com.jozufozu.flywheel.backend.instancing.MaterialRenderer; +import com.jozufozu.flywheel.core.Contexts; +import com.jozufozu.flywheel.core.WorldContext; +import com.jozufozu.flywheel.core.atlas.AtlasInfo; +import com.jozufozu.flywheel.core.atlas.SheetData; +import com.jozufozu.flywheel.core.shader.IProgramCallback; +import com.jozufozu.flywheel.core.shader.WorldProgram; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.inventory.container.PlayerContainer; +import net.minecraft.util.ResourceLocation; +import net.minecraft.util.math.vector.Matrix4f; + +import java.util.ArrayList; +import java.util.Map; + +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; + +public class CrumblingMaterialManager extends MaterialManager { + + public CrumblingMaterialManager() { + super(Contexts.CRUMBLING); + } + + /** + * Render every model for every material. + * + * @param layer Which vanilla {@link RenderType} is being drawn? + * @param viewProjection How do we get from camera space to clip space? + * @param callback Provide additional uniforms or state here. + */ + public void render(RenderType layer, Matrix4f viewProjection, double camX, double camY, double camZ, IProgramCallback callback) { + camX -= originCoordinate.getX(); + camY -= originCoordinate.getY(); + camZ -= originCoordinate.getZ(); + + Matrix4f translate = Matrix4f.translate((float) -camX, (float) -camY, (float) -camZ); + + translate.multiplyBackward(viewProjection); + + TextureManager textureManager = Minecraft.getInstance().textureManager; + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, textureManager.getTexture(PlayerContainer.BLOCK_ATLAS_TEXTURE) + .getGlTextureId()); + + for (MaterialRenderer material : atlasRenderers) { + material.render(layer, translate, camX, camY, camZ, CrumblingProgram::setDefaultAtlasSize); + } + + for (Map.Entry>> entry : renderers.entrySet()) { + glBindTexture(GL_TEXTURE_2D, textureManager.getTexture(entry.getKey()) + .getGlTextureId()); + SheetData atlasData = AtlasInfo.getAtlasData(entry.getKey()); + for (MaterialRenderer materialRenderer : entry.getValue()) { + materialRenderer.render(layer, translate, camX, camY, camZ, p -> p.setAtlasSize(atlasData.width, atlasData.height)); + } + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingProgram.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingProgram.java new file mode 100644 index 000000000..cafe62259 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingProgram.java @@ -0,0 +1,63 @@ +package com.jozufozu.flywheel.core.crumbling; + +import static org.lwjgl.opengl.GL20.glUniform2f; + +import java.util.List; + +import com.jozufozu.flywheel.backend.loading.Program; +import com.jozufozu.flywheel.core.atlas.AtlasInfo; +import com.jozufozu.flywheel.core.atlas.SheetData; +import com.jozufozu.flywheel.core.shader.WorldProgram; +import com.jozufozu.flywheel.core.shader.extension.IProgramExtension; + +import net.minecraft.client.renderer.model.ModelBakery; +import net.minecraft.client.renderer.texture.AtlasTexture; +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.inventory.container.PlayerContainer; + +public class CrumblingProgram extends WorldProgram { + protected final int uTextureScale; + protected int uCrumbling; + + public CrumblingProgram(Program program, List extensions) { + super(program, extensions); + + uTextureScale = getUniformLocation("uTextureScale"); + } + + @Override + public void bind() { + super.bind(); + setDefaultAtlasSize(); + } + + @Override + protected void registerSamplers() { + super.registerSamplers(); + uCrumbling = setSamplerBinding("uCrumbling", 4); + } + + public void setTextureScale(float x, float y) { + glUniform2f(uTextureScale, x, y); + } + + public void setDefaultAtlasSize() { + SheetData atlasData = AtlasInfo.getAtlasData(PlayerContainer.BLOCK_ATLAS_TEXTURE); + if (atlasData == null) return; + + int width = atlasData.width; + int height = atlasData.height; + + setAtlasSize(width, height); + } + + public void setAtlasSize(int width, int height) { + AtlasTexture blockAtlas = AtlasInfo.getAtlas(PlayerContainer.BLOCK_ATLAS_TEXTURE); + if (blockAtlas == null) return; + + TextureAtlasSprite sprite = blockAtlas.getSprite(ModelBakery.BLOCK_DESTRUCTION_STAGE_TEXTURES.get(0)); + + setTextureScale(width / (float) sprite.getWidth(), height / (float) sprite.getHeight()); + } + +} diff --git a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java index 178b34f5f..721355dc2 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java @@ -9,6 +9,8 @@ import com.jozufozu.flywheel.backend.model.IndexedModel; import com.jozufozu.flywheel.core.Formats; +import com.jozufozu.flywheel.core.atlas.AtlasInfo; + import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.util.Direction; import net.minecraft.util.math.vector.Vector3f; @@ -157,19 +159,16 @@ public class PartBuilder { float f11 = getV((float)textureOffsetV + sizeZ); float f12 = getV((float)textureOffsetV + sizeZ + sizeY); - - float textureWidth = partBuilder.sizeU; - float textureHeight = partBuilder.sizeV; - quad(buffer, new Vector3f[]{hlh, llh, lll, hll}, f5, f10, f6, f11, textureWidth, textureHeight, Direction.DOWN); - quad(buffer, new Vector3f[]{hhl, lhl, lhh, hhh}, f6, f11, f7, f10, textureWidth, textureHeight, Direction.UP); - quad(buffer, new Vector3f[]{lll, llh, lhh, lhl}, f4, f11, f5, f12, textureWidth, textureHeight, Direction.WEST); - quad(buffer, new Vector3f[]{hll, lll, lhl, hhl}, f5, f11, f6, f12, textureWidth, textureHeight, Direction.NORTH); - quad(buffer, new Vector3f[]{hlh, hll, hhl, hhh}, f6, f11, f8, f12, textureWidth, textureHeight, Direction.EAST); - quad(buffer, new Vector3f[]{llh, hlh, hhh, lhh}, f8, f11, f9, f12, textureWidth, textureHeight, Direction.SOUTH); + quad(buffer, new Vector3f[]{hlh, llh, lll, hll}, f5, f10, f6, f11, Direction.DOWN); + quad(buffer, new Vector3f[]{hhl, lhl, lhh, hhh}, f6, f11, f7, f10, Direction.UP); + quad(buffer, new Vector3f[]{lll, llh, lhh, lhl}, f4, f11, f5, f12, Direction.WEST); + quad(buffer, new Vector3f[]{hll, lll, lhl, hhl}, f5, f11, f6, f12, Direction.NORTH); + quad(buffer, new Vector3f[]{hlh, hll, hhl, hhh}, f6, f11, f8, f12, Direction.EAST); + quad(buffer, new Vector3f[]{llh, hlh, hhh, lhh}, f8, f11, f9, f12, Direction.SOUTH); } - public void quad(VecBuffer buffer, Vector3f[] vertices, float minU, float minV, float maxU, float maxV, float texWidth, float texHeight, Direction dir) { + public void quad(VecBuffer buffer, Vector3f[] vertices, float minU, float minV, float maxU, float maxV, Direction dir) { Vector3f normal = dir.getUnitVector(); @@ -182,14 +181,14 @@ public class PartBuilder { public float getU(float u) { if (sprite != null) - return sprite.getInterpolatedU(u / 4.); + return sprite.getInterpolatedU(u * 16 / partBuilder.sizeU); else return u; } public float getV(float v) { if (sprite != null) - return sprite.getInterpolatedV(v / 4.); + return sprite.getInterpolatedV(v * 16 / partBuilder.sizeV); else return v; } diff --git a/src/main/java/com/jozufozu/flywheel/mixin/atlas/AtlasDataMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/atlas/AtlasDataMixin.java new file mode 100644 index 000000000..27c5eb5c8 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/atlas/AtlasDataMixin.java @@ -0,0 +1,33 @@ +package com.jozufozu.flywheel.mixin.atlas; + +import com.jozufozu.flywheel.core.atlas.AtlasInfo; + +import net.minecraft.client.renderer.texture.AtlasTexture; + +import net.minecraft.profiler.IProfiler; +import net.minecraft.resources.IResourceManager; +import net.minecraft.util.ResourceLocation; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; + +import java.util.stream.Stream; + +@Mixin(AtlasTexture.class) +public abstract class AtlasDataMixin { + + @Shadow + public abstract ResourceLocation getId(); + + @Inject(method = "stitch", at = @At("RETURN")) + public void stealAtlasData(IResourceManager resourceManager, Stream locationStream, IProfiler profiler, int mipMapLevels, CallbackInfoReturnable cir) { + AtlasTexture.SheetData value = cir.getReturnValue(); + + SheetDataAccessor dataAccessor = (SheetDataAccessor) value; + + AtlasInfo.setAtlasData(getId(), dataAccessor); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/mixin/atlas/SheetDataAccessor.java b/src/main/java/com/jozufozu/flywheel/mixin/atlas/SheetDataAccessor.java new file mode 100644 index 000000000..daa4fdd8e --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/mixin/atlas/SheetDataAccessor.java @@ -0,0 +1,17 @@ +package com.jozufozu.flywheel.mixin.atlas; + +import net.minecraft.client.renderer.texture.AtlasTexture; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(AtlasTexture.SheetData.class) +public interface SheetDataAccessor { + + @Accessor("width") + int getWidth(); + + @Accessor("height") + int getHeight(); + +} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java new file mode 100644 index 000000000..6380e49e2 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java @@ -0,0 +1,78 @@ +package com.jozufozu.flywheel.vanilla; + +import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; +import com.jozufozu.flywheel.backend.instancing.MaterialManager; +import com.jozufozu.flywheel.backend.instancing.MaterialSpec; +import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; + +import com.jozufozu.flywheel.backend.model.BufferedModel; +import com.jozufozu.flywheel.core.Materials; +import com.jozufozu.flywheel.core.materials.OrientedData; + +import com.jozufozu.flywheel.core.model.ModelPart; +import com.jozufozu.flywheel.core.model.PartBuilder; + +import com.jozufozu.flywheel.util.AnimationTickHolder; + +import net.minecraft.client.renderer.tileentity.BellTileEntityRenderer; +import net.minecraft.tileentity.BellTileEntity; +import net.minecraft.util.Direction; +import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.vector.Quaternion; +import net.minecraft.util.math.vector.Vector3f; + +public class BellInstance extends TileEntityInstance implements IDynamicInstance { + + private final OrientedData bell; + + public BellInstance(MaterialManager materialManager, BellTileEntity tile) { + super(materialManager, tile); + + bell = createBellInstance() + .setPivot(0.5f, 0.75f, 0.5f) + .setPosition(getInstancePosition()); + } + + @Override + public void beginFrame() { + if (tile.isRinging) { + float ringness = (float)tile.ringingTicks + AnimationTickHolder.getPartialTicks(); + float angle = MathHelper.sin(ringness / (float)Math.PI) / (4.0F + ringness / 3.0F); + + Vector3f ringAxis = tile.ringDirection.rotateYCCW().getUnitVector(); + + bell.setRotation(ringAxis.getRadialQuaternion(angle)); + } + } + + @Override + public void updateLight() { + relight(getWorldPosition(), bell); + } + + @Override + public void remove() { + bell.delete(); + } + + private OrientedData createBellInstance() { + return materialManager.getMaterial(Materials.ORIENTED) + .get(tile.getType(), BellInstance::createBellModel) + .createInstance(); + } + + private static BufferedModel createBellModel() { + return ModelPart.builder(32, 32) + .sprite(BellTileEntityRenderer.field_217653_c.getSprite()) + .cuboid() + .start(5.0F, 6.0F, 5.0F) + .size(6.0F, 7.0F, 6.0F) + .endCuboid() + .cuboid() + .textureOffset(0, 13) + .start(4.0F, 4.0F, 4.0F) + .size(8.0F, 2.0F, 8.0F) + .endCuboid() + .build(); + } +} diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java b/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java index 7b7628b2d..36f6567ee 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/VanillaInstances.java @@ -4,6 +4,27 @@ import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import net.minecraft.tileentity.TileEntityType; +/** + * TODO: + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + *
{@link TileEntityType#SIGN} {@link net.minecraft.client.renderer.tileentity.SignTileEntityRenderer SignTileEntityRenderer}
{@link TileEntityType#MOB_SPAWNER} {@link net.minecraft.client.renderer.tileentity.MobSpawnerTileEntityRenderer MobSpawnerTileEntityRenderer}
{@link TileEntityType#PISTON} {@link net.minecraft.client.renderer.tileentity.PistonTileEntityRenderer PistonTileEntityRenderer}
{@link TileEntityType#ENCHANTING_TABLE} {@link net.minecraft.client.renderer.tileentity.EnchantmentTableTileEntityRenderer EnchantmentTableTileEntityRenderer}
{@link TileEntityType#LECTERN} {@link net.minecraft.client.renderer.tileentity.LecternTileEntityRenderer LecternTileEntityRenderer}
{@link TileEntityType#END_PORTAL} {@link net.minecraft.client.renderer.tileentity.EndPortalTileEntityRenderer EndPortalTileEntityRenderer}
{@link TileEntityType#END_GATEWAY} {@link net.minecraft.client.renderer.tileentity.EndGatewayTileEntityRenderer EndGatewayTileEntityRenderer}
{@link TileEntityType#BEACON} {@link net.minecraft.client.renderer.tileentity.BeaconTileEntityRenderer BeaconTileEntityRenderer}
{@link TileEntityType#SKULL} {@link net.minecraft.client.renderer.tileentity.SkullTileEntityRenderer SkullTileEntityRenderer}
{@link TileEntityType#BANNER} {@link net.minecraft.client.renderer.tileentity.BannerTileEntityRenderer BannerTileEntityRenderer}
{@link TileEntityType#STRUCTURE_BLOCK} {@link net.minecraft.client.renderer.tileentity.StructureTileEntityRenderer StructureTileEntityRenderer}
{@link TileEntityType#SHULKER_BOX} {@link net.minecraft.client.renderer.tileentity.ShulkerBoxTileEntityRenderer ShulkerBoxTileEntityRenderer}
{@link TileEntityType#BED} {@link net.minecraft.client.renderer.tileentity.BedTileEntityRenderer BedTileEntityRenderer}
{@link TileEntityType#CONDUIT} {@link net.minecraft.client.renderer.tileentity.ConduitTileEntityRenderer ConduitTileEntityRenderer}
{@link TileEntityType#BELL} {@link net.minecraft.client.renderer.tileentity.BellTileEntityRenderer BellTileEntityRenderer}
{@link TileEntityType#CAMPFIRE} {@link net.minecraft.client.renderer.tileentity.CampfireTileEntityRenderer CampfireTileEntityRenderer}
+ */ public class VanillaInstances { public static void init() { @@ -11,15 +32,16 @@ public class VanillaInstances { r.tile(TileEntityType.CHEST) .setSkipRender(true) - .factory(ChestInstance::new) - .register(); + .factory(ChestInstance::new); r.tile(TileEntityType.ENDER_CHEST) .setSkipRender(true) - .factory(ChestInstance::new) - .register(); + .factory(ChestInstance::new); r.tile(TileEntityType.TRAPPED_CHEST) .setSkipRender(true) - .factory(ChestInstance::new) - .register(); + .factory(ChestInstance::new); + + r.tile(TileEntityType.BELL) + .setSkipRender(true) + .factory(BellInstance::new); } } diff --git a/src/main/resources/flywheel.mixins.json b/src/main/resources/flywheel.mixins.json index 42f08b576..b930f4173 100644 --- a/src/main/resources/flywheel.mixins.json +++ b/src/main/resources/flywheel.mixins.json @@ -15,6 +15,8 @@ "StoreProjectionMatrixMixin", "TileRemoveMixin", "TileWorldHookMixin", + "atlas.AtlasDataMixin", + "atlas.SheetDataAccessor", "light.LightUpdateMixin", "light.NetworkLightUpdateMixin" ], From 3b288738ce7ff6bc8e00a67c59d7d55e49236a21 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 12 Jul 2021 15:02:08 -0700 Subject: [PATCH 09/12] Fix import ordering --- .../instancing/InstancedRenderDispatcher.java | 1 - .../entity/EntityInstanceManager.java | 1 - .../flywheel/core/atlas/AtlasInfo.java | 6 ++--- .../flywheel/core/atlas/SheetData.java | 6 ----- .../crumbling/CrumblingInstanceManager.java | 3 --- .../crumbling/CrumblingMaterialManager.java | 18 +++++++------- .../flywheel/core/materials/OrientedData.java | 1 - .../flywheel/core/model/PartBuilder.java | 24 ++++++++----------- .../mixin/CancelEntityRenderMixin.java | 3 +-- .../mixin/CancelTileEntityRenderMixin.java | 3 +-- .../flywheel/mixin/TileWorldHookMixin.java | 1 - .../flywheel/mixin/atlas/AtlasDataMixin.java | 15 ++++++------ .../mixin/atlas/SheetDataAccessor.java | 4 ++-- .../mixin/light/LightUpdateMixin.java | 2 -- .../transform/QuaternionTransformStack.java | 11 ++------- .../com/jozufozu/flywheel/util/vec/Vec4.java | 1 - .../flywheel/vanilla/BellInstance.java | 7 ------ .../flywheel/vanilla/ChestInstance.java | 16 ++++--------- 18 files changed, 38 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java index 6df479ddc..e1525c919 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java @@ -17,7 +17,6 @@ import com.jozufozu.flywheel.backend.instancing.entity.EntityInstanceManager; import com.jozufozu.flywheel.backend.instancing.tile.TileInstanceManager; import com.jozufozu.flywheel.core.Contexts; import com.jozufozu.flywheel.core.crumbling.CrumblingInstanceManager; -import com.jozufozu.flywheel.core.crumbling.CrumblingProgram; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java index a2b5bc186..5c6a533f5 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/entity/EntityInstanceManager.java @@ -7,7 +7,6 @@ import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import com.jozufozu.flywheel.backend.instancing.MaterialManager; import net.minecraft.entity.Entity; -import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; import net.minecraft.world.IBlockReader; import net.minecraft.world.World; diff --git a/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java b/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java index 91333f875..5875e6136 100644 --- a/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java +++ b/src/main/java/com/jozufozu/flywheel/core/atlas/AtlasInfo.java @@ -1,5 +1,8 @@ package com.jozufozu.flywheel.core.atlas; +import java.util.HashMap; +import java.util.Map; + import com.jozufozu.flywheel.mixin.atlas.SheetDataAccessor; import net.minecraft.client.Minecraft; @@ -8,9 +11,6 @@ import net.minecraft.client.renderer.texture.Texture; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.util.ResourceLocation; -import java.util.HashMap; -import java.util.Map; - public class AtlasInfo { private static final Map sheetData = new HashMap<>(); diff --git a/src/main/java/com/jozufozu/flywheel/core/atlas/SheetData.java b/src/main/java/com/jozufozu/flywheel/core/atlas/SheetData.java index 9841e6899..e015ab5cf 100644 --- a/src/main/java/com/jozufozu/flywheel/core/atlas/SheetData.java +++ b/src/main/java/com/jozufozu/flywheel/core/atlas/SheetData.java @@ -1,11 +1,5 @@ package com.jozufozu.flywheel.core.atlas; -import net.minecraft.client.renderer.texture.TextureAtlasSprite; -import net.minecraft.util.ResourceLocation; - -import java.util.List; -import java.util.Set; - public class SheetData { public final int width; public final int height; diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java index 8d2cbc8eb..708f800ab 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java @@ -1,10 +1,7 @@ package com.jozufozu.flywheel.core.crumbling; -import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.tile.TileInstanceManager; -import com.jozufozu.flywheel.core.Contexts; - import net.minecraft.util.math.BlockPos; public class CrumblingInstanceManager extends TileInstanceManager { diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java index 2d8a6a8cb..5d9a01754 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java @@ -1,13 +1,19 @@ package com.jozufozu.flywheel.core.crumbling; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.glActiveTexture; + +import java.util.ArrayList; +import java.util.Map; + import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.MaterialRenderer; import com.jozufozu.flywheel.core.Contexts; -import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.core.atlas.AtlasInfo; import com.jozufozu.flywheel.core.atlas.SheetData; import com.jozufozu.flywheel.core.shader.IProgramCallback; -import com.jozufozu.flywheel.core.shader.WorldProgram; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.RenderType; @@ -16,14 +22,6 @@ import net.minecraft.inventory.container.PlayerContainer; import net.minecraft.util.ResourceLocation; import net.minecraft.util.math.vector.Matrix4f; -import java.util.ArrayList; -import java.util.Map; - -import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; -import static org.lwjgl.opengl.GL11.glBindTexture; -import static org.lwjgl.opengl.GL13.GL_TEXTURE0; -import static org.lwjgl.opengl.GL13.glActiveTexture; - public class CrumblingMaterialManager extends MaterialManager { public CrumblingMaterialManager() { 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 92e8f8a7c..a74bf3a73 100644 --- a/src/main/java/com/jozufozu/flywheel/core/materials/OrientedData.java +++ b/src/main/java/com/jozufozu/flywheel/core/materials/OrientedData.java @@ -2,7 +2,6 @@ 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; diff --git a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java index 721355dc2..5aa2e92d9 100644 --- a/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java +++ b/src/main/java/com/jozufozu/flywheel/core/model/PartBuilder.java @@ -1,25 +1,21 @@ package com.jozufozu.flywheel.core.model; -import static com.jozufozu.flywheel.util.RenderMath.*; - -import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; -import com.jozufozu.flywheel.backend.model.BufferedModel; - -import com.jozufozu.flywheel.backend.model.IndexedModel; - -import com.jozufozu.flywheel.core.Formats; - -import com.jozufozu.flywheel.core.atlas.AtlasInfo; - -import net.minecraft.client.renderer.texture.TextureAtlasSprite; -import net.minecraft.util.Direction; -import net.minecraft.util.math.vector.Vector3f; +import static com.jozufozu.flywheel.util.RenderMath.nb; import java.util.ArrayList; import java.util.EnumSet; import java.util.List; import java.util.Set; +import com.jozufozu.flywheel.backend.gl.buffer.VecBuffer; +import com.jozufozu.flywheel.backend.model.BufferedModel; +import com.jozufozu.flywheel.backend.model.IndexedModel; +import com.jozufozu.flywheel.core.Formats; + +import net.minecraft.client.renderer.texture.TextureAtlasSprite; +import net.minecraft.util.Direction; +import net.minecraft.util.math.vector.Vector3f; + public class PartBuilder { private float sizeU = 64.0F; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java index 1042fccc7..84e78bee5 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/CancelEntityRenderMixin.java @@ -3,8 +3,6 @@ package com.jozufozu.flywheel.mixin; import java.util.ArrayList; import java.util.Iterator; -import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; - import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Group; @@ -12,6 +10,7 @@ import org.spongepowered.asm.mixin.injection.Redirect; import com.google.common.collect.Lists; import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.world.ClientWorld; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java index 7a73e73ee..e26f9ef79 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/CancelTileEntityRenderMixin.java @@ -2,14 +2,13 @@ package com.jozufozu.flywheel.mixin; import java.util.List; -import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; - import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.backend.instancing.InstancedRenderRegistry; import net.minecraft.client.renderer.chunk.ChunkRenderDispatcher; import net.minecraft.tileentity.TileEntity; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/TileWorldHookMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/TileWorldHookMixin.java index 33b86d182..071709622 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/TileWorldHookMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/TileWorldHookMixin.java @@ -12,7 +12,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; -import com.jozufozu.flywheel.backend.instancing.tile.TileInstanceManager; import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/atlas/AtlasDataMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/atlas/AtlasDataMixin.java index 27c5eb5c8..d458c21f3 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/atlas/AtlasDataMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/atlas/AtlasDataMixin.java @@ -1,12 +1,6 @@ package com.jozufozu.flywheel.mixin.atlas; -import com.jozufozu.flywheel.core.atlas.AtlasInfo; - -import net.minecraft.client.renderer.texture.AtlasTexture; - -import net.minecraft.profiler.IProfiler; -import net.minecraft.resources.IResourceManager; -import net.minecraft.util.ResourceLocation; +import java.util.stream.Stream; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; @@ -14,7 +8,12 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; -import java.util.stream.Stream; +import com.jozufozu.flywheel.core.atlas.AtlasInfo; + +import net.minecraft.client.renderer.texture.AtlasTexture; +import net.minecraft.profiler.IProfiler; +import net.minecraft.resources.IResourceManager; +import net.minecraft.util.ResourceLocation; @Mixin(AtlasTexture.class) public abstract class AtlasDataMixin { diff --git a/src/main/java/com/jozufozu/flywheel/mixin/atlas/SheetDataAccessor.java b/src/main/java/com/jozufozu/flywheel/mixin/atlas/SheetDataAccessor.java index daa4fdd8e..d959b088f 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/atlas/SheetDataAccessor.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/atlas/SheetDataAccessor.java @@ -1,10 +1,10 @@ package com.jozufozu.flywheel.mixin.atlas; -import net.minecraft.client.renderer.texture.AtlasTexture; - import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; +import net.minecraft.client.renderer.texture.AtlasTexture; + @Mixin(AtlasTexture.SheetData.class) public interface SheetDataAccessor { diff --git a/src/main/java/com/jozufozu/flywheel/mixin/light/LightUpdateMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/light/LightUpdateMixin.java index 8168e7d46..2c5cddc0b 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/light/LightUpdateMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/light/LightUpdateMixin.java @@ -9,8 +9,6 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.jozufozu.flywheel.backend.instancing.InstanceManager; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; -import com.jozufozu.flywheel.backend.instancing.entity.EntityInstanceManager; -import com.jozufozu.flywheel.backend.instancing.tile.TileInstanceManager; import com.jozufozu.flywheel.light.LightUpdater; import com.jozufozu.flywheel.util.ChunkUtil; diff --git a/src/main/java/com/jozufozu/flywheel/util/transform/QuaternionTransformStack.java b/src/main/java/com/jozufozu/flywheel/util/transform/QuaternionTransformStack.java index 7e4fb84b0..364371b72 100644 --- a/src/main/java/com/jozufozu/flywheel/util/transform/QuaternionTransformStack.java +++ b/src/main/java/com/jozufozu/flywheel/util/transform/QuaternionTransformStack.java @@ -1,16 +1,9 @@ package com.jozufozu.flywheel.util.transform; -import com.google.common.collect.Lists; +import java.util.ArrayDeque; +import java.util.Deque; import net.minecraft.util.math.vector.Quaternion; -import net.minecraft.util.math.vector.Vector3d; -import net.minecraft.util.math.vector.Vector3f; - -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Deque; -import java.util.Stack; -import java.util.Vector; public class QuaternionTransformStack implements TransformStack { diff --git a/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java b/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java index 668751fa9..3b2dbd185 100644 --- a/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java +++ b/src/main/java/com/jozufozu/flywheel/util/vec/Vec4.java @@ -1,7 +1,6 @@ package com.jozufozu.flywheel.util.vec; import net.minecraft.util.math.vector.Quaternion; -import net.minecraft.util.math.vector.Vector3f; public class Vec4 { diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java index 6380e49e2..3502222b3 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java @@ -2,23 +2,16 @@ package com.jozufozu.flywheel.vanilla; import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; import com.jozufozu.flywheel.backend.instancing.MaterialManager; -import com.jozufozu.flywheel.backend.instancing.MaterialSpec; import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; - import com.jozufozu.flywheel.backend.model.BufferedModel; import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.materials.OrientedData; - import com.jozufozu.flywheel.core.model.ModelPart; -import com.jozufozu.flywheel.core.model.PartBuilder; - import com.jozufozu.flywheel.util.AnimationTickHolder; import net.minecraft.client.renderer.tileentity.BellTileEntityRenderer; import net.minecraft.tileentity.BellTileEntity; -import net.minecraft.util.Direction; import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.vector.Quaternion; import net.minecraft.util.math.vector.Vector3f; public class BellInstance extends TileEntityInstance implements IDynamicInstance { diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java index f8108ebdc..14d4e57bd 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/ChestInstance.java @@ -1,21 +1,19 @@ package com.jozufozu.flywheel.vanilla; +import java.util.Calendar; + +import javax.annotation.Nonnull; + import com.jozufozu.flywheel.backend.instancing.IDynamicInstance; import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.tile.TileEntityInstance; - import com.jozufozu.flywheel.backend.model.BufferedModel; import com.jozufozu.flywheel.core.Materials; import com.jozufozu.flywheel.core.materials.ModelData; 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.transform.MatrixTransformStack; -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.AbstractChestBlock; @@ -29,13 +27,7 @@ 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.Vector3d; 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 { From d2350667a2c766ea7bb818ee672843a95e3601de Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 12 Jul 2021 15:19:20 -0700 Subject: [PATCH 10/12] Fix bell not stopping at the bottom of its swing --- .../com/jozufozu/flywheel/vanilla/BellInstance.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java index 3502222b3..5dafb5b6e 100644 --- a/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java +++ b/src/main/java/com/jozufozu/flywheel/vanilla/BellInstance.java @@ -12,12 +12,15 @@ import com.jozufozu.flywheel.util.AnimationTickHolder; import net.minecraft.client.renderer.tileentity.BellTileEntityRenderer; import net.minecraft.tileentity.BellTileEntity; import net.minecraft.util.math.MathHelper; +import net.minecraft.util.math.vector.Quaternion; import net.minecraft.util.math.vector.Vector3f; public class BellInstance extends TileEntityInstance implements IDynamicInstance { private final OrientedData bell; + private float lastRingTime = Float.NaN; + public BellInstance(MaterialManager materialManager, BellTileEntity tile) { super(materialManager, tile); @@ -28,13 +31,19 @@ public class BellInstance extends TileEntityInstance implements @Override public void beginFrame() { + float ringTime = (float)tile.ringingTicks + AnimationTickHolder.getPartialTicks(); + + if (ringTime == lastRingTime) return; + lastRingTime = ringTime; + if (tile.isRinging) { - float ringness = (float)tile.ringingTicks + AnimationTickHolder.getPartialTicks(); - float angle = MathHelper.sin(ringness / (float)Math.PI) / (4.0F + ringness / 3.0F); + float angle = MathHelper.sin(ringTime / (float) Math.PI) / (4.0F + ringTime / 3.0F); Vector3f ringAxis = tile.ringDirection.rotateYCCW().getUnitVector(); bell.setRotation(ringAxis.getRadialQuaternion(angle)); + } else { + bell.setRotation(Quaternion.IDENTITY); } } From a2100dc758655025d5824c30747a0a0a3ca91ca1 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 12 Jul 2021 16:42:43 -0700 Subject: [PATCH 11/12] Clean up - Material managers should not be inside WorldContext - InstancedRenderDispatcher now stores the material managers - Delete crumbling material manager on renderer reload - CrumblingRenderer gets its own class - CrumblingRenderer is less jank overall - Defer InstancedRenderRegistry deprecated function removal until 0.3 --- .../flywheel/backend/ShaderSources.java | 2 - .../backend/instancing/CrumblingRenderer.java | 130 ++++++++++++++++++ .../instancing/InstancedRenderDispatcher.java | 98 +------------ .../instancing/InstancedRenderRegistry.java | 4 +- .../jozufozu/flywheel/core/WorldContext.java | 16 --- .../crumbling/CrumblingInstanceManager.java | 6 +- .../crumbling/CrumblingMaterialManager.java | 6 +- .../jozufozu/flywheel/core/package-info.java | 6 + .../flywheel/mixin/RenderHooksMixin.java | 3 +- .../jozufozu/flywheel/util/WorldAttached.java | 2 +- 10 files changed, 155 insertions(+), 118 deletions(-) create mode 100644 src/main/java/com/jozufozu/flywheel/backend/instancing/CrumblingRenderer.java create mode 100644 src/main/java/com/jozufozu/flywheel/core/package-info.java diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java index ede6c9b81..8ceb05d3c 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java @@ -17,7 +17,6 @@ import java.util.function.Predicate; import java.util.stream.Stream; import javax.annotation.Nonnull; -import javax.annotation.ParametersAreNonnullByDefault; import org.lwjgl.system.MemoryUtil; @@ -47,7 +46,6 @@ import net.minecraftforge.resource.IResourceType; import net.minecraftforge.resource.ISelectiveResourceReloadListener; import net.minecraftforge.resource.VanillaResourceType; -@ParametersAreNonnullByDefault public class ShaderSources implements ISelectiveResourceReloadListener { public static final String SHADER_DIR = "flywheel/shaders/"; public static final String PROGRAM_DIR = "flywheel/programs/"; diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/CrumblingRenderer.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/CrumblingRenderer.java new file mode 100644 index 000000000..bb8e44ce2 --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/CrumblingRenderer.java @@ -0,0 +1,130 @@ +package com.jozufozu.flywheel.backend.instancing; + +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.glBindTexture; +import static org.lwjgl.opengl.GL13.GL_TEXTURE0; +import static org.lwjgl.opengl.GL13.GL_TEXTURE4; +import static org.lwjgl.opengl.GL13.glActiveTexture; + +import java.util.ArrayList; +import java.util.List; +import java.util.SortedSet; + +import com.jozufozu.flywheel.backend.Backend; +import com.jozufozu.flywheel.core.Contexts; +import com.jozufozu.flywheel.core.crumbling.CrumblingInstanceManager; +import com.jozufozu.flywheel.core.crumbling.CrumblingMaterialManager; +import com.jozufozu.flywheel.core.crumbling.CrumblingProgram; +import com.jozufozu.flywheel.event.ReloadRenderersEvent; + +import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.longs.Long2ObjectMap; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.ActiveRenderInfo; +import net.minecraft.client.renderer.DestroyBlockProgress; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.model.ModelBakery; +import net.minecraft.client.renderer.texture.Texture; +import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.client.world.ClientWorld; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.LazyValue; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.vector.Matrix4f; +import net.minecraftforge.api.distmarker.Dist; +import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +/** + * Responsible for rendering the block breaking overlay for instanced tiles. + */ +@OnlyIn(Dist.CLIENT) +@Mod.EventBusSubscriber(Dist.CLIENT) +public class CrumblingRenderer { + + private static final LazyValue> materialManager = new LazyValue<>(() -> new CrumblingMaterialManager(Contexts.CRUMBLING)); + private static final LazyValue> manager = new LazyValue<>(() -> new CrumblingInstanceManager(materialManager.getValue())); + + private static final RenderType crumblingLayer = ModelBakery.BLOCK_DESTRUCTION_RENDER_LAYERS.get(0); + + public static void renderBreaking(ClientWorld world, Matrix4f viewProjection, double cameraX, double cameraY, double cameraZ) { + if (!Backend.getInstance() + .canUseInstancing(world)) return; + + Int2ObjectMap> activeStages = getActiveStageTiles(world); + + if (activeStages.isEmpty()) return; + + InstanceManager renderer = manager.getValue(); + + TextureManager textureManager = Minecraft.getInstance().textureManager; + ActiveRenderInfo info = Minecraft.getInstance().gameRenderer.getActiveRenderInfo(); + + MaterialManager materials = materialManager.getValue(); + crumblingLayer.startDrawing(); + + for (Int2ObjectMap.Entry> stage : activeStages.int2ObjectEntrySet()) { + int i = stage.getIntKey(); + Texture breaking = textureManager.getTexture(ModelBakery.BLOCK_DESTRUCTION_STAGE_TEXTURES.get(i)); + + // something about when we call this means that the textures are not ready for use on the first frame they should appear + if (breaking != null) { + stage.getValue().forEach(renderer::add); + + renderer.beginFrame(info); + + glActiveTexture(GL_TEXTURE4); + glBindTexture(GL_TEXTURE_2D, breaking.getGlTextureId()); + materials.render(RenderType.getCutoutMipped(), viewProjection, cameraX, cameraY, cameraZ); + + renderer.invalidate(); + } + + } + + crumblingLayer.endDrawing(); + + glActiveTexture(GL_TEXTURE0); + Texture breaking = textureManager.getTexture(ModelBakery.BLOCK_DESTRUCTION_STAGE_TEXTURES.get(0)); + if (breaking != null) glBindTexture(GL_TEXTURE_2D, breaking.getGlTextureId()); + } + + /** + * Associate each breaking stage with a list of all tile entities at that stage. + */ + private static Int2ObjectMap> getActiveStageTiles(ClientWorld world) { + Long2ObjectMap> breakingProgressions = Minecraft.getInstance().worldRenderer.blockBreakingProgressions; + + Int2ObjectMap> breakingEntities = new Int2ObjectArrayMap<>(); + + for (Long2ObjectMap.Entry> entry : breakingProgressions.long2ObjectEntrySet()) { + BlockPos breakingPos = BlockPos.fromLong(entry.getLongKey()); + + SortedSet progresses = entry.getValue(); + if (progresses != null && !progresses.isEmpty()) { + int blockDamage = progresses.last() + .getPartialBlockDamage(); + + TileEntity tileEntity = world.getTileEntity(breakingPos); + + if (tileEntity != null) { + List tileEntities = breakingEntities.computeIfAbsent(blockDamage, $ -> new ArrayList<>()); + tileEntities.add(tileEntity); + } + } + } + + return breakingEntities; + } + + @SubscribeEvent + public static void onReloadRenderers(ReloadRenderersEvent event) { + ClientWorld world = event.getWorld(); + if (Backend.getInstance() + .canUseInstancing() && world != null) { + materialManager.getValue().delete(); + } + } +} diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java index e1525c919..5800b0f5a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderDispatcher.java @@ -1,44 +1,22 @@ package com.jozufozu.flywheel.backend.instancing; -import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; -import static org.lwjgl.opengl.GL11.glBindTexture; -import static org.lwjgl.opengl.GL13.GL_TEXTURE0; -import static org.lwjgl.opengl.GL13.GL_TEXTURE4; -import static org.lwjgl.opengl.GL13.glActiveTexture; - -import java.util.BitSet; -import java.util.SortedSet; -import java.util.Vector; - import javax.annotation.Nonnull; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.instancing.entity.EntityInstanceManager; import com.jozufozu.flywheel.backend.instancing.tile.TileInstanceManager; import com.jozufozu.flywheel.core.Contexts; -import com.jozufozu.flywheel.core.crumbling.CrumblingInstanceManager; +import com.jozufozu.flywheel.core.shader.WorldProgram; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; import com.jozufozu.flywheel.event.RenderLayerEvent; import com.jozufozu.flywheel.util.AnimationTickHolder; import com.jozufozu.flywheel.util.WorldAttached; -import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.ActiveRenderInfo; -import net.minecraft.client.renderer.DestroyBlockProgress; -import net.minecraft.client.renderer.RenderType; -import net.minecraft.client.renderer.WorldRenderer; -import net.minecraft.client.renderer.model.ModelBakery; -import net.minecraft.client.renderer.texture.Texture; -import net.minecraft.client.renderer.texture.TextureManager; import net.minecraft.client.world.ClientWorld; import net.minecraft.entity.Entity; -import net.minecraft.inventory.container.PlayerContainer; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.LazyValue; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.vector.Matrix4f; import net.minecraft.world.IWorld; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -50,16 +28,10 @@ import net.minecraftforge.fml.common.Mod; @Mod.EventBusSubscriber(Dist.CLIENT) public class InstancedRenderDispatcher { - private static final WorldAttached> entityInstanceManager = new WorldAttached<>(world -> new EntityInstanceManager(Contexts.WORLD.getMaterialManager(world))); - private static final WorldAttached> tileInstanceManager = new WorldAttached<>(world -> new TileInstanceManager(Contexts.WORLD.getMaterialManager(world))); + private static final WorldAttached> materialManagers = new WorldAttached<>($ -> new MaterialManager<>(Contexts.WORLD)); - private static final LazyValue> blockBreaking = new LazyValue<>(() -> { - Vector renderers = new Vector<>(10); - for (int i = 0; i < 10; i++) { - renderers.add(new CrumblingInstanceManager()); - } - return renderers; - }); + private static final WorldAttached> entityInstanceManager = new WorldAttached<>(world -> new EntityInstanceManager(materialManagers.get(world))); + private static final WorldAttached> tileInstanceManager = new WorldAttached<>(world -> new TileInstanceManager(materialManagers.get(world))); @Nonnull public static InstanceManager getTiles(IWorld world) { @@ -99,7 +71,7 @@ public class InstancedRenderDispatcher { @SubscribeEvent public static void onBeginFrame(BeginFrameEvent event) { - Contexts.WORLD.getMaterialManager(event.getWorld()) + materialManagers.get(event.getWorld()) .checkAndShiftOrigin(event.getInfo()); getTiles(event.getWorld()).beginFrame(event.getInfo()); @@ -114,7 +86,7 @@ public class InstancedRenderDispatcher { event.type.startDrawing(); - Contexts.WORLD.getMaterialManager(world) + materialManagers.get(world) .render(event.type, event.viewProjection, event.camX, event.camY, event.camZ); event.type.endDrawing(); @@ -129,64 +101,8 @@ public class InstancedRenderDispatcher { } } - private static final RenderType crumblingLayer = ModelBakery.BLOCK_DESTRUCTION_RENDER_LAYERS.get(0); - - public static void renderBreaking(ClientWorld world, Matrix4f viewProjection, double cameraX, double cameraY, double cameraZ) { - if (!Backend.getInstance() - .canUseInstancing(world)) return; - - WorldRenderer worldRenderer = Minecraft.getInstance().worldRenderer; - Long2ObjectMap> breakingProgressions = worldRenderer.blockBreakingProgressions; - - if (breakingProgressions.isEmpty()) return; - Vector renderers = blockBreaking.getValue(); - - BitSet bitSet = new BitSet(10); - - for (Long2ObjectMap.Entry> entry : breakingProgressions.long2ObjectEntrySet()) { - BlockPos breakingPos = BlockPos.fromLong(entry.getLongKey()); - - SortedSet progresses = entry.getValue(); - if (progresses != null && !progresses.isEmpty()) { - int blockDamage = progresses.last() - .getPartialBlockDamage(); - bitSet.set(blockDamage); - renderers.get(blockDamage) - .add(world.getTileEntity(breakingPos)); - } - } - - TextureManager textureManager = Minecraft.getInstance().textureManager; - ActiveRenderInfo info = Minecraft.getInstance().gameRenderer.getActiveRenderInfo(); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, textureManager.getTexture(PlayerContainer.BLOCK_ATLAS_TEXTURE) - .getGlTextureId()); - - crumblingLayer.startDrawing(); - bitSet.stream() - .forEach(i -> { - Texture breaking = textureManager.getTexture(ModelBakery.BLOCK_DESTRUCTION_STAGE_TEXTURES.get(i)); - CrumblingInstanceManager renderer = renderers.get(i); - renderer.beginFrame(info); - - if (breaking != null) { - glActiveTexture(GL_TEXTURE4); - glBindTexture(GL_TEXTURE_2D, breaking.getGlTextureId()); - renderer.materialManager.render(RenderType.getCutoutMipped(), viewProjection, cameraX, cameraY, cameraZ); - } - - renderer.invalidate(); - }); - crumblingLayer.endDrawing(); - - glActiveTexture(GL_TEXTURE0); - Texture breaking = textureManager.getTexture(ModelBakery.BLOCK_DESTRUCTION_STAGE_TEXTURES.get(0)); - if (breaking != null) glBindTexture(GL_TEXTURE_2D, breaking.getGlTextureId()); - } - public static void loadAllInWorld(ClientWorld world) { - Contexts.WORLD.getMaterialManager(world) + materialManagers.get(world) .delete(); InstanceManager tiles = tileInstanceManager.replace(world); 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 ee0c37e07..76a883bad 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstancedRenderRegistry.java @@ -57,7 +57,7 @@ public class InstancedRenderRegistry { } /** - * @deprecated will be removed in 0.2.0, use {@link #tile} + * @deprecated will be removed in 0.3.0, use {@link #tile} */ @Deprecated public void register(TileEntityType type, ITileInstanceFactory rendererFactory) { @@ -66,7 +66,7 @@ public class InstancedRenderRegistry { } /** - * @deprecated will be removed in 0.2.0, use {@link #entity} + * @deprecated will be removed in 0.3.0, use {@link #entity} */ @Deprecated public void register(EntityType type, IEntityInstanceFactory rendererFactory) { diff --git a/src/main/java/com/jozufozu/flywheel/core/WorldContext.java b/src/main/java/com/jozufozu/flywheel/core/WorldContext.java index a387fea1c..44d125f1f 100644 --- a/src/main/java/com/jozufozu/flywheel/core/WorldContext.java +++ b/src/main/java/com/jozufozu/flywheel/core/WorldContext.java @@ -12,7 +12,6 @@ import com.jozufozu.flywheel.backend.ResourceUtil; import com.jozufozu.flywheel.backend.ShaderContext; import com.jozufozu.flywheel.backend.ShaderSources; import com.jozufozu.flywheel.backend.gl.shader.ShaderType; -import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.MaterialSpec; import com.jozufozu.flywheel.backend.loading.InstancedArraysTemplate; import com.jozufozu.flywheel.backend.loading.Program; @@ -23,10 +22,8 @@ import com.jozufozu.flywheel.backend.loading.ShaderTransformer; import com.jozufozu.flywheel.core.shader.ExtensibleGlProgram; import com.jozufozu.flywheel.core.shader.StateSensitiveMultiProgram; import com.jozufozu.flywheel.core.shader.WorldProgram; -import com.jozufozu.flywheel.util.WorldAttached; import net.minecraft.util.ResourceLocation; -import net.minecraft.world.IWorld; public class WorldContext

extends ShaderContext

{ @@ -37,8 +34,6 @@ public class WorldContext

extends ShaderContext

{ protected Supplier> specStream; protected TemplateFactory templateFactory; - private final WorldAttached> worldAttachedMMs = new WorldAttached<>($ -> new MaterialManager<>(this)); - private final Map builtins = new EnumMap<>(ShaderType.class); private final Map builtinSources = new EnumMap<>(ShaderType.class); @@ -69,10 +64,6 @@ public class WorldContext

extends ShaderContext

{ return this; } - public MaterialManager

getMaterialManager(IWorld world) { - return worldAttachedMMs.get(world); - } - public WorldContext

withSpecStream(Supplier> specStream) { this.specStream = specStream; return this; @@ -122,13 +113,6 @@ public class WorldContext

extends ShaderContext

{ }); } - @Override - public void delete() { - super.delete(); - - worldAttachedMMs.empty(MaterialManager::delete); - } - @Override protected Shader getSource(ShaderType type, ResourceLocation name) { Shader source = super.getSource(type, name); diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java index 708f800ab..3e3446a88 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingInstanceManager.java @@ -1,12 +1,14 @@ package com.jozufozu.flywheel.core.crumbling; +import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.tile.TileInstanceManager; import net.minecraft.util.math.BlockPos; public class CrumblingInstanceManager extends TileInstanceManager { - public CrumblingInstanceManager() { - super(new CrumblingMaterialManager()); + + public CrumblingInstanceManager(MaterialManager materialManager) { + super(materialManager); } @Override diff --git a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java index 5d9a01754..9d8974ade 100644 --- a/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java +++ b/src/main/java/com/jozufozu/flywheel/core/crumbling/CrumblingMaterialManager.java @@ -10,7 +10,7 @@ import java.util.Map; import com.jozufozu.flywheel.backend.instancing.MaterialManager; import com.jozufozu.flywheel.backend.instancing.MaterialRenderer; -import com.jozufozu.flywheel.core.Contexts; +import com.jozufozu.flywheel.core.WorldContext; import com.jozufozu.flywheel.core.atlas.AtlasInfo; import com.jozufozu.flywheel.core.atlas.SheetData; import com.jozufozu.flywheel.core.shader.IProgramCallback; @@ -24,8 +24,8 @@ import net.minecraft.util.math.vector.Matrix4f; public class CrumblingMaterialManager extends MaterialManager { - public CrumblingMaterialManager() { - super(Contexts.CRUMBLING); + public CrumblingMaterialManager(WorldContext context) { + super(context); } /** diff --git a/src/main/java/com/jozufozu/flywheel/core/package-info.java b/src/main/java/com/jozufozu/flywheel/core/package-info.java new file mode 100644 index 000000000..4cef387dc --- /dev/null +++ b/src/main/java/com/jozufozu/flywheel/core/package-info.java @@ -0,0 +1,6 @@ +@ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault +package com.jozufozu.flywheel.core; + +import javax.annotation.ParametersAreNonnullByDefault; + +import mcp.MethodsReturnNonnullByDefault; diff --git a/src/main/java/com/jozufozu/flywheel/mixin/RenderHooksMixin.java b/src/main/java/com/jozufozu/flywheel/mixin/RenderHooksMixin.java index c28b21686..7ef6abf64 100644 --- a/src/main/java/com/jozufozu/flywheel/mixin/RenderHooksMixin.java +++ b/src/main/java/com/jozufozu/flywheel/mixin/RenderHooksMixin.java @@ -9,6 +9,7 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import com.jozufozu.flywheel.backend.Backend; import com.jozufozu.flywheel.backend.OptifineHandler; +import com.jozufozu.flywheel.backend.instancing.CrumblingRenderer; import com.jozufozu.flywheel.backend.instancing.InstancedRenderDispatcher; import com.jozufozu.flywheel.event.BeginFrameEvent; import com.jozufozu.flywheel.event.ReloadRenderersEvent; @@ -81,7 +82,7 @@ public class RenderHooksMixin { .getProjectionMatrix()); Vector3d cameraPos = info.getProjectedView(); - InstancedRenderDispatcher.renderBreaking(world, viewProjection, cameraPos.x, cameraPos.y, cameraPos.z); + CrumblingRenderer.renderBreaking(world, viewProjection, cameraPos.x, cameraPos.y, cameraPos.z); if (!OptifineHandler.usingShaders()) GL20.glUseProgram(0); } diff --git a/src/main/java/com/jozufozu/flywheel/util/WorldAttached.java b/src/main/java/com/jozufozu/flywheel/util/WorldAttached.java index 2859ad40b..4d4d944e9 100644 --- a/src/main/java/com/jozufozu/flywheel/util/WorldAttached.java +++ b/src/main/java/com/jozufozu/flywheel/util/WorldAttached.java @@ -12,7 +12,7 @@ import net.minecraft.world.IWorld; public class WorldAttached { - Map attached; + private final Map attached; private final Function factory; public WorldAttached(Function factory) { From 126cecd856546ab883bbdc6e47f24d77ce453057 Mon Sep 17 00:00:00 2001 From: Jozufozu Date: Mon, 12 Jul 2021 16:45:36 -0700 Subject: [PATCH 12/12] Protect against java versioning - Cast ByteBuffers to Buffer before calling #rewind or #flip --- src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java | 3 ++- .../com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java | 3 ++- .../jozufozu/flywheel/backend/instancing/InstanceMaterial.java | 3 ++- src/main/java/com/jozufozu/flywheel/core/QuadConverter.java | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java b/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java index 8ceb05d3c..5cd50306f 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java +++ b/src/main/java/com/jozufozu/flywheel/backend/ShaderSources.java @@ -5,6 +5,7 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; @@ -182,7 +183,7 @@ public class ShaderSources implements ISelectiveResourceReloadListener { try { bytebuffer = readToBuffer(is); int i = bytebuffer.position(); - bytebuffer.rewind(); + ((Buffer) bytebuffer).rewind(); return MemoryUtil.memASCII(bytebuffer, i); } catch (IOException e) { diff --git a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java index 5291cb324..801dab17a 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java +++ b/src/main/java/com/jozufozu/flywheel/backend/gl/buffer/VecBuffer.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.backend.gl.buffer; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -29,7 +30,7 @@ public class VecBuffer { } public VecBuffer rewind() { - internal.rewind(); + ((Buffer) internal).rewind(); return this; } diff --git a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceMaterial.java b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceMaterial.java index 608af6c89..5266a2759 100644 --- a/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceMaterial.java +++ b/src/main/java/com/jozufozu/flywheel/backend/instancing/InstanceMaterial.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.backend.instancing; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; @@ -137,7 +138,7 @@ public class InstanceMaterial { vertices.putFloat(reader.getV(i)); } - vertices.rewind(); + ((Buffer) vertices).rewind(); // return new BufferedModel(GlPrimitive.QUADS, format, vertices, vertexCount); diff --git a/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java b/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java index 1c717bee6..287cf37d5 100644 --- a/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java +++ b/src/main/java/com/jozufozu/flywheel/core/QuadConverter.java @@ -1,5 +1,6 @@ package com.jozufozu.flywheel.core; +import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.EnumMap; @@ -136,7 +137,7 @@ public class QuadConverter { type.castAndBuffer(indices, i + 2); type.castAndBuffer(indices, i + 3); } - indices.flip(); + ((Buffer) indices).flip(); } private GlBuffer getBuffer(GlNumericType type) {