0

我在 HoloLens 应用程序上工作,我想渲染一些简单的多边形。每个多边形标记一个限制区域。

我使用网格来解决此任务,在统一播放器上它工作正常,但在 HoloLens 上它不显示网格。HoloLens 是否因为空间映射而忽略了网格,或者有人知道这个问题吗?

        Vector3[] AllVertices = polygonCoordinates.ToArray();

        for (int i = 0; i < AllVertices.Length-3; i = i + 2)
        {
            GameObject polygonGameObject = new GameObject("Polygon");
            Mesh mesh = new Mesh();
            polygonGameObject.transform.SetParent(polygonContainer.transform, true);
            MeshFilter meshFilter = (MeshFilter)polygonGameObject.AddComponent(typeof(MeshFilter));

            //Styles for Mesh
            MeshRenderer renderer = polygonGameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
            renderer.material.shader = Shader.Find("Particles/Additive");
            Texture2D tex = new Texture2D(1, 1);
            tex.SetPixel(0, 0, Color.red);
            tex.Apply();
            renderer.material.mainTexture = tex;
            renderer.material.color = Color.red;

            Vector3[] vertices = new Vector3[4];
            vertices[0] = AllVertices[i];
            vertices[1] = AllVertices[i+1];
            vertices[2] = AllVertices[i+2];
            vertices[3] = AllVertices[i+3];
            mesh.vertices = vertices;

            int[] tri = new int[6];
            //Upper triangle
            tri[0] = 0;
            tri[1] = 1;
            tri[2] = 2;

            //Lower triangle  
            tri[3] = 2;
            tri[4] = 1;
            tri[5] = 3;
            mesh.triangles = tri;

            //Normals for nice rendering
            Vector3[] normals = new Vector3[4];
            normals[0] = -Vector3.forward;
            normals[1] = -Vector3.forward;
            normals[2] = -Vector3.forward;
            normals[3] = -Vector3.forward;
            mesh.normals = normals;

            //For texture
            Vector2[] uv = new Vector2[4];
            uv[0] = new Vector2(0, 0);
            uv[1] = new Vector2(1, 0);
            uv[2] = new Vector2(0, 1);
            uv[3] = new Vector2(1, 1);
            mesh.uv = uv;
            mesh.RecalculateNormals();

            meshFilter.mesh = mesh;
            GameObject.Instantiate(polygonGameObject);
        }
4

1 回答 1

0

我也将此问题发布到 HoloLens 论坛,我们解决了我的问题。

我在这里重新发布正确答案:

“Shader.Find 在您部署的项目中失败,因为 Particles/Additive 可能没有包含在构建中(尽管当您从编辑器运行时它就在那里)。尝试在 Assets 下添加一个 Resources 文件夹并将您的 Additive 着色器放在那里。任何Resources 中的文件应保证包含在您的构建中。然后使用 Find 方法而不是 Particles/Additive 调用 Assets/Resources。”

于 2016-11-29T16:03:11.200 回答