Esempio n. 1
0
    public void Execute()
    {
        int vertIndex = 0;
        int triIndex  = 0;

        x = -1;
        for (int i = 0; i < blockData.Length; i++)
        {
            x = x + 1;
            // convert the straight array to xyz coords  // maybe not necessary
            if (x == sizex)
            {
                x = 0;
                y = y + 1;

                if (y == sizey)
                {
                    y = 0;
                    z = z + 1;
                }
            }



            BlockData block = blockData[i];


            if (!block.isVisible)
            {
                continue;
            }

            for (int face = 0; face < 6; face++)
            {
                if (!isNeighborSolid(blockData, i, face))
                {
                    // add the face to the mesh

                    ChunkMeshVertexData vertsout0 = verts[vertIndex];
                    ChunkMeshVertexData vertsout1 = verts[vertIndex + 1];
                    ChunkMeshVertexData vertsout2 = verts[vertIndex + 2];
                    ChunkMeshVertexData vertsout3 = verts[vertIndex + 3];

                    vertsout0.pos = _vertices[_faceVertices[face, 0]] + new Vector3(x, y, z);
                    vertsout1.pos = _vertices[_faceVertices[face, 1]] + new Vector3(x, y, z);
                    vertsout2.pos = _vertices[_faceVertices[face, 2]] + new Vector3(x, y, z);
                    vertsout3.pos = _vertices[_faceVertices[face, 3]] + new Vector3(x, y, z);

                    vertsout0.normal = _normals[face];
                    vertsout1.normal = _normals[face];
                    vertsout2.normal = _normals[face];
                    vertsout3.normal = _normals[face];


                    vertsout0.uv = new Vector2(0, 0);
                    vertsout1.uv = new Vector2(0, 1);
                    vertsout2.uv = new Vector2(1, 1);
                    vertsout3.uv = new Vector2(1, 0);

                    verts[vertIndex]     = vertsout0;
                    verts[vertIndex + 1] = vertsout1;
                    verts[vertIndex + 2] = vertsout2;
                    verts[vertIndex + 3] = vertsout3;

                    tris[triIndex]     = _triangleVertices[face, 0] + vertIndex;
                    tris[triIndex + 1] = _triangleVertices[face, 1] + vertIndex;
                    tris[triIndex + 2] = _triangleVertices[face, 2] + vertIndex;
                    tris[triIndex + 3] = _triangleVertices[face, 3] + vertIndex;
                    tris[triIndex + 4] = _triangleVertices[face, 4] + vertIndex;
                    tris[triIndex + 5] = _triangleVertices[face, 5] + vertIndex;

                    vertIndex += 4;
                    triIndex  += 6;
                }
            }
        }

        counts[0] = vertIndex;
        counts[1] = triIndex;
    }
Esempio n. 2
0
    public Mesh BuildMeshOld()
    {
        marker.Begin();


        // get all the faces

        List <Vector3> vertices  = new List <Vector3>();
        List <int>     triangles = new List <int>();
        List <Vector2> uvs       = new List <Vector2>();
        List <Vector3> normals   = new List <Vector3>();

        int vertexCount = 0;

        for (int x = 0; x < chunkWidth; x++)
        {
            for (int y = 0; y < chunkHeight; y++)
            {
                for (int z = 0; z < chunkWidth; z++)
                {
                    Block block = chunkBlocks[x, y, z];

                    if (block == null)
                    {
                        continue;
                    }

                    int newVertices = block.addVoxelMeshData(vertices, triangles, uvs, normals, vertexCount);

                    vertexCount += newVertices;
                }
            }
        }



        NativeArray <int> returnCounts = new NativeArray <int>(2, Allocator.TempJob);

        MeshCreateJob job = new MeshCreateJob();

        job.blockData = blockData;
        job.verts     = meshVertices;
        job.tris      = triVerts;
        job.sizex     = chunkWidth;
        job.sizey     = chunkHeight;
        job.sizez     = chunkWidth;
        job.counts    = returnCounts;
        JobHandle handle = job.Schedule();

        handle.Complete();



        int vertCount = job.counts[0];
        int triCount  = job.counts[1];

        returnCounts.Dispose();


        marker.End();


        Mesh mesh = new Mesh();

        mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;

        var layout = new[]
        {
            new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.Normal, VertexAttributeFormat.Float32, 3),
            new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 2)
        };

        if (true)
        {
            mesh.SetVertexBufferParams(vertCount, layout);

            mesh.SetVertexBufferData(job.verts, 0, 0, vertCount);

            int[] trilist = new int[triCount];
            for (int t = 0; t < triCount; t++)
            {
                trilist[t] = job.tris[t];
            }

            mesh.triangles = trilist;
        }
        else
        {
            if (true)
            {
                Vector3[] verts = new Vector3[vertCount];
                Vector3[] norms = new Vector3[vertCount];
                Vector2[] uv    = new Vector2[vertCount];



                for (int i = 0; i < vertCount; i++)
                {
                    ChunkMeshVertexData data = job.verts[i];

                    verts[i] = data.pos;
                    norms[i] = data.normal;
                    uv[i]    = data.uv;
                }

                mesh.vertices = verts;
                mesh.normals  = norms;
                mesh.uv       = uv;


                int[] trilist = new int[triCount];
                for (int t = 0; t < triCount; t++)
                {
                    trilist[t] = job.tris[t];
                }

                mesh.triangles = trilist;
            }
            else
            {
                mesh.vertices  = vertices.ToArray();
                mesh.triangles = triangles.ToArray();
                mesh.uv        = uvs.ToArray();
                mesh.normals   = normals.ToArray();
            }
        }


        mesh.RecalculateBounds();
        return(mesh);
    }