1414
1515import java .util .HashMap ;
1616import java .util .Iterator ;
17+ import java .util .LinkedHashMap ;
1718import java .util .Map ;
1819
1920public class TheAbyssFogRenderer {
20- private static final Map <Long , Float > FOG_DENSITY_CACHE = new HashMap <>();
21-
21+ // 使用 LinkedHashMap 实现 LRU 缓存,自动移除最旧的条目
22+ private static final Map <Long , Float > FOG_DENSITY_CACHE = new LinkedHashMap <Long , Float >(256 , 0.75f , true ) {
23+ @ Override
24+ protected boolean removeEldestEntry (Map .Entry <Long , Float > eldest ) {
25+ return size () > 1024 ; // 增加缓存大小以减少频繁清理
26+ }
27+ };
2228 // 过渡控制变量
2329 private static final int TRANSITION_RANGE = 16 ; // 过渡范围
2430 private static float lastFogStart = 0 ;
2531 private static float lastFogEnd = 192 ;
2632 private static long lastUpdateTime = 0 ;
2733
2834 // 性能保护变量
29- private static int samplesThisFrame = 0 ;
30- private static long lastFrameTime = 0 ;
31- private static final int MAX_SAMPLES_PER_FRAME = 8 ;
35+ private static final ThreadLocal < Integer > samplesThisFrame = ThreadLocal . withInitial (() -> 0 ) ;
36+ private static final ThreadLocal < Long > lastFrameTime = ThreadLocal . withInitial (() -> 0L ) ;
37+ private static final int MAX_SAMPLES_PER_FRAME = 16 ;
3238
3339 public static boolean renderFog (Camera camera , FogRenderer .FogMode fogMode , float farPlaneDistance , float partialTick ) {
3440 var player = camera .getEntity ();
@@ -140,13 +146,15 @@ private static float getSmoothedBiomeFactor(Level level, double x, double y, dou
140146 private static float calculateBiomeBlendFactor (Level level , double x , double y , double z ) {
141147 // 性能保护:每帧重置采样计数器
142148 long currentTime = System .currentTimeMillis ();
143- if (currentTime != lastFrameTime ) {
144- samplesThisFrame = 0 ;
145- lastFrameTime = currentTime ;
149+ if (currentTime != lastFrameTime . get () ) {
150+ samplesThisFrame . set ( 0 ) ;
151+ lastFrameTime . set ( currentTime ) ;
146152 }
147- // 如果超过每帧最大采样次数,返回默认值
148- if (samplesThisFrame >= MAX_SAMPLES_PER_FRAME ) {
149- return 0.5f ; // 默认值
153+ // 如果超过每帧最大采样次数,返回基于位置的默认值,而不是固定的0.5
154+ if (samplesThisFrame .get () >= MAX_SAMPLES_PER_FRAME ) {
155+ // 基于位置计算一个伪随机但稳定的值,避免所有位置都返回相同值
156+ long seed = (long ) (x * 3129871 ) ^ (long ) (z * 116129781L ) ^ (long ) y ;
157+ return 0.3f + (Math .abs (seed % 40 ) / 100.0f );
150158 }
151159
152160 // 将世界坐标转换为区块坐标
@@ -161,7 +169,7 @@ private static float calculateBiomeBlendFactor(Level level, double x, double y,
161169 if (FOG_DENSITY_CACHE .containsKey (key )) {
162170 return FOG_DENSITY_CACHE .get (key );
163171 }
164- samplesThisFrame ++ ; // 增加采样计数
172+ samplesThisFrame . set ( samplesThisFrame . get () + 1 ) ; // 增加采样计数
165173
166174 // 采样3x3区块段
167175 int biomeFogCount = 0 ;
@@ -195,4 +203,9 @@ private static float calculateBiomeBlendFactor(Level level, double x, double y,
195203
196204 return blendFactor ;
197205 }
206+
207+ // 提供一个方法来清除缓存,可在游戏退出时调用
208+ public static void clearCache () {
209+ FOG_DENSITY_CACHE .clear ();
210+ }
198211}
0 commit comments