Hello everyone,
I started to work on a voxel planet game.(Octree structure and Dual contoruing algorithm)
But i don’t no what is the best way to generate Octree nodes?
In my first prototype i created a IJob which create nodes via recursive way.
struct DistanceCheck : IJob
{
public int maxLodLevel;
public float radius;
public NativeHashMap<float3, OctreeNode2> activeNodesIndex;
public float3 playerPosition;
private void CheckClosesNodes(int index, float3 position, float3 voxelPosition, byte lodLevel, float size)
{
float diagonal = Mathf.Sqrt(2 * (size * size));
if (lodLevel < maxLodLevel && Distance(playerPosition, position) < diagonal)
{
float delta = size / 4;
for (int i = 0; i < 8; i++)
{
float3 voxelPosition2 = voxelPosition + (GetVoxelBase(i) * size / 2);
CheckClosesNodes(i, GetPosition(i, position, delta), voxelPosition2, (byte)(lodLevel + 1), size / 2);
}
}
else
{
OctreeNode node = new OctreeNode(position, voxelPosition, size, lodLevel);
activeNodesIndex.TryAdd(position, node);
}
}
public void Execute()
{
CheckClosesNodes(0, new float3(radius / 2, radius / 2, radius / 2), float3.zero, 0, radius);
}
}[
And save this nodes as struct:
public struct OctreeNode
{
// node value
public float3 position;
public float3 voxelPosition;
public float size;
public byte lodLevel;
public OctreeNode
(
float3 position,
float3 voxelPosition,
float size,
byte lodLevel
)
{
this.position = position;
this.voxelPosition = voxelPosition;
this.size = size;
this.lodLevel = lodLevel;
}
}
I call this job from Update methode.
I used this solution because the player is moving and i need the closest node every time.
But i dont feel it is the best solution to call the job every seconds and create octree nodes from 0 to max lod level again and again.
I think it is the core of my project i would be happy if somebody share experience about DOTS and octree.
I used C# task parallel library and there was i lots of crashs thats way I started to change tasks to job system