BLaZeKiLL / VloxyEngine

Performance oriented voxel engine for unity.
MIT License
93 stars 10 forks source link

Runtime API #93

Closed BLaZeKiLL closed 9 months ago

BLaZeKiLL commented 1 year ago

Addition and removal of voxel, batched modification of internal voxel data

AldeRoberge commented 1 year ago

I tried adding a Set block, but didn't go very far.

image

I wrote this method, but I doubt it is of any help...

For UnsafeIntervalTree.cs

public void Set(int index, int id)
        {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
            if (index >= Length) throw new IndexOutOfRangeException($"{index} is out of range for the given data of length {Length}");
#endif

            // Find the interval that contains the index
            int intervalIndex = BinarySearch(index);
            Node interval = Internal[intervalIndex];

            // Check if the index is at the start or end of the interval
            bool atStart = interval.Count - index == 1;
            bool atEnd = intervalIndex == 0 || index - Internal[intervalIndex - 1].Count == 0;

            // If at the start or end, we can simply adjust the ID
            if (atStart || atEnd)
            {
                interval.ID = id;
                Internal[intervalIndex] = interval;
                return;
            }

            // Otherwise, we need to split the interval into two
            Node newInterval = new Node(id, index + 1);

            // Adjust the count of the original interval
            interval.Count -= newInterval.Count;
            Internal[intervalIndex] = interval;

            // Create a new UnsafeList to hold the adjusted intervals
            UnsafeList<Node> newInternal = new UnsafeList<Node>(Internal.Length + 1, Internal.Allocator);

            // Copy the intervals up to the insert point
            for (int i = 0; i < intervalIndex; i++)
            {
                newInternal.Add(Internal[i]);
            }

            // Add the new interval
            newInternal.Add(newInterval);

            // Copy the remaining intervals, adjusting their counts
            for (int i = intervalIndex; i < Internal.Length; i++)
            {
                Node nextInterval = Internal[i];
                nextInterval.Count -= newInterval.Count;
                newInternal.Add(nextInterval);
            }

            // Dispose of the old UnsafeList and replace it with the new one
            Internal.Dispose();
            Internal = newInternal;

            // Adjust the length of the tree
            Length += newInterval.Count;
        }

Maybe we can get inspired by : https://github.com/bbtarzan12/Unity-Procedural-Voxel-Terrain/blob/master/Assets/Scripts/Chunk.cs#L176

Sets "dirty" to true, the rest is handled by the scheduler.

BLaZeKiLL commented 9 months ago

First of all, I have been away from this project for a while, Homework has been piling on a lot :D

Thanks I managed to get this working Updating the native data structure was a nightmare, but I have written unit tests for the possible scenarios of updates I could think of.

Need to figure out a nice way to re-build the mesh will take a look at the "dirty" logic.

Here's the set block in action, after generation of chunk data I just use Set block to set a 4x4x4 center of each chunk to stone. Image

AldeRoberge commented 9 months ago

Really cool! Congrats. I wish this project gets more attention and momentum.

In the meantime, I have moved to Kronnect's Voxel Play 2, but this project was my second best choice.