world fog works, fluid fog still doesn't.

better dimension changes.
This commit is contained in:
JozsefA 2021-02-09 21:18:05 -08:00
parent 0cb9094913
commit e93562569b
15 changed files with 96 additions and 27 deletions

View file

@ -39,11 +39,13 @@ import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ActiveRenderInfo; import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.renderer.IRenderTypeBuffer; import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.fluid.Fluid; import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.IFluidState; import net.minecraft.fluid.IFluidState;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.math.Vec3d; import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
import net.minecraft.world.IWorld;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.EntityViewRenderEvent; import net.minecraftforge.client.event.EntityViewRenderEvent;
@ -112,15 +114,21 @@ public class ClientEvents {
@SubscribeEvent @SubscribeEvent
public static void onLoadWorld(WorldEvent.Load event) { public static void onLoadWorld(WorldEvent.Load event) {
IWorld world = event.getWorld();
if (world.isRemote() && world instanceof ClientWorld) {
CreateClient.invalidateRenderers(); CreateClient.invalidateRenderers();
AnimationTickHolder.reset(); AnimationTickHolder.reset();
((ClientWorld) world).loadedTileEntityList.forEach(CreateClient.kineticRenderer::add);
}
} }
@SubscribeEvent @SubscribeEvent
public static void onUnloadWorld(WorldEvent.Unload event) { public static void onUnloadWorld(WorldEvent.Unload event) {
if (event.getWorld().isRemote()) {
CreateClient.invalidateRenderers(); CreateClient.invalidateRenderers();
AnimationTickHolder.reset(); AnimationTickHolder.reset();
} }
}
@SubscribeEvent @SubscribeEvent
public static void onRenderWorld(RenderWorldLastEvent event) { public static void onRenderWorld(RenderWorldLastEvent event) {

View file

@ -20,8 +20,8 @@ public class RenderInLayerMixin {
* layer-correct custom rendering. RenderWorldLast is not refined enough for rendering world objects. * layer-correct custom rendering. RenderWorldLast is not refined enough for rendering world objects.
* This should probably be a forge event. * This should probably be a forge event.
*/ */
@Inject(at = @At("TAIL"), method = "renderLayer") @Inject(at = @At(value = "INVOKE", target = "Lnet/minecraft/profiler/IProfiler;endSection()V", ordinal = 1), method = "renderLayer")
private void renderLayer(RenderType type, MatrixStack stack, double cameraX, double cameraY, double cameraZ, CallbackInfo ci) { private void renderLayer(RenderType type, MatrixStack stack, double cameraX, double cameraY, double cameraZ, CallbackInfo ci) {
FastRenderDispatcher.renderLayer(type, stack, cameraX, cameraY, cameraZ); FastRenderDispatcher.renderLayer(type, stack, (float) cameraX, (float) cameraY, (float) cameraZ);
} }
} }

View file

@ -96,10 +96,10 @@ public class FastRenderDispatcher {
} }
} }
public static void renderLayer(RenderType type, MatrixStack stack, double cameraX, double cameraY, double cameraZ) { public static void renderLayer(RenderType type, MatrixStack stack, float cameraX, float cameraY, float cameraZ) {
if (!available()) return; if (!available()) return;
Matrix4f viewProjection = Matrix4f.translate((float) -cameraX, (float) -cameraY, (float) -cameraZ); Matrix4f viewProjection = Matrix4f.translate(-cameraX, -cameraY, -cameraZ);
viewProjection.multiplyBackward(stack.peek().getModel()); viewProjection.multiplyBackward(stack.peek().getModel());
viewProjection.multiplyBackward(getProjectionMatrix()); viewProjection.multiplyBackward(getProjectionMatrix());
@ -108,11 +108,12 @@ public class FastRenderDispatcher {
RenderSystem.enableDepthTest(); RenderSystem.enableDepthTest();
RenderSystem.enableCull(); RenderSystem.enableCull();
GL11.glCullFace(GL11.GL_BACK); GL11.glCullFace(GL11.GL_BACK);
CreateClient.kineticRenderer.render(type, viewProjection); CreateClient.kineticRenderer.render(type, viewProjection, cameraX, cameraY, cameraZ);
RenderSystem.disableCull(); RenderSystem.disableCull();
//RenderSystem.disableDepthTest(); //RenderSystem.disableDepthTest();
ContraptionRenderDispatcher.renderLayer(type, viewProjection); ContraptionRenderDispatcher.renderLayer(type, viewProjection, cameraX, cameraY, cameraZ);
if (!OptifineHandler.usingShaders())
GL20.glUseProgram(0); GL20.glUseProgram(0);
type.endDrawing(); type.endDrawing();
} }

View file

@ -1,6 +1,7 @@
package com.simibubi.create.foundation.render.contraption; package com.simibubi.create.foundation.render.contraption;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.platform.GlStateManager;
import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntity;
import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntityRenderer; import com.simibubi.create.content.contraptions.components.structureMovement.AbstractContraptionEntityRenderer;
import com.simibubi.create.content.contraptions.components.structureMovement.Contraption; import com.simibubi.create.content.contraptions.components.structureMovement.Contraption;
@ -86,7 +87,7 @@ public class ContraptionRenderDispatcher {
return renderer; return renderer;
} }
public static void renderLayer(RenderType layer, Matrix4f viewProjection) { public static void renderLayer(RenderType layer, Matrix4f viewProjection, float camX, float camY, float camZ) {
removeDeadContraptions(); removeDeadContraptions();
if (renderers.isEmpty()) return; if (renderers.isEmpty()) return;
@ -96,13 +97,13 @@ public class ContraptionRenderDispatcher {
GL13.glActiveTexture(GL40.GL_TEXTURE4); // the shaders expect light volumes to be in texture 4 GL13.glActiveTexture(GL40.GL_TEXTURE4); // the shaders expect light volumes to be in texture 4
ContraptionProgram structureShader = Backend.getProgram(AllProgramSpecs.CONTRAPTION_STRUCTURE); ContraptionProgram structureShader = Backend.getProgram(AllProgramSpecs.CONTRAPTION_STRUCTURE);
structureShader.bind(viewProjection, FastRenderDispatcher.getDebugMode()); structureShader.bind(viewProjection, camX, camY, camZ, FastRenderDispatcher.getDebugMode());
for (RenderedContraption renderer : renderers.values()) { for (RenderedContraption renderer : renderers.values()) {
renderer.doRenderLayer(layer, structureShader); renderer.doRenderLayer(layer, structureShader);
} }
for (RenderedContraption renderer : renderers.values()) { for (RenderedContraption renderer : renderers.values()) {
renderer.kinetics.render(layer, viewProjection, renderer::setup); renderer.kinetics.render(layer, viewProjection, camX, camY, camZ, renderer::setup);
renderer.teardown(); renderer.teardown();
} }

View file

@ -12,6 +12,9 @@ public class BasicProgram extends GlProgram {
protected final int uTime; protected final int uTime;
protected final int uViewProjection; protected final int uViewProjection;
protected final int uDebug; protected final int uDebug;
protected final int uCameraPos;
protected final int uFogRange;
protected final int uFogColor;
protected int uBlockAtlas; protected int uBlockAtlas;
protected int uLightMap; protected int uLightMap;
@ -22,6 +25,9 @@ public class BasicProgram extends GlProgram {
uTime = getUniformLocation("uTime"); uTime = getUniformLocation("uTime");
uViewProjection = getUniformLocation("uViewProjection"); uViewProjection = getUniformLocation("uViewProjection");
uDebug = getUniformLocation("uDebug"); uDebug = getUniformLocation("uDebug");
uCameraPos = getUniformLocation("uCameraPos");
uFogRange = getUniformLocation("uFogRange");
uFogColor = getUniformLocation("uFogColor");
bind(); bind();
registerSamplers(); registerSamplers();
@ -33,13 +39,19 @@ public class BasicProgram extends GlProgram {
uLightMap = setSamplerBinding("uLightMap", 2); uLightMap = setSamplerBinding("uLightMap", 2);
} }
public void bind(Matrix4f viewProjection, int debugMode) { public void bind(Matrix4f viewProjection, float camX, float camY, float camZ, int debugMode) {
super.bind(); super.bind();
GL20.glUniform1i(uDebug, debugMode);
GL20.glUniform1i(uTicks, AnimationTickHolder.getTicks()); GL20.glUniform1i(uTicks, AnimationTickHolder.getTicks());
GL20.glUniform1f(uTime, AnimationTickHolder.getRenderTick()); GL20.glUniform1f(uTime, AnimationTickHolder.getRenderTick());
uploadMatrixUniform(uViewProjection, viewProjection); uploadMatrixUniform(uViewProjection, viewProjection);
GL20.glUniform1i(uDebug, debugMode); GL20.glUniform3f(uCameraPos, camX, camY, camZ);
GL20.glUniform2f(uFogRange, GlFog.getFogStart(), GlFog.getFogEnd());
GL20.glGetFloatv(GL20.GL_FOG_COLOR, Backend.VEC4_BUFFER);
GL20.glUniform4fv(uFogColor, Backend.VEC4_BUFFER);
} }
protected static void uploadMatrixUniform(int uniform, Matrix4f mat) { protected static void uploadMatrixUniform(int uniform, Matrix4f mat) {

View file

@ -0,0 +1,13 @@
package com.simibubi.create.foundation.render.gl;
import org.lwjgl.opengl.GL20;
public class GlFog {
public static float getFogEnd() {
return GL20.glGetFloat(GL20.GL_FOG_END);
}
public static float getFogStart() {
return GL20.glGetFloat(GL20.GL_FOG_START);
}
}

View file

@ -33,7 +33,7 @@ import java.util.function.Predicate;
public class Backend { public class Backend {
public static final Logger log = LogManager.getLogger(Backend.class); public static final Logger log = LogManager.getLogger(Backend.class);
public static final FloatBuffer FLOAT_BUFFER = MemoryUtil.memAllocFloat(1); // TODO: these leak 80 bytes of memory per program launch public static final FloatBuffer FLOAT_BUFFER = MemoryUtil.memAllocFloat(1); // TODO: these leak 80 bytes of memory per program launch
public static final FloatBuffer VEC3_BUFFER = MemoryUtil.memAllocFloat(3); public static final FloatBuffer VEC4_BUFFER = MemoryUtil.memAllocFloat(4);
public static final FloatBuffer MATRIX_BUFFER = MemoryUtil.memAllocFloat(16); public static final FloatBuffer MATRIX_BUFFER = MemoryUtil.memAllocFloat(16);
private static final Map<ResourceLocation, ProgramSpec<?>> registry = new HashMap<>(); private static final Map<ResourceLocation, ProgramSpec<?>> registry = new HashMap<>();

View file

@ -108,14 +108,14 @@ public abstract class InstancedTileRenderer<P extends BasicProgram> {
instances.clear(); instances.clear();
} }
public void render(RenderType layer, Matrix4f viewProjection) { public void render(RenderType layer, Matrix4f viewProjection, float camX, float camY, float camZ) {
render(layer, viewProjection, null); render(layer, viewProjection, camX, camY, camZ, null);
} }
public void render(RenderType layer, Matrix4f viewProjection, ShaderCallback<P> callback) { public void render(RenderType layer, Matrix4f viewProjection, float camX, float camY, float camZ, ShaderCallback<P> callback) {
for (RenderMaterial<P, ?> material : materials.values()) { for (RenderMaterial<P, ?> material : materials.values()) {
if (material.canRenderInLayer(layer)) if (material.canRenderInLayer(layer))
material.render(layer, viewProjection, callback); material.render(layer, viewProjection, camX, camY, camZ, callback);
} }
GL20.glUseProgram(0); GL20.glUseProgram(0);

View file

@ -59,13 +59,13 @@ public class RenderMaterial<P extends BasicProgram, MODEL extends InstancedModel
return layerPredicate.test(layer); return layerPredicate.test(layer);
} }
public void render(RenderType layer, Matrix4f projection) { public void render(RenderType layer, Matrix4f projection, float camX, float camY, float camZ) {
render(layer, projection, null); render(layer, projection, camX, camY, camZ, null);
} }
public void render(RenderType layer, Matrix4f viewProjection, ShaderCallback<P> setup) { public void render(RenderType layer, Matrix4f viewProjection, float camX, float camY, float camZ, ShaderCallback<P> setup) {
P program = Backend.getProgram(programSpec); P program = Backend.getProgram(programSpec);
program.bind(viewProjection, FastRenderDispatcher.getDebugMode()); program.bind(viewProjection, camX, camY, camZ, FastRenderDispatcher.getDebugMode());
if (setup != null) setup.call(program); if (setup != null) setup.call(program);

View file

@ -33,6 +33,8 @@ uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
uniform vec3 uCameraPos;
out float FragDistance;
mat4 rotate(vec3 axis, float angle) { mat4 rotate(vec3 axis, float angle) {
float s = sin(angle); float s = sin(angle);
@ -83,6 +85,7 @@ void main() {
Diffuse = diffuse(norm); Diffuse = diffuse(norm);
TexCoords = aTexCoords - sourceTexture + scrollTexture.xy + vec2(0, scroll); TexCoords = aTexCoords - sourceTexture + scrollTexture.xy + vec2(0, scroll);
Light = light; Light = light;
FragDistance = length(worldPos.xyz - uCameraPos);
gl_Position = uViewProjection * worldPos; gl_Position = uViewProjection * worldPos;
#ifdef CONTRAPTION #ifdef CONTRAPTION

View file

@ -4,11 +4,15 @@ in vec2 TexCoords;
in vec4 Color; in vec4 Color;
in float Diffuse; in float Diffuse;
in vec2 Light; in vec2 Light;
in float FragDistance;
in vec3 BoxCoord; in vec3 BoxCoord;
out vec4 fragColor; out vec4 fragColor;
uniform vec2 uFogRange;
uniform vec4 uFogColor;
uniform sampler2D uBlockAtlas; uniform sampler2D uBlockAtlas;
uniform sampler2D uLightMap; uniform sampler2D uLightMap;
uniform sampler3D uLightVolume; uniform sampler3D uLightVolume;
@ -21,5 +25,11 @@ vec4 light() {
void main() { void main() {
vec4 tex = texture2D(uBlockAtlas, TexCoords); vec4 tex = texture2D(uBlockAtlas, TexCoords);
fragColor = vec4(tex.rgb * light().rgb * Diffuse * Color.rgb, tex.a); vec4 color = vec4(tex.rgb * light().rgb * Diffuse * Color.rgb, tex.a);
float fog = (uFogRange.y - FragDistance) / (uFogRange.y - uFogRange.x);
fog = clamp(fog, 0, 1);
fragColor = mix(uFogColor, color, fog);
fragColor.a = color.a;
} }

View file

@ -31,6 +31,8 @@ uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
uniform vec3 uCameraPos;
out float FragDistance;
mat4 rotate(vec3 axis, float angle) { mat4 rotate(vec3 axis, float angle) {
float s = sin(angle); float s = sin(angle);
@ -79,6 +81,7 @@ void main() {
Diffuse = diffuse(norm); Diffuse = diffuse(norm);
TexCoords = aTexCoords; TexCoords = aTexCoords;
Light = modelLight; Light = modelLight;
FragDistance = length(worldPos.xyz - uCameraPos);
gl_Position = uViewProjection * worldPos; gl_Position = uViewProjection * worldPos;
if (uDebug == 2) { if (uDebug == 2) {

View file

@ -22,6 +22,9 @@ uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
uniform vec3 uCameraPos;
out float FragDistance;
mat4 rotate(vec3 axis, float angle) { mat4 rotate(vec3 axis, float angle) {
float s = sin(angle); float s = sin(angle);
float c = cos(angle); float c = cos(angle);
@ -50,6 +53,7 @@ void main() {
Color = aColor / diffuse(aNormal); Color = aColor / diffuse(aNormal);
TexCoords = aTexCoords; TexCoords = aTexCoords;
Light = modelLight; Light = modelLight;
FragDistance = length(worldPos.xyz - uCameraPos);
gl_Position = uViewProjection * worldPos; gl_Position = uViewProjection * worldPos;
if (uDebug == 2) { if (uDebug == 2) {

View file

@ -4,9 +4,13 @@ in vec2 TexCoords;
in vec2 Light; in vec2 Light;
in float Diffuse; in float Diffuse;
in vec4 Color; in vec4 Color;
in float FragDistance;
out vec4 fragColor; out vec4 fragColor;
uniform vec2 uFogRange;
uniform vec4 uFogColor;
uniform sampler2D uBlockAtlas; uniform sampler2D uBlockAtlas;
uniform sampler2D uLightMap; uniform sampler2D uLightMap;
@ -18,5 +22,11 @@ vec4 light() {
void main() { void main() {
vec4 tex = texture2D(uBlockAtlas, TexCoords); vec4 tex = texture2D(uBlockAtlas, TexCoords);
fragColor = vec4(tex.rgb * light().rgb * Diffuse, tex.a) * Color; vec4 color = vec4(tex.rgb * light().rgb * Diffuse, tex.a) * Color;
float fog = (uFogRange.y - FragDistance) / (uFogRange.y - uFogRange.x);
fog = clamp(fog, 0, 1);
fragColor = mix(uFogColor, color, fog);
fragColor.a = color.a;
} }

View file

@ -29,6 +29,9 @@ uniform float uTime;
uniform mat4 uViewProjection; uniform mat4 uViewProjection;
uniform int uDebug; uniform int uDebug;
uniform vec3 uCameraPos;
out float FragDistance;
mat4 rotate(vec3 axis, float angle) { mat4 rotate(vec3 axis, float angle) {
float s = sin(angle); float s = sin(angle);
float c = cos(angle); float c = cos(angle);
@ -77,6 +80,7 @@ void main() {
Diffuse = diffuse(norm); Diffuse = diffuse(norm);
TexCoords = aTexCoords; TexCoords = aTexCoords;
Light = light; Light = light;
FragDistance = length(worldPos.xyz - uCameraPos);
gl_Position = uViewProjection * worldPos; gl_Position = uViewProjection * worldPos;
#ifdef CONTRAPTION #ifdef CONTRAPTION