Blender .java

net.minecraft.world.level.levelgen.blending.Blender (class, 495 lines): members: DENSITY_BLENDING_RANGE_CHUNKS, DENSITY_BLENDING_RANGE_CELLS, cellY.

net.minecraft.world.level.levelgen.blendingclass495 linescommon20.4 KB

API Surface (22)

Blender.BlendingOutput blendOffsetAndFactor(final int blockX, final int blockZ)
float blendDensity(final int blockX, final int blockY, final int blockZ, final float noiseValue)
BiomeResolver getBiomeResolver(final BiomeResolver biomeResolver)
Blender empty()
Blender of(final @Nullable WorldGenRegion region)
boolean isEmpty()
Blender.OutputBuffer blendOffsetAndFactor(final DensityVolume volume)
Blender.BlendingOutput blendOffsetAndFactor(final int blockX, final int blockZ)
float blendDensity(final int blockX, final int blockY, final int blockZ, final float noiseValue)
BiomeResolver getBiomeResolver(final BiomeResolver biomeResolver)
void generateBorderTicks(final WorldGenRegion region, final ChunkAccess chunk)
Blender.DistanceGetter makeOldChunkDistanceGetter(final @Nullable BlendingData centerBlendingData, final Map<CompositeDirection.Direction8, BlendingData> oldSidesBlendingData)
record BlendingOutput(float alpha, float blendingOffset)
record OutputBuffer(Blender blender, int minCellX, int minCellZ, int cellCountX, int cellCountZ, float[] alphas, float[] offsets)
DensitySampler createAlphaSampler()
void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume)
float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ)
DensitySampler createOffsetSampler()
void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume)
float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ)
float getAlpha(final int blockX, final int blockZ)
float getOffset(final int blockX, final int blockZ)

Fields & Constants (15)

CONTEXT_KEY
ALPHA_KEY
OFFSET_KEY
EMPTY
SHIFT_NOISE
HEIGHT_BLENDING_RANGE_CELLS
HEIGHT_BLENDING_RANGE_CHUNKS
DENSITY_BLENDING_RANGE_CELLS
DENSITY_BLENDING_RANGE_CHUNKS
OLD_CHUNK_XZ_RADIUS
heightAndBiomeBlendingData
densityBlendingData
carvingFilter
cellY
centerBlendingData

String literals (2)

"blender/alpha"
"blender/offset"

Same Package (2)

Full Source

Blender.java495 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.levelgen.blending;
2
3
import com.google.common.collect.ImmutableMap;
4
import com.google.common.collect.Lists;
5
import com.google.common.collect.ImmutableMap.Builder;
6
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
7
import java.util.List;
8
import java.util.Map;
9
import net.minecraft.SharedConstants;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.CompositeDirection;
12
import net.minecraft.core.Direction;
13
import net.minecraft.core.Holder;
14
import net.minecraft.core.QuartPos;
15
import net.minecraft.data.worldgen.NoiseData;
16
import net.minecraft.server.level.WorldGenRegion;
17
import net.minecraft.tags.BlockTags;
18
import net.minecraft.util.Mth;
19
import net.minecraft.util.context.ContextKey;
20
import net.minecraft.world.level.ChunkPos;
21
import net.minecraft.world.level.WorldGenLevel;
22
import net.minecraft.world.level.biome.Biome;
23
import net.minecraft.world.level.biome.BiomeResolver;
24
import net.minecraft.world.level.block.state.BlockState;
25
import net.minecraft.world.level.chunk.CarvingMask;
26
import net.minecraft.world.level.chunk.ChunkAccess;
27
import net.minecraft.world.level.levelgen.Heightmap;
28
import net.minecraft.world.level.levelgen.XoroshiroRandomSource;
29
import net.minecraft.world.level.levelgen.densityfunction.DensityBuffer;
30
import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
31
import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
32
import net.minecraft.world.level.levelgen.densityfunction.SamplerContext;
33
import net.minecraft.world.level.levelgen.synth.Noise;
34
import net.minecraft.world.level.material.FluidState;
35
import org.apache.commons.lang3.mutable.MutableDouble;
36
import org.apache.commons.lang3.mutable.MutableFloat;
37
import org.apache.commons.lang3.mutable.MutableObject;
38
import org.jspecify.annotations.Nullable;
39
40
public class Blender {
41
	public static final ContextKey<Blender> CONTEXT_KEY = ContextKey.vanilla("blender");
42
	public static final ContextKey<DensitySampler> ALPHA_KEY = ContextKey.vanilla("blender/alpha");
43
	public static final ContextKey<DensitySampler> OFFSET_KEY = ContextKey.vanilla("blender/offset");
44
	private static final Blender EMPTY = new Blender(new Long2ObjectOpenHashMap(), new Long2ObjectOpenHashMap(), null) {
45
		@Override
46
		protected Blender.BlendingOutput blendOffsetAndFactor(final int blockX, final int blockZ) {
47
			return new Blender.BlendingOutput(1.0F, 0.0F);
48
		}
49
50
		@Override
51
		public float blendDensity(final int blockX, final int blockY, final int blockZ, final float noiseValue) {
52
			return noiseValue;
53
		}
54
55
		@Override
56
		public BiomeResolver getBiomeResolver(final BiomeResolver biomeResolver) {
57
			return biomeResolver;
58
		}
59
	};
60
	private static final Noise SHIFT_NOISE = NoiseData.DEFAULT_SHIFT.create(new XoroshiroRandomSource(42L));
61
	private static final int HEIGHT_BLENDING_RANGE_CELLS = QuartPos.fromSection(7) - 1;
62
	private static final int HEIGHT_BLENDING_RANGE_CHUNKS = QuartPos.toSection(HEIGHT_BLENDING_RANGE_CELLS + 3);
63
	private static final int DENSITY_BLENDING_RANGE_CELLS = 2;
64
	private static final int DENSITY_BLENDING_RANGE_CHUNKS = QuartPos.toSection(5);
65
	private static final double OLD_CHUNK_XZ_RADIUS = 8.0;
66
	private final Long2ObjectOpenHashMap<BlendingData> heightAndBiomeBlendingData;
67
	private final Long2ObjectOpenHashMap<BlendingData> densityBlendingData;
68
	private final CarvingMask.@Nullable Filter carvingFilter;
69
70
	public static Blender empty() {
71
		return EMPTY;
72
	}
73
74
	public static Blender of(final @Nullable WorldGenRegion region) {
75
		if (!SharedConstants.DEBUG_DISABLE_BLENDING && region != null) {
76
			ChunkPos centerPos = region.getCenter();
77
			if (!region.isOldChunkAround(centerPos, HEIGHT_BLENDING_RANGE_CHUNKS)) {
78
				return EMPTY;
79
			}
80
81
			Long2ObjectOpenHashMap<BlendingData> heightAndBiomeData = new Long2ObjectOpenHashMap<>();
82
			Long2ObjectOpenHashMap<BlendingData> densityData = new Long2ObjectOpenHashMap<>();
83
			int maxDistSq = Mth.square(HEIGHT_BLENDING_RANGE_CHUNKS + 1);
84
85
			for (int dx = -HEIGHT_BLENDING_RANGE_CHUNKS; dx <= HEIGHT_BLENDING_RANGE_CHUNKS; dx++) {
86
				for (int dz = -HEIGHT_BLENDING_RANGE_CHUNKS; dz <= HEIGHT_BLENDING_RANGE_CHUNKS; dz++) {
87
					if (dx * dx + dz * dz <= maxDistSq) {
88
						int chunkX = centerPos.x() + dx;
89
						int chunkZ = centerPos.z() + dz;
90
						BlendingData blendingData = BlendingData.getOrUpdateBlendingData(region, chunkX, chunkZ);
91
						if (blendingData != null) {
92
							heightAndBiomeData.put(ChunkPos.pack(chunkX, chunkZ), blendingData);
93
							if (dx >= -DENSITY_BLENDING_RANGE_CHUNKS
94
								&& dx <= DENSITY_BLENDING_RANGE_CHUNKS
95
								&& dz >= -DENSITY_BLENDING_RANGE_CHUNKS
96
								&& dz <= DENSITY_BLENDING_RANGE_CHUNKS) {
97
								densityData.put(ChunkPos.pack(chunkX, chunkZ), blendingData);
98
							}
99
						}
100
					}
101
				}
102
			}
103
104
			ChunkAccess centerChunk = region.getChunk(centerPos.x(), centerPos.z());
105
			CarvingMask.Filter carvingFilter = createAroundOldChunksCarvingMaskFilter(region, centerChunk);
106
			return heightAndBiomeData.isEmpty() && densityData.isEmpty() && carvingFilter == null ? EMPTY : new Blender(heightAndBiomeData, densityData, carvingFilter);
107
		} else {
108
			return EMPTY;
109
		}
110
	}
111
112
	private Blender(
113
		final Long2ObjectOpenHashMap<BlendingData> heightAndBiomeBlendingData,
114
		final Long2ObjectOpenHashMap<BlendingData> densityBlendingData,
115
		final CarvingMask.@Nullable Filter carvingFilter
116
	) {
117
		this.heightAndBiomeBlendingData = heightAndBiomeBlendingData;
118
		this.densityBlendingData = densityBlendingData;
119
		this.carvingFilter = carvingFilter;
120
	}
121
122
	public boolean isEmpty() {
123
		return this.heightAndBiomeBlendingData.isEmpty() && this.densityBlendingData.isEmpty() && this.carvingFilter == null;
124
	}
125
126
	public Blender.OutputBuffer blendOffsetAndFactor(final DensityVolume volume) {
127
		int minCellX = QuartPos.fromBlock(volume.minBlockX());
128
		int minCellZ = QuartPos.fromBlock(volume.minBlockZ());
129
		int maxCellX = QuartPos.fromBlock(volume.maxBlockX()) + 1;
130
		int maxCellZ = QuartPos.fromBlock(volume.maxBlockZ()) + 1;
131
		int cellCountX = maxCellX - minCellX + 1;
132
		int cellCountZ = maxCellZ - minCellZ + 1;
133
		float[] alphas = new float[cellCountX * cellCountZ];
134
		float[] offsets = new float[cellCountX * cellCountZ];
135
136
		for (int cellZ = 0; cellZ < cellCountZ; cellZ++) {
137
			for (int cellX = 0; cellX < cellCountX; cellX++) {
138
				int index = cellX + cellZ * cellCountX;
139
				Blender.BlendingOutput output = this.blendOffsetAndFactor(QuartPos.toBlock(minCellX + cellX), QuartPos.toBlock(minCellZ + cellZ));
140
				alphas[index] = output.alpha;
141
				offsets[index] = output.blendingOffset;
142
			}
143
		}
144
145
		return new Blender.OutputBuffer(this, minCellX, minCellZ, cellCountX, cellCountZ, alphas, offsets);
146
	}
147
148
	protected Blender.BlendingOutput blendOffsetAndFactor(final int blockX, final int blockZ) {
149
		int cellX = QuartPos.fromBlock(blockX);
150
		int cellZ = QuartPos.fromBlock(blockZ);
151
		float fixedHeight = this.getBlendingDataValue(cellX, 0, cellZ, BlendingData::getHeight);
152
		if (fixedHeight != Float.MAX_VALUE) {
153
			return new Blender.BlendingOutput(0.0F, (float)heightToOffset(fixedHeight));
154
		}
155
156
		MutableDouble totalWeight = new MutableDouble(0.0);
157
		MutableDouble weightedHeights = new MutableDouble(0.0);
158
		MutableDouble closestDistance = new MutableDouble(Double.POSITIVE_INFINITY);
159
		this.heightAndBiomeBlendingData
160
			.forEach(
161
				(chunkPos, blendingData) -> blendingData.iterateHeights(
162
					QuartPos.fromSection(ChunkPos.getX(chunkPos)), QuartPos.fromSection(ChunkPos.getZ(chunkPos)), (testCellX, testCellZ, height) -> {
163
						double distance = Mth.length(cellX - testCellX, cellZ - testCellZ);
164
						if (!(distance > HEIGHT_BLENDING_RANGE_CELLS)) {
165
							if (distance < closestDistance.doubleValue()) {
166
								closestDistance.setValue(distance);
167
							}
168
169
							double weight = 1.0 / (distance * distance * distance * distance);
170
							weightedHeights.add(height * weight);
171
							totalWeight.add(weight);
172
						}
173
					}
174
				)
175
			);
176
		if (closestDistance.doubleValue() == Double.POSITIVE_INFINITY) {
177
			return new Blender.BlendingOutput(1.0F, 0.0F);
178
		}
179
180
		double averageHeight = weightedHeights.doubleValue() / totalWeight.doubleValue();
181
		double alpha = Mth.clamp(closestDistance.doubleValue() / (HEIGHT_BLENDING_RANGE_CELLS + 1), 0.0, 1.0);
182
		alpha = 3.0 * alpha * alpha - 2.0 * alpha * alpha * alpha;
183
		return new Blender.BlendingOutput((float)alpha, (float)heightToOffset(averageHeight));
184
	}
185
186
	private static double heightToOffset(final double height) {
187
		double dimensionFactor = 1.0;
188
		double targetY = height + 0.5;
189
		double targetYMod = Mth.positiveModulo(targetY, 8.0);
190
		return 1.0 * (32.0 * (targetY - 128.0) - 3.0 * (targetY - 120.0) * targetYMod + 3.0 * targetYMod * targetYMod) / (128.0 * (32.0 - 3.0 * targetYMod));
191
	}
192
193
	public float blendDensity(final int blockX, final int blockY, final int blockZ, final float noiseValue) {
194
		int cellX = QuartPos.fromBlock(blockX);
195
		int cellY = blockY / 8;
196
		int cellZ = QuartPos.fromBlock(blockZ);
197
		float fixedDensity = this.getBlendingDataValue(cellX, cellY, cellZ, BlendingData::getDensity);
198
		if (fixedDensity != Float.MAX_VALUE) {
199
			return fixedDensity;
200
		}
201
202
		MutableDouble totalWeight = new MutableDouble(0.0);
203
		MutableDouble weightedHeights = new MutableDouble(0.0);
204
		MutableDouble closestDistance = new MutableDouble(Double.POSITIVE_INFINITY);
205
		this.densityBlendingData
206
			.forEach(
207
				(chunkPos, blendingData) -> blendingData.iterateDensities(
208
					QuartPos.fromSection(ChunkPos.getX(chunkPos)),
209
					QuartPos.fromSection(ChunkPos.getZ(chunkPos)),
210
					cellY - 1,
211
					cellY + 1,
212
					(testCellX, testCellY, testCellZ, density) -> {
213
						double distance = Mth.length(cellX - testCellX, (cellY - testCellY) * 2, cellZ - testCellZ);
214
						if (!(distance > 2.0)) {
215
							if (distance < closestDistance.doubleValue()) {
216
								closestDistance.setValue(distance);
217
							}
218
219
							double weight = 1.0 / (distance * distance * distance * distance);
220
							weightedHeights.add(density * weight);
221
							totalWeight.add(weight);
222
						}
223
					}
224
				)
225
			);
226
		if (closestDistance.doubleValue() == Double.POSITIVE_INFINITY) {
227
			return noiseValue;
228
		}
229
230
		double averageDensity = weightedHeights.doubleValue() / totalWeight.doubleValue();
231
		double alpha = Mth.clamp(closestDistance.doubleValue() / 3.0, 0.0, 1.0);
232
		return (float)Mth.lerp(alpha, averageDensity, noiseValue);
233
	}
234
235
	private float getBlendingDataValue(final int cellX, final int cellY, final int cellZ, final Blender.CellValueGetter cellValueGetter) {
236
		int chunkX = QuartPos.toSection(cellX);
237
		int chunkZ = QuartPos.toSection(cellZ);
238
		boolean minX = (cellX & 3) == 0;
239
		boolean minZ = (cellZ & 3) == 0;
240
		float value = this.getBlendingDataValue(cellValueGetter, chunkX, chunkZ, cellX, cellY, cellZ);
241
		if (value == Float.MAX_VALUE) {
242
			if (minX && minZ) {
243
				value = this.getBlendingDataValue(cellValueGetter, chunkX - 1, chunkZ - 1, cellX, cellY, cellZ);
244
			}
245
246
			if (value == Float.MAX_VALUE) {
247
				if (minX) {
248
					value = this.getBlendingDataValue(cellValueGetter, chunkX - 1, chunkZ, cellX, cellY, cellZ);
249
				}
250
251
				if (value == Float.MAX_VALUE && minZ) {
252
					value = this.getBlendingDataValue(cellValueGetter, chunkX, chunkZ - 1, cellX, cellY, cellZ);
253
				}
254
			}
255
		}
256
257
		return value;
258
	}
259
260
	private float getBlendingDataValue(
261
		final Blender.CellValueGetter cellValueGetter, final int chunkX, final int chunkZ, final int cellX, final int cellY, final int cellZ
262
	) {
263
		BlendingData blendingData = this.heightAndBiomeBlendingData.get(ChunkPos.pack(chunkX, chunkZ));
264
		return blendingData != null
265
			? cellValueGetter.get(blendingData, cellX - QuartPos.fromSection(chunkX), cellY, cellZ - QuartPos.fromSection(chunkZ))
266
			: Float.MAX_VALUE;
267
	}
268
269
	public BiomeResolver getBiomeResolver(final BiomeResolver biomeResolver) {
270
		return (quartX, quartY, quartZ) -> {
271
			Holder<Biome> biome = this.blendBiome(quartX, quartY, quartZ);
272
			return biome == null ? biomeResolver.getNoiseBiome(quartX, quartY, quartZ) : biome;
273
		};
274
	}
275
276
	private Holder<Biome> blendBiome(final int quartX, final int quartY, final int quartZ) {
277
		MutableFloat closestDistance = new MutableFloat(Float.POSITIVE_INFINITY);
278
		MutableObject<Holder<Biome>> closestBiome = new MutableObject<>();
279
		this.heightAndBiomeBlendingData
280
			.forEach(
281
				(chunkPos, blendingData) -> blendingData.iterateBiomes(
282
					QuartPos.fromSection(ChunkPos.getX(chunkPos)), quartY, QuartPos.fromSection(ChunkPos.getZ(chunkPos)), (testCellX, testCellZ, biome) -> {
283
						float distance = Mth.length(quartX - testCellX, quartZ - testCellZ);
284
						if (!(distance > HEIGHT_BLENDING_RANGE_CELLS)) {
285
							if (distance < closestDistance.floatValue()) {
286
								closestBiome.setValue(biome);
287
								closestDistance.setValue(distance);
288
							}
289
						}
290
					}
291
				)
292
			);
293
		if (closestDistance.floatValue() == Float.POSITIVE_INFINITY) {
294
			return null;
295
		}
296
297
		float shiftNoise = SHIFT_NOISE.get(quartX, 0.0, quartZ) * 12.0F;
298
		float alpha = Mth.clamp((closestDistance.floatValue() + shiftNoise) / (HEIGHT_BLENDING_RANGE_CELLS + 1), 0.0F, 1.0F);
299
		return alpha > 0.5F ? null : closestBiome.get();
300
	}
301
302
	public static void generateBorderTicks(final WorldGenRegion region, final ChunkAccess chunk) {
303
		if (!SharedConstants.DEBUG_DISABLE_BLENDING) {
304
			ChunkPos chunkPos = chunk.getPos();
305
			boolean oldNoiseGeneration = chunk.isOldNoiseGeneration();
306
			BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
307
			BlockPos chunkOrigin = new BlockPos(chunkPos.getMinBlockX(), 0, chunkPos.getMinBlockZ());
308
			BlendingData blendingData = chunk.getBlendingData();
309
			if (blendingData != null) {
310
				int oldMinY = blendingData.getAreaWithOldGeneration().getMinY();
311
				int oldMaxY = blendingData.getAreaWithOldGeneration().getMaxY();
312
				if (oldNoiseGeneration) {
313
					for (int x = 0; x < 16; x++) {
314
						for (int z = 0; z < 16; z++) {
315
							generateBorderTick(chunk, pos.setWithOffset(chunkOrigin, x, oldMinY - 1, z));
316
							generateBorderTick(chunk, pos.setWithOffset(chunkOrigin, x, oldMinY, z));
317
							generateBorderTick(chunk, pos.setWithOffset(chunkOrigin, x, oldMaxY, z));
318
							generateBorderTick(chunk, pos.setWithOffset(chunkOrigin, x, oldMaxY + 1, z));
319
						}
320
					}
321
				}
322
323
				for (Direction direction : Direction.Plane.HORIZONTAL) {
324
					if (region.getChunk(chunkPos.x() + direction.getStepX(), chunkPos.z() + direction.getStepZ()).isOldNoiseGeneration() != oldNoiseGeneration) {
325
						int minX = direction == Direction.EAST ? 15 : 0;
326
						int maxX = direction == Direction.WEST ? 0 : 15;
327
						int minZ = direction == Direction.SOUTH ? 15 : 0;
328
						int maxZ = direction == Direction.NORTH ? 0 : 15;
329
330
						for (int x = minX; x <= maxX; x++) {
331
							for (int z = minZ; z <= maxZ; z++) {
332
								int maxY = Math.min(oldMaxY, chunk.getHeight(Heightmap.Types.MOTION_BLOCKING, x, z)) + 1;
333
334
								for (int y = oldMinY; y < maxY; y++) {
335
									generateBorderTick(chunk, pos.setWithOffset(chunkOrigin, x, y, z));
336
								}
337
							}
338
						}
339
					}
340
				}
341
			}
342
		}
343
	}
344
345
	private static void generateBorderTick(final ChunkAccess chunk, final BlockPos pos) {
346
		BlockState blockState = chunk.getBlockState(pos);
347
		if (blockState.is(BlockTags.LEAVES)) {
348
			chunk.markPosForPostProcessing(pos);
349
		}
350
351
		FluidState fluidState = chunk.getFluidState(pos);
352
		if (!fluidState.isEmpty()) {
353
			chunk.markPosForPostProcessing(pos);
354
		}
355
	}
356
357
	public CarvingMask.@Nullable Filter getCarvingFilter() {
358
		return this.carvingFilter;
359
	}
360
361
	private static CarvingMask.@Nullable Filter createAroundOldChunksCarvingMaskFilter(final WorldGenLevel region, final ChunkAccess chunk) {
362
		ChunkPos chunkPos = chunk.getPos();
363
		Builder<CompositeDirection.Direction8, BlendingData> builder = ImmutableMap.builder();
364
365
		for (CompositeDirection.Direction8 direction8 : CompositeDirection.Direction8.values()) {
366
			int testChunkX = chunkPos.x() + direction8.getStepX();
367
			int testChunkZ = chunkPos.z() + direction8.getStepZ();
368
			BlendingData blendingData = region.getChunk(testChunkX, testChunkZ).getBlendingData();
369
			if (blendingData != null) {
370
				builder.put(direction8, blendingData);
371
			}
372
		}
373
374
		ImmutableMap<CompositeDirection.Direction8, BlendingData> oldSidesBlendingData = builder.build();
375
		BlendingData centerBlendingData = chunk.getBlendingData();
376
		if (centerBlendingData == null && oldSidesBlendingData.isEmpty()) {
377
			return null;
378
		}
379
380
		Blender.DistanceGetter distanceGetter = makeOldChunkDistanceGetter(centerBlendingData, oldSidesBlendingData);
381
		return (x, y, z) -> {
382
			double shiftedX = x + 0.5 + SHIFT_NOISE.get(x, y, z) * 4.0F;
383
			double shiftedY = y + 0.5 + SHIFT_NOISE.get(y, z, x) * 4.0F;
384
			double shiftedZ = z + 0.5 + SHIFT_NOISE.get(z, x, y) * 4.0F;
385
			return distanceGetter.getDistance(shiftedX, shiftedY, shiftedZ) < 4.0;
386
		};
387
	}
388
389
	public static Blender.DistanceGetter makeOldChunkDistanceGetter(
390
		final @Nullable BlendingData centerBlendingData, final Map<CompositeDirection.Direction8, BlendingData> oldSidesBlendingData
391
	) {
392
		List<Blender.DistanceGetter> distanceGetters = Lists.newArrayList();
393
		if (centerBlendingData != null) {
394
			distanceGetters.add(makeOffsetOldChunkDistanceGetter(null, centerBlendingData));
395
		}
396
397
		oldSidesBlendingData.forEach((side, blendingData) -> distanceGetters.add(makeOffsetOldChunkDistanceGetter(side, blendingData)));
398
		return (x, y, z) -> {
399
			double closest = Double.POSITIVE_INFINITY;
400
401
			for (Blender.DistanceGetter getter : distanceGetters) {
402
				double distance = getter.getDistance(x, y, z);
403
				if (distance < closest) {
404
					closest = distance;
405
				}
406
			}
407
408
			return closest;
409
		};
410
	}
411
412
	private static Blender.DistanceGetter makeOffsetOldChunkDistanceGetter(final CompositeDirection.@Nullable Direction8 offset, final BlendingData blendingData) {
413
		double offsetX = 0.0;
414
		double offsetZ = 0.0;
415
		if (offset != null) {
416
			for (Direction direction : offset.getDirections()) {
417
				offsetX += direction.getStepX() * 16;
418
				offsetZ += direction.getStepZ() * 16;
419
			}
420
		}
421
422
		double finalOffsetX = offsetX;
423
		double finalOffsetZ = offsetZ;
424
		double oldChunkYRadius = blendingData.getAreaWithOldGeneration().getHeight() / 2.0;
425
		double oldChunkCenterY = blendingData.getAreaWithOldGeneration().getMinY() + oldChunkYRadius;
426
		return (x, y, z) -> distanceToCube(x - 8.0 - finalOffsetX, y - oldChunkCenterY, z - 8.0 - finalOffsetZ, 8.0, oldChunkYRadius, 8.0);
427
	}
428
429
	private static double distanceToCube(final double x, final double y, final double z, final double radiusX, final double radiusY, final double radiusZ) {
430
		double deltaX = Math.abs(x) - radiusX;
431
		double deltaY = Math.abs(y) - radiusY;
432
		double deltaZ = Math.abs(z) - radiusZ;
433
		return Mth.length(Math.max(0.0, deltaX), Math.max(0.0, deltaY), Math.max(0.0, deltaZ));
434
	}
435
436
	protected record BlendingOutput(float alpha, float blendingOffset) {
437
	}
438
439
	private interface CellValueGetter {
440
		float get(BlendingData data, int cellX, int cellY, int cellZ);
441
	}
442
443
	public interface DistanceGetter {
444
		double getDistance(double x, double y, double z);
445
	}
446
447
	public record OutputBuffer(Blender blender, int minCellX, int minCellZ, int cellCountX, int cellCountZ, float[] alphas, float[] offsets) {
448
		private static final int NO_VALUE = -1;
449
450
		public DensitySampler createAlphaSampler() {
451
			return new DensitySampler() {
452
				@Override
453
				public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
454
					DensitySampler.sampleVolumeNaive(context, outputBuffer, volume, this);
455
				}
456
457
				@Override
458
				public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
459
					return OutputBuffer.this.getAlpha(blockX, blockZ);
460
				}
461
			};
462
		}
463
464
		public DensitySampler createOffsetSampler() {
465
			return new DensitySampler() {
466
				@Override
467
				public void sampleVolume(final SamplerContext context, final DensityBuffer outputBuffer, final DensityVolume volume) {
468
					DensitySampler.sampleVolumeNaive(context, outputBuffer, volume, this);
469
				}
470
471
				@Override
472
				public float sampleValue(final SamplerContext context, final int blockX, final int blockY, final int blockZ) {
473
					return OutputBuffer.this.getOffset(blockX, blockZ);
474
				}
475
			};
476
		}
477
478
		public float getAlpha(final int blockX, final int blockZ) {
479
			int index = this.getIndex(blockX, blockZ);
480
			return index == -1 ? this.blender.blendOffsetAndFactor(blockX, blockZ).alpha() : this.alphas[index];
481
		}
482
483
		public float getOffset(final int blockX, final int blockZ) {
484
			int index = this.getIndex(blockX, blockZ);
485
			return index == -1 ? this.blender.blendOffsetAndFactor(blockX, blockZ).blendingOffset() : this.offsets[index];
486
		}
487
488
		private int getIndex(final int blockX, final int blockZ) {
489
			int relativeX = QuartPos.fromBlock(blockX) - this.minCellX;
490
			int relativeZ = QuartPos.fromBlock(blockZ) - this.minCellZ;
491
			return relativeX >= 0 && relativeZ >= 0 && relativeX < this.cellCountX && relativeZ < this.cellCountZ ? relativeX + relativeZ * this.cellCountX : -1;
492
		}
493
	}
494
}
495

Decompiled Minecraft 26.3 source of Blender (net/minecraft/world/level/levelgen/blending/Blender.java) with Mojang official mappings, stored verbatim on the site.