Skip to content

Commit

Permalink
Refactor WorldTexture2dBiomeStructure
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatRedox authored and leMaik committed Dec 23, 2023
1 parent 35e5ed1 commit 0146388
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void registerDefaults() {
//TODO: create a plugin api interface for registering implementations, and move this to that
BiomeStructure.register(new Trivial3dBiomeStructure());
BiomeStructure.register(new Trivial2dBiomeStructure());
BiomeStructure.register(new WorldTexture2dBiomeStructure());
BiomeStructure.register(new WorldTexture2dBiomeStructure.Factory());
BiomeStructure.register(new WorldTexture3dBiomeStructure.Factory());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,116 +21,119 @@
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import se.llbit.chunky.renderer.scene.biome.BiomeStructure;
import se.llbit.util.annotation.NotNull;
import se.llbit.util.interner.Interner;
import se.llbit.util.interner.StrongInterner;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class WorldTexture2dBiomeStructure implements BiomeStructure.Factory {
public class WorldTexture2dBiomeStructure implements BiomeStructure {
public static final String ID = "WORLD_TEXTURE_2D";

@Override
public BiomeStructure create() {
return new Impl();
}
private final Long2ObjectOpenHashMap<ChunkTexture> map = new Long2ObjectOpenHashMap<>();

@Override
public BiomeStructure load(DataInputStream in) throws IOException {
return Impl.load(in);
private static long chunkPos(int x, int z) {
return (((long) x) << 32) | ((long) z);
}

@Override
public boolean is3d() {
return false;
public static WorldTexture2dBiomeStructure load(DataInputStream in) throws IOException {
WorldTexture2dBiomeStructure texture = new WorldTexture2dBiomeStructure();
Interner<ChunkTexture> interner = new StrongInterner<>();

int numTiles = in.readInt();
for (int i = 0; i < numTiles; i++) {
int x = in.readInt();
int z = in.readInt();
ChunkTexture tile = ChunkTexture.load(in).intern(interner);
texture.map.put(chunkPos(x, z), tile);
}

return texture;
}

@Override
public String getName() {
return "World texture 2d";
public void store(DataOutputStream out) throws IOException {
out.writeInt(map.size());
for (Long2ObjectMap.Entry<ChunkTexture> entry : map.long2ObjectEntrySet()) {
long pos = entry.getLongKey();
ChunkTexture texture = entry.getValue();

out.writeInt((int) (pos >> 32));
out.writeInt((int) pos);
texture.store(out);
}
}

@Override
public String getDescription() {
return "A 2d biome format that uses de-duplicated bitmaps per chunk.";
public void endFinalization() {
Interner<ChunkTexture> interner = new StrongInterner<>();
for (Long2ObjectMap.Entry<ChunkTexture> entry : map.long2ObjectEntrySet()) {
entry.setValue(entry.getValue().intern(interner));
}
}

@Override
public String getId() {
public String biomeFormat() {
return ID;
}

static class Impl implements BiomeStructure {
private final Long2ObjectOpenHashMap<ChunkTexture> map = new Long2ObjectOpenHashMap<>();
@Override
public void set(int x, int y, int z, float[] data) {
long cp = chunkPos(x >> 4, z >> 4);
ChunkTexture texture = map.get(cp);

private static long chunkPos(int x, int z) {
return (((long) x) << 32) | ((long) z);
if (texture == null) {
texture = new ChunkTexture();
map.put(cp, texture);
}

public static Impl load(DataInputStream in) throws IOException {
Impl texture = new Impl();
Interner<ChunkTexture> interner = new StrongInterner<>();
ChunkTexture newTex = texture.set(x & 0xF, z & 0xF, data);
if (newTex != texture) {
map.put(cp, newTex);
}
}

int numTiles = in.readInt();
for (int i = 0; i < numTiles; i++) {
int x = in.readInt();
int z = in.readInt();
ChunkTexture tile = ChunkTexture.load(in).intern(interner);
texture.map.put(chunkPos(x, z), tile);
}
@Override
public float[] get(int x, int y, int z) {
ChunkTexture texture = map.get(chunkPos(x >> 4, z >> 4));
if (texture == null) {
return null;
}
return texture.get(x & 0xF, z & 0xF);
}

return texture;

public static class Factory implements BiomeStructure.Factory {
@Override
public BiomeStructure create() {
return new WorldTexture2dBiomeStructure();
}

@Override
public void store(DataOutputStream out) throws IOException {
out.writeInt(map.size());
for (Long2ObjectMap.Entry<ChunkTexture> entry : map.long2ObjectEntrySet()) {
long pos = entry.getLongKey();
ChunkTexture texture = entry.getValue();

out.writeInt((int) (pos >> 32));
out.writeInt((int) pos);
texture.store(out);
}
public BiomeStructure load(@NotNull DataInputStream in) throws IOException {
return WorldTexture2dBiomeStructure.load(in);
}

@Override
public void endFinalization() {
Interner<ChunkTexture> interner = new StrongInterner<>();
for (Long2ObjectMap.Entry<ChunkTexture> entry : map.long2ObjectEntrySet()) {
entry.setValue(entry.getValue().intern(interner));
}
public boolean is3d() {
return false;
}

@Override
public String biomeFormat() {
return ID;
public String getName() {
return "World texture 2d";
}

@Override
public void set(int x, int y, int z, float[] data) {
long cp = chunkPos(x >> 4, z >> 4);
ChunkTexture texture = map.get(cp);

if (texture == null) {
texture = new ChunkTexture();
}

ChunkTexture newTex = texture.set(x & 0xF, z & 0xF, data);
if (newTex != texture) {
map.put(cp, newTex);
}
public String getDescription() {
return "A 2d biome format that uses de-duplicated bitmaps per chunk.";
}

@Override
public float[] get(int x, int y, int z) {
ChunkTexture texture = map.get(chunkPos(x >> 4, z >> 4));
if (texture == null) {
return null;
}
return texture.get(x & 0xF, z & 0xF);
public String getId() {
return ID;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@

package se.llbit.chunky.renderer.scene.biome.worldtexture;

import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongArrayList;
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
import se.llbit.chunky.renderer.scene.biome.BiomeStructure;
import se.llbit.util.annotation.NotNull;
import se.llbit.util.interner.Interner;
import se.llbit.util.interner.StrongInterner;
import se.llbit.util.interner.WeakInterner;

import java.io.DataInputStream;
import java.io.DataOutputStream;
Expand All @@ -41,9 +38,6 @@ public class WorldTexture3dBiomeStructure implements BiomeStructure {
*/
private final Long2ObjectOpenHashMap<ChunkTexture3d> map = new Long2ObjectOpenHashMap<>();

/**
* List of live, un-interned chunk textures.
*/
private LongOpenHashSet live = null;
private Interner<ChunkTexture> interner2d = null;
private Interner<ChunkTexture3d> interner3d = null;
Expand Down

0 comments on commit 0146388

Please sign in to comment.