From c44908b2105f290998d12d4f467625337db0fae4 Mon Sep 17 00:00:00 2001 From: tterrag Date: Sun, 22 Mar 2020 15:19:01 -0400 Subject: [PATCH] Initial experimental rendering changes --- .../block/SafeTileEntityRenderer.java | 26 ++++---- .../block/SafeTileEntityRendererFast.java | 25 ------- .../foundation/utility/SuperByteBuffer.java | 66 ++++++++----------- .../utility/SuperByteBufferCache.java | 12 ++-- .../base/KineticTileEntityRenderer.java | 47 +++++++------ .../contraptions/ContraptionRenderer.java | 9 ++- 6 files changed, 78 insertions(+), 107 deletions(-) delete mode 100644 src/main/java/com/simibubi/create/foundation/block/SafeTileEntityRendererFast.java diff --git a/src/main/java/com/simibubi/create/foundation/block/SafeTileEntityRenderer.java b/src/main/java/com/simibubi/create/foundation/block/SafeTileEntityRenderer.java index a6c8aaab4..6056df8c5 100644 --- a/src/main/java/com/simibubi/create/foundation/block/SafeTileEntityRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/block/SafeTileEntityRenderer.java @@ -1,31 +1,27 @@ package com.simibubi.create.foundation.block; +import com.mojang.blaze3d.matrix.MatrixStack; + import net.minecraft.block.Blocks; -import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.tileentity.TileEntityRenderer; +import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.tileentity.TileEntity; public abstract class SafeTileEntityRenderer extends TileEntityRenderer { + + public SafeTileEntityRenderer(TileEntityRendererDispatcher dispatcher) { + super(dispatcher); + } @Override - public final void render(T te, double x, double y, double z, float partialTicks, int destroyStage) { + public void render(T te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay) { if (isInvalid(te)) return; - renderWithGL(te, x, y, z, partialTicks, destroyStage); + renderSafe(te, partialTicks, ms, buffer, light, overlay); } - protected abstract void renderWithGL(T tileEntityIn, double x, double y, double z, float partialTicks, int destroyStage); - - @Override - public final void renderTileEntityFast(T te, double x, double y, double z, float partialTicks, int destroyStage, - BufferBuilder buffer) { - if (isInvalid(te)) - return; - renderFast(te, x, y, z, partialTicks, destroyStage, buffer); - } - - protected void renderFast(T tileEntityIn, double x, double y, double z, float partialTicks, int destroyStage, BufferBuilder buffer) { - } + protected abstract void renderSafe(T te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, int light, int overlay); public boolean isInvalid(T te) { return te.getBlockState().getBlock() == Blocks.AIR; diff --git a/src/main/java/com/simibubi/create/foundation/block/SafeTileEntityRendererFast.java b/src/main/java/com/simibubi/create/foundation/block/SafeTileEntityRendererFast.java deleted file mode 100644 index 13e7523bf..000000000 --- a/src/main/java/com/simibubi/create/foundation/block/SafeTileEntityRendererFast.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.simibubi.create.foundation.block; - -import net.minecraft.block.Blocks; -import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.tileentity.TileEntity; -import net.minecraftforge.client.model.animation.TileEntityRendererFast; - -public abstract class SafeTileEntityRendererFast extends TileEntityRendererFast { - - @Override - public final void renderTileEntityFast(T te, double x, double y, double z, float partialTicks, int destroyStage, - BufferBuilder buffer) { - if (isInvalid(te)) - return; - renderFast(te, x, y, z, partialTicks, destroyStage, buffer); - } - - protected abstract void renderFast(T te, double x, double y, double z, float partialTicks, int destroyStage, - BufferBuilder buffer); - - public boolean isInvalid(T te) { - return te.getBlockState().getBlock() == Blocks.AIR; - } - -} diff --git a/src/main/java/com/simibubi/create/foundation/utility/SuperByteBuffer.java b/src/main/java/com/simibubi/create/foundation/utility/SuperByteBuffer.java index e74dc2c64..ee8517b93 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/SuperByteBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/utility/SuperByteBuffer.java @@ -2,13 +2,17 @@ package com.simibubi.create.foundation.utility; import java.nio.ByteBuffer; -import javax.vecmath.Matrix4f; +import com.mojang.blaze3d.matrix.MatrixStack; +import com.mojang.blaze3d.vertex.IVertexBuilder; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GLAllocation; +import net.minecraft.client.renderer.Matrix4f; +import net.minecraft.client.renderer.Vector4f; import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import net.minecraft.util.Direction.Axis; +import net.minecraft.util.Direction; +import net.minecraftforge.fml.common.ObfuscationReflectionHelper; public class SuperByteBuffer { @@ -21,8 +25,7 @@ public class SuperByteBuffer { protected ByteBuffer mutable; // Vertex Position - private Matrix4f transforms; - private Matrix4f t; + private MatrixStack transforms; // Vertex Texture Coords private boolean shouldShiftUV; @@ -38,7 +41,8 @@ public class SuperByteBuffer { private boolean shouldColor; private int r, g, b, a; - public SuperByteBuffer(ByteBuffer original) { + public SuperByteBuffer(BufferBuilder buf) { + ByteBuffer original = ObfuscationReflectionHelper.getPrivateValue(BufferBuilder.class, buf, "field_179001_a"); // FIXME speedup original.rewind(); this.original = original; @@ -48,28 +52,20 @@ public class SuperByteBuffer { mutable.put(this.original); mutable.rewind(); - t = new Matrix4f(); - transforms = new Matrix4f(); - transforms.setIdentity(); + transforms = new MatrixStack(); } - public ByteBuffer build() { + public ByteBuffer build(MatrixStack input) { original.rewind(); mutable.rewind(); - float x, y, z = 0; - float x2, y2, z2 = 0; - Matrix4f t = transforms; + Matrix4f t = transforms.peek().getModel(); + t.multiply(input.peek().getModel()); for (int vertex = 0; vertex < vertexCount(original); vertex++) { - x = getX(original, vertex); - y = getY(original, vertex); - z = getZ(original, vertex); - - x2 = t.m00 * x + t.m01 * y + t.m02 * z + t.m03; - y2 = t.m10 * x + t.m11 * y + t.m12 * z + t.m13; - z2 = t.m20 * x + t.m21 * y + t.m22 * z + t.m23; - - putPos(mutable, vertex, x2, y2, z2); + Vector4f pos = new Vector4f(getX(original, vertex), getY(original, vertex), getZ(original, vertex), 1F); + + pos.transform(t); + putPos(mutable, vertex, pos.getX(), pos.getY(), pos.getZ()); if (shouldColor) { byte lumByte = getR(original, vertex); @@ -86,23 +82,26 @@ public class SuperByteBuffer { if (shouldLight) { if (vertexLighter != null) putLight(mutable, vertex, - vertexLighter.getPackedLight(x2 + lightOffsetX, y2 + lightOffsetY, z2 + lightOffsetZ)); + vertexLighter.getPackedLight(pos.getX() + lightOffsetX, pos.getY() + lightOffsetY, pos.getZ() + lightOffsetZ)); else putLight(mutable, vertex, packedLightCoords); } } - t.setIdentity(); + transforms = new MatrixStack(); shouldShiftUV = false; shouldColor = false; shouldLight = false; return mutable; } - public void renderInto(BufferBuilder buffer) { + public void renderInto(MatrixStack input, IVertexBuilder buffer) { if (original.limit() == 0) return; - buffer.putBulkData(build()); + if (!(buffer instanceof BufferBuilder)) { + throw new IllegalArgumentException("Unsupported buffer type!"); + } + ((BufferBuilder)buffer).putBulkData(build(input)); } public SuperByteBuffer translate(double x, double y, double z) { @@ -110,27 +109,18 @@ public class SuperByteBuffer { } public SuperByteBuffer translate(float x, float y, float z) { - transforms.m03 += x; - transforms.m13 += y; - transforms.m23 += z; + transforms.translate(x, y, z); return this; } - public SuperByteBuffer rotate(Axis axis, float angle) { + public SuperByteBuffer rotate(Direction axis, float angle) { if (angle == 0) return this; - t.setIdentity(); - if (axis == Axis.X) - t.rotX(angle); - else if (axis == Axis.Y) - t.rotY(angle); - else - t.rotZ(angle); - transforms.mul(t, transforms); + transforms.multiply(axis.getUnitVector().getDegreesQuaternion(angle)); return this; } - public SuperByteBuffer rotateCentered(Axis axis, float angle) { + public SuperByteBuffer rotateCentered(Direction axis, float angle) { return translate(-.5f, -.5f, -.5f).rotate(axis, angle).translate(.5f, .5f, .5f); } diff --git a/src/main/java/com/simibubi/create/foundation/utility/SuperByteBufferCache.java b/src/main/java/com/simibubi/create/foundation/utility/SuperByteBufferCache.java index 2c891e32a..88f9d13d6 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/SuperByteBufferCache.java +++ b/src/main/java/com/simibubi/create/foundation/utility/SuperByteBufferCache.java @@ -11,6 +11,7 @@ import org.lwjgl.opengl.GL11; import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.AllBlockPartials; import net.minecraft.block.BlockState; @@ -19,6 +20,7 @@ import net.minecraft.client.renderer.BlockModelRenderer; import net.minecraft.client.renderer.BlockRendererDispatcher; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.model.IBakedModel; +import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.util.math.BlockPos; import net.minecraftforge.client.model.data.EmptyModelData; @@ -84,14 +86,16 @@ public class SuperByteBufferCache { BlockModelRenderer blockRenderer = dispatcher.getBlockModelRenderer(); BufferBuilder builder = new BufferBuilder(0); Random random = new Random(); + MatrixStack ms = new MatrixStack(); + ms.push(); + ms.translate(0, 1, 0); - builder.setTranslation(0, 1, 0); builder.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK); - blockRenderer.renderModelFlat(Minecraft.getInstance().world, model, referenceState, BlockPos.ZERO.down(), - builder, true, random, 42, EmptyModelData.INSTANCE); + blockRenderer.renderModelFlat(Minecraft.getInstance().world, model, referenceState, BlockPos.ZERO.down(), ms, + builder, true, random, 42, OverlayTexture.DEFAULT_UV, EmptyModelData.INSTANCE); builder.finishDrawing(); - return new SuperByteBuffer(builder.getByteBuffer()); + return new SuperByteBuffer(builder); } public void invalidate() { diff --git a/src/main/java/com/simibubi/create/modules/contraptions/base/KineticTileEntityRenderer.java b/src/main/java/com/simibubi/create/modules/contraptions/base/KineticTileEntityRenderer.java index fc111c54b..6dd08a981 100644 --- a/src/main/java/com/simibubi/create/modules/contraptions/base/KineticTileEntityRenderer.java +++ b/src/main/java/com/simibubi/create/modules/contraptions/base/KineticTileEntityRenderer.java @@ -1,8 +1,10 @@ package com.simibubi.create.modules.contraptions.base; +import com.mojang.blaze3d.matrix.MatrixStack; +import com.mojang.blaze3d.vertex.IVertexBuilder; import com.simibubi.create.AllBlocks; import com.simibubi.create.CreateClient; -import com.simibubi.create.foundation.block.SafeTileEntityRendererFast; +import com.simibubi.create.foundation.block.SafeTileEntityRenderer; import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.ColorHelper; import com.simibubi.create.foundation.utility.SuperByteBuffer; @@ -11,33 +13,39 @@ import com.simibubi.create.modules.contraptions.KineticDebugger; import net.minecraft.block.BlockState; import net.minecraft.client.renderer.BufferBuilder; +import net.minecraft.client.renderer.IRenderTypeBuffer; +import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; +import net.minecraft.util.Direction; import net.minecraft.util.Direction.Axis; +import net.minecraft.util.Direction.AxisDirection; import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; @EventBusSubscriber(value = Dist.CLIENT) -public class KineticTileEntityRenderer extends SafeTileEntityRendererFast { +public class KineticTileEntityRenderer extends SafeTileEntityRenderer { public static final Compartment KINETIC_TILE = new Compartment<>(); public static boolean rainbowMode = false; - + + public KineticTileEntityRenderer(TileEntityRendererDispatcher dispatcher) { + super(dispatcher); + } + @Override - public void renderFast(KineticTileEntity te, double x, double y, double z, float partialTicks, int destroyStage, - BufferBuilder buffer) { - renderRotatingBuffer(te, getWorld(), getRotatedModel(te), x, y, z, buffer); + protected void renderSafe(KineticTileEntity te, float partialTicks, MatrixStack ms, IRenderTypeBuffer buffer, + int light, int overlay) { + renderRotatingBuffer(te, getRotatedModel(te), ms, buffer.getBuffer(RenderType.getSolid())); } - public static void renderRotatingKineticBlock(KineticTileEntity te, World world, BlockState renderedState, double x, - double y, double z, BufferBuilder buffer) { + public static void renderRotatingKineticBlock(KineticTileEntity te, BlockState renderedState, MatrixStack ms, BufferBuilder buffer) { SuperByteBuffer superByteBuffer = CreateClient.bufferCache.renderBlockIn(KINETIC_TILE, renderedState); - renderRotatingBuffer(te, world, superByteBuffer, x, y, z, buffer); + renderRotatingBuffer(te, superByteBuffer, ms, buffer); } - public static void renderRotatingBuffer(KineticTileEntity te, World world, SuperByteBuffer superBuffer, double x, - double y, double z, BufferBuilder buffer) { - buffer.putBulkData(standardKineticRotationTransform(superBuffer, te, world).translate(x, y, z).build()); + public static void renderRotatingBuffer(KineticTileEntity te, SuperByteBuffer superBuffer, MatrixStack ms, IVertexBuilder buffer) { + standardKineticRotationTransform(superBuffer, te).renderInto(ms, buffer); } public static float getAngleForTe(KineticTileEntity te, final BlockPos pos, Axis axis) { @@ -47,18 +55,17 @@ public class KineticTileEntityRenderer extends SafeTileEntityRendererFast transform, @@ -158,7 +157,7 @@ public class ContraptionRenderer { } public static int getLight(World world, float lx, float ly, float lz) { - MutableBlockPos pos = new MutableBlockPos(); + BlockPos.Mutable pos = new BlockPos.Mutable(); float sky = 0, block = 0; float offset = 1 / 8f; @@ -166,8 +165,8 @@ public class ContraptionRenderer { for (float yOffset = offset; yOffset >= -offset; yOffset -= 2 * offset) for (float xOffset = offset; xOffset >= -offset; xOffset -= 2 * offset) { pos.setPos(lx + xOffset, ly + yOffset, lz + zOffset); - sky += world.getLightFor(LightType.SKY, pos) / 8f; - block += world.getLightFor(LightType.BLOCK, pos) / 8f; + sky += world.getLightLevel(LightType.SKY, pos) / 8f; + block += world.getLightLevel(LightType.BLOCK, pos) / 8f; } return ((int) sky) << 20 | ((int) block) << 4;