HoloLens多边形渲染与网格

时间:2016-11-28 09:01:25

标签: hololens

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

我使用网格来解决这个任务,在Unity播放器上它可以正常工作但是在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);
        }

1 个答案:

答案 0 :(得分:0)

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

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

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