Search in sources :

Example 71 with Vector3ic

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

the class BlockMeshGeneratorSingleShape method generateChunkMesh.

@Override
public void generateChunkMesh(ChunkView view, ChunkMesh chunkMesh, int x, int y, int z) {
    final BlockAppearance blockAppearance = block.getPrimaryAppearance();
    if (!blockAppearance.hasAppearance()) {
        // perf: Skip mesh generation for blocks without appearance, e.g., air blocks.
        return;
    }
    Color colorCache = new Color();
    // Gather adjacent blocks
    Block[] adjacentBlocks = new Block[Side.allSides().size()];
    for (Side side : Side.allSides()) {
        Vector3ic offset = side.direction();
        Block blockToCheck = view.getBlock(x + offset.x(), y + offset.y(), z + offset.z());
        adjacentBlocks[side.ordinal()] = blockToCheck;
    }
    final ChunkMesh.RenderType renderType = getRenderType(block);
    final ChunkVertexFlag vertexFlag = getChunkVertexFlag(view, x, y, z, block);
    boolean isRendered = false;
    for (final Side side : Side.allSides()) {
        if (isSideVisibleForBlockTypes(adjacentBlocks[side.ordinal()], block, side)) {
            isRendered = true;
            BlockMeshPart blockMeshPart = blockAppearance.getPart(BlockPart.fromSide(side));
            // If the selfBlock isn't lowered, some more faces may have to be drawn
            if (block.isLiquid()) {
                final Block topBlock = adjacentBlocks[Side.TOP.ordinal()];
                // Draw horizontal sides if visible from below
                if (topBlock.isLiquid() && Side.horizontalSides().contains(side)) {
                    final Vector3ic offset = side.direction();
                    final Block adjacentAbove = view.getBlock(x + offset.x(), y + 1, z + offset.z());
                    final Block adjacent = adjacentBlocks[side.ordinal()];
                    if (adjacent.isLiquid() && !adjacentAbove.isLiquid()) {
                        blockMeshPart = block.getTopLiquidMesh(side);
                    }
                } else {
                    if (blockMeshPart != null) {
                        blockMeshPart = block.getLowLiquidMesh(side);
                    }
                }
            }
            if (blockMeshPart != null) {
                // TODO: Needs review since the new per-vertex flags introduce a lot of special scenarios - probably a per-side setting?
                ChunkVertexFlag sideVertexFlag = vertexFlag;
                if (block.isGrass() && side != Side.TOP && side != Side.BOTTOM) {
                    sideVertexFlag = ChunkVertexFlag.COLOR_MASK;
                }
                Colorc colorOffset = block.getColorOffset(BlockPart.fromSide(side));
                Colorc colorSource = block.getColorSource(BlockPart.fromSide(side)).calcColor(view, x, y, z);
                colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
                blockMeshPart.appendTo(chunkMesh, view, x, y, z, renderType, colorCache, sideVertexFlag);
            }
        }
    }
    if (isRendered && blockAppearance.getPart(BlockPart.CENTER) != null) {
        Colorc colorOffset = block.getColorOffset(BlockPart.CENTER);
        Colorc colorSource = block.getColorSource(BlockPart.CENTER).calcColor(view, x, y, z);
        colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
        blockAppearance.getPart(BlockPart.CENTER).appendTo(chunkMesh, view, x, y, z, renderType, colorCache, vertexFlag);
    }
}
Also used : Side(org.terasology.engine.math.Side) Colorc(org.terasology.nui.Colorc) BlockAppearance(org.terasology.engine.world.block.BlockAppearance) Vector3ic(org.joml.Vector3ic) Color(org.terasology.nui.Color) Block(org.terasology.engine.world.block.Block) BlockMeshPart(org.terasology.engine.world.block.shapes.BlockMeshPart)

Example 72 with Vector3ic

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

the class ChunkRelevanceRegion method reviewRelevantChunks.

private void reviewRelevantChunks(Vector3ic distance) {
    Vector3i extents = new Vector3i(distance.x() / 2, distance.y() / 2, distance.z() / 2);
    BlockRegion retainRegion = new BlockRegion(center).expand(extents);
    Iterator<Vector3ic> iter = relevantChunks.iterator();
    while (iter.hasNext()) {
        Vector3ic pos = iter.next();
        if (!retainRegion.contains(pos)) {
            sendChunkIrrelevant(pos);
            iter.remove();
        }
    }
}
Also used : Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) BlockRegion(org.terasology.engine.world.block.BlockRegion)

Example 73 with Vector3ic

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

the class LocalChunkProvider method processReadyChunk.

private void processReadyChunk(final Chunk chunk) {
    Vector3ic chunkPos = chunk.getPosition();
    if (chunkCache.get(chunkPos) != null) {
        // TODO move it in pipeline;
        return;
    }
    chunkCache.put(new Vector3i(chunkPos), chunk);
    chunk.markReady();
    // TODO, it is not clear if the activate/addedBlocks event logic is correct.
    // See https://github.com/MovingBlocks/Terasology/issues/3244
    ChunkStore store = this.storageManager.loadChunkStore(chunkPos);
    TShortObjectMap<TIntList> mappings = createBatchBlockEventMappings(chunk);
    if (store != null) {
        store.restoreEntities();
        PerformanceMonitor.startActivity("Sending OnAddedBlocks");
        mappings.forEachEntry((id, positions) -> {
            if (positions.size() > 0) {
                blockManager.getBlock(id).getEntity().send(new OnAddedBlocks(positions, registry));
            }
            return true;
        });
        PerformanceMonitor.endActivity();
        // send on activate
        PerformanceMonitor.startActivity("Sending OnActivateBlocks");
        mappings.forEachEntry((id, positions) -> {
            if (positions.size() > 0) {
                blockManager.getBlock(id).getEntity().send(new OnActivatedBlocks(positions, registry));
            }
            return true;
        });
        PerformanceMonitor.endActivity();
    } else {
        PerformanceMonitor.startActivity("Generating queued Entities");
        generateQueuedEntities.remove(chunkPos).forEach(this::generateQueuedEntities);
        PerformanceMonitor.endActivity();
        // send on activate
        PerformanceMonitor.startActivity("Sending OnActivateBlocks");
        mappings.forEachEntry((id, positions) -> {
            if (positions.size() > 0) {
                blockManager.getBlock(id).getEntity().send(new OnActivatedBlocks(positions, registry));
            }
            return true;
        });
        PerformanceMonitor.endActivity();
        worldEntity.send(new OnChunkGenerated(chunkPos));
    }
    worldEntity.send(new OnChunkLoaded(chunkPos));
}
Also used : OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) OnActivatedBlocks(org.terasology.engine.world.block.OnActivatedBlocks) OnChunkGenerated(org.terasology.engine.world.chunks.event.OnChunkGenerated) Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i) OnAddedBlocks(org.terasology.engine.world.block.OnAddedBlocks) TIntList(gnu.trove.list.TIntList) ChunkStore(org.terasology.engine.persistence.ChunkStore)

Example 74 with Vector3ic

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

the class LocalChunkProvider method purgeWorld.

@Override
public void purgeWorld() {
    ChunkMonitor.fireChunkProviderDisposed(this);
    loadingPipeline.shutdown();
    unloadRequestTaskMaster.shutdown(new ChunkUnloadRequest(), true);
    getAllChunks().stream().filter(Chunk::isReady).forEach(chunk -> {
        worldEntity.send(new BeforeChunkUnload(chunk.getPosition(new Vector3i())));
        storageManager.deactivateChunk(chunk);
        chunk.dispose();
    });
    chunkCache.clear();
    storageManager.deleteWorld();
    worldEntity.send(new PurgeWorldEvent());
    loadingPipeline = new ChunkProcessingPipeline(this::getChunk, relevanceSystem.createChunkTaskComporator());
    loadingPipeline.addStage(ChunkTaskProvider.create("Chunk generate internal lightning", (Consumer<Chunk>) InternalLightProcessor::generateInternalLighting)).addStage(ChunkTaskProvider.create("Chunk deflate", Chunk::deflate)).addStage(ChunkTaskProvider.createMulti("Light merging", chunks -> {
        Chunk[] localChunks = chunks.toArray(new Chunk[0]);
        return LightMerger.merge(localChunks);
    }, LightMerger::requiredChunks)).addStage(ChunkTaskProvider.create("Chunk ready", readyChunks::add));
    unloadRequestTaskMaster = TaskMaster.createFIFOTaskMaster("Chunk-Unloader", 8);
    ChunkMonitor.fireChunkProviderInitialized(this);
    for (ChunkRelevanceRegion chunkRelevanceRegion : relevanceSystem.getRegions()) {
        for (Vector3ic pos : chunkRelevanceRegion.getCurrentRegion()) {
            createOrLoadChunk(pos);
        }
        chunkRelevanceRegion.setUpToDate();
    }
}
Also used : BeforeChunkUnload(org.terasology.engine.world.chunks.event.BeforeChunkUnload) LightMerger(org.terasology.engine.world.propagation.light.LightMerger) Vector3ic(org.joml.Vector3ic) ChunkRelevanceRegion(org.terasology.engine.world.chunks.internal.ChunkRelevanceRegion) Vector3i(org.joml.Vector3i) PurgeWorldEvent(org.terasology.engine.world.chunks.event.PurgeWorldEvent) ChunkProcessingPipeline(org.terasology.engine.world.chunks.pipeline.ChunkProcessingPipeline) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 75 with Vector3ic

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

the class LocalChunkProvider method checkForUnload.

private void checkForUnload() {
    PerformanceMonitor.startActivity("Unloading irrelevant chunks");
    int unloaded = 0;
    Iterator<Vector3ic> iterator = Iterators.concat(Iterators.transform(chunkCache.keySet().iterator(), v -> new Vector3i(v.x(), v.y(), v.z())), loadingPipeline.getProcessingPosition().iterator());
    while (iterator.hasNext()) {
        Vector3ic pos = iterator.next();
        // TODO: move it to relevance system.
        boolean keep = relevanceSystem.isChunkInRegions(pos);
        if (!keep && unloadChunkInternal(pos)) {
            iterator.remove();
            if (++unloaded >= UNLOAD_PER_FRAME) {
                break;
            }
        }
    }
    if (unloaded > 0) {
        logger.debug("Unload {} chunks", unloaded);
    }
    PerformanceMonitor.endActivity();
}
Also used : ChunkImpl(org.terasology.engine.world.chunks.internal.ChunkImpl) TaskMaster(org.terasology.engine.utilities.concurrency.TaskMaster) Component(org.terasology.gestalt.entitysystem.component.Component) TIntArrayList(gnu.trove.list.array.TIntArrayList) LoggerFactory(org.slf4j.LoggerFactory) BlockRegionc(org.terasology.engine.world.block.BlockRegionc) OnAddedBlocks(org.terasology.engine.world.block.OnAddedBlocks) TShortObjectMap(gnu.trove.map.TShortObjectMap) Vector3i(org.joml.Vector3i) Map(java.util.Map) StorageManager(org.terasology.engine.persistence.StorageManager) TShortObjectHashMap(gnu.trove.map.hash.TShortObjectHashMap) OnChunkGenerated(org.terasology.engine.world.chunks.event.OnChunkGenerated) ChunkProcessingPipeline(org.terasology.engine.world.chunks.pipeline.ChunkProcessingPipeline) TIntList(gnu.trove.list.TIntList) ChunkViewCoreImpl(org.terasology.engine.world.internal.ChunkViewCoreImpl) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ChunkProvider(org.terasology.engine.world.chunks.ChunkProvider) OnChunkLoaded(org.terasology.engine.world.chunks.event.OnChunkLoaded) ChunkViewCore(org.terasology.engine.world.internal.ChunkViewCore) BlockingQueue(java.util.concurrent.BlockingQueue) Queues(com.google.common.collect.Queues) List(java.util.List) BeforeChunkUnload(org.terasology.engine.world.chunks.event.BeforeChunkUnload) ExtraBlockDataManager(org.terasology.engine.world.chunks.blockdata.ExtraBlockDataManager) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) ChunkStore(org.terasology.engine.persistence.ChunkStore) ChunkBlockIterator(org.terasology.engine.world.chunks.ChunkBlockIterator) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Iterators(com.google.common.collect.Iterators) Block(org.terasology.engine.world.block.Block) Lists(com.google.common.collect.Lists) Vector3ic(org.joml.Vector3ic) Chunk(org.terasology.engine.world.chunks.Chunk) WorldGenerator(org.terasology.engine.world.generator.WorldGenerator) OnActivatedBlocks(org.terasology.engine.world.block.OnActivatedBlocks) ChunkTaskProvider(org.terasology.engine.world.chunks.pipeline.stages.ChunkTaskProvider) Logger(org.slf4j.Logger) EntityBufferImpl(org.terasology.engine.world.generation.impl.EntityBufferImpl) Iterator(java.util.Iterator) PerformanceMonitor(org.terasology.engine.monitoring.PerformanceMonitor) LightMerger(org.terasology.engine.world.propagation.light.LightMerger) EntityManager(org.terasology.engine.entitySystem.entity.EntityManager) BeforeDeactivateBlocks(org.terasology.engine.world.block.BeforeDeactivateBlocks) InternalLightProcessor(org.terasology.engine.world.propagation.light.InternalLightProcessor) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) Consumer(java.util.function.Consumer) ChunkRelevanceRegion(org.terasology.engine.world.chunks.internal.ChunkRelevanceRegion) EntityStore(org.terasology.engine.entitySystem.entity.EntityStore) BlockManager(org.terasology.engine.world.block.BlockManager) BlockEntityRegistry(org.terasology.engine.world.BlockEntityRegistry) PurgeWorldEvent(org.terasology.engine.world.chunks.event.PurgeWorldEvent) ChunkMonitor(org.terasology.engine.monitoring.chunk.ChunkMonitor) Vector3ic(org.joml.Vector3ic) Vector3i(org.joml.Vector3i)

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