Skip to content

Commit

Permalink
Improve biome cache handling
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 9, 2024
1 parent 9525934 commit a8f0424
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 18 deletions.
11 changes: 6 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ plugins {
id("fabric-loom") version ("1.7.3") apply (false)
}

val MINECRAFT_VERSION by extra { "1.21" }
val NEOFORGE_VERSION by extra { "21.0.163" }
val FABRIC_LOADER_VERSION by extra { "0.16.0" }
val FABRIC_API_VERSION by extra { "0.101.0+1.21" }
val MINECRAFT_VERSION by extra { "1.21.1" }
val NEOFORGE_VERSION by extra { "21.1.46" }
val FABRIC_LOADER_VERSION by extra { "0.16.4" }
val FABRIC_API_VERSION by extra { "0.103.0+1.21.1" }

// This value can be set to null to disable Parchment.
val PARCHMENT_VERSION by extra { "2024.07.28" }
// TODO: Re-add Parchment
val PARCHMENT_VERSION by extra { null }

// https://semver.org/
val MOD_VERSION by extra { "0.6.0-beta.1" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ public class BiomeColorMaps {
private static final int HEIGHT = 256;

private static final int INVALID_INDEX = -1;
private static final int DEFAULT_COLOR = 0xffff00ff;

public static int getGrassColor(int index) {
if (index == INVALID_INDEX || index >= GrassColor.pixels.length) {
return DEFAULT_COLOR;
return GrassColor.getDefaultColor();
}

return GrassColor.pixels[index];
}

public static int getFoliageColor(int index) {
if (index == INVALID_INDEX || index >= FoliageColor.pixels.length) {
return DEFAULT_COLOR;
return FoliageColor.getDefaultColor();
}

return FoliageColor.pixels[index];
Expand Down
1 change: 0 additions & 1 deletion common/src/main/resources/sodium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
"features.textures.mipmaps.MipmapGeneratorMixin",
"features.textures.mipmaps.SpriteContentsMixin",
"features.textures.scan.SpriteContentsMixin",
"features.world.biome.BiomeMixin",
"workarounds.context_creation.WindowMixin",
"workarounds.event_loop.RenderSystemMixin"
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.caffeinemc.mods.sodium.mixin.features.world.biome;
package net.caffeinemc.mods.sodium.mixin.fabric.features.world.biome;

import net.caffeinemc.mods.sodium.client.world.biome.BiomeColorMaps;
import net.minecraft.util.Mth;
Expand All @@ -13,12 +13,11 @@
public abstract class BiomeMixin {
@Shadow
@Final
private BiomeSpecialEffects specialEffects;
private Biome.ClimateSettings climateSettings;

@Shadow
@Final
private Biome.ClimateSettings climateSettings;

private BiomeSpecialEffects specialEffects;
@Unique
private boolean hasCustomGrassColor;

Expand Down Expand Up @@ -46,18 +45,22 @@ private void onInit(CallbackInfo ci) {
private void setupColors() {
this.cachedSpecialEffects = specialEffects;

var grassColor = this.specialEffects.getGrassColorOverride();
var grassColor = this.cachedSpecialEffects.getGrassColorOverride();

if (grassColor.isPresent()) {
this.hasCustomGrassColor = true;
this.customGrassColor = grassColor.get();
} else {
this.hasCustomGrassColor = false;
}

var foliageColor = this.specialEffects.getFoliageColorOverride();
var foliageColor = this.cachedSpecialEffects.getFoliageColorOverride();

if (foliageColor.isPresent()) {
this.hasCustomFoliageColor = true;
this.customFoliageColor = foliageColor.get();
} else {
this.hasCustomFoliageColor = false;
}

this.defaultColorIndex = this.getDefaultColorIndex();
Expand All @@ -81,7 +84,7 @@ public int getGrassColor(double x, double z) {
color = BiomeColorMaps.getGrassColor(this.defaultColorIndex);
}

var modifier = this.specialEffects.getGrassColorModifier();
var modifier = this.cachedSpecialEffects.getGrassColorModifier();

if (modifier != BiomeSpecialEffects.GrassColorModifier.NONE) {
color = modifier.modifyColor(x, z, color);
Expand Down
3 changes: 2 additions & 1 deletion fabric/src/main/resources/sodium-fabric.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"core.model.quad.BakedQuadMixin",
"features.model.MultiPartBakedModelMixin",
"features.model.WeightedBakedModelMixin",
"features.render.model.block.ModelBlockRendererMixin"
"features.render.model.block.ModelBlockRendererMixin",
"features.world.biome.BiomeMixin"
]
}
2 changes: 1 addition & 1 deletion neoforge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ dependencies {
compileOnly(project.project(":common").sourceSets.getByName("api").output)
includeDep("org.sinytra.forgified-fabric-api:fabric-api-base:0.4.42+d1308dedd1")
includeDep("org.sinytra.forgified-fabric-api:fabric-renderer-api-v1:3.4.0+acb05a39d1")
includeDep("net.fabricmc:fabric_rendering_data_attachment_v1:0.3.46+${MINECRAFT_VERSION}") {
includeDep("net.fabricmc:fabric_rendering_data_attachment_v1:0.3.46+1.21") {
isTransitive = false
}
includeDep("org.sinytra.forgified-fabric-api:fabric-block-view-api-v2:1.0.10+9afaaf8cd1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package net.caffeinemc.mods.sodium.neoforge.mixin.features.world.biome;

import net.caffeinemc.mods.sodium.client.world.biome.BiomeColorMaps;
import net.minecraft.util.Mth;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeSpecialEffects;
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(Biome.class)
public abstract class BiomeMixin {
@Shadow
public abstract BiomeSpecialEffects getModifiedSpecialEffects();

@Shadow
@Final
private Biome.ClimateSettings climateSettings;

@Unique
private boolean hasCustomGrassColor;

@Unique
private int customGrassColor;

@Unique
private boolean hasCustomFoliageColor;

@Unique
private int customFoliageColor;

@Unique
private int defaultColorIndex;

@Unique
private BiomeSpecialEffects cachedSpecialEffects;

@Inject(method = "<init>", at = @At("RETURN"))
private void onInit(CallbackInfo ci) {
setupColors();
}

@Unique
private void setupColors() {
this.cachedSpecialEffects = getModifiedSpecialEffects();

var grassColor = this.cachedSpecialEffects.getGrassColorOverride();

if (grassColor.isPresent()) {
this.hasCustomGrassColor = true;
this.customGrassColor = grassColor.get();
} else {
this.hasCustomGrassColor = false;
}

var foliageColor = this.cachedSpecialEffects.getFoliageColorOverride();

if (foliageColor.isPresent()) {
this.hasCustomFoliageColor = true;
this.customFoliageColor = foliageColor.get();
} else {
this.hasCustomFoliageColor = false;
}

this.defaultColorIndex = this.getDefaultColorIndex();
}

/**
* @author JellySquid
* @reason Avoid unnecessary pointer de-references and allocations
*/
@Overwrite
public int getGrassColor(double x, double z) {
if (this.getModifiedSpecialEffects() != this.cachedSpecialEffects) {
setupColors();
}

int color;

if (this.hasCustomGrassColor) {
color = this.customGrassColor;
} else {
color = BiomeColorMaps.getGrassColor(this.defaultColorIndex);
}

var modifier = this.cachedSpecialEffects.getGrassColorModifier();

if (modifier != BiomeSpecialEffects.GrassColorModifier.NONE) {
color = modifier.modifyColor(x, z, color);
}

return color;
}

/**
* @author JellySquid
* @reason Avoid unnecessary pointer de-references and allocations
*/
@Overwrite
public int getFoliageColor() {
if (this.getModifiedSpecialEffects() != this.cachedSpecialEffects) {
setupColors();
}

int color;

if (this.hasCustomFoliageColor) {
color = this.customFoliageColor;
} else {
color = BiomeColorMaps.getFoliageColor(this.defaultColorIndex);
}

return color;
}

@Unique
private int getDefaultColorIndex() {
double temperature = Mth.clamp(this.climateSettings.temperature(), 0.0F, 1.0F);
double humidity = Mth.clamp(this.climateSettings.downfall(), 0.0F, 1.0F);

return BiomeColorMaps.getIndex(temperature, humidity);
}
}
1 change: 1 addition & 0 deletions neoforge/src/main/resources/sodium-forge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"features.model.MultiPartBakedModelMixin",
"features.model.WeightedBakedModelMixin",
"features.render.model.block.ModelBlockRendererMixin",
"features.world.biome.BiomeMixin",
"AbstractBlockRenderContextMixin",
"AuxiliaryLightManagerMixin",
"ChunkRenderTypeSetAccessor",
Expand Down

0 comments on commit a8f0424

Please sign in to comment.