Search in sources :

Example 1 with ChunkMesh

use of org.terasology.engine.rendering.primitives.ChunkMesh in project Terasology by MovingBlocks.

the class ChunkMeshTypeHandler method deserialize.

@Override
public Optional<ChunkMesh> deserialize(PersistedData data) {
    List<ByteBuffer> asBuffers = new ArrayList<>();
    for (PersistedData datum : data.getAsArray()) {
        ByteBuffer buffer = datum.getAsByteBuffer();
        ByteBuffer directBuffer = BufferUtils.createByteBuffer(buffer.limit());
        directBuffer.put(buffer);
        directBuffer.rewind();
        asBuffers.add(directBuffer);
    }
    ChunkMesh result = new ChunkMeshImpl();
    for (ChunkMesh.RenderType renderType : ChunkMesh.RenderType.values()) {
        result.getVertexElements(renderType).buffer.replace(asBuffers.remove(0));
        result.getVertexElements(renderType).indices.replace(asBuffers.remove(0));
    }
    result.updateMesh();
    return Optional.of(result);
}
Also used : ChunkMesh(org.terasology.engine.rendering.primitives.ChunkMesh) ChunkMeshImpl(org.terasology.engine.rendering.primitives.ChunkMeshImpl) ArrayList(java.util.ArrayList) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ChunkMesh

use of org.terasology.engine.rendering.primitives.ChunkMesh in project Terasology by MovingBlocks.

the class RenderableWorldImpl method pregenerateChunks.

/**
 * @return true if pregeneration is complete
 */
@Override
public boolean pregenerateChunks() {
    boolean pregenerationIsComplete = true;
    chunkProvider.update();
    Chunk chunk;
    ChunkMesh newMesh;
    ChunkView localView;
    for (Vector3ic chunkCoordinates : calculateRenderableRegion(renderingConfig.getViewDistance())) {
        chunk = chunkProvider.getChunk(chunkCoordinates);
        if (chunk == null) {
            pregenerationIsComplete = false;
        } else if (chunk.isDirty()) {
            localView = worldProvider.getLocalView(chunkCoordinates);
            if (localView == null) {
                continue;
            }
            chunk.setDirty(false);
            newMesh = chunkTessellator.generateMesh(localView);
            newMesh.updateMesh();
            newMesh.discardData();
            if (chunk.hasMesh()) {
                chunk.getMesh().dispose();
            }
            chunk.setMesh(newMesh);
            pregenerationIsComplete = false;
            break;
        }
    }
    return pregenerationIsComplete;
}
Also used : ChunkMesh(org.terasology.engine.rendering.primitives.ChunkMesh) Vector3ic(org.joml.Vector3ic) RenderableChunk(org.terasology.engine.world.chunks.RenderableChunk) Chunk(org.terasology.engine.world.chunks.Chunk) ChunkView(org.terasology.engine.world.ChunkView)

Example 3 with ChunkMesh

use of org.terasology.engine.rendering.primitives.ChunkMesh 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 4 with ChunkMesh

use of org.terasology.engine.rendering.primitives.ChunkMesh in project Terasology by MovingBlocks.

the class RenderableWorldImpl method queueVisibleChunks.

/**
 * Updates the currently visible chunks (in sight of the player).
 */
@Override
public int queueVisibleChunks(boolean isFirstRenderingStageForCurrentFrame) {
    PerformanceMonitor.startActivity("Queueing Visible Chunks");
    statDirtyChunks = 0;
    statVisibleChunks = 0;
    statIgnoredPhases = 0;
    int processedChunks = 0;
    int chunkCounter = 0;
    renderQueues.clear();
    ChunkMesh mesh;
    boolean isDynamicShadows = renderingConfig.isDynamicShadows();
    int billboardLimit = (int) renderingConfig.getBillboardLimit();
    List<RenderableChunk> allChunks = new ArrayList<>(chunksInProximityOfCamera);
    allChunks.addAll(chunkMeshRenderer.getRenderableChunks());
    if (lodChunkProvider != null) {
        lodChunkProvider.addAllChunks(allChunks);
    }
    for (RenderableChunk chunk : allChunks) {
        if (isChunkValidForRender(chunk)) {
            mesh = chunk.getMesh();
            if (isDynamicShadows && isFirstRenderingStageForCurrentFrame && chunkCounter < maxChunksForShadows && isChunkVisibleFromMainLight(chunk)) {
                if (triangleCount(mesh, ChunkMesh.RenderPhase.OPAQUE) > 0) {
                    renderQueues.chunksOpaqueShadow.add(chunk);
                } else {
                    statIgnoredPhases++;
                }
            }
            if (isChunkVisible(chunk)) {
                if (triangleCount(mesh, ChunkMesh.RenderPhase.OPAQUE) > 0) {
                    renderQueues.chunksOpaque.add(chunk);
                } else {
                    statIgnoredPhases++;
                }
                if (triangleCount(mesh, ChunkMesh.RenderPhase.REFRACTIVE) > 0) {
                    renderQueues.chunksAlphaBlend.add(chunk);
                } else {
                    statIgnoredPhases++;
                }
                if (triangleCount(mesh, ChunkMesh.RenderPhase.ALPHA_REJECT) > 0 && (billboardLimit == 0 || chunkCounter < billboardLimit)) {
                    renderQueues.chunksAlphaReject.add(chunk);
                } else {
                    statIgnoredPhases++;
                }
                statVisibleChunks++;
                chunk.setAnimated(statVisibleChunks < MAX_ANIMATED_CHUNKS);
            }
            if (isChunkVisibleReflection(chunk)) {
                renderQueues.chunksOpaqueReflection.add(chunk);
            }
        }
        chunkCounter++;
    }
    if (isFirstRenderingStageForCurrentFrame) {
        for (Chunk chunk : chunksInProximityOfCamera) {
            if (isChunkValidForRender(chunk) && (chunk.isDirty() || !chunk.hasMesh())) {
                statDirtyChunks++;
                chunkMeshUpdateManager.queueChunkUpdate(chunk);
                processedChunks++;
            }
        }
    }
    PerformanceMonitor.endActivity();
    return processedChunks;
}
Also used : ChunkMesh(org.terasology.engine.rendering.primitives.ChunkMesh) RenderableChunk(org.terasology.engine.world.chunks.RenderableChunk) ArrayList(java.util.ArrayList) RenderableChunk(org.terasology.engine.world.chunks.RenderableChunk) Chunk(org.terasology.engine.world.chunks.Chunk)

Example 5 with ChunkMesh

use of org.terasology.engine.rendering.primitives.ChunkMesh in project Terasology by MovingBlocks.

the class RenderableWorldImpl method generateVBOs.

@Override
public void generateVBOs() {
    PerformanceMonitor.startActivity("Building Mesh VBOs");
    ChunkMesh pendingMesh;
    chunkMeshUpdateManager.setCameraPosition(playerCamera.getPosition());
    for (Chunk chunk : chunkMeshUpdateManager.availableChunksForUpdate()) {
        if (chunk.hasPendingMesh() && chunksInProximityOfCamera.contains(chunk)) {
            pendingMesh = chunk.getPendingMesh();
            pendingMesh.updateMesh();
            pendingMesh.discardData();
            if (chunk.hasMesh()) {
                chunk.getMesh().dispose();
            }
            chunk.setMesh(pendingMesh);
            chunk.setPendingMesh(null);
        } else {
            if (chunk.hasPendingMesh()) {
                chunk.getPendingMesh().dispose();
                chunk.setPendingMesh(null);
            }
        }
    }
    PerformanceMonitor.endActivity();
}
Also used : ChunkMesh(org.terasology.engine.rendering.primitives.ChunkMesh) RenderableChunk(org.terasology.engine.world.chunks.RenderableChunk) Chunk(org.terasology.engine.world.chunks.Chunk)

Aggregations

ChunkMesh (org.terasology.engine.rendering.primitives.ChunkMesh)6 Chunk (org.terasology.engine.world.chunks.Chunk)3 RenderableChunk (org.terasology.engine.world.chunks.RenderableChunk)3 ArrayList (java.util.ArrayList)2 Vector3ic (org.joml.Vector3ic)2 ChunkView (org.terasology.engine.world.ChunkView)2 ByteBuffer (java.nio.ByteBuffer)1 Vector2f (org.joml.Vector2f)1 Vector3f (org.joml.Vector3f)1 Vector3i (org.joml.Vector3i)1 ChunkMeshImpl (org.terasology.engine.rendering.primitives.ChunkMeshImpl)1 Block (org.terasology.engine.world.block.Block)1 BlockRegion (org.terasology.engine.world.block.BlockRegion)1 PreLodChunk (org.terasology.engine.world.chunks.internal.PreLodChunk)1 ChunkViewCoreImpl (org.terasology.engine.world.internal.ChunkViewCoreImpl)1 PersistedData (org.terasology.persistence.typeHandling.PersistedData)1