Example #1
0
        PopulateMeshData()
        {
            Profiler.BeginSample("PopulateMeshData");

            foreach (var map in attributes)
            {
                map.Dispose();
            }
            attributes = null;

            Profiler.BeginSample("MeshAssign");

            const MeshUpdateFlags flags = DracoMeshLoader.defaultMeshUpdateFlags;

#if !DRACO_MESH_DATA
            for (var streamIndex = 0; streamIndex < streamCount; streamIndex++)
            {
                mesh.SetVertexBufferData(vData[streamIndex], 0, 0, vData[streamIndex].Length, streamIndex, flags);
            }

            mesh.SetIndexBufferData(indices, 0, 0, indices.Length);
            var indicesCount = indices.Length;
#endif

            mesh.subMeshCount = 1;
            var submeshDescriptor = new SubMeshDescriptor(0, indicesCount)
            {
                firstVertex = 0, baseVertex = 0, vertexCount = mesh.vertexCount
            };
            mesh.SetSubMesh(0, submeshDescriptor, flags);
            Profiler.EndSample(); // CreateUnityMesh.CreateMesh

#if DRACO_MESH_DATA
#else
            Profiler.BeginSample("Dispose");
            indices.Dispose();
            foreach (var nativeArray in vData)
            {
                nativeArray.Dispose();
            }
            Profiler.EndSample();
#endif
            Profiler.EndSample();

#if DRACO_MESH_DATA
            return(true);
#else
            return(mesh);
#endif
        }
            public void Execute()
            {
                var subMeshesArray = this.subMeshes[contentsIndex].AsArray();
                var positionsArray = this.positions[contentsIndex].AsArray();
                var indicesArray   = this.indices[contentsIndex].AsArray();
                var normalsArray   = this.normals[contentsIndex].AsArray();
                var tangentsArray  = this.tangents[contentsIndex].AsArray();
                var uv0Array       = this.uv0[contentsIndex].AsArray();

                var dstPositions = data.GetVertexData <float3>(stream: 0);

                dstPositions.CopyFrom(positionsArray);

                var dstTexCoord0 = data.GetVertexData <float2>(stream: 1);

                dstTexCoord0.CopyFrom(uv0Array);

                var dstNormals = data.GetVertexData <float3>(stream: 2);

                dstNormals.CopyFrom(normalsArray);

                var dstTangents = data.GetVertexData <float4>(stream: 3);

                dstTangents.CopyFrom(tangentsArray);

                var dstIndices = data.GetIndexData <int>();

                dstIndices.CopyFrom(indicesArray);

                data.subMeshCount = subMeshesArray.Length;
                for (int i = 0; i < subMeshesArray.Length; i++)
                {
                    var srcBounds = subMeshesArray[i].bounds;
                    var center    = (Vector3)((srcBounds.Max + srcBounds.Min) * 0.5f);
                    var size      = (Vector3)(srcBounds.Max - srcBounds.Min);
                    var dstBounds = new Bounds(center, size);
                    data.SetSubMesh(i, new SubMeshDescriptor
                    {
                        baseVertex  = subMeshesArray[i].baseVertex,
                        firstVertex = 0,
                        vertexCount = subMeshesArray[i].vertexCount,
                        indexStart  = subMeshesArray[i].baseIndex,
                        indexCount  = subMeshesArray[i].indexCount,
                        bounds      = dstBounds,
                        topology    = UnityEngine.MeshTopology.Triangles,
                    }, MeshUpdateFlags.DontRecalculateBounds);
                }
            }
            public void Execute()
            {
                if (!this.subMeshes.IsIndexCreated(contentsIndex) ||
                    !this.positions.IsIndexCreated(contentsIndex) ||
                    !this.indices.IsIndexCreated(contentsIndex))
                {
                    data.subMeshCount = 0;
                    return;
                }

                var subMeshesArray = this.subMeshes[contentsIndex].AsArray();
                var positionsArray = this.positions[contentsIndex].AsArray();
                var indicesArray   = this.indices[contentsIndex].AsArray();

                var dstPositions = data.GetVertexData <float3>(stream: 0);

                dstPositions.CopyFrom(positionsArray);

                var dstIndices = data.GetIndexData <int>();

                dstIndices.CopyFrom(indicesArray);

                data.subMeshCount = subMeshesArray.Length;
                for (int i = 0; i < subMeshesArray.Length; i++)
                {
                    var srcBounds = subMeshesArray[i].bounds;
                    var center    = (Vector3)((srcBounds.Max + srcBounds.Min) * 0.5f);
                    var size      = (Vector3)(srcBounds.Max - srcBounds.Min);
                    var dstBounds = new Bounds(center, size);

                    data.SetSubMesh(i, new SubMeshDescriptor
                    {
                        baseVertex  = subMeshesArray[i].baseVertex,
                        firstVertex = 0,
                        vertexCount = subMeshesArray[i].vertexCount,
                        indexStart  = subMeshesArray[i].baseIndex,
                        indexCount  = subMeshesArray[i].indexCount,
                        bounds      = dstBounds,
                        topology    = UnityEngine.MeshTopology.Triangles,
                    }, MeshUpdateFlags.DontRecalculateBounds);
                }

                // TODO: Figure out why sometimes setting a mesh on a MeshCollider causes BakeMesh to be called by unity
                //       (in which case this would happen serially on the main thread, which would be slower than calling it here)
                //       And sometimes it's not called? (in which case calling BakeMesh here would be *slower*)
                //       Also, if we use Unity.Physics then this wouldn't make sense at all
                //Physics.BakeMesh(instanceID, false);
            }
Example #4
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();
    }