Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Custom Mesh rendering problems

jahrupjahrup
edited November 2016 in Questions And Answers

Hi guys

I work on a HoloLens application and i want to render some simple polygons. Each polygon marks a restricted area.

I used a mesh to solve this task, at the unity player it works fine but on HoloLens it doesn't display the mesh. Does the HoloLens ignore the mesh or have anybody an idea for this problem?

`
Vector3[] AllVertices = polygonCoordinates.ToArray();

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

            //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(1, 1, 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();
            mesh.RecalculateBounds();
            mesh.Optimize();

            MeshFilter meshFilter = (MeshFilter)polygonGameObject.AddComponent(typeof(MeshFilter));
            meshFilter.sharedMesh = mesh;
            GameObject.Instantiate(polygonGameObject);
        }`

Best Answer

  • Options
    james_ashleyjames_ashley ✭✭✭✭
    Answer ✓

    @jahrup,

    Shader.Find is failing in your deployed project because Particles/Additive probably isn't getting included in the build (though it's there when you run from the editor). Try adding a Resources folder under Assets and putting your Additive shader there. Any files in Resources should be guaranteed to be included in your build. Then call Assets/Resources with your Find method instead of Particles/Additive.

    James Ashley
    VS 2017 v5.3.3, Unity 2017.3.0f3, MRTK 2017.1.2, W10 17063
    Microsoft MVP, Freelance HoloLens/MR Developer
    www.imaginativeuniversal.com

Answers

  • Options
    james_ashleyjames_ashley ✭✭✭✭
    Answer ✓

    @jahrup,

    Shader.Find is failing in your deployed project because Particles/Additive probably isn't getting included in the build (though it's there when you run from the editor). Try adding a Resources folder under Assets and putting your Additive shader there. Any files in Resources should be guaranteed to be included in your build. Then call Assets/Resources with your Find method instead of Particles/Additive.

    James Ashley
    VS 2017 v5.3.3, Unity 2017.3.0f3, MRTK 2017.1.2, W10 17063
    Microsoft MVP, Freelance HoloLens/MR Developer
    www.imaginativeuniversal.com

  • Options

    @james_ashley

    Thanks a lot you're great!

    I'd try to debug it but i didn't see this error, probably i've got a problem with my debugger. :neutral:

Sign In or Register to comment.