从具有空间感知网格观察者 MHL2

问题描述

我是新来的,也是 C#/Unity2019.4.14f 的新手,使用 VS2019、MRTK V2.5.3 和 Microsoft Hololens 2 编程。我想就一个我已经好几个星期没能解决的问题向您征求意见。首先,我想快速说明一下这是一个什么样的问题。我的任务是使用 Spatial Mesh 跟踪检查立方体中的对象,并尽可能准确地表示其形状。Explanation screen for the task description

检查立方体在太空中的位置的计算没有任何问题。但由于某种原因,我无法查询空间感知网格观察器。无论如何,尽管它们是可见的,但似乎没有网格存在。

由于我完全不知所措,到目前为止我问过的人都无法帮助我,因此我将在下面发布此功能的代码。请耐心等待,因为我仍然是编写代码的初学者。

  public void ReadAndDrawMesh(){

        //Provide a list of the cube coordinates
        Vector3[] CubeCoordinateList = new Vector3[24];

        //Convert Local to World Coordinates
        var localToWorld = transform.localToWorldMatrix;

        Vector3 cubeWorldPos = Cube.transform.position; // Reading out the centre position
        Vector3[] cubeVertices = Cube.GetComponent<MeshFilter>().mesh.vertices; //World coordinates? 
        List<Vector3> cubeWorldVertices = new List<Vector3>();

        for (int i = 0; i <= (cubeVertices.Length) - 1; i++)
        {
            CubeCoordinateList[i] = localToWorld.MultiplyPoint3x4(cubeVertices[i]);
        }

        //CubeVerticies from Vector A[0] to E[4]
        float normalX1 = CubeCoordinateList[4].x - CubeCoordinateList[0].x;
        float normalY1 = CubeCoordinateList[4].y - CubeCoordinateList[0].y;
        float normalZ1 = CubeCoordinateList[4].z - CubeCoordinateList[0].z;

        float amount1 = Mathf.Sqrt((normalX1 * normalX1) + (normalY1 * normalY1) + (normalZ1 * normalZ1));

        //Create a new vector
        Vector3 direction1 = new Vector3(normalX1,normalY1,normalZ1);
        direction1 = direction1 / amount1;

        //CubeVerticies from Vector A[0] to B[2]
        float normalX2 = CubeCoordinateList[2].x - CubeCoordinateList[0].x;
        float normalY2 = CubeCoordinateList[2].y - CubeCoordinateList[0].y;
        float normalZ2 = CubeCoordinateList[2].z - CubeCoordinateList[0].z;

        float amount2 = Mathf.Sqrt((normalX2 * normalX2) + (normalY2 * normalY2) + (normalZ2 * normalZ2));


        //Create a new vector
        Vector3 direction2 = new Vector3(normalX2,normalY2,normalZ2);
        direction2 = direction2 / amount2;

        //CubeVerticies from Vector A[0] to D[3]
        float normalX3 = CubeCoordinateList[3].x - CubeCoordinateList[0].x;
        float normalY3 = CubeCoordinateList[3].y - CubeCoordinateList[0].y;
        float normalZ3 = CubeCoordinateList[3].z - CubeCoordinateList[0].z;

        float amount3 = Mathf.Sqrt((normalX3 * normalX3) + (normalY3 * normalY3) + (normalZ3 * normalZ3));

        //Create a new vector
        Vector3 direction3 = new Vector3(normalX3,normalY3,normalZ3);
        direction3 = direction3 / amount3;

        //From MRTK 2.5.3
        // Use CoreServices to quickly get access to the IMixedRealitySpatialAwarenessSystem
        var spatialAwarenessService = CoreServices.SpatialAwarenessSystem;

        // Cast to the IMixedRealityDataProviderAccess to get access to the data providers
        var dataProviderAccess = spatialAwarenessService as IMixedRealityDataProviderAccess;

        var meshObserverName = "SpatialAwarenessMeshObserverProfile";
        var MeshObserver = dataProviderAccess.GetDataProvider<IMixedRealitySpatialAwarenessMeshObserver>(meshObserverName);

        foreach (SpatialAwarenessMeshObject meshObject in MeshObserver.Meshes.Values)
        {

            Vector3[] meshObjectarray = meshObject.Filter.mesh.vertices;

            //Reading the Spatial Mesh of the room
            foreach (Vector3 verticiesCoordinaten in meshObjectarray)
            {

                //List for the mesh coordinates that come out of the scalar product calculation
                Vector3[] MeshPositionList = new Vector3[meshObjectarray.Length]; ;

                //Determining Direction between verticiesCoordinates of MeshObject with Centre of Cube
                var dir_vectorMesh = verticiesCoordinaten - cubeWorldPos;

                //Calculating the scalar product of the coordinates

                var result1 = Mathf.Abs(Vector3.Dot(dir_vectorMesh,direction1)) * 2;
                var result2 = Mathf.Abs(Vector3.Dot(dir_vectorMesh,direction2)) * 2;
                var result3 = Mathf.Abs(Vector3.Dot(dir_vectorMesh,direction3)) * 2;

                // If scalar product Negative,then write it in the list
                if (result1 > amount1 && result2 > amount2 && result3 > amount3)
                {
                    MeshPositionList[meshObjectarray.Length] = verticiesCoordinaten;
                }

                //Creating a new visible mesh from the points in the list 
                Mesh mesh = new Mesh();
                mesh.vertices = MeshPositionList;
                mesh.Optimize();
                Graphics.DrawMeshNow(mesh,Vector3.zero,Quaternion.identity);

                mesh.RecalculateNormals();
            }
        }
    }

我希望你们中的一个能帮助我,我期待任何建设性的答案。感谢所有阅读这篇文章并可能做出回应的人。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...