diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/ContraptionBuffer.java b/src/main/java/com/simibubi/create/foundation/utility/render/ContraptionBuffer.java index d667788ca..44ac249d2 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/ContraptionBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/ContraptionBuffer.java @@ -2,15 +2,19 @@ package com.simibubi.create.foundation.utility.render; import com.mojang.blaze3d.platform.GlStateManager; import com.simibubi.create.CreateClient; +import com.simibubi.create.foundation.utility.render.instancing.VertexFormat; import net.minecraft.client.renderer.BufferBuilder; import net.minecraft.client.renderer.GLAllocation; +import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.vertex.VertexFormatElement; import org.lwjgl.opengl.*; +import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*; import java.nio.Buffer; import java.nio.ByteBuffer; public class ContraptionBuffer extends TemplateBuffer { + public static final VertexFormat FORMAT = new VertexFormat(POSITION, NORMAL, UV, COLOR); protected int vao, ebo, vbo; @@ -39,7 +43,7 @@ public class ContraptionBuffer extends TemplateBuffer { GL40.glDrawElements(GL11.GL_QUADS, count, GL11.GL_UNSIGNED_SHORT, 0); - for (int i = 0; i <= 3; i++) { + for (int i = 0; i <= FORMAT.getNumAttributes(); i++) { GL40.glDisableVertexAttribArray(i); } @@ -48,9 +52,7 @@ public class ContraptionBuffer extends TemplateBuffer { } private void setup() { - int floatSize = VertexFormatElement.Type.FLOAT.getSize(); - - int stride = floatSize * 8; + int stride = FORMAT.getStride(); int invariantSize = count * stride; ByteBuffer constant = GLAllocation.createDirectByteBuffer(invariantSize); @@ -75,6 +77,11 @@ public class ContraptionBuffer extends TemplateBuffer { constant.putFloat(getU(template, i)); constant.putFloat(getV(template, i)); + constant.putFloat(getR(template, i) / 255f); + constant.putFloat(getG(template, i) / 255f); + constant.putFloat(getB(template, i) / 255f); + constant.putFloat(getA(template, i) / 255f); + indices.putShort((short) i); } constant.rewind(); @@ -92,14 +99,7 @@ public class ContraptionBuffer extends TemplateBuffer { GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, ebo); GlStateManager.bufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW); - // vertex positions - GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, stride, 0); - - // vertex normals - GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, stride, floatSize * 3L); - - // uv position - GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, stride, floatSize * 6L); + FORMAT.informAttributes(0); GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, 0); GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/FastContraptionRenderer.java b/src/main/java/com/simibubi/create/foundation/utility/render/FastContraptionRenderer.java index 99bc7dabe..7871e86de 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/FastContraptionRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/FastContraptionRenderer.java @@ -10,6 +10,7 @@ import com.simibubi.create.foundation.utility.render.shader.Shader; import com.simibubi.create.foundation.utility.render.shader.ShaderHelper; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.*; +import net.minecraft.client.renderer.color.BlockColors; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; import net.minecraftforge.client.event.RenderWorldLastEvent; diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/FastKineticRenderer.java b/src/main/java/com/simibubi/create/foundation/utility/render/FastKineticRenderer.java index 804bd174c..dbee28fca 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/FastKineticRenderer.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/FastKineticRenderer.java @@ -12,6 +12,7 @@ import com.simibubi.create.foundation.utility.render.shader.ShaderCallback; import com.simibubi.create.foundation.utility.render.shader.ShaderHelper; import net.minecraft.block.BlockState; import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientChunkProvider; import net.minecraft.client.renderer.*; import net.minecraft.client.renderer.model.IBakedModel; import net.minecraft.client.renderer.texture.Texture; @@ -74,7 +75,7 @@ public class FastKineticRenderer { } public void tick() { - // TODO: (later) detect changes in lighting with a mixin to ClientChunkProvider.markLightChanged() + // TODO: (later) detect changes in lighting with a mixin (or forge hook) to ClientChunkProvider.markLightChanged() for (Cache cache : rotating.values()) { for (RotatingBuffer renderer : cache.asMap().values()) { renderer.clearInstanceData(); diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/BeltData.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/BeltData.java index 079e56174..a4f717903 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/BeltData.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/BeltData.java @@ -9,7 +9,7 @@ import java.nio.ByteBuffer; import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*; public class BeltData extends BasicData { - public static VertexFormat FORMAT = new VertexFormat(VEC3, VEC2, VEC3, FLOAT, VEC2, VEC4, FLOAT); + public static VertexFormat FORMAT = new VertexFormat(POSITION, LIGHT, VEC3, FLOAT, VEC2, VEC4, FLOAT); private float rotX; private float rotY; diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceBuffer.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceBuffer.java index cde07b894..ec46b91f6 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceBuffer.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/InstanceBuffer.java @@ -14,7 +14,10 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.function.Consumer; +import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*; + public abstract class InstanceBuffer extends TemplateBuffer { + public static final VertexFormat FORMAT = new VertexFormat(POSITION, NORMAL, UV); protected int vao, ebo, invariantVBO, instanceVBO, instanceCount; @@ -27,9 +30,7 @@ public abstract class InstanceBuffer extends TemplateBuf } private void setupMainData() { - int floatSize = VertexFormatElement.Type.FLOAT.getSize(); - - int stride = floatSize * 8; + int stride = FORMAT.getStride(); int invariantSize = count * stride; ByteBuffer constant = GLAllocation.createDirectByteBuffer(invariantSize); @@ -72,14 +73,7 @@ public abstract class InstanceBuffer extends TemplateBuf GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, ebo); GlStateManager.bufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW); - // vertex positions - GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, stride, 0); - - // vertex normals - GL20.glVertexAttribPointer(1, 3, GL11.GL_FLOAT, false, stride, floatSize * 3L); - - // uv position - GL20.glVertexAttribPointer(2, 2, GL11.GL_FLOAT, false, stride, floatSize * 6L); + FORMAT.informAttributes(0); GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, 0); GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/RotatingData.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/RotatingData.java index 0d556af4b..92e60202f 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/RotatingData.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/RotatingData.java @@ -8,7 +8,7 @@ import java.nio.ByteBuffer; import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*; public class RotatingData extends BasicData { - public static VertexFormat FORMAT = new VertexFormat(VEC3, VEC2, FLOAT, FLOAT, VEC3); + public static VertexFormat FORMAT = new VertexFormat(POSITION, LIGHT, FLOAT, FLOAT, VEC3); private float rotationalSpeed; private float rotationOffset; diff --git a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexAttribute.java b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexAttribute.java index 1a353623e..60d94311d 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexAttribute.java +++ b/src/main/java/com/simibubi/create/foundation/utility/render/instancing/VertexAttribute.java @@ -4,12 +4,17 @@ import net.minecraft.client.renderer.vertex.VertexFormatElement; import org.lwjgl.opengl.GL20; public class VertexAttribute { + public static final VertexAttribute MAT4 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 16); + public static final VertexAttribute VEC4 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 4); + public static final VertexAttribute VEC3 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 3); + public static final VertexAttribute VEC2 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 2); + public static final VertexAttribute FLOAT = new VertexAttribute(VertexFormatElement.Type.FLOAT, 1); - 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); + public static final VertexAttribute POSITION = VEC3; + public static final VertexAttribute NORMAL = VEC3; + public static final VertexAttribute COLOR = VEC4; + public static final VertexAttribute UV = VEC2; + public static final VertexAttribute LIGHT= VEC2; private final VertexFormatElement.Type type; private final int count; diff --git a/src/main/resources/assets/create/shader/belt.vert b/src/main/resources/assets/create/shader/belt.vert index fe4188c5e..fa9a03d32 100644 --- a/src/main/resources/assets/create/shader/belt.vert +++ b/src/main/resources/assets/create/shader/belt.vert @@ -13,9 +13,10 @@ layout (location = 7) in vec2 sourceUV; layout (location = 8) in vec4 scrollTexture; layout (location = 9) in float scrollMult; -out vec3 Normal; out vec2 TexCoords; out vec2 Light; +out vec4 Color; +out float Diffuse; uniform float time; uniform int ticks; @@ -33,6 +34,13 @@ mat4 rotate(vec3 axis, float angle) { 0., 0., 0., 1.); } +float diffuse(vec3 normal) { + float x = normal.x; + float y = normal.y; + float z = normal.z; + return min(x * x * 0.6f + y * y * ((3f + y) / 4f) + z * z * 0.8f, 1f); +} + void main() { vec3 rot = fract(rotationDegrees / 360.) * PI * 2.; @@ -45,7 +53,8 @@ void main() { float scroll = fract(speed * time / (36 * 16.)) * scrollSize * scrollMult; - Normal = normalize((rotation * vec4(aNormal, 0.)).xyz); + Diffuse = diffuse(normalize((rotation * vec4(aNormal, 0.)).xyz)); + Color = vec4(1f); Light = light; TexCoords = aTexCoords - sourceUV + scrollTexture.xy + vec2(0., scroll); gl_Position = projection * view * renderPos; diff --git a/src/main/resources/assets/create/shader/contraption_static.vert b/src/main/resources/assets/create/shader/contraption_static.vert index 673d3bdde..310c82f25 100644 --- a/src/main/resources/assets/create/shader/contraption_static.vert +++ b/src/main/resources/assets/create/shader/contraption_static.vert @@ -4,10 +4,12 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords; +layout (location = 3) in vec4 aColor; -out vec3 Normal; +out float Diffuse; out vec2 TexCoords; out vec2 Light; +out vec4 Color; layout (binding = 2) uniform sampler3D lightVolume; @@ -27,9 +29,9 @@ mat4 rotate(vec3 axis, float angle) float oc = 1.0 - c; return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0., - oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0., - oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0., - 0., 0., 0., 1.); + oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0., + oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0., + 0., 0., 0., 1.); } mat4 contraptionRotation() { @@ -37,6 +39,13 @@ mat4 contraptionRotation() { return rotate(vec3(0, 1, 0), rot.y) * rotate(vec3(0, 0, 1), rot.z) * rotate(vec3(1, 0, 0), rot.x); } +float diffuse(vec3 normal) { + float x = normal.x; + float y = normal.y; + float z = normal.z; + return min(x * x * 0.6f + y * y * ((3f + y) / 4f) + z * z * 0.8f, 1f); +} + void main() { mat4 rotation = contraptionRotation(); @@ -46,8 +55,11 @@ void main() { vec3 boxCoord = (worldPos.xyz - cPos - cSize * 0.5) / cSize; + float df = diffuse(normalize(aNormal)); + + Diffuse = diffuse(normalize((rotation * vec4(aNormal, 0.)).xyz)); + Color = vec4(aColor.rgb / df, aColor.a); Light = vec2(1.); - Normal = normalize((rotation * vec4(aNormal, 0.)).xyz); TexCoords = aTexCoords; gl_Position = projection * view * worldPos; } diff --git a/src/main/resources/assets/create/shader/instanced.frag b/src/main/resources/assets/create/shader/instanced.frag index f10008954..9ef766648 100644 --- a/src/main/resources/assets/create/shader/instanced.frag +++ b/src/main/resources/assets/create/shader/instanced.frag @@ -1,21 +1,15 @@ #version 440 core -in vec3 Normal; in vec2 TexCoords; in vec2 Light; +in vec4 Color; +in float Diffuse; out vec4 fragColor; layout(binding=0) uniform sampler2D BlockAtlas; layout(binding=1) uniform sampler2D LightMap; -float diffuse() { - float x = Normal.x; - float y = Normal.y; - float z = Normal.z; - return min(x * x * 0.6f + y * y * ((3f + y) / 4f) + z * z * 0.8f, 1f); -} - vec4 light() { vec2 lm = Light * 0.9375 + 0.03125; return texture2D(LightMap, lm); @@ -25,10 +19,7 @@ vec4 light() { void main() { vec4 tex = texture2D(BlockAtlas, TexCoords); - tex *= vec4(light().rgb, 1); - - float df = diffuse(); - tex *= vec4(df, df, df, 1); + tex *= vec4(light().rgb * Diffuse, 1); fragColor = tex; } \ No newline at end of file diff --git a/src/main/resources/assets/create/shader/rotating.vert b/src/main/resources/assets/create/shader/rotating.vert index 9c1efea5d..c5ffa4aa2 100644 --- a/src/main/resources/assets/create/shader/rotating.vert +++ b/src/main/resources/assets/create/shader/rotating.vert @@ -10,9 +10,10 @@ layout (location = 5) in float speed; layout (location = 6) in float rotationOffset; layout (location = 7) in vec3 rotationAxis; -out vec3 Normal; out vec2 TexCoords; out vec2 Light; +out vec4 Color; +out float Diffuse; uniform float time; uniform int ticks; @@ -34,14 +35,21 @@ mat4 kineticRotation() { 0., 0., 0., 1.); } +float diffuse(vec3 normal) { + float x = normal.x; + float y = normal.y; + float z = normal.z; + return min(x * x * 0.6f + y * y * ((3f + y) / 4f) + z * z * 0.8f, 1f); +} void main() { mat4 rotation = kineticRotation(); vec4 renderPos = rotation * vec4(aPos - vec3(0.5), 1); renderPos += vec4(instancePos + vec3(0.5), 0); + Diffuse = diffuse(normalize((rotation * vec4(aNormal, 0.)).xyz)); + Color = vec4(1f); TexCoords = aTexCoords; - Normal = normalize((rotation * vec4(aNormal, 0.)).xyz); gl_Position = projection * view * renderPos; Light = light; } \ No newline at end of file