예제 #1
0
        public void Execute()
        {
            int nVoxels = Voxels.Length(0) * Voxels.Length(1) * Voxels.Length(2);
            int nCells  = (Voxels.Length(0) - 1) * (Voxels.Length(1) - 1) * (Voxels.Length(2) - 1);

            var MemoryCache = new NativeMemoryCache(Allocator.Temp);
            var DedupeCache = new VoxelMeshTessellation.NativeDeduplicationCache(Allocator.Temp);

            var Components = new NativeList <VoxelMeshComponent>(Allocator.Temp);
            var Indices    = new NativeList <PackedIndex>(Allocator.Temp);
            var Vertices   = new NativeList <VoxelMeshComponentVertex>(Allocator.Temp);

            var Materials     = new NativeArray <int>(8, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            var Intersections = new NativeArray <float>(12, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            var Normals       = new NativeArray <float3>(12, Allocator.Temp, NativeArrayOptions.UninitializedMemory);

            var solver = new SvdQefSolver <RawArrayVoxelCell>
            {
                Clamp = false
            };
            var polygonizer = new CMSVoxelPolygonizer <RawArrayVoxelCell, CMSProperties.DataStruct, SvdQefSolver <RawArrayVoxelCell>, IntersectionSharpFeatureSolver <RawArrayVoxelCell> >(PolygonizationProperties, solver, new IntersectionSharpFeatureSolver <RawArrayVoxelCell>(), MemoryCache);

            int xSize = Voxels.Length(0);
            int ySize = Voxels.Length(1);
            int zSize = Voxels.Length(2);

            TIndexer indexer = Voxels.Indexer;

            for (int index = 0; index < nVoxels; ++index)
            {
                int x = 0, y = 0, z = 0;
                indexer.FromIndex(index, ref x, ref y, ref z);

                if (x < xSize - 1 && y < ySize - 1 && z < zSize - 1 && FillCell(Voxels, x, y, z, 0, Materials, Intersections, Normals))
                {
                    //TODO Directly operate on voxel array
                    RawArrayVoxelCell cell = new RawArrayVoxelCell(0, new float3(x, y, z), Materials, Intersections, Normals);

                    polygonizer.Polygonize(cell, Components, Indices, Vertices);
                }
            }

            VoxelMeshTessellation.Tessellate(Components, Indices, Vertices, Matrix4x4.identity, MeshVertices, MeshTriangles, MeshNormals, MeshMaterials, new MaterialColors(), MeshColors, DedupeCache);

            MemoryCache.Dispose();
            DedupeCache.Dispose();

            Materials.Dispose();
            Intersections.Dispose();
            Normals.Dispose();

            Components.Dispose();
            Indices.Dispose();
            Vertices.Dispose();

            //Cells.Dispose();
        }
    private void GenerateMesh()
    {
        int size = (int)Mathf.Ceil(fieldSize * scale);

        /*field = new TestVoxelField(size, size, size);
         * GenerateScene(field);*/

        var dedupedTable = new Dictionary <int, List <VoxelMeshTessellation.DedupedVertex> >();

        var components        = new NativeList <VoxelMeshComponent>(Allocator.Persistent);
        var componentIndices  = new NativeList <PackedIndex>(Allocator.Persistent);
        var componentVertices = new NativeList <VoxelMeshComponentVertex>(Allocator.Persistent);

        int voxels = /*1;//*/ (size - 1) * (size - 1) * (size - 1);

        var cellMaterials     = new NativeArray <int>(voxels * 8, Allocator.Persistent);
        var cellIntersections = new NativeArray <float>(voxels * 12, Allocator.Persistent);
        var cellNormals       = new NativeArray <float3>(voxels * 12, Allocator.Persistent);

        var cells = new NativeArray <float3>(voxels, Allocator.Persistent);

        int voxelIndex = 0;

        for (int z = 0; z < size - 1; z++)
        {
            for (int y = 0; y < size - 1; y++)
            {
                for (int x = 0; x < size - 1; x++)
                {
                    if (renderLockedSelectionOnly && !(x == lockedSelection.x && y == lockedSelection.y && z == lockedSelection.z))
                    {
                        continue;
                    }

                    //field.FillCell(x, y, z, voxelIndex, cellMaterials, cellIntersections, cellNormals);

                    cells[voxelIndex] = new float3(x, y, z);

                    voxelIndex++;
                }
            }
        }

        NativeMemoryCache memoryCache = new NativeMemoryCache(Allocator.Persistent);

        VoxelMeshTessellation.NativeDeduplicationCache dedupeCache = new VoxelMeshTessellation.NativeDeduplicationCache(Allocator.Persistent);

        var meshVertices  = new NativeList <float3>(Allocator.Persistent);
        var meshNormals   = new NativeList <float3>(Allocator.Persistent);
        var meshTriangles = new NativeList <int>(Allocator.Persistent);
        var meshColors    = new NativeList <Color32>(Allocator.Persistent);
        var meshMaterials = new NativeList <int>(Allocator.Persistent);

        var polygonizerJob = new PolygonizeJob
        {
            Cells         = cells,
            MemoryCache   = memoryCache,
            Materials     = cellMaterials,
            Intersections = cellIntersections,
            Normals       = cellNormals,
            Components    = components,
            Indices       = componentIndices,
            Vertices      = componentVertices,
            MeshVertices  = meshVertices,
            MeshNormals   = meshNormals,
            MeshTriangles = meshTriangles,
            MeshColors    = meshColors,
            MeshMaterials = meshMaterials,
            DedupeCache   = dedupeCache
        };

        var watch = System.Diagnostics.Stopwatch.StartNew();

        polygonizerJob.Schedule().Complete();

        watch.Stop();

        string text = "Polygonized voxel field in " + watch.ElapsedMilliseconds + "ms. Vertices: " + meshVertices.Length + ". Run: " + run;

        Debug.Log(text);

        var cam = FindObjectOfType <Camera>();

        if (cam != null)
        {
            var display = cam.GetComponent <FPSDisplay>();
            if (display != null)
            {
                display.SetInfo(text);
            }
        }

        var vertices  = new List <Vector3>(meshVertices.Length);
        var indices   = new List <int>(meshTriangles.Length);
        var materials = new List <int>(meshMaterials.Length);
        var colors    = new List <Color32>(meshColors.Length);
        var normals   = new List <Vector3>(meshNormals.Length);

        for (int i = 0; i < meshVertices.Length; i++)
        {
            vertices.Add(meshVertices[i]);
        }
        for (int i = 0; i < meshTriangles.Length; i++)
        {
            indices.Add(meshTriangles[i]);
        }
        for (int i = 0; i < meshMaterials.Length; i++)
        {
            materials.Add(meshMaterials[i]);
        }
        for (int i = 0; i < meshColors.Length; i++)
        {
            colors.Add(meshColors[i]);
        }
        for (int i = 0; i < meshNormals.Length; i++)
        {
            normals.Add(meshNormals[i]);
        }

        dedupeCache.Dispose();

        meshVertices.Dispose();
        meshNormals.Dispose();
        meshTriangles.Dispose();
        meshColors.Dispose();
        meshMaterials.Dispose();

        memoryCache.Dispose();

        cells.Dispose();

        cellMaterials.Dispose();
        cellIntersections.Dispose();
        cellNormals.Dispose();

        components.Dispose();
        componentIndices.Dispose();
        componentVertices.Dispose();


        run++;

        voxelMesh.Clear(false);
        voxelMesh.SetVertices(vertices);
        voxelMesh.SetNormals(normals);
        voxelMesh.SetTriangles(indices, 0);
        if (colors.Count > 0)
        {
            voxelMesh.SetColors(colors);
        }

        if (meshCollider != null)
        {
            meshCollider.sharedMesh = voxelMesh;
        }
    }