From 00d12e9ca31c15115470fa5b43f01e60a064ad29 Mon Sep 17 00:00:00 2001 From: Waterpicker Date: Sun, 21 Nov 2021 07:29:41 -0600 Subject: [PATCH] Fixed Sky renderering for private pockets and simplified sky render. Will need to later on look into overriding sky color when custom biomes are added to pocket schematics to prevent sky from changing. --- .../dimdoors/client/DimensionRenderering.java | 111 +++++++++++ .../DimensionalDoorsClientInitializer.java | 7 +- .../mixin/client/BackgroundRendererMixin.java | 29 +++ .../mixin/client/WorldRendererMixin.java | 187 +++--------------- .../org/dimdev/dimdoors/world/ModBiomes.java | 4 +- .../data/dimdoors/dimension_type/pocket.json | 2 +- src/main/resources/dimdoors.accesswidener | 2 + src/main/resources/dimdoors.mixins.json | 1 + 8 files changed, 174 insertions(+), 169 deletions(-) create mode 100644 src/main/java/org/dimdev/dimdoors/client/DimensionRenderering.java create mode 100644 src/main/java/org/dimdev/dimdoors/mixin/client/BackgroundRendererMixin.java diff --git a/src/main/java/org/dimdev/dimdoors/client/DimensionRenderering.java b/src/main/java/org/dimdev/dimdoors/client/DimensionRenderering.java new file mode 100644 index 00000000..f95429a7 --- /dev/null +++ b/src/main/java/org/dimdev/dimdoors/client/DimensionRenderering.java @@ -0,0 +1,111 @@ +package org.dimdev.dimdoors.client; + +import java.util.List; + +import com.mojang.blaze3d.systems.RenderSystem; +import org.dimdev.dimdoors.listener.pocket.PocketListenerUtil; +import org.dimdev.dimdoors.mixin.client.WorldRendererMixin; +import org.dimdev.dimdoors.world.ModDimensions; +import org.dimdev.dimdoors.world.pocket.type.addon.SkyAddon; +import org.spongepowered.asm.mixin.Unique; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.BufferBuilder; +import net.minecraft.client.render.BufferRenderer; +import net.minecraft.client.render.GameRenderer; +import net.minecraft.client.render.Tessellator; +import net.minecraft.client.render.VertexFormat; +import net.minecraft.client.render.VertexFormats; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.client.world.ClientWorld; +import net.minecraft.util.Identifier; +import net.minecraft.util.math.Matrix4f; +import net.minecraft.util.math.Vec3f; +import net.minecraft.util.registry.RegistryKey; +import net.minecraft.world.World; + +import net.fabricmc.fabric.api.client.rendering.v1.DimensionRenderingRegistry; + +public class DimensionRenderering { + private static final Identifier MOON_RENDER_PATH = new Identifier("dimdoors:textures/other/limbo_moon.png"); + private static final Identifier SUN_RENDER_PATH = new Identifier("dimdoors:textures/other/limbo_sun.png"); + + public static void initClient() { + DimensionRenderingRegistry.CloudRenderer noCloudRenderer = context -> { + }; + DimensionRenderingRegistry.registerCloudRenderer(ModDimensions.LIMBO, noCloudRenderer); + DimensionRenderingRegistry.registerCloudRenderer(ModDimensions.DUNGEON, noCloudRenderer); + DimensionRenderingRegistry.registerCloudRenderer(ModDimensions.PERSONAL, noCloudRenderer); + DimensionRenderingRegistry.registerCloudRenderer(ModDimensions.PUBLIC, noCloudRenderer); + + DimensionRenderingRegistry.registerSkyRenderer(ModDimensions.LIMBO, context -> renderLimboSky(context.matrixStack())); + + DimensionRenderingRegistry.SkyRenderer pocketRenderer = context -> { + ClientWorld world = context.world(); + MatrixStack matrices = context.matrixStack(); + List skyAddons = PocketListenerUtil.applicableAddonsClient(SkyAddon.class, world, context.camera().getBlockPos()); + SkyAddon skyAddon = null; + if (skyAddons.size() > 0) { + // There should really only be one of these. + // If anyone needs to use multiple SkyAddons then go ahead and change this. + skyAddon = skyAddons.get(0); + } + + if (skyAddon != null) { + RegistryKey key = skyAddon.getWorld(); + + DimensionRenderingRegistry.SkyRenderer skyRenderer = DimensionRenderingRegistry.getSkyRenderer(key); + + if (skyRenderer != null) { + skyRenderer.render(context); + } else { + + if (key.equals(World.END)) { + context.gameRenderer().getClient().worldRenderer.renderEndSky(matrices); + } else if (key.equals(ModDimensions.LIMBO)) { + renderLimboSky(matrices); + } + } + } + }; + + DimensionRenderingRegistry.registerSkyRenderer(ModDimensions.DUNGEON, pocketRenderer); + DimensionRenderingRegistry.registerSkyRenderer(ModDimensions.PERSONAL, pocketRenderer); + DimensionRenderingRegistry.registerSkyRenderer(ModDimensions.PUBLIC, pocketRenderer); + } + + private static void renderLimboSky(MatrixStack matrices) { + Matrix4f matrix4f = matrices.peek().getModel(); + Tessellator tessellator = Tessellator.getInstance(); + BufferBuilder bufferBuilder = tessellator.getBuffer(); + RenderSystem.enableBlend(); + RenderSystem.defaultBlendFunc(); + RenderSystem.depthMask(false); + RenderSystem.setShader(GameRenderer::getPositionTexColorShader); + RenderSystem.setShaderColor(1, 1, 1, 1); + + float s = 30.0F; + RenderSystem.setShader(GameRenderer::getPositionTexShader); + RenderSystem.setShaderTexture(0, SUN_RENDER_PATH); + bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE); + bufferBuilder.vertex(matrix4f, -s, 100.0F, -s).texture(0.0F, 0.0F).next(); + bufferBuilder.vertex(matrix4f, s, 100.0F, -s).texture(1.0F, 0.0F).next(); + bufferBuilder.vertex(matrix4f, s, 100.0F, s).texture(1.0F, 1.0F).next(); + bufferBuilder.vertex(matrix4f, -s, 100.0F, s).texture(0.0F, 1.0F).next(); + bufferBuilder.end(); + BufferRenderer.draw(bufferBuilder); + RenderSystem.setShaderTexture(0, MOON_RENDER_PATH); + bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE); + bufferBuilder.vertex(matrix4f, -s, -100.0F, -s).texture(0.0F, 0.0F).next(); + bufferBuilder.vertex(matrix4f, s, -100.0F, -s).texture(1.0F, 0.0F).next(); + bufferBuilder.vertex(matrix4f, s, -100.0F, s).texture(1.0F, 1.0F).next(); + bufferBuilder.vertex(matrix4f, -s, -100.0F, s).texture(0.0F, 1.0F).next(); + bufferBuilder.end(); + BufferRenderer.draw(bufferBuilder); + + RenderSystem.depthMask(true); + RenderSystem.enableTexture(); + RenderSystem.disableBlend(); + } + +} diff --git a/src/main/java/org/dimdev/dimdoors/client/DimensionalDoorsClientInitializer.java b/src/main/java/org/dimdev/dimdoors/client/DimensionalDoorsClientInitializer.java index efaa9921..7f538e3d 100644 --- a/src/main/java/org/dimdev/dimdoors/client/DimensionalDoorsClientInitializer.java +++ b/src/main/java/org/dimdev/dimdoors/client/DimensionalDoorsClientInitializer.java @@ -19,6 +19,9 @@ import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.client.rendereregistry.v1.BlockEntityRendererRegistry; +import net.fabricmc.fabric.api.client.rendering.v1.DimensionRenderingRegistry; +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; + import org.dimdev.dimdoors.world.ModDimensions; @Environment(EnvType.CLIENT) @@ -37,9 +40,9 @@ public class DimensionalDoorsClientInitializer implements ClientModInitializer { ModEntityModelLayers.initClient(); ModParticleTypes.initClient(); - registerListeners(); + DimensionRenderering.initClient(); - SkyPropertiesAccessor.getIdMap().put(ModDimensions.LIMBO_TYPE_KEY.getValue(), new LimboSkyProperties()); + registerListeners(); } private void registerListeners() { diff --git a/src/main/java/org/dimdev/dimdoors/mixin/client/BackgroundRendererMixin.java b/src/main/java/org/dimdev/dimdoors/mixin/client/BackgroundRendererMixin.java new file mode 100644 index 00000000..dd958400 --- /dev/null +++ b/src/main/java/org/dimdev/dimdoors/mixin/client/BackgroundRendererMixin.java @@ -0,0 +1,29 @@ +package org.dimdev.dimdoors.mixin.client; + +import org.dimdev.dimdoors.world.ModDimensions; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyVariable; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.render.BackgroundRenderer; +import net.minecraft.client.world.ClientWorld; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +@Environment(EnvType.CLIENT) +@Mixin(BackgroundRenderer.class) +public class BackgroundRendererMixin { + @ModifyVariable( + method = "render", + at = @At(value = "STORE", ordinal = 0), + ordinal = 0 + ) + private static double modifyVoidColor(double scale) { + if(ModDimensions.isPrivatePocketDimension(MinecraftClient.getInstance().world)) { + scale = 1.0; + } + return scale; + } +} \ No newline at end of file diff --git a/src/main/java/org/dimdev/dimdoors/mixin/client/WorldRendererMixin.java b/src/main/java/org/dimdev/dimdoors/mixin/client/WorldRendererMixin.java index 8f904809..652bc726 100644 --- a/src/main/java/org/dimdev/dimdoors/mixin/client/WorldRendererMixin.java +++ b/src/main/java/org/dimdev/dimdoors/mixin/client/WorldRendererMixin.java @@ -13,8 +13,14 @@ import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.gen.Invoker; import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Constant; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyConstant; +import org.spongepowered.asm.mixin.injection.ModifyVariable; +import org.spongepowered.asm.mixin.injection.Slice; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import net.minecraft.client.MinecraftClient; @@ -35,10 +41,6 @@ import java.util.Map; @Mixin(WorldRenderer.class) @Environment(EnvType.CLIENT) public abstract class WorldRendererMixin { - @Unique - private static final Identifier MOON_RENDER_PATH = new Identifier("dimdoors:textures/other/limbo_moon.png"); - @Unique - private static final Identifier SUN_RENDER_PATH = new Identifier("dimdoors:textures/other/limbo_sun.png"); @Shadow private ClientWorld world; @@ -53,166 +55,6 @@ public abstract class WorldRendererMixin { @Shadow private int ticks; - @Shadow - @Final - private static Identifier END_SKY; - - @Shadow - protected abstract void renderEndSky(MatrixStack matrices); - - @Inject(method = "renderSky", at = @At("HEAD"), cancellable = true) - public void beforeRenderSky(MatrixStack matrices, Matrix4f matrix4f, float f, Runnable runnable, CallbackInfo ci) { - List skyAddons = PocketListenerUtil.applicableAddonsClient(SkyAddon.class, this.world, this.client.gameRenderer.getCamera().getBlockPos()); - SkyAddon skyAddon = null; - if (skyAddons.size() > 0) { - // There should really only be one of these. - // If anyone needs to use multiple SkyAddons then go ahead and change this. - skyAddon = skyAddons.get(0); - } - - if (skyAddon != null) { - RegistryKey world = skyAddon.getWorld(); - - if (world.equals(World.END)) { - this.renderEndSky(matrices); - ci.cancel(); - return; - } else if (world.equals(ModDimensions.LIMBO)) { - this.renderLimboSky(matrices); - ci.cancel(); - return; - } - } - if (ModDimensions.isLimboDimension(this.world)) { - renderLimboSky(matrices); - ci.cancel(); - } else if (ModDimensions.isPrivatePocketDimension(this.world)) { - this.renderPocketSky(matrices, 255, 255, 255); - ci.cancel(); - } else if (ModDimensions.isPocketDimension(this.world)) { - this.renderPocketSky(matrices, 0, 0, 0); - ci.cancel(); - } - } - - @Inject(method = "renderClouds(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/util/math/Matrix4f;FDDD)V", at = @At("HEAD"), cancellable = true) - public void beforeRendererCloud(MatrixStack matrices, Matrix4f matrix4f, float f, double d, double e, double g, CallbackInfo ci) { - if (ModDimensions.isLimboDimension(this.world) || ModDimensions.isPrivatePocketDimension(this.world) || ModDimensions.isPocketDimension(this.world)) { - ci.cancel(); - } - } - - @Unique - private void renderLimboSky(MatrixStack matrices) { - Matrix4f matrix4f = matrices.peek().getModel(); - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder bufferBuilder = tessellator.getBuffer(); - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - RenderSystem.depthMask(false); - RenderSystem.setShader(GameRenderer::getPositionTexColorShader); - RenderSystem.setShaderColor(1, 1, 1, 1); - - for (int i = 0; i < 6; ++i) { - matrices.push(); - if (i == 1) { - matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(90.0F)); - } - - if (i == 2) { - matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(-90.0F)); - } - - if (i == 3) { - matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(180.0F)); - } - - if (i == 4) { - matrices.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(90.0F)); - } - - if (i == 5) { - matrices.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(-90.0F)); - } - - bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR); - bufferBuilder.vertex(matrix4f, -100.0F, -100.0F, -100.0F).color(40, 40, 40, 255).next(); - bufferBuilder.vertex(matrix4f, -100.0F, -100.0F, 100.0F).color(40, 40, 40, 255).next(); - bufferBuilder.vertex(matrix4f, 100.0F, -100.0F, 100.0F).color(40, 40, 40, 255).next(); - bufferBuilder.vertex(matrix4f, 100.0F, -100.0F, -100.0F).color(40, 40, 40, 255).next(); - tessellator.draw(); - matrices.pop(); - } - - float s = 30.0F; - RenderSystem.setShader(GameRenderer::getPositionTexShader); - RenderSystem.setShaderTexture(0, SUN_RENDER_PATH); - bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE); - bufferBuilder.vertex(matrix4f, -s, 100.0F, -s).texture(0.0F, 0.0F).next(); - bufferBuilder.vertex(matrix4f, s, 100.0F, -s).texture(1.0F, 0.0F).next(); - bufferBuilder.vertex(matrix4f, s, 100.0F, s).texture(1.0F, 1.0F).next(); - bufferBuilder.vertex(matrix4f, -s, 100.0F, s).texture(0.0F, 1.0F).next(); - bufferBuilder.end(); - BufferRenderer.draw(bufferBuilder); - RenderSystem.setShaderTexture(0, MOON_RENDER_PATH); - bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE); - bufferBuilder.vertex(matrix4f, -s, -100.0F, -s).texture(0.0F, 0.0F).next(); - bufferBuilder.vertex(matrix4f, s, -100.0F, -s).texture(1.0F, 0.0F).next(); - bufferBuilder.vertex(matrix4f, s, -100.0F, s).texture(1.0F, 1.0F).next(); - bufferBuilder.vertex(matrix4f, -s, -100.0F, s).texture(0.0F, 1.0F).next(); - bufferBuilder.end(); - BufferRenderer.draw(bufferBuilder); - - RenderSystem.depthMask(true); - RenderSystem.enableTexture(); - RenderSystem.disableBlend(); - } - - private void renderPocketSky(MatrixStack matrices, int r, int g, int b) { - RenderSystem.enableBlend(); - RenderSystem.defaultBlendFunc(); - RenderSystem.depthMask(false); - RenderSystem.setShader(GameRenderer::getPositionColorShader); - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder bufferBuilder = tessellator.getBuffer(); - - for (int i = 0; i < 6; ++i) { - matrices.push(); - if (i == 1) { - matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(90.0F)); - } - - if (i == 2) { - matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(-90.0F)); - } - - if (i == 3) { - matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(180.0F)); - } - - if (i == 4) { - matrices.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(90.0F)); - } - - if (i == 5) { - matrices.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(-90.0F)); - } - - Matrix4f matrix4f = matrices.peek().getModel(); - bufferBuilder.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_COLOR); - bufferBuilder.vertex(matrix4f, -100.0F, -100.0F, -100.0F).color(r, g, b, 255).next(); - bufferBuilder.vertex(matrix4f, -100.0F, -100.0F, 100.0F).color(r, g, b, 255).next(); - bufferBuilder.vertex(matrix4f, 100.0F, -100.0F, 100.0F).color(r, g, b, 255).next(); - bufferBuilder.vertex(matrix4f, 100.0F, -100.0F, -100.0F).color(r, g, b, 255).next(); - tessellator.draw(); - matrices.pop(); - } - - RenderSystem.depthMask(true); - RenderSystem.enableTexture(); - RenderSystem.disableBlend(); - } - @Inject(method = "render(Lnet/minecraft/client/util/math/MatrixStack;FJZLnet/minecraft/client/render/Camera;Lnet/minecraft/client/render/GameRenderer;Lnet/minecraft/client/render/LightmapTextureManager;Lnet/minecraft/util/math/Matrix4f;)V", at = @At(value = "FIELD", target = "Lnet/minecraft/client/render/WorldRenderer;blockBreakingProgressions:Lit/unimi/dsi/fastutil/longs/Long2ObjectMap;", ordinal = 1)) // bytecode order is flipped from java code order, notice the ordinal public void renderCustomBreakBlockAnimation(MatrixStack matrices, float tickDelta, long limitTime, boolean renderBlockOutline, Camera camera, GameRenderer gameRenderer, LightmapTextureManager lightmapTextureManager, Matrix4f matrix4f, CallbackInfo ci) { @@ -240,4 +82,21 @@ public abstract class WorldRendererMixin { } } } + + @ModifyConstant( + method = "renderSky(Lnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/util/math/Matrix4f;FLjava/lang/Runnable;)V", + constant = @Constant(doubleValue = 0.0, ordinal = 0), + slice = @Slice( + from = @At( + value = "INVOKE", + target = "Lnet/minecraft/client/world/ClientWorld$Properties;getSkyDarknessHeight(Lnet/minecraft/world/HeightLimitView;)D" + ) + ) + ) + private double modifyVoidBackgroundCondition(double zero) { + if(ModDimensions.isPrivatePocketDimension(world)) { + zero = Double.NEGATIVE_INFINITY; + } + return zero; + } } diff --git a/src/main/java/org/dimdev/dimdoors/world/ModBiomes.java b/src/main/java/org/dimdev/dimdoors/world/ModBiomes.java index b3972ec4..51e096d6 100644 --- a/src/main/java/org/dimdev/dimdoors/world/ModBiomes.java +++ b/src/main/java/org/dimdev/dimdoors/world/ModBiomes.java @@ -48,8 +48,8 @@ public final class ModBiomes { BiomeEffects.Builder builder = new BiomeEffects.Builder() .waterColor(0x3f76e4) .waterFogColor(0x50533) - .fogColor(white ? 0xFFFFFF : 0xc0d8ff) - .skyColor(white ? 0xFFFFFF : 0x111111) + .fogColor(white ? 0xFFFFFF : 0x000000) + .skyColor(white ? 0xFFFFFF : 0x000000) .grassColorModifier(BiomeEffects.GrassColorModifier.NONE); if (white) { builder.music(new MusicSound(ModSoundEvents.WHITE_VOID, 0, 0, true)); diff --git a/src/main/resources/data/dimdoors/dimension_type/pocket.json b/src/main/resources/data/dimdoors/dimension_type/pocket.json index 704819f6..d54ee29e 100644 --- a/src/main/resources/data/dimdoors/dimension_type/pocket.json +++ b/src/main/resources/data/dimdoors/dimension_type/pocket.json @@ -9,7 +9,7 @@ "bed_works" : false, "respawn_anchor_works": true, "has_raids" : false, - "min_y": 0, + "min_y": -16, "height": 256, "logical_height" : 256, "infiniburn": "minecraft:infiniburn_overworld", diff --git a/src/main/resources/dimdoors.accesswidener b/src/main/resources/dimdoors.accesswidener index 26502320..c4eabbc0 100644 --- a/src/main/resources/dimdoors.accesswidener +++ b/src/main/resources/dimdoors.accesswidener @@ -18,3 +18,5 @@ accessible field net/minecraft/state/State codec Lcom/mojang/seriali # for lazy pocket gen/ getting all currently loaded chunks accessible method net/minecraft/server/world/ThreadedAnvilChunkStorage entryIterator ()Ljava/lang/Iterable; + +accessible method net/minecraft/client/render/WorldRenderer renderEndSky (Lnet/minecraft/client/util/math/MatrixStack;)V \ No newline at end of file diff --git a/src/main/resources/dimdoors.mixins.json b/src/main/resources/dimdoors.mixins.json index d883fa5d..833f9686 100644 --- a/src/main/resources/dimdoors.mixins.json +++ b/src/main/resources/dimdoors.mixins.json @@ -38,6 +38,7 @@ "client.InGameHudMixin", "client.PostProcessShaderMixin", "client.WorldRendererMixin", + "client.BackgroundRendererMixin", "client.accessor.RenderLayerAccessor", "client.accessor.SkyPropertiesAccessor" ],