From b766658415745b0d9c6943f893cd82947074bd28 Mon Sep 17 00:00:00 2001 From: JozsefA Date: Tue, 2 Feb 2021 13:11:22 -0800 Subject: [PATCH] probably fix all issues with ghost instances instanced tiles on contraptions now self light properly --- .../components/actors/DrillRenderer.java | 2 ++ .../components/actors/HarvesterRenderer.java | 5 ++- .../foundation/render/BufferedModel.java | 4 +-- .../foundation/render/TemplateBuffer.java | 5 ++- .../contraption/RenderedContraption.java | 33 +++++++++++-------- .../create/foundation/render/gl/Backend.java | 7 ---- .../foundation/render/gl/backend/Backend.java | 20 +++++++++++ .../render/gl/backend/MapBuffer.java | 4 --- .../render/gl/shader/AllShaderPrograms.java | 2 +- .../render/instancing/InstancedModel.java | 22 +++++++++---- .../actors/StaticRotatingActorData.java | 15 ++++++++- .../assets/create/shader/contraption.frag | 3 +- .../create/shader/contraption_actor.vert | 13 +++++--- .../create/shader/contraption_belt.vert | 2 ++ .../create/shader/contraption_rotating.vert | 2 ++ .../create/shader/contraption_structure.frag | 24 -------------- 16 files changed, 93 insertions(+), 70 deletions(-) delete mode 100644 src/main/java/com/simibubi/create/foundation/render/gl/Backend.java create mode 100644 src/main/java/com/simibubi/create/foundation/render/gl/backend/Backend.java delete mode 100644 src/main/resources/assets/create/shader/contraption_structure.frag 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 e528be196..069627f80 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 @@ -21,6 +21,7 @@ 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.world.LightType; import static net.minecraft.state.properties.BlockStateProperties.FACING; @@ -50,6 +51,7 @@ public class DrillRenderer extends KineticTileEntityRenderer { float eulerX = AngleHelper.verticalAngle(facing) + ((facing.getAxis() == Direction.Axis.Y) ? 180 : 0); float eulerY = facing.getHorizontalAngle(); data.setPosition(context.localPos) + .setBlockLight(contraption.renderWorld.getLightLevel(LightType.BLOCK, context.localPos)) .setRotationOffset(0) .setRotationAxis(0, 0, 1) .setLocalRotation(eulerX, eulerY, 0); 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 094ab403f..08522970e 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 @@ -20,6 +20,7 @@ import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher; import net.minecraft.util.Direction; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Vec3d; +import net.minecraft.world.LightType; import static net.minecraft.state.properties.BlockStateProperties.HORIZONTAL_FACING; @@ -49,6 +50,7 @@ public class HarvesterRenderer extends SafeTileEntityRenderer { + Backend.mapBuffer(GL15.GL_ARRAY_BUFFER, invariantSize, buffer -> { for (int i = 0; i < vertexCount; i++) { copyVertex(buffer, i); } diff --git a/src/main/java/com/simibubi/create/foundation/render/TemplateBuffer.java b/src/main/java/com/simibubi/create/foundation/render/TemplateBuffer.java index b81201c5b..1d81fe2dc 100644 --- a/src/main/java/com/simibubi/create/foundation/render/TemplateBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/render/TemplateBuffer.java @@ -1,8 +1,7 @@ package com.simibubi.create.foundation.render; -import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.datafixers.util.Pair; -import com.simibubi.create.foundation.render.gl.Backend; +import com.simibubi.create.foundation.render.gl.backend.Backend; import com.simibubi.create.foundation.render.gl.GlBuffer; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.vertex.VertexFormatElement; @@ -42,7 +41,7 @@ public class TemplateBuffer { ebo.bind(GL15.GL_ELEMENT_ARRAY_BUFFER); GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesSize, GL15.GL_STATIC_DRAW); - Backend.MAP_BUFFER.mapBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesSize, indices -> { + Backend.mapBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesSize, indices -> { for (int i = 0; i < vertexCount; i++) { indices.putShort((short) i); } 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 a1c3575ee..1d59aaded 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 @@ -19,6 +19,7 @@ import net.minecraft.client.renderer.model.IBakedModel; import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.gen.feature.template.Template; import net.minecraft.world.lighting.WorldLightManager; @@ -36,6 +37,8 @@ import java.util.Random; public class RenderedContraption { private HashMap renderLayers = new HashMap<>(); + public final PlacementSimulationWorld renderWorld; + private final ContraptionLighter lighter; public final ContraptionKineticRenderer kinetics; @@ -48,10 +51,11 @@ public class RenderedContraption { this.contraption = contraption; this.lighter = contraption.makeLighter(); this.kinetics = new ContraptionKineticRenderer(); + this.renderWorld = setupRenderWorld(world, contraption); - buildLayers(contraption); - buildInstancedTiles(contraption); - buildActors(contraption); + buildLayers(); + buildInstancedTiles(); + buildActors(); } public int getEntityId() { @@ -79,34 +83,37 @@ public class RenderedContraption { } } - private void buildLayers(Contraption c) { + private void buildLayers() { for (ContraptionModel buffer : renderLayers.values()) { buffer.delete(); } renderLayers.clear(); - PlacementSimulationWorld renderWorld = setupRenderWorld(c); List blockLayers = RenderType.getBlockLayers(); for (RenderType layer : blockLayers) { - renderLayers.put(layer, buildStructureModel(renderWorld, c, layer)); + renderLayers.put(layer, buildStructureModel(renderWorld, contraption, layer)); } } - private void buildInstancedTiles(Contraption c) { - Collection tileEntities = c.maybeInstancedTileEntities; + private void buildInstancedTiles() { + Collection tileEntities = contraption.maybeInstancedTileEntities; if (!tileEntities.isEmpty()) { for (TileEntity te : tileEntities) { if (te instanceof IInstanceRendered) { - kinetics.getInstance(te); // this is enough to instantiate the model instance + World world = te.getWorld(); + BlockPos pos = te.getPos(); + te.setLocation(renderWorld, pos); + kinetics.add(te); + te.setLocation(world, pos); } } } } - private void buildActors(Contraption c) { - List> actors = c.getActors(); + private void buildActors() { + List> actors = contraption.getActors(); for (MutablePair actor : actors) { Template.BlockInfo blockInfo = actor.left; @@ -172,8 +179,8 @@ public class RenderedContraption { return new ContraptionModel(builder); } - public static PlacementSimulationWorld setupRenderWorld(Contraption c) { - PlacementSimulationWorld renderWorld = new PlacementSimulationWorld(Minecraft.getInstance().world); + public static PlacementSimulationWorld setupRenderWorld(World world, Contraption c) { + PlacementSimulationWorld renderWorld = new PlacementSimulationWorld(world); renderWorld.setTileEntities(c.presentTileEntities.values()); diff --git a/src/main/java/com/simibubi/create/foundation/render/gl/Backend.java b/src/main/java/com/simibubi/create/foundation/render/gl/Backend.java deleted file mode 100644 index d534209a3..000000000 --- a/src/main/java/com/simibubi/create/foundation/render/gl/Backend.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.simibubi.create.foundation.render.gl; - -import com.simibubi.create.foundation.render.gl.backend.MapBuffer; - -public class Backend { - public static final MapBuffer MAP_BUFFER = MapBuffer.GL30_RANGE; -} diff --git a/src/main/java/com/simibubi/create/foundation/render/gl/backend/Backend.java b/src/main/java/com/simibubi/create/foundation/render/gl/backend/Backend.java new file mode 100644 index 000000000..bbadc9fce --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/render/gl/backend/Backend.java @@ -0,0 +1,20 @@ +package com.simibubi.create.foundation.render.gl.backend; + +import java.nio.ByteBuffer; +import java.util.function.Consumer; + +public class Backend { + private static final MapBuffer MAP_BUFFER = MapBuffer.GL30_RANGE; + + private Backend() { + throw new UnsupportedOperationException(); + } + + public static void mapBuffer(int target, int offset, int length, Consumer upload) { + MAP_BUFFER.mapBuffer(target, offset, length, upload); + } + + public static void mapBuffer(int target, int size, Consumer upload) { + MAP_BUFFER.mapBuffer(target, 0, size, upload); + } +} diff --git a/src/main/java/com/simibubi/create/foundation/render/gl/backend/MapBuffer.java b/src/main/java/com/simibubi/create/foundation/render/gl/backend/MapBuffer.java index 82a3e795b..2d50966b0 100644 --- a/src/main/java/com/simibubi/create/foundation/render/gl/backend/MapBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/render/gl/backend/MapBuffer.java @@ -51,8 +51,4 @@ public enum MapBuffer { public abstract void mapBuffer(int target, int offset, int length, Consumer upload); - - public final void mapBuffer(int target, int size, Consumer upload) { - mapBuffer(target, 0, size, upload); - } } diff --git a/src/main/java/com/simibubi/create/foundation/render/gl/shader/AllShaderPrograms.java b/src/main/java/com/simibubi/create/foundation/render/gl/shader/AllShaderPrograms.java index df1903d07..d6ab91d97 100644 --- a/src/main/java/com/simibubi/create/foundation/render/gl/shader/AllShaderPrograms.java +++ b/src/main/java/com/simibubi/create/foundation/render/gl/shader/AllShaderPrograms.java @@ -6,7 +6,7 @@ import net.minecraft.util.ResourceLocation; public enum AllShaderPrograms { ROTATING("shader/rotating.vert", "shader/instanced.frag"), BELT("shader/belt.vert", "shader/instanced.frag"), - CONTRAPTION_STRUCTURE("shader/contraption_structure.vert", "shader/contraption_structure.frag"), + CONTRAPTION_STRUCTURE("shader/contraption_structure.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"), diff --git a/src/main/java/com/simibubi/create/foundation/render/instancing/InstancedModel.java b/src/main/java/com/simibubi/create/foundation/render/instancing/InstancedModel.java index dbeb93c1f..bddec4394 100644 --- a/src/main/java/com/simibubi/create/foundation/render/instancing/InstancedModel.java +++ b/src/main/java/com/simibubi/create/foundation/render/instancing/InstancedModel.java @@ -1,10 +1,9 @@ package com.simibubi.create.foundation.render.instancing; -import com.google.common.collect.Range; import com.simibubi.create.foundation.render.BufferedModel; import com.simibubi.create.foundation.render.RenderMath; -import com.simibubi.create.foundation.render.gl.Backend; +import com.simibubi.create.foundation.render.gl.backend.Backend; import com.simibubi.create.foundation.render.gl.GlBuffer; import net.minecraft.client.renderer.BufferBuilder; import org.lwjgl.opengl.GL11; @@ -161,7 +160,8 @@ public abstract class InstancedModel extends BufferedMod VertexFormat instanceFormat = getInstanceFormat(); int stride = instanceFormat.getStride(); - int instanceSize = RenderMath.nextPowerOf2((instanceCount() + 1) * stride); + int newInstanceCount = instanceCount(); + int instanceSize = RenderMath.nextPowerOf2((newInstanceCount + 1) * stride); instanceVBO.bind(GL15.GL_ARRAY_BUFFER); @@ -170,19 +170,29 @@ public abstract class InstancedModel extends BufferedMod GL15.glBufferData(GL15.GL_ARRAY_BUFFER, instanceSize, GL15.GL_STATIC_DRAW); glBufferSize = instanceSize; minIndexChanged = 0; - maxIndexChanged = data.size() - 1; + maxIndexChanged = newInstanceCount - 1; } int offset = minIndexChanged * stride; int length = (1 + maxIndexChanged - minIndexChanged) * stride; - Backend.MAP_BUFFER.mapBuffer(GL15.GL_ARRAY_BUFFER, offset, length, buffer -> { + Backend.mapBuffer(GL15.GL_ARRAY_BUFFER, offset, length, buffer -> { for (int i = minIndexChanged; i <= maxIndexChanged; i++) { data.get(i).write(buffer); } }); - glInstanceCount = data.size(); + if (newInstanceCount < glInstanceCount) { + int clearFrom = (maxIndexChanged + 1) * stride; + int clearTo = (glInstanceCount) * stride; + Backend.mapBuffer(GL15.GL_ARRAY_BUFFER, clearFrom, clearTo - clearFrom, buffer -> { + for (int i = clearFrom; i < clearTo; i++) { + buffer.put((byte) 0); + } + }); + } + + glInstanceCount = newInstanceCount; int staticAttributes = getModelFormat().getShaderAttributeCount(); instanceFormat.informAttributes(staticAttributes); 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 1b99cbd59..16ee52c25 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 @@ -10,11 +10,13 @@ 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, VEC3, NORMAL); + public static VertexFormat FORMAT = new VertexFormat(POSITION, LIGHT, FLOAT, NORMAL, VEC3, NORMAL); private float x; private float y; private float z; + private byte blockLight; + private byte skyLight; private float rotationOffset; private byte rotationAxisX; private byte rotationAxisY; @@ -33,6 +35,16 @@ public class StaticRotatingActorData extends InstanceData { return this; } + public StaticRotatingActorData setBlockLight(int blockLight) { + this.blockLight = (byte) ((blockLight & 0xF) << 4); + return this; + } + + public StaticRotatingActorData setSkyLight(int skyLight) { + this.skyLight = (byte) ((skyLight & 0xF) << 4); + return this; + } + public StaticRotatingActorData setRotationOffset(float rotationOffset) { this.rotationOffset = rotationOffset; return this; @@ -77,6 +89,7 @@ public class StaticRotatingActorData extends InstanceData { @Override public void write(ByteBuffer buf) { putVec3(buf, x, y, z); + putVec2(buf, blockLight, skyLight); put(buf, rotationOffset); putVec3(buf, rotationAxisX, rotationAxisY, rotationAxisZ); putVec3(buf, localRotationX, localRotationY, localRotationZ); diff --git a/src/main/resources/assets/create/shader/contraption.frag b/src/main/resources/assets/create/shader/contraption.frag index d2ebc1d33..b8a07b4dc 100644 --- a/src/main/resources/assets/create/shader/contraption.frag +++ b/src/main/resources/assets/create/shader/contraption.frag @@ -4,6 +4,7 @@ in vec2 TexCoords; in vec4 Color; in float Diffuse; in vec3 BoxCoord; +in vec2 ModelLight; out vec4 fragColor; @@ -13,7 +14,7 @@ layout(binding=4) uniform sampler3D LightVolume; vec4 light() { vec2 lm = texture(LightVolume, BoxCoord).rg * 0.9375 + 0.03125; - return texture2D(LightMap, lm); + return texture2D(LightMap, max(lm, ModelLight)); } void main() { diff --git a/src/main/resources/assets/create/shader/contraption_actor.vert b/src/main/resources/assets/create/shader/contraption_actor.vert index 58c00ee1e..e0f1b59ef 100644 --- a/src/main/resources/assets/create/shader/contraption_actor.vert +++ b/src/main/resources/assets/create/shader/contraption_actor.vert @@ -7,18 +7,20 @@ layout (location = 2) in vec2 aTexCoords; // instance data layout (location = 3) in vec3 instancePos; -layout (location = 4) in float rotationOffset; -layout (location = 5) in vec3 localRotationAxis; -layout (location = 6) in vec3 localRotation; -layout (location = 7) in vec3 rotationCenter; +layout (location = 4) in vec2 modelLight; +layout (location = 5) in float rotationOffset; +layout (location = 6) in vec3 localRotationAxis; +layout (location = 7) in vec3 localRotation; +layout (location = 8) in vec3 rotationCenter; // dynamic data -//layout (location = 7) in vec3 relativeMotion; +//layout (location = 9) in vec3 relativeMotion; out float Diffuse; out vec2 TexCoords; out vec4 Color; out vec3 BoxCoord; +out vec2 ModelLight; uniform vec3 lightBoxSize; uniform vec3 lightBoxMin; @@ -77,6 +79,7 @@ void main() { BoxCoord = (worldPos.xyz - lightBoxMin) / lightBoxSize; Diffuse = diffuse(norm); TexCoords = aTexCoords; + ModelLight = modelLight; gl_Position = projection * view * worldPos; if (debug == 2) { diff --git a/src/main/resources/assets/create/shader/contraption_belt.vert b/src/main/resources/assets/create/shader/contraption_belt.vert index 8a0b8fbeb..520e5ce72 100644 --- a/src/main/resources/assets/create/shader/contraption_belt.vert +++ b/src/main/resources/assets/create/shader/contraption_belt.vert @@ -19,6 +19,7 @@ out float Diffuse; out vec2 TexCoords; out vec4 Color; out vec3 BoxCoord; +out vec2 ModelLight; uniform vec3 lightBoxSize; uniform vec3 lightBoxMin; @@ -72,6 +73,7 @@ void main() { BoxCoord = (worldPos.xyz - lightBoxMin) / lightBoxSize; Diffuse = diffuse(norm); TexCoords = aTexCoords - uv + scrollTexture.xy + vec2(0, scroll); + ModelLight = light; gl_Position = projection * view * worldPos; if (debug == 2) { diff --git a/src/main/resources/assets/create/shader/contraption_rotating.vert b/src/main/resources/assets/create/shader/contraption_rotating.vert index 6506bdd09..6b91caf3e 100644 --- a/src/main/resources/assets/create/shader/contraption_rotating.vert +++ b/src/main/resources/assets/create/shader/contraption_rotating.vert @@ -15,6 +15,7 @@ out float Diffuse; out vec2 TexCoords; out vec4 Color; out vec3 BoxCoord; +out vec2 ModelLight; uniform vec3 lightBoxSize; uniform vec3 lightBoxMin; @@ -67,6 +68,7 @@ void main() { BoxCoord = (worldPos.xyz - lightBoxMin) / lightBoxSize; Diffuse = diffuse(norm); TexCoords = aTexCoords; + ModelLight = light; gl_Position = projection * view * worldPos; if (debug == 2) { diff --git a/src/main/resources/assets/create/shader/contraption_structure.frag b/src/main/resources/assets/create/shader/contraption_structure.frag deleted file mode 100644 index b8a07b4dc..000000000 --- a/src/main/resources/assets/create/shader/contraption_structure.frag +++ /dev/null @@ -1,24 +0,0 @@ -#version 440 core - -in vec2 TexCoords; -in vec4 Color; -in float Diffuse; -in vec3 BoxCoord; -in vec2 ModelLight; - -out vec4 fragColor; - -layout(binding=0) uniform sampler2D BlockAtlas; -layout(binding=2) uniform sampler2D LightMap; -layout(binding=4) uniform sampler3D LightVolume; - -vec4 light() { - vec2 lm = texture(LightVolume, BoxCoord).rg * 0.9375 + 0.03125; - return texture2D(LightMap, max(lm, ModelLight)); -} - -void main() { - vec4 tex = texture2D(BlockAtlas, TexCoords); - - fragColor = vec4(tex.rgb * light().rgb * Diffuse * Color.rgb, tex.a); -} \ No newline at end of file