Example #1
0
        public void CreateMesh(
            out bool calculateNormals,
            bool requireNormals    = false,
            bool requireTangents   = false,
            int weightsAttributeId = -1,
            int jointsAttributeId  = -1
            )
        {
            Profiler.BeginSample("CreateMesh");

            var dracoMesh = (DracoMesh *)dracoTempResources[meshPtrIndex];

            allocator = dracoMesh->numVertices > persistentDataThreshold ? Allocator.Persistent : Allocator.TempJob;

            CalculateVertexParams(dracoMesh, requireNormals, requireTangents, weightsAttributeId, jointsAttributeId, out calculateNormals);

            Profiler.BeginSample("SetParameters");
#if DRACO_MESH_DATA
            indicesCount = dracoMesh->numFaces * 3;
#else
            mesh = new Mesh();
#endif
            mesh.SetIndexBufferParams(dracoMesh->numFaces * 3, IndexFormat.UInt32);
            var vertexParams = new List <VertexAttributeDescriptor>(attributes.Count);
            foreach (var map in attributes)
            {
                vertexParams.Add(map.GetVertexAttributeDescriptor());
            }
            mesh.SetVertexBufferParams(dracoMesh->numVertices, vertexParams.ToArray());
#if !DRACO_MESH_DATA
            AllocateIndices(dracoMesh);
            AllocateVertexBuffers(dracoMesh);
#endif
            Profiler.EndSample(); // SetParameters
            Profiler.EndSample(); // CreateMesh
        }
Example #2
0
    public void CombineMesh(ChunkMeshData chunkMeshData)
    {
        //获取输出meshData
        Mesh.MeshDataArray outMeshDataArray = Mesh.AllocateWritableMeshData(1);
        Mesh.MeshData      outMesh          = outMeshDataArray[0];

        Mesh.MeshDataArray outMeshDataArrayCollider = Mesh.AllocateWritableMeshData(1);
        Mesh.MeshData      outMeshCollider          = outMeshDataArrayCollider[0];

        Mesh.MeshDataArray outMeshDataArrayTrigger = Mesh.AllocateWritableMeshData(1);
        Mesh.MeshData      outMeshTrigger          = outMeshDataArrayTrigger[0];

        int subMeshCount      = 0;
        int subMeshIndexCount = 0;

        List <int> trisDataAll = new List <int>();
        List <SubMeshDescriptor> listSubMeshDescriptor = new List <SubMeshDescriptor>();

        for (int i = 0; i < chunkMeshData.dicTris.Length; i++)
        {
            List <int> trisData = chunkMeshData.dicTris[i];
            if (trisData.IsNull())
            {
                continue;
            }
            trisDataAll.AddRange(trisData);
            SubMeshDescriptor subMeshDesc = new SubMeshDescriptor
            {
                indexStart = subMeshIndexCount,
                indexCount = trisData.Count
            };
            listSubMeshDescriptor.Add(subMeshDesc);
            subMeshCount++;
            subMeshIndexCount += trisData.Count;
        }

        VertexStruct[] listVertex = chunkMeshData.GetVertexStruct();
        outMesh.SetVertexBufferParams(listVertex.Length, vertexAttributeDescriptors);

        VertexStruct[] listVertexCollider = chunkMeshData.GetVertexStructCollider();
        outMeshCollider.SetVertexBufferParams(listVertexCollider.Length, vertexAttributeDescriptors);

        VertexStruct[] listVertexTrigger = chunkMeshData.GetVertexStructTrigger();
        outMeshTrigger.SetVertexBufferParams(listVertexTrigger.Length, vertexAttributeDescriptors);

        //获取点信息
        NativeArray <VertexStruct> vertexData         = outMesh.GetVertexData <VertexStruct>();
        NativeArray <VertexStruct> vertexDataCollider = outMeshCollider.GetVertexData <VertexStruct>();
        NativeArray <VertexStruct> vertexDataTrigger  = outMeshTrigger.GetVertexData <VertexStruct>();

        //设置点信息
        NativeArray <VertexStruct> .Copy(listVertex, vertexData);

        NativeArray <VertexStruct> .Copy(listVertexCollider, vertexDataCollider);

        NativeArray <VertexStruct> .Copy(listVertexTrigger, vertexDataTrigger);

        //设置三角数量
        outMesh.SetIndexBufferParams(trisDataAll.Count, IndexFormat.UInt32);
        outMeshCollider.SetIndexBufferParams(chunkMeshData.trisCollider.Count, IndexFormat.UInt32);
        outMeshTrigger.SetIndexBufferParams(chunkMeshData.trisTrigger.Count, IndexFormat.UInt32);
        //获取三角下标
        NativeArray <int> triangelData         = outMesh.GetIndexData <int>();
        NativeArray <int> triangelDataCollider = outMeshCollider.GetIndexData <int>();
        NativeArray <int> triangelDataTrigger  = outMeshTrigger.GetIndexData <int>();

        NativeArray <int> .Copy(trisDataAll.ToArray(), triangelData);

        NativeArray <int> .Copy(chunkMeshData.trisCollider.ToArray(), triangelDataCollider);

        NativeArray <int> .Copy(chunkMeshData.trisTrigger.ToArray(), triangelDataTrigger);

        outMesh.subMeshCount         = subMeshCount;
        outMeshCollider.subMeshCount = 1;
        outMeshTrigger.subMeshCount  = 1;

        for (int i = 0; i < listSubMeshDescriptor.Count; i++)
        {
            outMesh.SetSubMesh(i, listSubMeshDescriptor[i]);
        }
        outMeshCollider.SetSubMesh(0, new SubMeshDescriptor
        {
            indexStart = 0,
            indexCount = chunkMeshData.trisCollider.Count
        });
        outMeshTrigger.SetSubMesh(0, new SubMeshDescriptor
        {
            indexStart = 0,
            indexCount = chunkMeshData.trisTrigger.Count
        });

        Mesh.ApplyAndDisposeWritableMeshData(outMeshDataArray, chunkMesh);
        Mesh.ApplyAndDisposeWritableMeshData(outMeshDataArrayCollider, chunkMeshCollider);
        Mesh.ApplyAndDisposeWritableMeshData(outMeshDataArrayTrigger, chunkMeshTrigger);

        chunkMesh.RecalculateNormals();
        chunkMesh.RecalculateBounds();

        //chunkMeshCollider.RecalculateNormals();
        //chunkMeshCollider.RecalculateBounds();

        //chunkMeshTrigger.RecalculateNormals();
        //chunkMeshTrigger.RecalculateBounds();

        vertexData.Dispose();
        triangelData.Dispose();

        vertexDataCollider.Dispose();
        triangelDataCollider.Dispose();

        vertexDataTrigger.Dispose();
        triangelDataTrigger.Dispose();
    }