Ejemplo n.º 1
0
 public void Execute(int index)
 {
     ClusterFunctions.SetPointToVoxel(fragments[index], allPoint, voxelFragments, leftPoint, distance);
 }
Ejemplo n.º 2
0
        /// <returns></returns> Cluster Count
        public static int GenerateCluster(NativeList <Point> pointsFromMesh, NativeList <int> triangles, Bounds bd, string fileName)
        {
            List <Vector4Int> value = ClusterFunctions.AddTrianglesToDictionary(triangles);
            GetFragmentJob    gt    = new GetFragmentJob(bd.center, bd.extents, pointsFromMesh, value);

            (gt.Schedule(value.Count, 16)).Complete();
            var        voxel      = GetFragmentJob.voxelFragments;
            const int  VoxelCount = ClusterFunctions.VoxelCount;
            Fragment   first      = new Fragment();
            Vector3Int inputPoint = new Vector3Int();

            for (int i = 0; i < VoxelCount; ++i)
            {
                for (int j = 0; j < VoxelCount; ++j)
                {
                    for (int k = 0; k < VoxelCount; ++k)
                    {
                        var lst = voxel[i, j, k];
                        if (lst.Count > 0)
                        {
                            first      = lst[0];
                            inputPoint = new Vector3Int(i, j, k);
                            goto BREAK;
                        }
                    }
                }
            }
BREAK:
            NativeArray <Fragment> resultCluster;
            List <NativeArray <Fragment> > allFragments = new List <NativeArray <Fragment> >();

LOOP:
            bool stillLooping = ClusterFunctions.GetFragFromVoxel(voxel, first.position, inputPoint, out resultCluster);
            List <Vector3> vertices = new List <Vector3>(16 * 6);
            List <Vector3> normals  = new List <Vector3>(16 * 6);

            if (stillLooping)
            {
                first = resultCluster[resultCluster.Length - 1];
                allFragments.Add(resultCluster);
                goto LOOP;
            }
            if (resultCluster.Length < (ClusterFunctions.ClusterCount + 1))
            {
                NativeArray <Fragment> lastResultCluster = new NativeArray <Fragment>(ClusterFunctions.ClusterCount + 1, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
                for (int i = 0; i < resultCluster.Length; ++i)
                {
                    lastResultCluster[i] = resultCluster[i];
                }
                for (int i = resultCluster.Length; i < lastResultCluster.Length; ++i)
                {
                    Fragment fg;
                    fg.indices           = new Vector4Int(0, 0, 0, 0);
                    fg.position          = Vector3.zero;
                    fg.voxel             = Vector3Int.zero;
                    lastResultCluster[i] = fg;
                }
                resultCluster.Dispose();
                resultCluster = lastResultCluster;
                allFragments.Add(resultCluster);
            }
            gt.Dispose();
            NativeArray <ClusterMeshData> meshData = new NativeArray <ClusterMeshData>(allFragments.Count, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            NativeArray <Point>[] allpoints = new NativeArray <Point> [allFragments.Count];
            int pointCount = 0;

            for (int i = 0; i < allpoints.Length; ++i)
            {
                allpoints[i] = new NativeArray <Point>((allFragments[i].Length - 1) * 4, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
                pointCount  += allpoints[i].Length;
            }
            CollectJob.meshDatas      = meshData;
            CollectJob.allFragments   = allFragments;
            CollectJob.pointsArray    = allpoints;
            CollectJob.pointsFromMesh = pointsFromMesh;
            CollectJob jb = new CollectJob();

            jb.Schedule(allFragments.Count, 1).Complete();
            CollectJob.allFragments = null;
            CollectJob.pointsArray  = null;
            NativeArray <Point> pointsList = new NativeArray <Point>(pointCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            for (int i = 0, pointsCount = 0; i < allpoints.Length; ++i)
            {
                var current = allpoints[i];
                for (int a = 0; a < current.Length; ++a)
                {
                    pointsList[pointsCount] = current[a];
                    pointsCount++;
                }
                current.Dispose();
            }
            byte[] meshDataArray;
            byte[] pointDataArray;
            ClusterFunctions.GetByteDataFromArray(meshData, pointsList, out meshDataArray, out pointDataArray);
            int    len = meshData.Length;
            string filenameWithExtent = fileName + ".txt";

            File.WriteAllBytes("Assets/Resources/MapInfos/" + filenameWithExtent, meshDataArray);
            File.WriteAllBytes("Assets/Resources/MapPoints/" + filenameWithExtent, pointDataArray);
            //Dispose Native Array
            meshData.Dispose();
            pointsList.Dispose();
            pointsFromMesh.Dispose();
            triangles.Dispose();
            foreach (var i in allFragments)
            {
                i.Dispose();
            }
            return(len);
        }