Пример #1
0
    IEnumerator DelayBuildChunks()
    {
        while (toGenerate.Count > 0)
        {
            int4 cp = toGenerate[0];

            var blocks = new NativeArray <int3>(Chunk.BlocksCount, Allocator.TempJob);

            BuildChunkJob job = new BuildChunkJob()
            {
                chunkX = cp.x,
                chunkZ = cp.z,
                count  = Chunk.BlocksCount,
                width  = Chunk.Width,
                blocks = blocks
            };

            JobHandle handle = job.Schedule();

            yield return(new WaitForSeconds(0.4f));

            handle.Complete();
            BuildChunk(cp, blocks.ToArray());
            blocks.Dispose();
            if (toGenerate.Count > 0)
            {
                toGenerate.RemoveAt(0);
            }
        }
    }
    /// <summary>
    /// Method that calculate cubes, vertex and mesh in that order of a chunk.
    /// </summary>
    /// <param name="b"> data of the chunk</param>
    public Mesh BuildChunk(byte[] b)
    {
        BuildChunkJob buildChunkJob = new BuildChunkJob
        {
            chunkData   = new NativeArray <byte>(b, Allocator.TempJob),
            isoLevel    = this.isoLevel,
            interpolate = this.interpolate,
            vertex      = new NativeList <float3>(500, Allocator.TempJob),
            uv          = new NativeList <float2>(100, Allocator.TempJob),
        };
        JobHandle jobHandle = buildChunkJob.Schedule();

        jobHandle.Complete();

        //Get all the data from the jobs and use to generate a Mesh
        Mesh meshGenerated = new Mesh();

        Vector3[] meshVert      = new Vector3[buildChunkJob.vertex.Length];
        int[]     meshTriangles = new int[buildChunkJob.vertex.Length];
        for (int i = 0; i < buildChunkJob.vertex.Length; i++)
        {
            meshVert[i]      = buildChunkJob.vertex[i];
            meshTriangles[i] = i;
        }
        meshGenerated.vertices = meshVert;

        Vector2[] meshUV = new Vector2[buildChunkJob.vertex.Length];

        for (int i = 0; i < buildChunkJob.vertex.Length; i++)
        {
            meshUV[i] = buildChunkJob.uv[i];
        }
        meshGenerated.uv        = meshUV;
        meshGenerated.triangles = meshTriangles;
        meshGenerated.RecalculateNormals();
        meshGenerated.RecalculateTangents();

        //Dispose (Clear the jobs NativeLists)
        buildChunkJob.vertex.Dispose();
        buildChunkJob.uv.Dispose();
        buildChunkJob.chunkData.Dispose();

        return(meshGenerated);
    }