1.15 port checkpoint: kinetic rendering mostly complete

This commit is contained in:
tterrag 2020-04-04 21:54:24 -04:00
parent f16e534a89
commit 1e0711fa95
16 changed files with 117 additions and 85 deletions

View file

@ -83,7 +83,7 @@ repositories {
}
dependencies {
minecraft 'net.minecraftforge:forge:1.15.2-31.1.27'
minecraft 'net.minecraftforge:forge:1.15.2-31.1.36'
// compile against the JEI API but do not include it at runtime
compileOnly fg.deobf("mezz.jei:jei-1.15.2:6.0.0.2:api")

View file

@ -5,14 +5,15 @@ import static net.minecraft.state.properties.BlockStateProperties.HORIZONTAL_FAC
import java.util.Map;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.Lang;
import com.simibubi.create.foundation.utility.SuperByteBuffer;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.Vector3f;
import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.util.Direction;
import net.minecraft.util.Direction.Axis;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ModelBakeEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
@ -120,9 +121,13 @@ public enum AllBlockPartials {
}
public SuperByteBuffer renderOnDirectional(BlockState referenceState, Direction facing) {
SuperByteBuffer renderPartial = CreateClient.bufferCache.renderPartial(this, referenceState);
renderPartial.rotateCentered(Axis.X, AngleHelper.rad(AngleHelper.verticalAngle(facing)));
renderPartial.rotateCentered(Axis.Y, AngleHelper.rad(AngleHelper.horizontalAngle(facing)));
MatrixStack ms = new MatrixStack();
// TODO 1.15 find a way to cache this model matrix computation
ms.translate(0.5, 0.5, 0.5);
ms.multiply(Vector3f.POSITIVE_Y.getRadialQuaternion(AngleHelper.rad(AngleHelper.horizontalAngle(facing))));
ms.multiply(Vector3f.POSITIVE_X.getRadialQuaternion(AngleHelper.rad(AngleHelper.verticalAngle(facing))));
ms.translate(-0.5, -0.5, -0.5);
SuperByteBuffer renderPartial = CreateClient.bufferCache.renderDirectionalPartial(this, referenceState, facing, ms);
return renderPartial;
}

View file

@ -18,9 +18,11 @@ import com.simibubi.create.modules.contraptions.relays.belt.BeltConnectorItemHan
import com.simibubi.create.modules.curiosities.zapper.terrainzapper.TerrainZapperRenderHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.InputEvent.KeyInputEvent;
@ -70,13 +72,23 @@ public class ClientEvents {
@SubscribeEvent
public static void onRenderWorld(RenderWorldLastEvent event) {
MatrixStack ms = event.getMatrixStack();
IRenderTypeBuffer buffer = Minecraft.getInstance().getBufferBuilders().getEntityVertexConsumers();
ActiveRenderInfo info = Minecraft.getInstance().gameRenderer.getActiveRenderInfo();
Vec3d view = info.getProjectedView();
ms.push();
ms.translate(-view.getX(), -view.getY(), -view.getZ());
IRenderTypeBuffer.Impl buffer = Minecraft.getInstance().getBufferBuilders().getEntityVertexConsumers();
CreateClient.schematicHandler.render(ms, buffer, 0xF000F0, OverlayTexture.DEFAULT_UV);
CreateClient.schematicAndQuillHandler.render(ms, buffer);
CreateClient.schematicHologram.render(ms, buffer);
KineticDebugger.renderSourceOutline(ms, buffer);
ChassisRangeDisplay.renderOutlines(event.getPartialTicks(), ms, buffer);
TerrainZapperRenderHandler.render(ms, buffer);
ms.pop();
buffer.draw();
}
@SubscribeEvent

View file

@ -1,11 +1,14 @@
package com.simibubi.create.foundation.utility;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import com.mojang.datafixers.util.Pair;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.BufferBuilder.DrawState;
import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.Matrix4f;
import net.minecraft.client.renderer.Vector4f;
@ -14,7 +17,6 @@ import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.util.Direction;
import net.minecraft.util.Direction.Axis;
import net.minecraft.util.Direction.AxisDirection;
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
public class SuperByteBuffer {
@ -44,11 +46,12 @@ public class SuperByteBuffer {
private int r, g, b, a;
public SuperByteBuffer(BufferBuilder buf) {
ByteBuffer original = ObfuscationReflectionHelper.getPrivateValue(BufferBuilder.class, buf, "field_179001_a"); // FIXME speedup
original.rewind();
Pair<DrawState, ByteBuffer> state = buf.popData();
ByteBuffer original = state.getSecond();
original.order(ByteOrder.nativeOrder()); // Vanilla bug, endianness does not carry over into sliced buffers
this.original = original;
this.mutable = GLAllocation.createDirectByteBuffer(original.capacity());
this.mutable = GLAllocation.createDirectByteBuffer(state.getFirst().getCount() * buf.getVertexFormat().getSize());
this.mutable.order(original.order());
this.mutable.limit(original.limit());
mutable.put(this.original);
@ -61,8 +64,8 @@ public class SuperByteBuffer {
original.rewind();
mutable.rewind();
Matrix4f t = transforms.peek().getModel();
t.multiply(input.peek().getModel());
Matrix4f t = input.peek().getModel().copy();
t.multiply(transforms.peek().getModel());
for (int vertex = 0; vertex < vertexCount(original); vertex++) {
Vector4f pos = new Vector4f(getX(original, vertex), getY(original, vertex), getZ(original, vertex), 1F);
@ -94,6 +97,7 @@ public class SuperByteBuffer {
shouldShiftUV = false;
shouldColor = false;
shouldLight = false;
mutable.rewind();
return mutable;
}
@ -134,7 +138,7 @@ public class SuperByteBuffer {
}
public SuperByteBuffer rotateCentered(Direction axis, float radians) {
return translate(-.5f, -.5f, -.5f).rotate(axis, radians).translate(.5f, .5f, .5f);
return translate(.5f, .5f, .5f).rotate(axis, radians).translate(-.5f, -.5f, -.5f);
}
public SuperByteBuffer shiftUV(TextureAtlasSprite from, TextureAtlasSprite to) {
@ -245,7 +249,8 @@ public class SuperByteBuffer {
}
protected void putLight(ByteBuffer buffer, int index, int packedLight) {
buffer.putInt(getBufferPosition(index) + 24, packedLight);
buffer.putShort(getBufferPosition(index) + 24, (short) (packedLight & 0xFF));
buffer.putShort(getBufferPosition(index) + 26, (short) ((packedLight >> 16) & 0xFF));
}
protected void putColor(ByteBuffer buffer, int index, byte r, byte g, byte b, byte a) {

View file

@ -7,6 +7,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.apache.commons.lang3.tuple.Pair;
import org.lwjgl.opengl.GL11;
import com.google.common.cache.Cache;
@ -22,6 +23,7 @@ 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.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.model.data.EmptyModelData;
@ -32,6 +34,7 @@ public class SuperByteBufferCache {
public static final Compartment<BlockState> GENERIC_TILE = new Compartment<>();
public static final Compartment<AllBlockPartials> PARTIAL = new Compartment<>();
public static final Compartment<Pair<Direction, AllBlockPartials>> DIRECTIONAL_PARTIAL = new Compartment<>();
Map<Compartment<?>, Cache<Object, SuperByteBuffer>> cache;
@ -39,6 +42,7 @@ public class SuperByteBufferCache {
cache = new HashMap<>();
registerCompartment(GENERIC_TILE);
registerCompartment(PARTIAL);
registerCompartment(DIRECTIONAL_PARTIAL);
}
public SuperByteBuffer renderBlock(BlockState toRender) {
@ -49,6 +53,18 @@ public class SuperByteBufferCache {
return get(PARTIAL, partial, () -> standardModelRender(partial.get(), referenceState));
}
public SuperByteBuffer renderPartial(AllBlockPartials partial, BlockState referenceState, MatrixStack modelTransform) {
return get(PARTIAL, partial, () -> standardModelRender(partial.get(), referenceState, modelTransform));
}
public SuperByteBuffer renderDirectionalPartial(AllBlockPartials partial, BlockState referenceState, Direction dir) {
return get(DIRECTIONAL_PARTIAL, Pair.of(dir, partial), () -> standardModelRender(partial.get(), referenceState));
}
public SuperByteBuffer renderDirectionalPartial(AllBlockPartials partial, BlockState referenceState, Direction dir, MatrixStack modelTransform) {
return get(DIRECTIONAL_PARTIAL, Pair.of(dir, partial), () -> standardModelRender(partial.get(), referenceState, modelTransform));
}
public SuperByteBuffer renderBlockIn(Compartment<BlockState> compartment, BlockState toRender) {
return get(compartment, toRender, () -> standardBlockRender(toRender));
}
@ -82,16 +98,16 @@ public class SuperByteBufferCache {
}
private SuperByteBuffer standardModelRender(IBakedModel model, BlockState referenceState) {
return standardModelRender(model, referenceState, new MatrixStack());
}
private SuperByteBuffer standardModelRender(IBakedModel model, BlockState referenceState, MatrixStack ms) {
BlockRendererDispatcher dispatcher = Minecraft.getInstance().getBlockRendererDispatcher();
BlockModelRenderer blockRenderer = dispatcher.getBlockModelRenderer();
BufferBuilder builder = new BufferBuilder(0);
BufferBuilder builder = new BufferBuilder(DefaultVertexFormats.BLOCK.getIntegerSize());
Random random = new Random();
MatrixStack ms = new MatrixStack();
ms.push();
ms.translate(0, 1, 0);
builder.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
blockRenderer.renderModelFlat(Minecraft.getInstance().world, model, referenceState, BlockPos.ZERO.down(), ms,
blockRenderer.renderModelFlat(Minecraft.getInstance().world, model, referenceState, BlockPos.ZERO.up(255), ms,
builder, true, random, 42, OverlayTexture.DEFAULT_UV, EmptyModelData.INSTANCE);
builder.finishDrawing();

View file

@ -3,17 +3,21 @@ package com.simibubi.create.modules.contraptions;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import com.simibubi.create.config.AllConfigs;
import com.simibubi.create.modules.contraptions.base.IRotate;
import com.simibubi.create.modules.contraptions.base.KineticTileEntity;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction.Axis;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3i;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.World;
@ -28,6 +32,7 @@ public class KineticDebugger {
World world = Minecraft.getInstance().world;
BlockPos toOutline = te.hasSource() ? te.source : te.getPos();
BlockState state = te.getBlockState();
VoxelShape shape = world.getBlockState(toOutline).getShape(world, toOutline);
IVertexBuilder vb = buffer.getBuffer(RenderType.getLines());
@ -39,6 +44,27 @@ public class KineticDebugger {
WorldRenderer.func_228431_a_(ms, vb, shape, 0, 0, 0, te.hasSource() ? .5f : 1, .75f, .75f, 1);
Vec3i offset = te.getPos().subtract(toOutline);
ms.translate(offset.getX(), offset.getY(), offset.getZ());
if (state.getBlock() instanceof IRotate) {
Axis axis = ((IRotate)state.getBlock()).getRotationAxis(state);
switch (axis) {
case X:
vb.vertex(ms.peek().getModel(), 0, 0.5f, 0.5f).color(1f, 1f, 1f, 1f).endVertex();
vb.vertex(ms.peek().getModel(), 1, 0.5f, 0.5f).color(1f, 1f, 1f, 1f).endVertex();
break;
case Y:
vb.vertex(ms.peek().getModel(), 0.5f, 0, 0.5f).color(1f, 1f, 1f, 1f).endVertex();
vb.vertex(ms.peek().getModel(), 0.5f, 1, 0.5f).color(1f, 1f, 1f, 1f).endVertex();
break;
case Z:
vb.vertex(ms.peek().getModel(), 0.5f, 0.5f, 0).color(1f, 1f, 1f, 1f).endVertex();
vb.vertex(ms.peek().getModel(), 0.5f, 0.5f, 1).color(1f, 1f, 1f, 1f).endVertex();
break;
}
}
ms.pop();
}

View file

@ -109,7 +109,7 @@ public abstract class DirectionalAxisKineticBlock extends DirectionalKineticBloc
if (pistonAxis == Axis.Z)
return alongFirst ? Axis.X : Axis.Y;
return super.getRotationAxis(state);
throw new IllegalStateException("Unknown axis??");
}
@Override

View file

@ -11,7 +11,6 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.Direction.Axis;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
@ -58,11 +57,6 @@ public abstract class KineticBlock extends Block implements IRotate {
return false;
}
@Override
public Axis getRotationAxis(BlockState state) {
return null;
}
// Block
@Override

View file

@ -12,13 +12,10 @@ import com.simibubi.create.foundation.utility.SuperByteBufferCache.Compartment;
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.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@ -64,7 +61,7 @@ public class KineticTileEntityRenderer extends SafeTileEntityRenderer<KineticTil
public static SuperByteBuffer kineticRotationTransform(SuperByteBuffer buffer, KineticTileEntity te, Axis axis,
float angle) {
int light = te.getBlockState().getLightValue(te.getWorld(), te.getPos());
buffer.light((0xF0 << 24) | (light << 4));
buffer.light((0xF0 << 16) | (light << 4));
buffer.rotateCentered(axis, angle);
int white = 0xFFFFFF;

View file

@ -5,7 +5,6 @@ import static net.minecraft.state.properties.BlockStateProperties.HORIZONTAL_FAC
import java.util.Random;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.AllBlocks;
@ -25,12 +24,12 @@ import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.Vector3f;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.model.ItemCameraTransforms.TransformType;
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.MathHelper;
@ -85,10 +84,14 @@ public class BeltTileEntityRenderer extends SafeTileEntityRenderer<BeltTileEntit
beltBuffer.light(packedLightmapCoords).renderInto(ms, vb);
if (te.hasPulley()) {
SuperByteBuffer superBuffer = AllBlockPartials.BELT_PULLEY.renderOn(blockState);
Axis axis = blockState.get(BeltBlock.HORIZONTAL_FACING).getAxis();
superBuffer.rotateCentered(Axis.X, (float) (Math.PI / 2));
superBuffer.rotateCentered(Axis.Y, (float) (axis == Axis.X ? 0 : Math.PI / 2));
// TODO 1.15 find a way to cache this model matrix computation
MatrixStack modelTransform = new MatrixStack();
Direction dir = blockState.get(BeltBlock.HORIZONTAL_FACING);
modelTransform.translate(0.5, 0.5, 0.5);
modelTransform.multiply(Vector3f.POSITIVE_Y.getRadialQuaternion((float) (dir.getAxis() == Axis.X ? 0 : Math.PI / 2)));
modelTransform.multiply(Vector3f.POSITIVE_X.getRadialQuaternion((float) (Math.PI / 2)));
modelTransform.translate(-0.5, -0.5, -0.5);
SuperByteBuffer superBuffer = CreateClient.bufferCache.renderDirectionalPartial(AllBlockPartials.BELT_PULLEY, blockState, dir, modelTransform);
KineticTileEntityRenderer.standardKineticRotationTransform(superBuffer, te)
.renderInto(ms, vb);
}

View file

@ -78,4 +78,8 @@ public class GearboxBlock extends RotatedPillarKineticBlock {
return true;
}
@Override
public Axis getRotationAxis(BlockState state) {
return state.get(AXIS);
}
}

View file

@ -1,13 +1,7 @@
{
"forge_marker": 1,
"defaults": {
"model": "create:block/cogwheel"
},
"variants": {
"axis" : {
"x": { "x": 90, "y": 90 },
"y": {},
"z": { "x": 90 }
}
"axis=x" : { "model": "create:block/cogwheel", "x": 90, "y": 90 },
"axis=y" : { "model": "create:block/cogwheel" },
"axis=z" : { "model": "create:block/cogwheel", "x": 90 }
}
}

View file

@ -1,14 +1,8 @@
{
"forge_marker": 1,
"defaults": {
"model": "create:block/motor"
},
"variants": {
"facing": {
"north": { "y": 180 },
"south": { },
"east": { "y": 270 },
"west": { "y": 90 }
}
"facing=north": { "model": "create:block/motor", "y": 180 },
"facing=south": { "model": "create:block/motor" },
"facing=east": { "model": "create:block/motor", "y": 270 },
"facing=west": { "model": "create:block/motor", "y": 90 }
}
}

View file

@ -1,13 +1,7 @@
{
"forge_marker": 1,
"defaults": {
"model": "create:block/large_cogwheel"
},
"variants": {
"axis" : {
"x": { "x": 90, "y": 90 },
"y": {},
"z": { "x": 90 }
}
"axis=x" : { "model": "create:block/large_cogwheel", "x": 90, "y": 90 },
"axis=y" : { "model": "create:block/large_cogwheel" },
"axis=z" : { "model": "create:block/large_cogwheel", "x": 90 }
}
}

View file

@ -1,14 +1,8 @@
{
"forge_marker": 1,
"defaults": {
"model": "create:block/mechanical_press_base"
},
"variants": {
"facing": {
"north": { "y": 0 },
"east": { "y": 90 },
"west": { "y": 90 },
"south": { "y": 0 }
}
"facing=north": {"model": "create:block/mechanical_press_base", "y": 0 },
"facing=east": {"model": "create:block/mechanical_press_base", "y": 90 },
"facing=west": {"model": "create:block/mechanical_press_base", "y": 90 },
"facing=south": {"model": "create:block/mechanical_press_base", "y": 0 }
}
}

View file

@ -1,13 +1,7 @@
{
"forge_marker": 1,
"defaults": {
"model": "create:block/shaft"
},
"variants": {
"axis" : {
"x": { "x": 90, "y": 90 },
"y": {},
"z": { "x": 90 }
}
"axis=x" : { "model": "create:block/shaft", "x": 90, "y": 90 },
"axis=y" : { "model": "create:block/shaft" },
"axis=z" : { "model": "create:block/shaft", "x": 90 }
}
}