Draw trees in the studio's 3D world as whole blocks

Each column carried only its highest solid block, so a tree's leaves
stood as a pillar down to the ground. The ground is now the terrain's
top, and the solid blocks above it go to the studio as a list per
column. The 3D world draws them as cubes and leaves out the faces that
touch the ground or each other. The 2D map still colours and shades a
column by its highest block.
This commit is contained in:
piexel committed 2026-11-12 07:02:17 +01:00
1 parent 55f6a06f9f
commit 4e46eb4a6a
17 files changed
+457 -57

No files matched your search

@@ -36,8 +36,10 @@ import java.util.function.Function;
* u32 header length | header, UTF-8 JSON | padding to 4 bytes
* i16 subjects[grid²] | padding to 4 | i16 regions[grid²] | padding to 4
* f32 heights[grid²], when the header says so | f32 pickNoise[grid²], when the header says so
* i16 ground[grid²] | padding to 4 | i16 cover[grid²], when the header names blocks
* i16 ground[grid²] | padding to 4 | i16 cover[grid²] | padding to 4 | i16 raisedCounts[grid²] | padding to 4
* | i16 raisedY[raised] | padding to 4 | i16 raised[raised] | padding to 4, when the header names blocks
* </pre>
* {@code raised} is the sum of the raised counts, which the header also holds.
* The header holds the size, the scale, the subject kind, the labels the grids index, and the blocks the ground and
* cover grids index when the tile carries the tops of its columns, each as the state players see. Every array starts on a
* multiple of 4 bytes, so the studio views them in place.
@@ -56,15 +58,16 @@ final class MapTileEncoding {
boolean hasHeights = tile.heights() != null;
boolean hasPickNoise = tile.pickNoise() != null;
MapTile.Blocks blocks = tile.blocks();
int raised = blocks == null ? 0 : blocks.raised().length;
byte[] header = gson.toJson(new Header(tile.size(), tile.blocksPerPixel(), tile.subjectKind(), tile.labels(),
hasHeights, hasPickNoise, blocks == null ? null : statesPlayersSee(blocks.names(), pluginLooks)))
hasHeights, hasPickNoise, blocks == null ? null : statesPlayersSee(blocks.names(), pluginLooks), raised))
.getBytes(StandardCharsets.UTF_8);
int cells = tile.gridSize() * tile.gridSize();
int length = aligned(Integer.BYTES + header.length)
+ aligned(cells * Short.BYTES) * 2
+ (hasHeights ? cells * Float.BYTES : 0)
+ (hasPickNoise ? cells * Float.BYTES : 0)
+ (blocks == null ? 0 : aligned(cells * Short.BYTES) * 2);
+ (blocks == null ? 0 : aligned(cells * Short.BYTES) * 3 + aligned(raised * Short.BYTES) * 2);
ByteBuffer buffer = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(header.length).put(header);
align(buffer);
@@ -81,6 +84,9 @@ final class MapTileEncoding {
if (blocks != null) {
putShorts(buffer, blocks.ground());
putShorts(buffer, blocks.cover());
putShorts(buffer, blocks.raisedCounts());
putShorts(buffer, blocks.raisedY());
putShorts(buffer, blocks.raised());
}
return buffer.array();
}
@@ -105,6 +111,6 @@ final class MapTileEncoding {
}
private record Header(int size, double blocksPerPixel, MapLabel.Kind subjectKind, List<MapLabel> labels,
boolean heights, boolean pickNoise, List<String> blocks) {
boolean heights, boolean pickNoise, List<String> blocks, int raised) {
}
}
@@ -18,6 +18,8 @@
package art.arcane.iris.core.studio.map;
import java.util.Arrays;
/** What lies on top of the world's columns, from one {@link BlockSource}. */
@FunctionalInterface
interface ColumnTops {
@@ -26,11 +28,47 @@ interface ColumnTops {
/**
* The top of one column.
*
* @param groundY the height of its ground block above the dimension's bottom
* @param groundY the height of its ground block above the dimension's bottom: the terrain's top, under what stands
* on it
* @param ground the ground block's key, or null where the column holds no ground
* @param cover the key of the block standing on the ground, such as grass or a layer of snow, or null for none
* @param raised the solid blocks above the ground, such as a tree's wood and leaves
*/
record ColumnTop(int groundY, String ground, String cover) {
static final ColumnTop NOTHING = new ColumnTop(0, null, null);
record ColumnTop(int groundY, String ground, String cover, Raised raised) {
static final ColumnTop NOTHING = new ColumnTop(0, null, null, Raised.NONE);
ColumnTop(int groundY, String ground, String cover) {
this(groundY, ground, cover, Raised.NONE);
}
}
/**
* The solid blocks above a column's ground, highest first, with the gaps between them left out. Kept as two arrays,
* as a forest's chunk holds over a thousand of them.
*
* @param ys each block's height above the dimension's bottom
* @param blocks each block's key
*/
record Raised(short[] ys, String[] blocks) {
static final Raised NONE = new Raised(new short[0], new String[0]);
int count() {
return ys.length;
}
@Override
public boolean equals(Object other) {
return other instanceof Raised raised && Arrays.equals(ys, raised.ys) && Arrays.equals(blocks, raised.blocks);
}
@Override
public int hashCode() {
return 31 * Arrays.hashCode(ys) + Arrays.hashCode(blocks);
}
@Override
public String toString() {
return "Raised" + Arrays.toString(ys) + Arrays.toString(blocks);
}
}
}
@@ -29,10 +29,14 @@ import art.arcane.iris.util.project.context.IrisContext;
import art.arcane.iris.util.project.hunk.Hunk;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.IntBinaryOperator;
import java.util.function.UnaryOperator;
/**
@@ -45,8 +49,10 @@ final class GeneratedChunks implements ColumnTops {
private static final int CHUNK_SHIFT = 4;
private static final int BLOCKS_ACROSS = 1 << CHUNK_SHIFT;
private static final int CHUNK_MASK = BLOCKS_ACROSS - 1;
// A chunk's top is about 5 KB, so this many stay under 50 MB.
private static final int MOST_CHUNKS_KEPT = 8192;
// A chunk's top is about 5 KB, and each block raised over its ground adds a height and a shared key.
private static final int CHUNK_TOP_BYTES = 5 * 1024;
private static final int RAISED_BLOCK_BYTES = Short.BYTES + Integer.BYTES;
private static final long MOST_BYTES_KEPT = 64L * 1024 * 1024;
private static final String LEASE_NAME = "studio_block_preview";
private final Engine engine;
@@ -55,7 +61,10 @@ final class GeneratedChunks implements ColumnTops {
GeneratedChunks(Engine engine) {
this.engine = engine;
this.tops = Caffeine.newBuilder().maximumSize(MOST_CHUNKS_KEPT).build(this::generate);
this.tops = Caffeine.newBuilder()
.maximumWeight(MOST_BYTES_KEPT)
.weigher((Long key, ChunkTop top) -> top.bytes())
.build(this::generate);
}
@Override
@@ -74,7 +83,8 @@ final class GeneratedChunks implements ColumnTops {
IrisContext.Scope ignored = IrisContext.open(engine, lease.sessionId(), null)) {
engine.getGeneration().generate(chunkX << CHUNK_SHIFT, chunkZ << CHUNK_SHIFT, blocks, biomes, false, lease.sessionId());
}
return new ChunkTop(topsOf(blocks, this::seenByPlayers));
IntBinaryOperator terrainY = (x, z) -> engine.getHeight((chunkX << CHUNK_SHIFT) + x, (chunkZ << CHUNK_SHIFT) + z, true);
return new ChunkTop(topsOf(blocks, terrainY, this::seenByPlayers));
}
// A plugin's block can be solid on the server where players see grass they walk through.
@@ -93,43 +103,77 @@ final class GeneratedChunks implements ColumnTops {
}
private record ChunkTop(ColumnTop[] columns) {
int bytes() {
int raisedBlocks = 0;
for (ColumnTop column : columns) {
raisedBlocks += column.raised().count();
}
return CHUNK_TOP_BYTES + raisedBlocks * RAISED_BLOCK_BYTES;
}
}
/**
* The top of each column of a chunk's blocks, row by row from the north west.
*
* @param seenByPlayers the state players see in place of a block, which decides whether it is ground; the block
* @param terrainY the height of the terrain's top block in each column, by its x and z in the chunk, which
* the blocks above stand on
* @param seenByPlayers the state players see in place of a block, which decides whether it is solid; the block
* itself for all but a plugin's
*/
static ColumnTop[] topsOf(Hunk<PlatformBlockState> blocks, UnaryOperator<PlatformBlockState> seenByPlayers) {
static ColumnTop[] topsOf(Hunk<PlatformBlockState> blocks, IntBinaryOperator terrainY, UnaryOperator<PlatformBlockState> seenByPlayers) {
ColumnTop[] columns = new ColumnTop[BLOCKS_ACROSS * BLOCKS_ACROSS];
for (int z = 0; z < BLOCKS_ACROSS; z++) {
for (int x = 0; x < BLOCKS_ACROSS; x++) {
columns[z * BLOCKS_ACROSS + x] = topOf(blocks, x, z, seenByPlayers);
columns[z * BLOCKS_ACROSS + x] = topOf(blocks, x, z, terrainY.applyAsInt(x, z), seenByPlayers);
}
}
return columns;
}
// The ground is the highest block something can stand on, so a tree's leaves and a roof count; what stands on it
// without being ground, such as grass or a layer of snow, is its cover.
private static ColumnTop topOf(Hunk<PlatformBlockState> blocks, int x, int z, UnaryOperator<PlatformBlockState> seenByPlayers) {
// The ground is the highest solid block at or under the terrain's top, so a cave open to the sky lowers it. Solid
// blocks above it, such as a tree, are raised; what stands on the ground without being solid, such as grass or a
// layer of snow, is its cover. A column with nothing solid under its raised blocks takes the highest as its ground.
private static ColumnTop topOf(Hunk<PlatformBlockState> blocks, int x, int z, int terrainY, UnaryOperator<PlatformBlockState> seenByPlayers) {
RaisedBlocks raised = new RaisedBlocks();
for (int y = blocks.getHeight() - 1; y >= 0; y--) {
PlatformBlockState block = blocks.get(x, y, z);
if (block == null || !isGround(seenByPlayers.apply(block))) {
if (block == null || !isSolid(seenByPlayers.apply(block))) {
continue;
}
PlatformBlockState above = y + 1 < blocks.getHeight() ? blocks.get(x, y + 1, z) : null;
return new ColumnTop(y, block.key(), isCover(above) ? above.key() : null);
if (y > terrainY) {
raised.add(y, block.key());
continue;
}
return new ColumnTop(y, block.key(), coverOn(blocks, x, y, z, seenByPlayers), raised.toRaised());
}
return ColumnTop.NOTHING;
return raised.isEmpty() ? ColumnTop.NOTHING : topOf(blocks, x, z, blocks.getHeight(), seenByPlayers);
}
private static boolean isGround(PlatformBlockState block) {
private static String coverOn(Hunk<PlatformBlockState> blocks, int x, int groundY, int z, UnaryOperator<PlatformBlockState> seenByPlayers) {
PlatformBlockState above = groundY + 1 < blocks.getHeight() ? blocks.get(x, groundY + 1, z) : null;
boolean isCover = above != null && !above.isAir() && !above.isFluid() && !isSolid(seenByPlayers.apply(above));
return isCover ? above.key() : null;
}
private static boolean isSolid(PlatformBlockState block) {
return !block.isAir() && !block.isFluid() && block.isSolid();
}
private static boolean isCover(PlatformBlockState block) {
return block != null && !block.isAir() && !block.isFluid();
private static final class RaisedBlocks {
private final ShortArrayList ys = new ShortArrayList();
private final List<String> blocks = new ArrayList<>();
void add(int y, String block) {
ys.add((short) y);
blocks.add(block);
}
boolean isEmpty() {
return blocks.isEmpty();
}
Raised toRaised() {
return isEmpty() ? Raised.NONE : new Raised(ys.toShortArray(), blocks.toArray(String[]::new));
}
}
}
@@ -40,11 +40,14 @@ public record MapTile(int size, double blocksPerPixel, MapLabel.Kind subjectKind
/**
* The top of each cell's column.
*
* @param names the block keys the grids index
* @param ground each cell's ground block, or {@link #NO_BLOCK} where the column holds none
* @param cover the block standing on each cell's ground, such as grass, or {@link #NO_BLOCK}
* @param names the block keys the grids index
* @param ground each cell's ground block, the terrain's top, or {@link #NO_BLOCK} where the column holds none
* @param cover the block standing on each cell's ground, such as grass, or {@link #NO_BLOCK}
* @param raisedCounts how many solid blocks stand above each cell's ground, such as a tree's wood and leaves
* @param raisedY the height of each raised block, cell after cell and each cell's highest first
* @param raised each raised block, in the order of {@code raisedY}
*/
public record Blocks(List<String> names, short[] ground, short[] cover) {
public record Blocks(List<String> names, short[] ground, short[] cover, short[] raisedCounts, short[] raisedY, short[] raised) {
public static final short NO_BLOCK = -1;
}
@@ -24,6 +24,7 @@ import art.arcane.iris.engine.framework.render.RenderType;
import art.arcane.iris.engine.object.world.biome.IrisBiome;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import art.arcane.iris.util.project.stream.ProceduralStream;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import java.util.ArrayList;
import java.util.Arrays;
@@ -73,8 +74,8 @@ public final class MapTileRenderer {
/**
* {@code tile}, drawn from ({@code startX}, {@code startZ}), with the block on top of each of its columns from
* {@code source}. Where the tile carries heights they become the heights of that ground, which is the terrain's
* with what generation put on it.
* {@code source}, and the solid blocks raised above it. Where the tile carries heights they become the heights of
* that ground: the terrain's top as generated, under what stands on it.
*
* @throws IllegalArgumentException when the tile is coarser than the source is drawn at
* @throws CancellationException as soon as {@code cancelled} answers true
@@ -90,6 +91,9 @@ public final class MapTileRenderer {
int cells = grid.size() * grid.size();
short[] ground = new short[cells];
short[] cover = new short[cells];
short[] raisedCounts = new short[cells];
ShortArrayList raisedY = new ShortArrayList();
ShortArrayList raised = new ShortArrayList();
float[] heights = tile.heights() == null ? null : new float[cells];
BlockNames names = new BlockNames();
for (int gridZ = 0; gridZ < grid.size(); gridZ++) {
@@ -100,12 +104,19 @@ public final class MapTileRenderer {
ColumnTops.ColumnTop top = tops.at((int) Math.floor(grid.worldX(gridX)), worldZ);
ground[cell] = names.indexOf(top.ground());
cover[cell] = names.indexOf(top.cover());
ColumnTops.Raised raisedHere = top.raised();
raisedCounts[cell] = (short) raisedHere.count();
raisedY.addElements(raisedY.size(), raisedHere.ys());
for (String block : raisedHere.blocks()) {
raised.add(names.indexOf(block));
}
if (heights != null) {
heights[cell] = top.groundY();
}
}
}
return tile.withBlocks(new MapTile.Blocks(names.names(), ground, cover), heights);
MapTile.Blocks blocks = new MapTile.Blocks(names.names(), ground, cover, raisedCounts, raisedY.toShortArray(), raised.toShortArray());
return tile.withBlocks(blocks, heights);
}
private ColumnTops topsOf(BlockSource source) {
@@ -4,6 +4,7 @@ import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.iris.util.project.hunk.Hunk;
import org.junit.Test;
import java.util.function.IntBinaryOperator;
import java.util.function.UnaryOperator;
import static org.junit.Assert.assertEquals;
@@ -11,38 +12,84 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class GeneratedChunksTest {
private static final int HEIGHT = 8;
private static final int HEIGHT = 12;
private static final PlatformBlockState STONE = block("minecraft:stone", true);
private static final PlatformBlockState POPPY = block("minecraft:poppy", false);
private static final PlatformBlockState LOG = block("minecraft:oak_log", true);
private static final PlatformBlockState LEAVES = block("minecraft:oak_leaves", true);
private static final PlatformBlockState TRIPWIRE = block("minecraft:tripwire", false);
// A plugin's grass: solid on the server, a tripwire to players.
private static final PlatformBlockState PLUGIN_GRASS = block("craftengine:custom_7", true);
@Test
public void theGroundIsTheHighestSolidBlockAndWhatStandsOnItItsCover() {
public void theGroundIsTheTerrainsTopAndWhatStandsOnItItsCover() {
Hunk<PlatformBlockState> blocks = Hunk.newArrayHunk(16, HEIGHT, 16);
blocks.set(0, 3, 0, STONE);
blocks.set(0, 4, 0, POPPY);
blocks.set(1, 2, 0, STONE);
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, UnaryOperator.identity());
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, terrainAt(3), UnaryOperator.identity());
assertEquals(new ColumnTops.ColumnTop(3, "minecraft:stone", "minecraft:poppy"), tops[0]);
assertEquals(new ColumnTops.ColumnTop(2, "minecraft:stone", null), tops[1]);
assertEquals(ColumnTops.ColumnTop.NOTHING, tops[2]);
}
@Test
public void aTreeIsRaisedOverTheGroundItStandsOnInsteadOfBeingTheGround() {
Hunk<PlatformBlockState> blocks = Hunk.newArrayHunk(16, HEIGHT, 16);
blocks.set(0, 3, 0, STONE);
blocks.set(0, 4, 0, LOG);
blocks.set(0, 5, 0, LOG);
blocks.set(0, 6, 0, LEAVES);
blocks.set(1, 3, 0, STONE);
blocks.set(1, 4, 0, POPPY);
blocks.set(1, 6, 0, LEAVES);
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, terrainAt(3), UnaryOperator.identity());
ColumnTops.Raised trunk = new ColumnTops.Raised(new short[]{6, 5, 4}, new String[]{"minecraft:oak_leaves", "minecraft:oak_log", "minecraft:oak_log"});
assertEquals(new ColumnTops.ColumnTop(3, "minecraft:stone", null, trunk), tops[0]);
ColumnTops.Raised canopy = new ColumnTops.Raised(new short[]{6}, new String[]{"minecraft:oak_leaves"});
assertEquals(new ColumnTops.ColumnTop(3, "minecraft:stone", "minecraft:poppy", canopy), tops[1]);
}
@Test
public void aCaveOpenToTheSkyLowersTheGround() {
Hunk<PlatformBlockState> blocks = Hunk.newArrayHunk(16, HEIGHT, 16);
blocks.set(0, 1, 0, STONE);
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, terrainAt(3), UnaryOperator.identity());
assertEquals(new ColumnTops.ColumnTop(1, "minecraft:stone", null), tops[0]);
}
@Test
public void blocksWithNothingSolidUnderThemAreTheGround() {
Hunk<PlatformBlockState> blocks = Hunk.newArrayHunk(16, HEIGHT, 16);
blocks.set(0, 8, 0, STONE);
blocks.set(0, 9, 0, STONE);
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, terrainAt(3), UnaryOperator.identity());
assertEquals(new ColumnTops.ColumnTop(9, "minecraft:stone", null), tops[0]);
}
@Test
public void aPluginBlockPlayersWalkThroughIsCoverThoughTheServerHoldsItSolid() {
Hunk<PlatformBlockState> blocks = Hunk.newArrayHunk(16, HEIGHT, 16);
blocks.set(0, 3, 0, STONE);
blocks.set(0, 4, 0, PLUGIN_GRASS);
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, block -> block == PLUGIN_GRASS ? TRIPWIRE : block);
ColumnTops.ColumnTop[] tops = GeneratedChunks.topsOf(blocks, terrainAt(3), block -> block == PLUGIN_GRASS ? TRIPWIRE : block);
assertEquals(new ColumnTops.ColumnTop(3, "minecraft:stone", "craftengine:custom_7"), tops[0]);
}
private static IntBinaryOperator terrainAt(int y) {
return (x, z) -> y;
}
private static PlatformBlockState block(String key, boolean solid) {
PlatformBlockState block = mock(PlatformBlockState.class);
when(block.key()).thenReturn(key);