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);
            }
Exemple #3
0
            public void Execute()
            {
                if (result[0] < 0)
                {
                    return;
                }
                var        dracoMesh = (DracoMesh *)dracoTempResources[meshPtrIndex];
                DracoData *dracoIndices;

                GetMeshIndices(dracoMesh, &dracoIndices, flip);
                int  indexSize   = DataTypeSize((DataType)dracoIndices->dataType);
                int *indicesData = (int *)dracoIndices->data;

#if DRACO_MESH_DATA
                var indices = mesh.GetIndexData <uint>();
#endif
                UnsafeUtility.MemCpy(indices.GetUnsafePtr(), indicesData, indices.Length * indexSize);
                ReleaseDracoData(&dracoIndices);
            }
Exemple #4
0
        public void Execute(int index)
        {
            var data   = meshData[index];
            var vCount = data.vertexCount;
            var mat    = xform[index];
            var vStart = vertexStart[index];

            // Allocate temporary arrays for input mesh vertices/normals
            if (!tempVertices.IsCreated || tempVertices.Length < vCount)
            {
                if (tempVertices.IsCreated)
                {
                    tempVertices.Dispose();
                }
                tempVertices = new NativeArray <float3>(vCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            }
            if (!tempNormals.IsCreated || tempNormals.Length < vCount)
            {
                if (tempNormals.IsCreated)
                {
                    tempNormals.Dispose();
                }
                tempNormals = new NativeArray <float3>(vCount, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
            }
            // Read input mesh vertices/normals into temporary arrays -- this will
            // do any necessary format conversions into float3 data
            data.GetVertices(tempVertices.Reinterpret <Vector3>());
            data.GetNormals(tempNormals.Reinterpret <Vector3>());

            var outputVerts   = outputMesh.GetVertexData <Vector3>();
            var outputNormals = outputMesh.GetVertexData <Vector3>(stream: 1);

            // Transform input mesh vertices/normals, write into destination mesh,
            // compute transformed mesh bounds.
            var b = bounds[index];

            for (var i = 0; i < vCount; ++i)
            {
                var pos = tempVertices[i];
                pos = math.mul(mat, new float4(pos, 1)).xyz;
                outputVerts[i + vStart] = pos;
                var nor = tempNormals[i];
                nor = math.normalize(math.mul(mat, new float4(nor, 0)).xyz);
                outputNormals[i + vStart] = nor;
                b.c0 = math.min(b.c0, pos);
                b.c1 = math.max(b.c1, pos);
            }
            bounds[index] = b;

            // Write input mesh indices into destination index buffer
            var tStart     = indexStart[index];
            var tCount     = data.GetSubMesh(0).indexCount;
            var outputTris = outputMesh.GetIndexData <int>();

            if (data.indexFormat == IndexFormat.UInt16)
            {
                var tris = data.GetIndexData <ushort>();
                for (var i = 0; i < tCount; ++i)
                {
                    outputTris[i + tStart] = vStart + tris[i];
                }
            }
            else
            {
                var tris = data.GetIndexData <int>();
                for (var i = 0; i < tCount; ++i)
                {
                    outputTris[i + tStart] = vStart + tris[i];
                }
            }
        }
    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();
    }
 /// <summary>
 /// Get the actual index count of a <see cref="Mesh.MeshData"/> object.
 /// </summary>
 public static int GetIndexCount(this Mesh.MeshData md)
 {
     return(md.indexFormat == IndexFormat.UInt16
             ? md.GetIndexData <ushort>().Length
             : md.GetIndexData <uint>().Length);
 }