BLaZeKiLL / VloxyEngine

Performance oriented voxel engine for unity.
MIT License
99 stars 9 forks source link

Regenerating dirty chunks #125

Open dexsper opened 4 months ago

dexsper commented 4 months ago

I wanted to build my game based on your library and add a block building system, but there is one problem: if I put a new block and then walk away from it so that it disappears from view and come back again, it will be completely regenerated

dexsper commented 4 months ago

My temporary solution is to add the following code inside ChunkManager, looks like a crutch but I don't see any other options yet

private readonly Dictionary<int3, Chunk> _unloadedDirtyChunks;

internal void AddChunks(NativeParallelHashMap<int3, Chunk> chunks)
{
    foreach (var pair in chunks)
    {
        var position = pair.Key;
        var chunk = pair.Value;

        if (_chunks.ContainsKey(chunk.Position))
            throw new InvalidOperationException($"Chunk {position} already exists");

        if (_queue.Count >= _chunkStoreSize)
        {
            int3 removePosition = _queue.Dequeue();

            if (!_chunks.TryGetValue(removePosition, out Chunk removeChunk))
                return;

            if (removeChunk.Dirty)
                _unloadedDirtyChunks.Add(removePosition, removeChunk);

            _chunks.Remove(removePosition);
        }

        if (_unloadedDirtyChunks.ContainsKey(position))
        {
            chunk = _unloadedDirtyChunks[position];
            _unloadedDirtyChunks.Remove(position);
        }

        _chunks.Add(position, chunk);
        _queue.Enqueue(position, -(position - _focus).SqrMagnitude());
    }
}
BLaZeKiLL commented 3 months ago

The proper way to fix this would be to add serialization support. Long ago, I had implemented binary serializers for the internal chunk data structure (I need to find that code again). My next immediate goal is an overhaul of the noise API, which will make the serialization support even more required as a performance improvement.