Search in sources :

Example 36 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class LocalChunkProvider method getSubview.

@Override
public ChunkViewCore getSubview(BlockRegionc region, Vector3ic offset) {
    Chunk[] chunks = new Chunk[region.volume()];
    for (Vector3ic chunkPos : region) {
        Chunk chunk = chunkCache.get(chunkPos);
        int index = (chunkPos.x() - region.minX()) + region.getSizeX() * ((chunkPos.z() - region.minZ()) + region.getSizeZ() * (chunkPos.y() - region.minY()));
        chunks[index] = chunk;
    }
    return new ChunkViewCoreImpl(chunks, region, offset, blockManager.getBlock(BlockManager.AIR_ID));
}
Also used : ChunkViewCoreImpl(org.terasology.engine.world.internal.ChunkViewCoreImpl) Vector3ic(org.joml.Vector3ic) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 37 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class LodChunkProvider method createChunks.

private void createChunks() {
    Block unloaded = blockManager.getBlock(BlockManager.UNLOADED_ID);
    try {
        while (true) {
            Vector3ic pos = neededChunks.take();
            // Actually the log scale
            Integer scale = requiredChunks.get(pos);
            if (scale == null) {
                // This chunk is being removed in the main thread.
                continue;
            }
            Chunk chunk = new PreLodChunk(scaleDown(pos, scale), blockManager, extraDataManager);
            generator.createChunk(chunk, (1 << scale) * (2f / (Chunks.SIZE_X - 2) + 1));
            InternalLightProcessor.generateInternalLighting(chunk, 1 << scale);
            // tintChunk(chunk);
            ChunkView view = new ChunkViewCoreImpl(new Chunk[] { chunk }, new BlockRegion(chunk.getPosition(new Vector3i())), new Vector3i(), unloaded);
            ChunkMesh mesh = tessellator.generateMesh(view, 1 << scale, 1);
            readyChunks.add(new LodChunk(pos, mesh, scale));
        }
    } catch (InterruptedException ignored) {
    }
}
Also used : PreLodChunk(org.terasology.engine.world.chunks.internal.PreLodChunk) ChunkViewCoreImpl(org.terasology.engine.world.internal.ChunkViewCoreImpl) ChunkMesh(org.terasology.engine.rendering.primitives.ChunkMesh) Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) BlockRegion(org.terasology.engine.world.block.BlockRegion) PreLodChunk(org.terasology.engine.world.chunks.internal.PreLodChunk) PreLodChunk(org.terasology.engine.world.chunks.internal.PreLodChunk) ChunkView(org.terasology.engine.world.ChunkView)

Example 38 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class LodChunkProvider method updateRenderableRegion.

public void updateRenderableRegion(ViewDistance newViewDistance, int newChunkLods, Vector3i newCenter) {
    viewDistanceSetting = newViewDistance;
    center = new Vector3i(delay(center.x, newCenter.x), delay(center.y, newCenter.y), delay(center.z, newCenter.z));
    chunkLods = newChunkLods;
    nearby.pos = center;
    Vector3i viewDistance = new Vector3i(newViewDistance.getChunkDistance()).div(2);
    Vector3i altViewDistance = viewDistance.add(1 - Math.abs(viewDistance.x % 2), 1 - Math.abs(viewDistance.y % 2), 1 - Math.abs(viewDistance.z % 2), new Vector3i());
    BlockRegion newPossiblyLoadedRegion = new BlockRegion(newCenter).expand(viewDistance);
    BlockRegion newProbablyLoadedRegion = new BlockRegion(newPossiblyLoadedRegion).expand(-1, -1, -1);
    BlockRegion[] newLodRegions = new BlockRegion[newChunkLods == 0 ? 0 : 1 + newChunkLods];
    while (chunks.size() < newLodRegions.length) {
        chunks.add(new ConcurrentHashMap<>());
    }
    while (chunks.size() > newLodRegions.length) {
        for (LodChunk chunk : chunks.remove(chunks.size() - 1).values()) {
            chunk.disposeMesh();
        }
    }
    boolean lodRegionChange = newLodRegions.length != lodRegions.length;
    for (int i = 0; i < newLodRegions.length; i++) {
        if (i == 0) {
            newLodRegions[i] = new BlockRegion(newPossiblyLoadedRegion);
        } else {
            // By making viewDistance odd, we ensure that every time a chunk boundary is crossed, at most a single
            // lodRegion changes (except possibly for lodRegions[0], which is more closely tied to the renderable
            // region).
            newLodRegions[i] = new BlockRegion(scaleDown(center, i)).expand(altViewDistance);
        }
        Vector3i min = newLodRegions[i].getMin(new Vector3i());
        Vector3i max = newLodRegions[i].getMax(new Vector3i());
        newLodRegions[i].addToMin(-Math.abs(min.x % 2), -Math.abs(min.y % 2), -Math.abs(min.z % 2));
        newLodRegions[i].addToMax(1 - Math.abs(max.x % 2), 1 - Math.abs(max.y % 2), 1 - Math.abs(max.z % 2));
        if (!lodRegionChange && !newLodRegions[i].equals(lodRegions[i])) {
            lodRegionChange = true;
        }
    }
    if (lodRegionChange || !newProbablyLoadedRegion.equals(probablyLoadedRegion) || !newPossiblyLoadedRegion.equals(possiblyLoadedRegion)) {
        // Remove previously present chunks.
        Set<Vector3ic> previouslyRequiredChunks = new HashSet<>(requiredChunks.keySet());
        for (Vector3ic pos : previouslyRequiredChunks) {
            int scale = requiredChunks.get(pos);
            // Whether this entry in requiredChunks should be removed entirely (i.e. the chunk at the actually
            // required scale is not at this position).
            boolean gone = false;
            boolean increased = false;
            while (scale < newLodRegions.length && !gone && !newLodRegions[scale].contains(scaleDown(pos, scale))) {
                LodChunk chunk = chunks.get(scale).get(new Vector3i(pos));
                if (chunk != null) {
                    chunk.disposeMesh();
                    chunks.get(scale).remove(new Vector3i(pos));
                }
                gone = ((pos.x() | pos.y() | pos.z()) & (1 << scale)) != 0;
                scale++;
                increased = true;
            }
            if (gone || scale >= newLodRegions.length) {
                neededChunks.remove(pos);
                requiredChunks.remove(pos);
            } else if (increased) {
                LodChunk chunk = chunks.get(scale).get(new Vector3i(pos));
                if (chunk != null) {
                    requiredChunks.put(new Vector3i(pos), scale);
                    chunk.hiddenness = 0;
                } else {
                    requiredChunks.remove(pos);
                }
            }
        }
        // Add new chunks.
        for (int scale = 0; scale < newLodRegions.length; scale++) {
            for (Vector3ic pos : newLodRegions[scale]) {
                if (scale == 0 && newProbablyLoadedRegion.contains(pos) || scale == 0 && newPossiblyLoadedRegion.contains(pos) && chunkProvider.isChunkReady(pos) || scale > 0 && newLodRegions[scale - 1].contains(pos.mul(2, new Vector3i()))) {
                    continue;
                }
                Vector3i globalPos = pos.mul(1 << scale, new Vector3i());
                Integer previousScale = requiredChunks.get(globalPos);
                if (previousScale == null || previousScale > scale) {
                    addChunk(globalPos, scale);
                }
            }
        }
    }
    lodRegions = newLodRegions;
    probablyLoadedRegion = newProbablyLoadedRegion;
    possiblyLoadedRegion = newPossiblyLoadedRegion;
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion) PreLodChunk(org.terasology.engine.world.chunks.internal.PreLodChunk) HashSet(java.util.HashSet)

Example 39 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class VoxelBlockFluidWorld method loadChunk.

@Override
public void loadChunk(Chunk chunk, ShortBuffer buffer) {
    Vector3ic chunkPos = chunk.getPosition();
    wrapper.setRegion(chunkPos.x(), chunkPos.y(), chunkPos.z(), buffer);
}
Also used : Vector3ic(org.joml.Vector3ic)

Example 40 with Vector3ic

use of org.joml.Vector3ic in project Terasology by MovingBlocks.

the class VoxelBlockWorld method loadChunk.

@Override
public void loadChunk(Chunk chunk, ShortBuffer buffer) {
    Vector3ic chunkPos = chunk.getPosition();
    wrapper.setRegion(chunkPos.x(), chunkPos.y(), chunkPos.z(), buffer);
}
Also used : Vector3ic(org.joml.Vector3ic)

Aggregations

Vector3ic (org.joml.Vector3ic)87 Vector3i (org.joml.Vector3i)52 Test (org.junit.jupiter.api.Test)35 BlockRegion (org.terasology.engine.world.block.BlockRegion)28 Chunk (org.terasology.engine.world.chunks.Chunk)28 ChunkImpl (org.terasology.engine.world.chunks.internal.ChunkImpl)16 Block (org.terasology.engine.world.block.Block)12 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)11 Map (java.util.Map)8 Lists (com.google.common.collect.Lists)5 List (java.util.List)5 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)5 LocationComponent (org.terasology.engine.logic.location.LocationComponent)5 BlockRegionComponent (org.terasology.engine.world.block.regions.BlockRegionComponent)5 Maps (com.google.common.collect.Maps)4 Sets (com.google.common.collect.Sets)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 Set (java.util.Set)4 Vector3f (org.joml.Vector3f)4