start fixing structure tint

This commit is contained in:
JozsefA 2021-01-08 14:53:22 -08:00
parent 05ab482f70
commit 7443ac5031
11 changed files with 73 additions and 52 deletions

View file

@ -2,15 +2,19 @@ package com.simibubi.create.foundation.utility.render;
import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.GlStateManager;
import com.simibubi.create.CreateClient; 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.BufferBuilder;
import net.minecraft.client.renderer.GLAllocation; import net.minecraft.client.renderer.GLAllocation;
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.renderer.vertex.VertexFormatElement; import net.minecraft.client.renderer.vertex.VertexFormatElement;
import org.lwjgl.opengl.*; import org.lwjgl.opengl.*;
import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*;
import java.nio.Buffer; import java.nio.Buffer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
public class ContraptionBuffer extends TemplateBuffer { public class ContraptionBuffer extends TemplateBuffer {
public static final VertexFormat FORMAT = new VertexFormat(POSITION, NORMAL, UV, COLOR);
protected int vao, ebo, vbo; 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); 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); GL40.glDisableVertexAttribArray(i);
} }
@ -48,9 +52,7 @@ public class ContraptionBuffer extends TemplateBuffer {
} }
private void setup() { private void setup() {
int floatSize = VertexFormatElement.Type.FLOAT.getSize(); int stride = FORMAT.getStride();
int stride = floatSize * 8;
int invariantSize = count * stride; int invariantSize = count * stride;
ByteBuffer constant = GLAllocation.createDirectByteBuffer(invariantSize); ByteBuffer constant = GLAllocation.createDirectByteBuffer(invariantSize);
@ -75,6 +77,11 @@ public class ContraptionBuffer extends TemplateBuffer {
constant.putFloat(getU(template, i)); constant.putFloat(getU(template, i));
constant.putFloat(getV(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); indices.putShort((short) i);
} }
constant.rewind(); constant.rewind();
@ -92,14 +99,7 @@ public class ContraptionBuffer extends TemplateBuffer {
GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, ebo); GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, ebo);
GlStateManager.bufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW); GlStateManager.bufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW);
// vertex positions FORMAT.informAttributes(0);
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);
GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, 0); GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, 0);
GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

View file

@ -10,6 +10,7 @@ import com.simibubi.create.foundation.utility.render.shader.Shader;
import com.simibubi.create.foundation.utility.render.shader.ShaderHelper; import com.simibubi.create.foundation.utility.render.shader.ShaderHelper;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.*; import net.minecraft.client.renderer.*;
import net.minecraft.client.renderer.color.BlockColors;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderWorldLastEvent; import net.minecraftforge.client.event.RenderWorldLastEvent;

View file

@ -12,6 +12,7 @@ import com.simibubi.create.foundation.utility.render.shader.ShaderCallback;
import com.simibubi.create.foundation.utility.render.shader.ShaderHelper; import com.simibubi.create.foundation.utility.render.shader.ShaderHelper;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientChunkProvider;
import net.minecraft.client.renderer.*; import net.minecraft.client.renderer.*;
import net.minecraft.client.renderer.model.IBakedModel; import net.minecraft.client.renderer.model.IBakedModel;
import net.minecraft.client.renderer.texture.Texture; import net.minecraft.client.renderer.texture.Texture;
@ -74,7 +75,7 @@ public class FastKineticRenderer {
} }
public void tick() { 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<Object, RotatingBuffer> cache : rotating.values()) { for (Cache<Object, RotatingBuffer> cache : rotating.values()) {
for (RotatingBuffer renderer : cache.asMap().values()) { for (RotatingBuffer renderer : cache.asMap().values()) {
renderer.clearInstanceData(); renderer.clearInstanceData();

View file

@ -9,7 +9,7 @@ import java.nio.ByteBuffer;
import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*; import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*;
public class BeltData extends BasicData<BeltData> { public class BeltData extends BasicData<BeltData> {
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 rotX;
private float rotY; private float rotY;

View file

@ -14,7 +14,10 @@ import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.function.Consumer; import java.util.function.Consumer;
import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*;
public abstract class InstanceBuffer<D extends InstanceData> extends TemplateBuffer { public abstract class InstanceBuffer<D extends InstanceData> extends TemplateBuffer {
public static final VertexFormat FORMAT = new VertexFormat(POSITION, NORMAL, UV);
protected int vao, ebo, invariantVBO, instanceVBO, instanceCount; protected int vao, ebo, invariantVBO, instanceVBO, instanceCount;
@ -27,9 +30,7 @@ public abstract class InstanceBuffer<D extends InstanceData> extends TemplateBuf
} }
private void setupMainData() { private void setupMainData() {
int floatSize = VertexFormatElement.Type.FLOAT.getSize(); int stride = FORMAT.getStride();
int stride = floatSize * 8;
int invariantSize = count * stride; int invariantSize = count * stride;
ByteBuffer constant = GLAllocation.createDirectByteBuffer(invariantSize); ByteBuffer constant = GLAllocation.createDirectByteBuffer(invariantSize);
@ -72,14 +73,7 @@ public abstract class InstanceBuffer<D extends InstanceData> extends TemplateBuf
GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, ebo); GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, ebo);
GlStateManager.bufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW); GlStateManager.bufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indices, GL15.GL_STATIC_DRAW);
// vertex positions FORMAT.informAttributes(0);
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);
GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, 0); GlStateManager.bindBuffers(GL15.GL_ARRAY_BUFFER, 0);
GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, 0); GlStateManager.bindBuffers(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);

View file

@ -8,7 +8,7 @@ import java.nio.ByteBuffer;
import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*; import static com.simibubi.create.foundation.utility.render.instancing.VertexAttribute.*;
public class RotatingData extends BasicData<RotatingData> { public class RotatingData extends BasicData<RotatingData> {
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 rotationalSpeed;
private float rotationOffset; private float rotationOffset;

View file

@ -4,12 +4,17 @@ import net.minecraft.client.renderer.vertex.VertexFormatElement;
import org.lwjgl.opengl.GL20; import org.lwjgl.opengl.GL20;
public class VertexAttribute { 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 final VertexAttribute POSITION = VEC3;
public static VertexAttribute VEC4 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 4); public static final VertexAttribute NORMAL = VEC3;
public static VertexAttribute VEC3 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 3); public static final VertexAttribute COLOR = VEC4;
public static VertexAttribute VEC2 = new VertexAttribute(VertexFormatElement.Type.FLOAT, 2); public static final VertexAttribute UV = VEC2;
public static VertexAttribute FLOAT = new VertexAttribute(VertexFormatElement.Type.FLOAT, 1); public static final VertexAttribute LIGHT= VEC2;
private final VertexFormatElement.Type type; private final VertexFormatElement.Type type;
private final int count; private final int count;

View file

@ -13,9 +13,10 @@ layout (location = 7) in vec2 sourceUV;
layout (location = 8) in vec4 scrollTexture; layout (location = 8) in vec4 scrollTexture;
layout (location = 9) in float scrollMult; layout (location = 9) in float scrollMult;
out vec3 Normal;
out vec2 TexCoords; out vec2 TexCoords;
out vec2 Light; out vec2 Light;
out vec4 Color;
out float Diffuse;
uniform float time; uniform float time;
uniform int ticks; uniform int ticks;
@ -33,6 +34,13 @@ mat4 rotate(vec3 axis, float angle) {
0., 0., 0., 1.); 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() { void main() {
vec3 rot = fract(rotationDegrees / 360.) * PI * 2.; vec3 rot = fract(rotationDegrees / 360.) * PI * 2.;
@ -45,7 +53,8 @@ void main() {
float scroll = fract(speed * time / (36 * 16.)) * scrollSize * scrollMult; 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; Light = light;
TexCoords = aTexCoords - sourceUV + scrollTexture.xy + vec2(0., scroll); TexCoords = aTexCoords - sourceUV + scrollTexture.xy + vec2(0., scroll);
gl_Position = projection * view * renderPos; gl_Position = projection * view * renderPos;

View file

@ -4,10 +4,12 @@
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords; layout (location = 2) in vec2 aTexCoords;
layout (location = 3) in vec4 aColor;
out vec3 Normal; out float Diffuse;
out vec2 TexCoords; out vec2 TexCoords;
out vec2 Light; out vec2 Light;
out vec4 Color;
layout (binding = 2) uniform sampler3D lightVolume; layout (binding = 2) uniform sampler3D lightVolume;
@ -27,9 +29,9 @@ mat4 rotate(vec3 axis, float angle)
float oc = 1.0 - c; 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., 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.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., 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.); 0., 0., 0., 1.);
} }
mat4 contraptionRotation() { 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); 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() { void main() {
mat4 rotation = contraptionRotation(); mat4 rotation = contraptionRotation();
@ -46,8 +55,11 @@ void main() {
vec3 boxCoord = (worldPos.xyz - cPos - cSize * 0.5) / cSize; 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.); Light = vec2(1.);
Normal = normalize((rotation * vec4(aNormal, 0.)).xyz);
TexCoords = aTexCoords; TexCoords = aTexCoords;
gl_Position = projection * view * worldPos; gl_Position = projection * view * worldPos;
} }

View file

@ -1,21 +1,15 @@
#version 440 core #version 440 core
in vec3 Normal;
in vec2 TexCoords; in vec2 TexCoords;
in vec2 Light; in vec2 Light;
in vec4 Color;
in float Diffuse;
out vec4 fragColor; out vec4 fragColor;
layout(binding=0) uniform sampler2D BlockAtlas; layout(binding=0) uniform sampler2D BlockAtlas;
layout(binding=1) uniform sampler2D LightMap; 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() { vec4 light() {
vec2 lm = Light * 0.9375 + 0.03125; vec2 lm = Light * 0.9375 + 0.03125;
return texture2D(LightMap, lm); return texture2D(LightMap, lm);
@ -25,10 +19,7 @@ vec4 light() {
void main() { void main() {
vec4 tex = texture2D(BlockAtlas, TexCoords); vec4 tex = texture2D(BlockAtlas, TexCoords);
tex *= vec4(light().rgb, 1); tex *= vec4(light().rgb * Diffuse, 1);
float df = diffuse();
tex *= vec4(df, df, df, 1);
fragColor = tex; fragColor = tex;
} }

View file

@ -10,9 +10,10 @@ layout (location = 5) in float speed;
layout (location = 6) in float rotationOffset; layout (location = 6) in float rotationOffset;
layout (location = 7) in vec3 rotationAxis; layout (location = 7) in vec3 rotationAxis;
out vec3 Normal;
out vec2 TexCoords; out vec2 TexCoords;
out vec2 Light; out vec2 Light;
out vec4 Color;
out float Diffuse;
uniform float time; uniform float time;
uniform int ticks; uniform int ticks;
@ -34,14 +35,21 @@ mat4 kineticRotation() {
0., 0., 0., 1.); 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() { void main() {
mat4 rotation = kineticRotation(); mat4 rotation = kineticRotation();
vec4 renderPos = rotation * vec4(aPos - vec3(0.5), 1); vec4 renderPos = rotation * vec4(aPos - vec3(0.5), 1);
renderPos += vec4(instancePos + vec3(0.5), 0); renderPos += vec4(instancePos + vec3(0.5), 0);
Diffuse = diffuse(normalize((rotation * vec4(aNormal, 0.)).xyz));
Color = vec4(1f);
TexCoords = aTexCoords; TexCoords = aTexCoords;
Normal = normalize((rotation * vec4(aNormal, 0.)).xyz);
gl_Position = projection * view * renderPos; gl_Position = projection * view * renderPos;
Light = light; Light = light;
} }