From cfd60fbc8d0de750065421eb8f196e6b411d8145 Mon Sep 17 00:00:00 2001 From: JozsefA Date: Fri, 22 Jan 2021 18:00:14 -0800 Subject: [PATCH] fast actor rendering but it's not correct yet --- .../actors/DrillMovementBehaviour.java | 7 ++-- .../components/actors/DrillRenderer.java | 20 +++++++++- .../actors/HarvesterMovementBehaviour.java | 8 +++- .../components/actors/HarvesterRenderer.java | 24 ++++++++++++ .../structureMovement/MovementBehaviour.java | 3 +- .../render/FastKineticRenderer.java | 2 +- .../ContraptionKineticRenderer.java | 7 +--- .../contraption/RenderedContraption.java | 14 ++++--- .../render/instancing/InstanceBuffer.java | 4 +- .../instancing/KineticRenderMaterials.java | 4 ++ .../actors/StaticRotatingActorData.java | 39 +++++++++++++------ .../foundation/render/shader/Shader.java | 2 +- .../create/shader/contraption_actor.vert | 18 +++++++-- 13 files changed, 115 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillMovementBehaviour.java index 00f01202b..dd5441db7 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillMovementBehaviour.java @@ -2,6 +2,7 @@ package com.simibubi.create.content.contraptions.components.actors; import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.foundation.render.contraption.RenderedContraption; import com.simibubi.create.foundation.utility.VecHelper; import net.minecraft.block.BlockState; @@ -31,12 +32,12 @@ public class DrillMovementBehaviour extends BlockBreakingMovementBehaviour { @OnlyIn(value = Dist.CLIENT) public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffer) { - DrillRenderer.renderInContraption(context, ms, msLocal, buffer); + //DrillRenderer.renderInContraption(context, ms, msLocal, buffer); } @Override - public void addInstance(MovementContext context) { - super.addInstance(context); + public void addInstance(RenderedContraption contraption, MovementContext context) { + DrillRenderer.addInstanceForContraption(contraption, context); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java index ca8474509..e9e267ad9 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/DrillRenderer.java @@ -7,6 +7,9 @@ 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.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.foundation.render.contraption.RenderedContraption; +import com.simibubi.create.foundation.render.instancing.RenderMaterial; +import com.simibubi.create.foundation.render.instancing.actors.StaticRotatingActorData; import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.AnimationTickHolder; import com.simibubi.create.foundation.utility.MatrixStacker; @@ -19,6 +22,7 @@ import com.simibubi.create.foundation.render.instancing.RotatingData; import net.minecraft.block.BlockState; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.Vector3f; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.util.Direction; @@ -37,8 +41,20 @@ public class DrillRenderer extends KineticTileEntityRenderer { return AllBlockPartials.DRILL_HEAD.renderOnDirectionalSouth(state); } - public static void addInstanceForContraption(MovementContext context) { - //AllBlockPartials.DRILL_HEAD. + public static void addInstanceForContraption(RenderedContraption contraption, MovementContext context) { + RenderMaterial> renderMaterial = contraption.getActorMaterial(); + + BlockState state = context.state; + InstanceBuffer model = renderMaterial.getModel(AllBlockPartials.DRILL_HEAD, state); + + model.setupInstance(data -> { + Direction facing = state.get(DrillBlock.FACING); + Vector3f orientation = facing.getOpposite().getUnitVector(); + data.setPosition(context.localPos) + .setRotationOffset(0) + .setRotationAxis(orientation) + .setLocalRotation(AngleHelper.verticalAngle(facing), facing.getHorizontalAngle(), 0); + }); } public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterMovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterMovementBehaviour.java index 38d5f55f8..a70af09ab 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterMovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterMovementBehaviour.java @@ -2,6 +2,7 @@ package com.simibubi.create.content.contraptions.components.actors; import static net.minecraft.block.HorizontalBlock.HORIZONTAL_FACING; +import com.simibubi.create.foundation.render.contraption.RenderedContraption; import org.apache.commons.lang3.mutable.MutableBoolean; import com.mojang.blaze3d.matrix.MatrixStack; @@ -35,10 +36,15 @@ public class HarvesterMovementBehaviour extends MovementBehaviour { .getOpposite()); } + @Override + public void addInstance(RenderedContraption contraption, MovementContext context) { + HarvesterRenderer.addInstanceForContraption(contraption, context); + } + @Override public void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffers) { - HarvesterRenderer.renderInContraption(context, ms, msLocal, buffers); + //HarvesterRenderer.renderInContraption(context, ms, msLocal, buffers); } @Override diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterRenderer.java b/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterRenderer.java index f3933f0c8..c07e61054 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterRenderer.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/actors/HarvesterRenderer.java @@ -5,6 +5,10 @@ import static net.minecraft.state.properties.BlockStateProperties.HORIZONTAL_FAC import com.mojang.blaze3d.matrix.MatrixStack; import com.simibubi.create.AllBlockPartials; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; +import com.simibubi.create.foundation.render.contraption.RenderedContraption; +import com.simibubi.create.foundation.render.instancing.InstanceBuffer; +import com.simibubi.create.foundation.render.instancing.RenderMaterial; +import com.simibubi.create.foundation.render.instancing.actors.StaticRotatingActorData; import com.simibubi.create.foundation.tileEntity.renderer.SafeTileEntityRenderer; import com.simibubi.create.foundation.utility.AngleHelper; import com.simibubi.create.foundation.utility.AnimationTickHolder; @@ -14,6 +18,7 @@ import com.simibubi.create.foundation.utility.VecHelper; import net.minecraft.block.BlockState; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.RenderType; +import net.minecraft.client.renderer.Vector3f; import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; @@ -34,6 +39,25 @@ public class HarvesterRenderer extends SafeTileEntityRenderer> renderMaterial = contraption.getActorMaterial(); + + BlockState state = context.state; + InstanceBuffer model = renderMaterial.getModel(AllBlockPartials.HARVESTER_BLADE, state); + + model.setupInstance(data -> { + Direction facing = state.get(HORIZONTAL_FACING); + Direction rotationAxis = facing.rotateY(); + float originOffset = 1 / 16f; + Vector3f rotOffset = new Vector3f(0.5f, -2 * originOffset + 0.5f, originOffset + 0.5f); + data.setPosition(context.localPos) + .setRotationOffset(0) + .setRotationCenter(rotOffset) + .setRotationAxis(rotationAxis.getUnitVector()) + .setLocalRotation(0, facing.getHorizontalAngle(), 0); + }); + } + public static void renderInContraption(MovementContext context, MatrixStack ms, MatrixStack msLocal, IRenderTypeBuffer buffers) { BlockState blockState = context.state; diff --git a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java index b4b3bf220..d82dcfe38 100644 --- a/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java +++ b/src/main/java/com/simibubi/create/content/contraptions/components/structureMovement/MovementBehaviour.java @@ -2,6 +2,7 @@ package com.simibubi.create.content.contraptions.components.structureMovement; import com.mojang.blaze3d.matrix.MatrixStack; +import com.simibubi.create.foundation.render.contraption.RenderedContraption; import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.entity.item.ItemEntity; import net.minecraft.item.ItemStack; @@ -56,7 +57,7 @@ public abstract class MovementBehaviour { IRenderTypeBuffer buffer) {} @OnlyIn(Dist.CLIENT) - public void addInstance(MovementContext context) {} + public void addInstance(RenderedContraption contraption, MovementContext context) {} public void onSpeedChanged(MovementContext context, Vec3d oldMotion, Vec3d motion) { diff --git a/src/main/java/com/simibubi/create/foundation/render/FastKineticRenderer.java b/src/main/java/com/simibubi/create/foundation/render/FastKineticRenderer.java index d35a580b2..a0634c0ac 100644 --- a/src/main/java/com/simibubi/create/foundation/render/FastKineticRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/render/FastKineticRenderer.java @@ -73,7 +73,7 @@ public class FastKineticRenderer { public void invalidate() { for (RenderMaterial material : materials.values()) { - material.runOnAll(InstanceBuffer::delete); + material.delete(); } dirty = true; } diff --git a/src/main/java/com/simibubi/create/foundation/render/contraption/ContraptionKineticRenderer.java b/src/main/java/com/simibubi/create/foundation/render/contraption/ContraptionKineticRenderer.java index b06626d16..215180d86 100644 --- a/src/main/java/com/simibubi/create/foundation/render/contraption/ContraptionKineticRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/render/contraption/ContraptionKineticRenderer.java @@ -5,12 +5,8 @@ import com.simibubi.create.foundation.render.instancing.BeltBuffer; import com.simibubi.create.foundation.render.instancing.KineticRenderMaterials; import com.simibubi.create.foundation.render.instancing.RenderMaterial; import com.simibubi.create.foundation.render.instancing.RotatingBuffer; +import com.simibubi.create.foundation.render.instancing.actors.RotatingActorBuffer; import com.simibubi.create.foundation.render.shader.Shader; -import com.simibubi.create.foundation.render.shader.ShaderCallback; -import com.simibubi.create.foundation.render.shader.ShaderHelper; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.Matrix4f; -import net.minecraft.client.renderer.RenderType; public class ContraptionKineticRenderer extends FastKineticRenderer { @@ -18,6 +14,7 @@ public class ContraptionKineticRenderer extends FastKineticRenderer { public void registerMaterials() { materials.put(KineticRenderMaterials.BELTS, new RenderMaterial<>(Shader.CONTRAPTION_BELT, BeltBuffer::new)); materials.put(KineticRenderMaterials.ROTATING, new RenderMaterial<>(Shader.CONTRAPTION_ROTATING, RotatingBuffer::new)); + materials.put(KineticRenderMaterials.ACTORS, new RenderMaterial<>(Shader.CONTRAPTION_ACTOR, RotatingActorBuffer::new)); } @Override diff --git a/src/main/java/com/simibubi/create/foundation/render/contraption/RenderedContraption.java b/src/main/java/com/simibubi/create/foundation/render/contraption/RenderedContraption.java index e000cb20e..fab94150c 100644 --- a/src/main/java/com/simibubi/create/foundation/render/contraption/RenderedContraption.java +++ b/src/main/java/com/simibubi/create/foundation/render/contraption/RenderedContraption.java @@ -7,8 +7,8 @@ import com.simibubi.create.content.contraptions.components.structureMovement.Con import com.simibubi.create.content.contraptions.components.structureMovement.MovementBehaviour; import com.simibubi.create.content.contraptions.components.structureMovement.MovementContext; import com.simibubi.create.foundation.render.FastKineticRenderer; -import com.simibubi.create.foundation.render.instancing.IInstanceRendered; -import com.simibubi.create.foundation.render.instancing.IInstancedTileEntityRenderer; +import com.simibubi.create.foundation.render.instancing.*; +import com.simibubi.create.foundation.render.instancing.actors.StaticRotatingActorData; import com.simibubi.create.foundation.render.light.ContraptionLighter; import com.simibubi.create.foundation.render.shader.ShaderHelper; import net.minecraft.client.renderer.BufferBuilder; @@ -43,6 +43,8 @@ public class RenderedContraption { buildLayers(contraption); buildInstancedTiles(contraption); + buildActors(contraption); + kinetics.markAllDirty(); } public int getEntityId() { @@ -57,6 +59,10 @@ public class RenderedContraption { return lighter; } + public RenderMaterial> getActorMaterial() { + return kinetics.get(KineticRenderMaterials.ACTORS); + } + public void doRenderLayer(RenderType layer, int shader) { ContraptionBuffer buffer = renderLayers.get(layer); if (buffer != null) { @@ -93,8 +99,6 @@ public class RenderedContraption { } } } - - kinetics.markAllDirty(); } private void buildActors(Contraption c) { @@ -107,7 +111,7 @@ public class RenderedContraption { MovementBehaviour movementBehaviour = AllMovementBehaviours.of(blockInfo.state); if (movementBehaviour != null) { - movementBehaviour.addInstance(context); + movementBehaviour.addInstance(this, context); } } } diff --git a/src/main/java/com/simibubi/create/foundation/render/instancing/InstanceBuffer.java b/src/main/java/com/simibubi/create/foundation/render/instancing/InstanceBuffer.java index 67a6ddcbf..bbb729de3 100644 --- a/src/main/java/com/simibubi/create/foundation/render/instancing/InstanceBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/render/instancing/InstanceBuffer.java @@ -99,7 +99,7 @@ public abstract class InstanceBuffer extends GPUBuffer { } protected int getTotalShaderAttributeCount() { - return getInstanceFormat().getShaderAttributeCount() + FORMAT.getShaderAttributeCount(); + return getInstanceFormat().getShaderAttributeCount() + super.getTotalShaderAttributeCount(); } @Override @@ -130,7 +130,7 @@ public abstract class InstanceBuffer extends GPUBuffer { buffer.rewind(); GL15.glUnmapBuffer(GL15.GL_ARRAY_BUFFER); - int staticAttributes = FORMAT.getShaderAttributeCount(); + int staticAttributes = getModelFormat().getShaderAttributeCount(); instanceFormat.informAttributes(staticAttributes); for (int i = 0; i < instanceFormat.getShaderAttributeCount(); i++) { diff --git a/src/main/java/com/simibubi/create/foundation/render/instancing/KineticRenderMaterials.java b/src/main/java/com/simibubi/create/foundation/render/instancing/KineticRenderMaterials.java index 5c0d4e658..cd941f5d9 100644 --- a/src/main/java/com/simibubi/create/foundation/render/instancing/KineticRenderMaterials.java +++ b/src/main/java/com/simibubi/create/foundation/render/instancing/KineticRenderMaterials.java @@ -1,6 +1,10 @@ package com.simibubi.create.foundation.render.instancing; +import com.simibubi.create.foundation.render.instancing.actors.StaticRotatingActorData; + public class KineticRenderMaterials { public static final MaterialType> ROTATING = new MaterialType<>(); public static final MaterialType> BELTS = new MaterialType<>(); + + public static final MaterialType> ACTORS = new MaterialType<>(); } diff --git a/src/main/java/com/simibubi/create/foundation/render/instancing/actors/StaticRotatingActorData.java b/src/main/java/com/simibubi/create/foundation/render/instancing/actors/StaticRotatingActorData.java index 48c71e91b..1b99cbd59 100644 --- a/src/main/java/com/simibubi/create/foundation/render/instancing/actors/StaticRotatingActorData.java +++ b/src/main/java/com/simibubi/create/foundation/render/instancing/actors/StaticRotatingActorData.java @@ -1,7 +1,6 @@ package com.simibubi.create.foundation.render.instancing.actors; import com.simibubi.create.foundation.render.instancing.InstanceData; -import com.simibubi.create.foundation.render.instancing.RotatingData; import com.simibubi.create.foundation.render.instancing.VertexFormat; import net.minecraft.client.renderer.Vector3f; import net.minecraft.util.math.BlockPos; @@ -11,7 +10,7 @@ import java.nio.ByteBuffer; import static com.simibubi.create.foundation.render.instancing.VertexAttribute.*; public class StaticRotatingActorData extends InstanceData { - public static VertexFormat FORMAT = new VertexFormat(POSITION, FLOAT, NORMAL, NORMAL); + public static VertexFormat FORMAT = new VertexFormat(POSITION, FLOAT, NORMAL, VEC3, NORMAL); private float x; private float y; @@ -20,9 +19,12 @@ public class StaticRotatingActorData extends InstanceData { private byte rotationAxisX; private byte rotationAxisY; private byte rotationAxisZ; - private byte localOrientationX; - private byte localOrientationY; - private byte localOrientationZ; + private float localRotationX; + private float localRotationY; + private float localRotationZ; + private byte rotationCenterX = 64; + private byte rotationCenterY = 64; + private byte rotationCenterZ = 64; public StaticRotatingActorData setPosition(BlockPos pos) { this.x = pos.getX(); @@ -48,15 +50,27 @@ public class StaticRotatingActorData extends InstanceData { return this; } - public StaticRotatingActorData setLocalOrientation(Vector3f axis) { - setRotationAxis(axis.getX(), axis.getY(), axis.getZ()); + public StaticRotatingActorData setRotationCenter(Vector3f axis) { + setRotationCenter(axis.getX(), axis.getY(), axis.getZ()); return this; } - public StaticRotatingActorData setLocalOrientation(float localOrientationX, float localOrientationY, float localOrientationZ) { - this.localOrientationX = (byte) (localOrientationX * 127); - this.localOrientationY = (byte) (localOrientationY * 127); - this.localOrientationZ = (byte) (localOrientationZ * 127); + public StaticRotatingActorData setRotationCenter(float rotationCenterX, float rotationCenterY, float rotationCenterZ) { + this.rotationCenterX = (byte) (rotationCenterX * 127); + this.rotationCenterY = (byte) (rotationCenterY * 127); + this.rotationCenterZ = (byte) (rotationCenterZ * 127); + return this; + } + + public StaticRotatingActorData setLocalRotation(Vector3f axis) { + setLocalRotation(axis.getX(), axis.getY(), axis.getZ()); + return this; + } + + public StaticRotatingActorData setLocalRotation(float localRotationX, float localRotationY, float localRotationZ) { + this.localRotationX = localRotationX; + this.localRotationY = localRotationY; + this.localRotationZ = localRotationZ; return this; } @@ -65,7 +79,8 @@ public class StaticRotatingActorData extends InstanceData { putVec3(buf, x, y, z); put(buf, rotationOffset); putVec3(buf, rotationAxisX, rotationAxisY, rotationAxisZ); - putVec3(buf, localOrientationX, localOrientationY, localOrientationZ); + putVec3(buf, localRotationX, localRotationY, localRotationZ); + putVec3(buf, rotationCenterX, rotationCenterY, rotationCenterZ); } } diff --git a/src/main/java/com/simibubi/create/foundation/render/shader/Shader.java b/src/main/java/com/simibubi/create/foundation/render/shader/Shader.java index 38d0df8a9..075b963b5 100644 --- a/src/main/java/com/simibubi/create/foundation/render/shader/Shader.java +++ b/src/main/java/com/simibubi/create/foundation/render/shader/Shader.java @@ -6,7 +6,7 @@ public enum Shader { CONTRAPTION_STRUCTURE("shader/contraption.vert", "shader/contraption.frag"), CONTRAPTION_ROTATING("shader/contraption_rotating.vert", "shader/contraption.frag"), CONTRAPTION_BELT("shader/contraption_belt.vert", "shader/contraption.frag"), - //CONTRAPTION_ACTOR("shader/contraption_actor.vert", "shader/contraption.frag"), + CONTRAPTION_ACTOR("shader/contraption_actor.vert", "shader/contraption.frag"), ; public final String vert; diff --git a/src/main/resources/assets/create/shader/contraption_actor.vert b/src/main/resources/assets/create/shader/contraption_actor.vert index 1c30f9d71..94083048e 100644 --- a/src/main/resources/assets/create/shader/contraption_actor.vert +++ b/src/main/resources/assets/create/shader/contraption_actor.vert @@ -9,10 +9,11 @@ layout (location = 2) in vec2 aTexCoords; layout (location = 3) in vec3 instancePos; layout (location = 4) in float rotationOffset; layout (location = 5) in vec3 localRotationAxis; -layout (location = 6) in vec3 localOrientation; +layout (location = 6) in vec3 localRotation; +layout (location = 7) in vec3 rotationCenter; // dynamic data -layout (location = 7) in vec3 relativeMotion; +//layout (location = 7) in vec3 relativeMotion; out float Diffuse; out vec2 TexCoords; @@ -40,7 +41,12 @@ mat4 rotate(vec3 axis, float angle) { 0., 0., 0., 1.); } +mat4 rotation(vec3 rot) { + return rotate(vec3(0, 1, 0), rot.y) * rotate(vec3(0, 0, 1), rot.z) * rotate(vec3(1, 0, 0), rot.x); +} + mat4 kineticRotation() { + const float speed = 20; float degrees = rotationOffset + time * speed * -3./10.; float angle = fract(degrees / 360.) * PI * 2.; @@ -55,13 +61,17 @@ float diffuse(vec3 normal) { } void main() { + vec3 rot = fract(localRotation / 360.) * PI * 2.; + mat4 localRot = rotation(rot); + vec4 localPos = localRot * vec4(aPos - 0.5, 1f) + vec4(0.5, 0.5, 0.5, 0); + mat4 kineticRotation = kineticRotation(); - vec4 localPos = kineticRotation * vec4(aPos - 0.5, 1f) + vec4(instancePos + 0.5, 0); + localPos = kineticRotation * vec4(localPos.xyz - rotationCenter, 1) + vec4(instancePos + rotationCenter, 0); vec4 worldPos = model * localPos; BoxCoord = (worldPos.xyz - lightBoxMin) / lightBoxSize; - Diffuse = diffuse(normalize(model * kineticRotation * vec4(aNormal, 0.)).xyz); + Diffuse = diffuse(normalize(model * localRot * kineticRotation * vec4(aNormal, 0.)).xyz); Color = vec4(1.); TexCoords = aTexCoords; gl_Position = projection * view * worldPos;