Hello everyone.

We have decided to phase out the Mixed Reality Forums over the next few months in favor of other ways to connect with us.

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.

The plan between now and the beginning of May is to clean up old, unanswered questions that are no longer relevant. The forums will remain open and usable.

On May 1st we will be locking the forums to new posts and replies. They will remain available for another three months for the purposes of searching them, and then they will be closed altogether on August 1st.

So, where does that leave our awesome community to ask questions? Well, there are a few places we want to engage with you. 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. And always feel free to hit us up on Twitter @MxdRealityDev.

SpatialUnderstanding MeshRenderer color change during run-time

BokuBoku
edited August 2018 in Questions And Answers

Hi guys,

I'm currently working with SpatialUnderstanding, which is a module for the Microsoft HoloLens. Within the module, mesh of the space the user is currently in is mapped (essentially a processed spatial map). I'm looking to modify the mesh material color during run-time, but the following code I have seems to have no effect (visually):

 void TestFunction()
 {
     List<MeshRenderer> roomMeshRenderers = SpatialUnderstanding.Instance.UnderstandingCustomMesh.GetMeshRenderers() as List<MeshRenderer>;
     foreach (currentRenderer in roomMeshRenderers)
     {
         Material material = currentRenderer.material;
         Debug.Log(material.color);
         material.color = Color.white;
         currentRenderer.material = material;
     }
 }

Would appreciate it if anyone could point why this code has no effect.

Thanks!

Best Answer

Answers

  • pstuevenpstueven ✭✭
    edited August 2018

    Hey @Boku ,

    I did something similar with but also used spatialUnterstanding in that scenario. Try this:
    SpatialUnderstandingCustomMesh spatialMesh = GameObject.Find("SpatialMapping").GetComponent<SpatialUnderstandingCustomMesh>(); foreach (SpatialMappingSource.SurfaceObject surfaceObject in spatialMesh.SurfaceObjects) { surfaceObject.Renderer.material = material }
    Of course you need to adjust the string "SpatialMapping" to the name of the object holding your SpatialMapping

    Regards
    @pstueven

  • @pstueven said:
    Hey @Boku ,

    I did something similar with but also used spatialUnterstanding in that scenario. Try this:
    SpatialUnderstandingCustomMesh spatialMesh = GameObject.Find("SpatialMapping").GetComponent<SpatialUnderstandingCustomMesh>(); foreach (SpatialMappingSource.SurfaceObject surfaceObject in spatialMesh.SurfaceObjects) { surfaceObject.Renderer.material = material }
    Of course you need to adjust the string "SpatialMapping" to the name of the object holding your SpatialMapping

    Regards
    @pstueven

    Hey @pstueven

    That works and changes the material. I found out there is already a function available in SpatialUnderstandingCustomMesh.cs which does this, called "MeshMaterial".

    I have a slightly different problem however, when I try to change the texture of the material to be rendered the new texture doesn't seem to get applied. The code is as follows:
    SpatialUnderstanding.Instance.UnderstandingCustomMesh.ChangeProcessedMeshTexture(targetTexture);

    And in SpatialUnderstandingCustomMesh.cs I have:
    public void ChangeProcessedMeshTexture(Texture2D targetTexture) { for (int i = 0; i < SurfaceObjects.Count; ++i) { SurfaceObjects[i].Renderer.material.SetTexture("_MainTex", targetTexture); } }

  • @pstueven said:
    Hey @Boku ,
    To be honest I am not into materials that much. So I am just giving suggestions here:
    Are you sure the current material has a texture called "_MainTex" applied? The first argument of SetTexture determines which texture you want to set. Maybe try using "_BumpMap" or Material.mainTex .

    That was the problem! My shader didn't have a _MainTex. Think I'll have to use another shader.

    Thanks :)

Sign In or Register to comment.