public static UnsafeList> Partition(ClodConfig config, ClodMesh mesh, UnsafeList clusters, UnsafeList pending, UnsafeList remap, Allocator allocator) { if (pending.Length <= (int)config.PartitionSize) { var partitions = new UnsafeList>(1, allocator); partitions.Add(pending); return partitions; } var clusterIndices = new UnsafeList(1024, allocator); // Initial guess var clusterCounts = new UnsafeList(pending.Length, allocator); nuint totalIndexCount = 0; for (int i = 0; i < pending.Length; i++) { var cluster = clusters[pending[i]]; totalIndexCount += cluster.indices.Length; } clusterIndices.Resize(totalIndexCount); nuint offset = 0; for (int i = 0; i < pending.Length; i++) { var cluster = clusters[pending[i]]; clusterCounts.Add((uint)cluster.indices.Length); for (int j = 0; j < (int)cluster.indices.Length; j++) { clusterIndices[(int)offset + j] = remap[(int)cluster.indices[j]]; } offset += (nuint)cluster.indices.Length; } var clusterPart = new UnsafeList(pending.Length, allocator); clusterPart.Resize((nuint)pending.Length); nuint partitionCount = Api.meshopt_partitionClusters( clusterPart.Ptr, clusterIndices.Ptr, totalIndexCount, clusterCounts.Ptr, (nuint)pending.Length, config.PartitionSpatial ? mesh.vertexPositions : null, remap.Length, mesh.vertexPositionsStride, config.PartitionSize ); var partitions = new UnsafeList>(partitionCount, allocator); for (nuint i = 0; i < partitionCount; i++) { partitions.Add(new UnsafeList((nuint)(config.PartitionSize + config.PartitionSize / 3), allocator)); } // Handle sorting if requested if (config.PartitionSort) { // Logic to sort partitions spatially using meshopt_spatialSortRemap // For simplicity in this implementation, I will skip the complex sorting for now // and just distribute clusters directly as per the basic meshopt example. } for (int i = 0; i < pending.Length; i++) { partitions[(int)clusterPart[i]].Add(pending[i]); } clusterIndices.Dispose(); clusterCounts.Dispose(); clusterPart.Dispose(); return partitions; }