time for some organization pt I

This commit is contained in:
JozsefA 2021-01-07 14:57:10 -08:00
parent ba2d84e92e
commit 0b3615b50e
26 changed files with 389 additions and 361 deletions

View file

@ -13,8 +13,8 @@ import com.simibubi.create.content.contraptions.fluids.FluidTransportBehaviour.A
import com.simibubi.create.content.contraptions.processing.burner.BlazeBurnerBlock.HeatLevel;
import com.simibubi.create.foundation.utility.*;
import com.simibubi.create.foundation.utility.render.BeltBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.BeltBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import net.minecraft.block.BlockState;
import net.minecraft.client.renderer.model.IBakedModel;

View file

@ -8,8 +8,7 @@ import com.simibubi.create.content.contraptions.relays.elementary.CogWheelBlock;
import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer;
import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.ColorHelper;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBufferCache.Compartment;

View file

@ -10,8 +10,7 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Mov
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import com.simibubi.create.foundation.utility.VecHelper;

View file

@ -7,8 +7,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.content.contraptions.components.clock.CuckooClockTileEntity.Animation;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import net.minecraft.block.BlockState;

View file

@ -7,9 +7,8 @@ import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.content.contraptions.base.IRotate;
import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;

View file

@ -9,8 +9,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.content.contraptions.components.flywheel.FlywheelBlock.ConnectionState;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import net.minecraft.block.BlockState;

View file

@ -4,9 +4,8 @@ import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.CreateClient;
import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
public class MillstoneRenderer extends KineticTileEntityRenderer {

View file

@ -3,9 +3,8 @@ package com.simibubi.create.content.contraptions.components.motor;
import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
public class CreativeMotorRenderer extends KineticTileEntityRenderer {

View file

@ -12,8 +12,7 @@ import com.simibubi.create.foundation.tileEntity.behaviour.filtering.FilteringRe
import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer;
import com.simibubi.create.foundation.utility.*;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;

View file

@ -5,8 +5,7 @@ import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import net.minecraft.client.renderer.IRenderTypeBuffer;

View file

@ -6,8 +6,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import net.minecraft.block.BlockState;

View file

@ -4,9 +4,8 @@ import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.CreateClient;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.foundation.tileEntity.renderer.SmartTileEntityRenderer;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;

View file

@ -3,7 +3,6 @@ package com.simibubi.create.content.contraptions.relays.belt;
import java.util.Random;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.AllBlocks;
import com.simibubi.create.AllSpriteShifts;
@ -12,13 +11,10 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.content.contraptions.relays.belt.transport.TransportedItemStack;
import com.simibubi.create.foundation.block.render.SpriteShiftEntry;
import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer;
import com.simibubi.create.foundation.utility.AngleHelper;
import com.simibubi.create.foundation.utility.AnimationTickHolder;
import com.simibubi.create.foundation.utility.Iterate;
import com.simibubi.create.foundation.utility.MatrixStacker;
import com.simibubi.create.foundation.utility.render.BeltBuffer;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.BeltBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.ShadowRenderHelper;
import net.minecraft.block.BlockState;

View file

@ -6,9 +6,8 @@ import com.simibubi.create.content.contraptions.base.IRotate;
import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.foundation.utility.Iterate;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;

View file

@ -5,9 +5,8 @@ import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.foundation.utility.Iterate;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
import net.minecraft.state.properties.BlockStateProperties;

View file

@ -8,8 +8,7 @@ import com.simibubi.create.content.contraptions.base.KineticTileEntityRenderer;
import com.simibubi.create.content.logistics.block.mechanicalArm.ArmTileEntity.Phase;
import com.simibubi.create.foundation.utility.*;
import com.simibubi.create.foundation.utility.render.InstancedBuffer;
import com.simibubi.create.foundation.utility.render.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.SuperByteBuffer;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;

View file

@ -1,166 +0,0 @@
package com.simibubi.create.foundation.utility.render;
import com.mojang.blaze3d.platform.GlStateManager;
import com.simibubi.create.foundation.block.render.SpriteShiftEntry;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.client.model.pipeline.VertexLighterFlat;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL40;
import java.nio.Buffer;
import java.nio.ByteBuffer;
public class BeltBuffer extends InstancedBuffer<BeltBuffer.BeltData> {
public BeltBuffer(BufferBuilder buf) {
super(buf);
}
@Override
protected BeltData newInstance() {
return new BeltData();
}
@Override
protected int numAttributes() {
return 9;
}
@Override
protected void finishBufferingInternal() {
int floatSize = VertexFormatElement.Type.FLOAT.getSize();
int intSize = VertexFormatElement.Type.INT.getSize();
int stride = floatSize * 16;
int instanceSize = instanceCount * stride;
ByteBuffer buffer = GLAllocation.createDirectByteBuffer(instanceSize);
buffer.order(template.order());
((Buffer) buffer).limit(instanceSize);
data.forEach(instanceData -> instanceData.buffer(buffer));
buffer.rewind();
GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, instanceVBO);
GlStateManager.bufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
// render position
GL20.glVertexAttribPointer(3, 3, GL11.GL_FLOAT, false, stride, 0);
// render rotation
GL20.glVertexAttribPointer(4, 3, GL11.GL_FLOAT, false, stride, floatSize * 3L);
// light map
GL20.glVertexAttribPointer(5, 2, GL11.GL_FLOAT, false, stride, floatSize * 6L);
// speed
GL20.glVertexAttribPointer(6, 1, GL11.GL_FLOAT, false, stride, floatSize * 8L);
// uv data
GL20.glVertexAttribPointer(7, 2, GL11.GL_FLOAT, false, stride, floatSize * 9L);
GL20.glVertexAttribPointer(8, 4, GL11.GL_FLOAT, false, stride, floatSize * 11L);
GL20.glVertexAttribPointer(9, 1, GL11.GL_FLOAT, false, stride, floatSize * 15L);
for (int i = 3; i <= numAttributes(); i++) {
GL40.glVertexAttribDivisor(i, 1);
}
// Deselect (bind to 0) the VBO
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
public static class BeltData {
private float x;
private float y;
private float z;
private float rotX;
private float rotY;
private float rotZ;
private int packedLight;
private float rotationalSpeed;
private float sourceU;
private float sourceV;
private float minU;
private float minV;
private float maxU;
private float maxV;
private float scrollMult;
public BeltData setPosition(BlockPos pos) {
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
return this;
}
public BeltData setRotation(float rotX, float rotY, float rotZ) {
this.rotX = rotX;
this.rotY = rotY;
this.rotZ = rotZ;
return this;
}
public BeltData setPackedLight(int packedLight) {
this.packedLight = packedLight;
return this;
}
public BeltData setRotationalSpeed(float rotationalSpeed) {
this.rotationalSpeed = rotationalSpeed;
return this;
}
public BeltData setScrollTexture(SpriteShiftEntry spriteShift) {
TextureAtlasSprite source = spriteShift.getOriginal();
TextureAtlasSprite target = spriteShift.getTarget();
this.sourceU = source.getMinU();
this.sourceV = source.getMinV();
this.minU = target.getMinU();
this.minV = target.getMinV();
this.maxU = target.getMaxU();
this.maxV = target.getMaxV();
return this;
}
public BeltData setScrollMult(float scrollMult) {
this.scrollMult = scrollMult;
return this;
}
void buffer(ByteBuffer buf) {
float blockLightCoordinates = LightUtil.getProperBlockLight(packedLight);
float skyLightCoordinates = LightUtil.getProperSkyLight(packedLight);
buf.putFloat(x);
buf.putFloat(y);
buf.putFloat(z);
buf.putFloat(rotX);
buf.putFloat(rotY);
buf.putFloat(rotZ);
buf.putFloat(blockLightCoordinates);
buf.putFloat(skyLightCoordinates);
buf.putFloat(rotationalSpeed);
buf.putFloat(sourceU);
buf.putFloat(sourceV);
buf.putFloat(minU);
buf.putFloat(minV);
buf.putFloat(maxU);
buf.putFloat(maxV);
buf.putFloat(scrollMult);
}
}
}

View file

@ -7,6 +7,9 @@ import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.simibubi.create.AllBlockPartials;
import com.simibubi.create.Create;
import com.simibubi.create.foundation.utility.render.instancing.BeltBuffer;
import com.simibubi.create.foundation.utility.render.instancing.InstanceBuffer;
import com.simibubi.create.foundation.utility.render.instancing.RotatingBuffer;
import com.simibubi.create.foundation.utility.render.shader.Shader;
import com.simibubi.create.foundation.utility.render.shader.ShaderCallback;
import com.simibubi.create.foundation.utility.render.shader.ShaderHelper;
@ -17,7 +20,6 @@ import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.texture.Texture;
import net.minecraft.inventory.container.PlayerContainer;
import net.minecraft.util.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.fml.common.Mod;
@ -81,7 +83,7 @@ public class FastKineticRenderer {
.stream()
.flatMap(cache -> cache.asMap().values().stream())
.filter(type -> !type.isEmpty())
.forEach(InstancedBuffer::render);
.forEach(InstanceBuffer::render);
ShaderHelper.useShader(Shader.BELT_INSTANCED, callback);
@ -89,7 +91,7 @@ public class FastKineticRenderer {
.stream()
.flatMap(cache -> cache.asMap().values().stream())
.filter(type -> !type.isEmpty())
.forEach(InstancedBuffer::render);
.forEach(InstanceBuffer::render);
ShaderHelper.releaseShader();
@ -221,12 +223,12 @@ public class FastKineticRenderer {
public void invalidate() {
rotating.values().forEach(cache -> {
cache.asMap().values().forEach(InstancedBuffer::invalidate);
cache.asMap().values().forEach(InstanceBuffer::invalidate);
cache.invalidateAll();
});
belts.values().forEach(cache -> {
cache.asMap().values().forEach(InstancedBuffer::invalidate);
cache.asMap().values().forEach(InstanceBuffer::invalidate);
cache.invalidateAll();
});
}

View file

@ -1,140 +0,0 @@
package com.simibubi.create.foundation.utility.render;
import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.Vector3f;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.util.math.BlockPos;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL40;
import java.nio.Buffer;
import java.nio.ByteBuffer;
public class RotatingBuffer extends InstancedBuffer<RotatingBuffer.InstanceData> {
public RotatingBuffer(BufferBuilder buf) {
super(buf);
}
@Override
protected InstanceData newInstance() {
return new InstanceData();
}
@Override
protected int numAttributes() {
return 7;
}
@Override
protected void finishBufferingInternal() {
int floatSize = VertexFormatElement.Type.FLOAT.getSize();
int stride = floatSize * 10;
int instanceSize = instanceCount * stride;
ByteBuffer buffer = GLAllocation.createDirectByteBuffer(instanceSize);
buffer.order(template.order());
((Buffer) buffer).limit(instanceSize);
data.forEach(instanceData -> instanceData.buffer(buffer));
buffer.rewind();
GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, instanceVBO);
GlStateManager.bufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
// the render position
GL20.glVertexAttribPointer(3, 3, GL11.GL_FLOAT, false, stride, 0);
// vertex lighting
GL20.glVertexAttribPointer(4, 2, GL11.GL_FLOAT, false, stride, floatSize * 3L);
// rotational speed and offset
GL20.glVertexAttribPointer(5, 1, GL11.GL_FLOAT, false, stride, floatSize * 5L);
GL20.glVertexAttribPointer(6, 1, GL11.GL_FLOAT, false, stride, floatSize * 6L);
// rotation axis
GL20.glVertexAttribPointer(7, 3, GL11.GL_FLOAT, false, stride, floatSize * 7L);
for (int i = 3; i <= numAttributes(); i++) {
GL40.glVertexAttribDivisor(i, 1);
}
// Deselect (bind to 0) the VBO
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
}
public static class InstanceData {
private float x;
private float y;
private float z;
private int packedLight;
private float rotationalSpeed;
private float rotationOffset;
private float rotationAxisX;
private float rotationAxisY;
private float rotationAxisZ;
public InstanceData setPackedLight(int packedLight) {
this.packedLight = packedLight;
return this;
}
public InstanceData setRotationalSpeed(float rotationalSpeed) {
this.rotationalSpeed = rotationalSpeed;
return this;
}
public InstanceData setRotationOffset(float rotationOffset) {
this.rotationOffset = rotationOffset;
return this;
}
public InstanceData setPosition(Vector3f pos) {
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
return this;
}
public InstanceData setPosition(BlockPos pos) {
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
return this;
}
public InstanceData setRotationAxis(Vector3f axis) {
this.rotationAxisX = axis.getX();
this.rotationAxisY = axis.getY();
this.rotationAxisZ = axis.getZ();
return this;
}
public InstanceData setRotationAxis(float rotationAxisX, float rotationAxisY, float rotationAxisZ) {
this.rotationAxisX = rotationAxisX;
this.rotationAxisY = rotationAxisY;
this.rotationAxisZ = rotationAxisZ;
return this;
}
void buffer(ByteBuffer buf) {
float blockLightCoordinates = LightUtil.getProperBlockLight(packedLight);
float skyLightCoordinates = LightUtil.getProperSkyLight(packedLight);
buf.putFloat(x);
buf.putFloat(y);
buf.putFloat(z);
buf.putFloat(blockLightCoordinates);
buf.putFloat(skyLightCoordinates);
buf.putFloat(rotationalSpeed);
buf.putFloat(rotationOffset);
buf.putFloat(rotationAxisX);
buf.putFloat(rotationAxisY);
buf.putFloat(rotationAxisZ);
}
}
}

View file

@ -0,0 +1,107 @@
package com.simibubi.create.foundation.utility.render.instancing;
import com.simibubi.create.foundation.block.render.SpriteShiftEntry;
import com.simibubi.create.foundation.utility.render.LightUtil;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.math.BlockPos;
import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*;
import java.nio.ByteBuffer;
public class BeltBuffer extends InstanceBuffer<BeltBuffer.BeltData> {
public BeltBuffer(BufferBuilder buf) {
super(buf);
}
@Override
protected BeltData newInstance() {
return new BeltData();
}
@Override
protected VertexFormat getInstanceFormat() {
return BeltData.FORMAT;
}
public static class BeltData extends InstanceData {
public static VertexFormat FORMAT = new VertexFormat(VEC3, VEC3, VEC2, FLOAT, VEC2, VEC4, FLOAT);
private float x;
private float y;
private float z;
private float rotX;
private float rotY;
private float rotZ;
private int packedLight;
private float rotationalSpeed;
private float sourceU;
private float sourceV;
private float minU;
private float minV;
private float maxU;
private float maxV;
private float scrollMult;
public BeltData setPosition(BlockPos pos) {
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
return this;
}
public BeltData setRotation(float rotX, float rotY, float rotZ) {
this.rotX = rotX;
this.rotY = rotY;
this.rotZ = rotZ;
return this;
}
public BeltData setPackedLight(int packedLight) {
this.packedLight = packedLight;
return this;
}
public BeltData setRotationalSpeed(float rotationalSpeed) {
this.rotationalSpeed = rotationalSpeed;
return this;
}
public BeltData setScrollTexture(SpriteShiftEntry spriteShift) {
TextureAtlasSprite source = spriteShift.getOriginal();
TextureAtlasSprite target = spriteShift.getTarget();
this.sourceU = source.getMinU();
this.sourceV = source.getMinV();
this.minU = target.getMinU();
this.minV = target.getMinV();
this.maxU = target.getMaxU();
this.maxV = target.getMaxV();
return this;
}
public BeltData setScrollMult(float scrollMult) {
this.scrollMult = scrollMult;
return this;
}
@Override
public void write(ByteBuffer buf) {
float blockLightCoordinates = LightUtil.getProperBlockLight(packedLight);
float skyLightCoordinates = LightUtil.getProperSkyLight(packedLight);
putVec3(buf, x, y, z);
putVec3(buf, rotX, rotY, rotZ);
putVec2(buf, blockLightCoordinates, skyLightCoordinates);
putFloat(buf, rotationalSpeed);
putVec2(buf, sourceU, sourceV);
putVec4(buf, minU, minV, maxU, maxV);
putFloat(buf, scrollMult);
}
}
}

View file

@ -1,8 +1,9 @@
package com.simibubi.create.foundation.utility.render;
package com.simibubi.create.foundation.utility.render.instancing;
import com.mojang.blaze3d.platform.GlStateManager;
import com.simibubi.create.CreateClient;
import com.simibubi.create.foundation.utility.render.TemplateBuffer;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
@ -13,14 +14,14 @@ import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.function.Consumer;
public abstract class InstancedBuffer<T> extends TemplateBuffer {
public abstract class InstanceBuffer<D extends InstanceData> extends TemplateBuffer {
protected int vao, ebo, invariantVBO, instanceVBO, instanceCount;
protected final ArrayList<T> data = new ArrayList<>();
protected final ArrayList<D> data = new ArrayList<>();
protected boolean shouldBuild = true;
public InstancedBuffer(BufferBuilder buf) {
public InstanceBuffer(BufferBuilder buf) {
super(buf);
setupMainData();
}
@ -86,6 +87,8 @@ public abstract class InstancedBuffer<T> extends TemplateBuffer {
GL30.glBindVertexArray(0);
}
protected abstract VertexFormat getInstanceFormat();
public int numInstances() {
return instanceCount;
}
@ -110,14 +113,12 @@ public abstract class InstancedBuffer<T> extends TemplateBuffer {
});
}
protected abstract T newInstance();
protected abstract D newInstance();
protected abstract int numAttributes();
public void setupInstance(Consumer<T> setup) {
public void setupInstance(Consumer<D> setup) {
if (!shouldBuild) return;
T instanceData = newInstance();
D instanceData = newInstance();
setup.accept(instanceData);
data.add(instanceData);
@ -129,7 +130,8 @@ public abstract class InstancedBuffer<T> extends TemplateBuffer {
GL30.glBindVertexArray(vao);
finishBuffering();
for (int i = 0; i <= numAttributes(); i++) {
int numAttributes = getInstanceFormat().getNumAttributes() + 3;
for (int i = 0; i <= numAttributes; i++) {
GL40.glEnableVertexAttribArray(i);
}
@ -137,7 +139,7 @@ public abstract class InstancedBuffer<T> extends TemplateBuffer {
GL40.glDrawElementsInstanced(GL11.GL_QUADS, count, GL11.GL_UNSIGNED_SHORT, 0, instanceCount);
for (int i = 0; i <= numAttributes(); i++) {
for (int i = 0; i <= numAttributes; i++) {
GL40.glDisableVertexAttribArray(i);
}
@ -148,11 +150,30 @@ public abstract class InstancedBuffer<T> extends TemplateBuffer {
private void finishBuffering() {
if (!shouldBuild) return;
finishBufferingInternal();
VertexFormat instanceFormat = getInstanceFormat();
int instanceSize = instanceCount * instanceFormat.getStride();
ByteBuffer buffer = GLAllocation.createDirectByteBuffer(instanceSize);
buffer.order(template.order());
((Buffer) buffer).limit(instanceSize);
data.forEach(instanceData -> instanceData.write(buffer));
buffer.rewind();
GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, instanceVBO);
GlStateManager.bufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
instanceFormat.informAttributes(3);
for (int i = 0; i < instanceFormat.getNumAttributes(); i++) {
GL40.glVertexAttribDivisor(i + 3, 1);
}
// Deselect (bind to 0) the VBO
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
shouldBuild = false;
data.clear();
}
protected abstract void finishBufferingInternal();
}

View file

@ -0,0 +1,30 @@
package com.simibubi.create.foundation.utility.render.instancing;
import java.nio.ByteBuffer;
public abstract class InstanceData {
public abstract void write(ByteBuffer buf);
public void putVec4(ByteBuffer buf, float x, float y, float z, float w) {
putFloat(buf, x);
putFloat(buf, y);
putFloat(buf, z);
putFloat(buf, w);
}
public void putVec3(ByteBuffer buf, float x, float y, float z) {
putFloat(buf, x);
putFloat(buf, y);
putFloat(buf, z);
}
public void putVec2(ByteBuffer buf, float x, float y) {
putFloat(buf, x);
putFloat(buf, y);
}
public void putFloat(ByteBuffer buf, float f) {
buf.putFloat(f);
}
}

View file

@ -0,0 +1,12 @@
package com.simibubi.create.foundation.utility.render.instancing;
import net.minecraft.tileentity.TileEntity;
import java.util.function.Supplier;
public abstract class InstancedTileEntityRenderer<T extends TileEntity, D extends InstanceData> {
public abstract D getInstanceData(T te);
public abstract InstanceBuffer<D> getModel(T te);
}

View file

@ -0,0 +1,106 @@
package com.simibubi.create.foundation.utility.render.instancing;
import com.mojang.blaze3d.platform.GlStateManager;
import com.simibubi.create.foundation.utility.render.LightUtil;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.Vector3f;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import net.minecraft.util.math.BlockPos;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL40;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*;
import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.FLOAT;
public class RotatingBuffer extends InstanceBuffer<RotatingBuffer.RotatingData> {
public RotatingBuffer(BufferBuilder buf) {
super(buf);
}
@Override
protected RotatingData newInstance() {
return new RotatingData();
}
@Override
protected VertexFormat getInstanceFormat() {
return RotatingData.FORMAT;
}
public static class RotatingData extends InstanceData {
public static VertexFormat FORMAT = new VertexFormat(VEC3, VEC2, FLOAT, FLOAT, VEC3);
private float x;
private float y;
private float z;
private int packedLight;
private float rotationalSpeed;
private float rotationOffset;
private float rotationAxisX;
private float rotationAxisY;
private float rotationAxisZ;
public RotatingData setPackedLight(int packedLight) {
this.packedLight = packedLight;
return this;
}
public RotatingData setRotationalSpeed(float rotationalSpeed) {
this.rotationalSpeed = rotationalSpeed;
return this;
}
public RotatingData setRotationOffset(float rotationOffset) {
this.rotationOffset = rotationOffset;
return this;
}
public RotatingData setPosition(Vector3f pos) {
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
return this;
}
public RotatingData setPosition(BlockPos pos) {
this.x = pos.getX();
this.y = pos.getY();
this.z = pos.getZ();
return this;
}
public RotatingData setRotationAxis(Vector3f axis) {
this.rotationAxisX = axis.getX();
this.rotationAxisY = axis.getY();
this.rotationAxisZ = axis.getZ();
return this;
}
public RotatingData setRotationAxis(float rotationAxisX, float rotationAxisY, float rotationAxisZ) {
this.rotationAxisX = rotationAxisX;
this.rotationAxisY = rotationAxisY;
this.rotationAxisZ = rotationAxisZ;
return this;
}
@Override
public void write(ByteBuffer buf) {
float blockLightCoordinates = LightUtil.getProperBlockLight(packedLight);
float skyLightCoordinates = LightUtil.getProperSkyLight(packedLight);
putVec3(buf, x, y, z);
putVec2(buf, blockLightCoordinates, skyLightCoordinates);
putFloat(buf, rotationalSpeed);
putFloat(buf, rotationOffset);
putVec3(buf, rotationAxisX, rotationAxisY, rotationAxisZ);
}
}
}

View file

@ -0,0 +1,37 @@
package com.simibubi.create.foundation.utility.render.instancing;
import net.minecraft.client.renderer.vertex.VertexFormatElement;
import org.lwjgl.opengl.GL20;
public class VertexAttribute {
public static VertexAttribute MAT4 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 16);
public static VertexAttribute VEC4 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 4);
public static VertexAttribute VEC3 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 3);
public static VertexAttribute VEC2 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 2);
public static VertexAttribute FLOAT = new VertexAttribute(VertexFormatElement.Type.FLOAT, 1);
private final VertexFormatElement.Type type;
private final int count;
private final int size;
private final int attributeCount;
public VertexAttribute(VertexFormatElement.Type type, int count) {
this.type = type;
this.count = count;
this.size = type.getSize() * count;
this.attributeCount = (this.size + 15) / 16; // ceiling division. GLSL vertex attributes can only be 16 bytes wide
}
public void registerForBuffer(int stride, int indexAcc, int offsetAcc) {
GL20.glVertexAttribPointer(indexAcc, count, type.getGlConstant(), false, stride, offsetAcc);
}
public int getSize() {
return size;
}
public int getAttributeCount() {
return attributeCount;
}
}

View file

@ -0,0 +1,37 @@
package com.simibubi.create.foundation.utility.render.instancing;
public class VertexFormat {
private final VertexAttribute[] elements;
private final int numAttributes;
private final int stride;
public VertexFormat(VertexAttribute... elements) {
this.elements = elements;
int numAttributes = 0, stride = 0;
for (VertexAttribute element : elements) {
numAttributes += element.getAttributeCount();
stride += element.getSize();
}
this.numAttributes = numAttributes;
this.stride = stride;
}
public int getNumAttributes() {
return numAttributes;
}
public int getStride() {
return stride;
}
public void informAttributes(int index) {
int offset = 0;
for (VertexAttribute element : this.elements) {
element.registerForBuffer(stride, index, offset);
index += element.getAttributeCount();
offset += element.getSize();
}
}
}