コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="meshes"></param>
        /// <returns></returns>
        private static Mesh UnityMeshFromMeshData(SpatialAwarenessSceneObject.MeshData meshData)
        {
            Mesh unityMesh = new Mesh();

            // Unity has a limit of 65,535 vertices in a mesh.
            // This limit exists because by default Unity uses 16-bit index buffers.
            // Starting with 2018.1, Unity allows one to use 32-bit index buffers.
            if (meshData.vertices.Length > 65535)
            {
                unityMesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
            }

            unityMesh.SetVertices(new List <Vector3>(meshData.vertices));
            unityMesh.SetIndices(meshData.indices, MeshTopology.Triangles, 0);
            unityMesh.SetUVs(0, new List <Vector2>(meshData.uvs));
            unityMesh.RecalculateNormals();

            return(unityMesh);
        }
コード例 #2
0
        private SpatialAwarenessSceneObject.MeshData MeshData(SceneMesh sceneMesh)
        {
            // Indices

            var indices = new int[sceneMesh.TriangleIndexCount];

            var meshIndices = new uint[sceneMesh.TriangleIndexCount];

            sceneMesh.GetTriangleIndices(meshIndices);

            var ilength = meshIndices.Length;

            for (int i = 0; i < ilength; ++i)
            {
                indices[i] = (int)meshIndices[i];
            }

            // Vertices

            var vertices = new UnityEngine.Vector3[sceneMesh.VertexCount];
            var uvs      = new UnityEngine.Vector2[sceneMesh.VertexCount];

            var meshVertices = new System.Numerics.Vector3[sceneMesh.VertexCount];

            sceneMesh.GetVertexPositions(meshVertices);

            var vlength = meshVertices.Length;

            var   minx = meshVertices[0].X;
            var   miny = meshVertices[0].Y;
            var   maxx = minx;
            var   maxy = miny;
            float x    = minx;
            float y    = miny;

            for (int i = 0; i < vlength; ++i)
            {
                x = meshVertices[i].X;
                y = meshVertices[i].Y;

                vertices[i] = new UnityEngine.Vector3(x, y, -meshVertices[i].Z);
                minx        = Math.Min(minx, x);
                miny        = Math.Min(miny, y);
                maxx        = Math.Max(maxx, x);
                maxy        = Math.Max(maxy, y);
            }

            // UVs - planar square projection

            float smallestDimension = Math.Min(minx, miny);
            float biggestDimension  = Math.Max(maxx, maxy);

            for (int i = 0; i < vlength; ++i)
            {
                uvs[i] = new Vector2(
                    Mathf.InverseLerp(smallestDimension, biggestDimension, vertices[i].x),
                    Mathf.InverseLerp(smallestDimension, biggestDimension, vertices[i].y));
            }

            var result = new SpatialAwarenessSceneObject.MeshData
            {
                indices  = indices,
                vertices = vertices,
                guid     = sceneMesh.Id,
                uvs      = uvs
            };

            return(result);
        }