Skip to content

Commit

Permalink
fix(day 2, Na compat): disable biome blending and modify block tint
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv256 committed Oct 13, 2024
1 parent d1d9adb commit 8b13ffc
Show file tree
Hide file tree
Showing 10 changed files with 227 additions and 17 deletions.
19 changes: 17 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ var modId = project.mod_id
repositories {
mavenLocal()
mavenCentral()

exclusiveContent {
forRepository {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
}
}
filter {
includeGroup "maven.modrinth"
}
}
}

base {
Expand All @@ -34,8 +46,8 @@ neoForge {
version = libs.versions.neoforge.asProvider().get()

parchment {
mappingsVersion = libs.versions.parchment.get()
minecraftVersion = libs.versions.minecraft.asProvider().get()
mappingsVersion = libs.versions.parchment.asProvider().get()
minecraftVersion = libs.versions.parchment.minecraft.get()
}

// This line is optional. Access Transformers are automatically detected
Expand Down Expand Up @@ -135,6 +147,9 @@ dependencies {
// For more info:
// http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
// http://www.gradle.org/docs/current/userguide/dependency_management.html

compileOnly libs.sodium
runtimeOnly libs.sodium
}

// This block of code expands all declared replace properties in the specified resource targets.
Expand Down
11 changes: 7 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@
# You can find the latest versions here: https://projects.neoforged.net/neoforged/neoforge
moddevgradle = "1.0.20"
# The Neo version must agree with the Minecraft version to get a valid artifact
neoforge = "21.0.167"
neoforge = "21.1.68"
# The Neo version range can use any version of Neo as bounds
neoforge_range="[21.0.0-beta,)"
neoforge_range="[21.1.0-beta,)"
# The Minecraft version must agree with the Neo version to get a valid artifact
minecraft = "1.21"
minecraft = "1.21.1"
# The Minecraft version range can use any release version of Minecraft as bounds.
# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly
# as they do not follow standard versioning conventions.
minecraft_range = "[1.21,1.21.1)"
minecraft_range = "[1.21.1,1.22)"
# The loader version range can only use the major version of FML as bounds
loader_range = "[4,)"
#read more on this at https://github.com/neoforged/ModDevGradle?tab=readme-ov-file#better-minecraft-parameter-names--javadoc-parchment
# you can also find the latest versions at: https://parchmentmc.org/docs/getting-started
parchment = "2024.07.28"
parchment_minecraft = "1.21"
sodium = "mc1.21-0.6.0-beta.2-neoforge"

[libraries]
neoforge = { module = "net.neoforged:neoforge", version.ref = "neoforge" }
sodium = { module = "maven.modrinth:sodium", version.ref = "sodium" }

[plugins]
moddevgradle = { id = "net.neoforged.moddev", version.ref = "moddevgradle" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import gay.sylv.legacy_landscape.util.TintUtil;
import net.minecraft.client.OptionInstance;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.BiomeColors;
Expand Down Expand Up @@ -48,16 +49,10 @@ private int saturateBlockTint(ColorResolver colorResolver, Biome biome, double x
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
if (!colorResolver.equals(BiomeColors.WATER_COLOR_RESOLVER)) {
int tint = original.call(colorResolver, biome, x, z);
int red = ((tint & 0x00FF0000)) >> 16;
red += 0x28;
red = Math.clamp(red, 0, 0x98);
int green = ((tint & 0x0000FF00)) >> 8;
green += 0x28;
green = Math.clamp(green, 0, 0xFF); // prevent overflow/underflow
tint = (tint & 0xFF0000FF) | green << 8 | red << 16; // clear green channel and set new green
tint = TintUtil.saturateTint(tint);
return tint;
} else {
return 0x334FDD;
return TintUtil.WATER_COLOR;
}
}
return original.call(colorResolver, biome, x, z);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(LevelRenderer.class)
@Mixin(value = LevelRenderer.class, priority = 1001)
public final class Mixin_LevelRenderer {
private Mixin_LevelRenderer() {}

Expand All @@ -24,7 +24,8 @@ private Mixin_LevelRenderer() {}
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/renderer/ShaderInstance;setDefaultUniforms(Lcom/mojang/blaze3d/vertex/VertexFormat$Mode;Lorg/joml/Matrix4f;Lorg/joml/Matrix4f;Lcom/mojang/blaze3d/platform/Window;)V"
)
),
require = 0
)
private void setLegacyTextures(RenderType renderType, double x, double y, double z, Matrix4f frustrumMatrix, Matrix4f projectionMatrix, CallbackInfo ci, @Local ShaderInstance shaderInstance) {
Minecraft client = Minecraft.getInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gay.sylv.legacy_landscape.mixin.client.sodium;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import gay.sylv.legacy_landscape.util.TintUtil;
import net.caffeinemc.mods.sodium.client.model.color.ColorProvider;
import net.caffeinemc.mods.sodium.client.model.color.ColorProviderRegistry;
import net.caffeinemc.mods.sodium.client.model.color.DefaultColorProviders;
import net.caffeinemc.mods.sodium.client.render.chunk.compile.pipeline.BlockRenderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BiomeColors;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.injection.At;

import java.util.Arrays;

@Mixin(BlockRenderer.class)
@Pseudo
public class Mixin_BlockRenderer {
@WrapOperation(
method = "renderModel",
at = @At(
value = "INVOKE",
target = "Lnet/caffeinemc/mods/sodium/client/model/color/ColorProviderRegistry;getColorProvider(Lnet/minecraft/world/level/block/Block;)Lnet/caffeinemc/mods/sodium/client/model/color/ColorProvider;"
)
)
private ColorProvider<BlockState> hijackColorProvider(ColorProviderRegistry instance, Block block, Operation<ColorProvider<BlockState>> original, @Local(argsOnly = true, ordinal = 0) BlockPos pos) {
Minecraft client = Minecraft.getInstance();
assert client.level != null;
LevelChunk chunk = client.level.getChunkAt(pos);
ColorProvider<BlockState> colorProvider = original.call(instance, block);
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
return (levelSlice, blockPos, mutableBlockPos, blockState, modelQuadView, ints) -> {
if (colorProvider.equals(DefaultColorProviders.GrassColorProvider.BLOCKS)) {
Arrays.fill(ints, TintUtil.saturateTint(BiomeColors.getAverageGrassColor(levelSlice, blockPos)));
} else if (colorProvider.equals(DefaultColorProviders.FoliageColorProvider.BLOCKS)) {
Arrays.fill(ints, TintUtil.saturateTint(BiomeColors.getAverageFoliageColor(levelSlice, blockPos)));
}
};
} else {
return colorProvider;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package gay.sylv.legacy_landscape.mixin.client.sodium;

import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import gay.sylv.legacy_landscape.util.TintUtil;
import net.caffeinemc.mods.sodium.client.model.color.ColorProvider;
import net.caffeinemc.mods.sodium.client.model.color.ColorProviderRegistry;
import net.minecraft.client.Minecraft;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Pseudo;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Arrays;

@Mixin(ColorProviderRegistry.class)
@Pseudo
public abstract class Mixin_ColorProviderRegistry {
@Shadow
protected abstract void registerFluids(ColorProvider<FluidState> provider, Fluid... fluids);

@Shadow
public abstract ColorProvider<FluidState> getColorProvider(Fluid fluid);

private Mixin_ColorProviderRegistry() {}

@Inject(
method = "installOverrides",
at = @At(
value = "INVOKE",
target = "Lnet/caffeinemc/mods/sodium/client/model/color/ColorProviderRegistry;registerFluids(Lnet/caffeinemc/mods/sodium/client/model/color/ColorProvider;[Lnet/minecraft/world/level/material/Fluid;)V",
shift = At.Shift.AFTER
)
)
private void installWaterOverride(CallbackInfo ci) {
var colorProvider = this.getColorProvider(Fluids.WATER);
this.registerFluids((levelSlice, blockPos, mutableBlockPos, fluidState, modelQuadView, ints) -> {
Minecraft client = Minecraft.getInstance();
assert client.level != null;
LevelChunk chunk = client.level.getChunkAt(blockPos);
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
Arrays.fill(ints, TintUtil.WATER_COLOR);
} else {
colorProvider.getColors(levelSlice, blockPos, mutableBlockPos, fluidState, modelQuadView, ints);
}
}, Fluids.WATER, Fluids.FLOWING_WATER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package gay.sylv.legacy_landscape.mixin.client.sodium;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import gay.sylv.legacy_landscape.data_attachment.LegacyAttachments;
import net.caffeinemc.mods.sodium.client.util.color.BoxBlur;
import net.caffeinemc.mods.sodium.client.world.biome.LevelColorCache;
import net.caffeinemc.mods.sodium.client.world.cloned.ChunkRenderContext;
import net.minecraft.client.Minecraft;
import net.minecraft.world.level.chunk.LevelChunk;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(LevelColorCache.class)
public final class Mixin_LevelColorCache {
@Mutable
@Shadow
@Final
private int blendRadius;

@Unique
private int legacy_landscape$previousBlendRadius;

@Unique
private boolean legacy_landscape$isLegacyChunk;

private Mixin_LevelColorCache() {}

@Inject(
method = "update",
at = @At("HEAD")
)
private void disableBiomeBlending(ChunkRenderContext context, CallbackInfo ci) {
Minecraft client = Minecraft.getInstance();
assert client.level != null;
LevelChunk chunk = client.level.getChunk(context.getOrigin().x(), context.getOrigin().z());
if (chunk.getData(LegacyAttachments.LEGACY_CHUNK)) {
legacy_landscape$previousBlendRadius = this.blendRadius;
this.blendRadius = 0;
legacy_landscape$isLegacyChunk = true;
} else {
legacy_landscape$isLegacyChunk = false;
}
}

@Inject(
method = "update",
at = @At("TAIL")
)
private void enableBiomeBlending(ChunkRenderContext context, CallbackInfo ci) {
if (this.blendRadius == 0) {
this.blendRadius = legacy_landscape$previousBlendRadius;
}
}

@WrapOperation(
method = "updateColorBuffers",
at = @At(
value = "INVOKE",
target = "Lnet/caffeinemc/mods/sodium/client/util/color/BoxBlur;blur(Lnet/caffeinemc/mods/sodium/client/util/color/BoxBlur$ColorBuffer;Lnet/caffeinemc/mods/sodium/client/util/color/BoxBlur$ColorBuffer;I)V"
)
)
private void disableBoxBlur(BoxBlur.ColorBuffer buf, BoxBlur.ColorBuffer tmp, int radius, Operation<Void> original) {
if (!legacy_landscape$isLegacyChunk) {
original.call(buf, tmp, radius);
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/gay/sylv/legacy_landscape/util/TintUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package gay.sylv.legacy_landscape.util;

public final class TintUtil {
public static final int WATER_COLOR = 0xFF334FDD;

private TintUtil() {}

public static int saturateTint(int tint) {
int red = ((tint & 0x00FF0000)) >> 16;
red += 0x28;
red = Math.clamp(red, 0, 0x98);
int green = ((tint & 0x0000FF00)) >> 8;
green += 0x28;
green = Math.clamp(green, 0, 0xFF); // prevent overflow/underflow
tint = (tint & 0xFF0000FF) | green << 8 | red << 16; // clear green channel and set new green
return tint;
}
}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
public net.minecraft.client.renderer.RenderType$CompositeRenderType
public net.caffeinemc.mods.sodium.neoforge.render.FluidRendererImpl$DefaultRenderContext
5 changes: 4 additions & 1 deletion src/main/resources/legacy_landscape.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"client.Accessor_RenderSystem",
"client.Accessor_TextureStateShard",
"client.Mixin_ClientLevel",
"client.Mixin_LevelRenderer"
"client.Mixin_LevelRenderer",
"client.sodium.Mixin_BlockRenderer",
"client.sodium.Mixin_ColorProviderRegistry",
"client.sodium.Mixin_LevelColorCache"
],
"server": [],
"injectors": {
Expand Down

0 comments on commit 8b13ffc

Please sign in to comment.