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

Changing shaders at runtime disables main color

I am trying to change the shaders of all children of an object in the scene at runtime.
They have different materials, so I am trying something like this:

public void SetShader(Shader newShader)
    {
        MeshRenderer[] meshRenderers = currentlyActiveObject.GetComponentsInChildren<MeshRenderer>();

        for (int i = 0; i < meshRenderers.Length; i++)
        {
            meshRenderers[i].material.shader = newShader;
            //meshRenderers[i].material.EnableKeyword("_USECOLOR_ON");
        }
    }

This resulted in the main color of the material being disabled.
So I added the "EnableKeyword" line (commented out above), which resolved the problem, when starting the app in the Unity Editor.

But apparently, this doesn't work, when deploying the app to the HoloLens.
In this case, all children are displayed with what appears to be the correct shader, but in the color white.

So I modified the target shaders to have their main color enabled by default, but it didn't help either.

Again, this issue does not appear, when starting the app in the Unity Editor, nor when using Holographic Remoting. Just, when I deploy the app to the HoloLens.

Any ideas, anyone? (I am aware of this post here: https://forums.hololens.com/discussion/2231/unable-to-change-material-settings-from-script-when-using-holotoolkit-vertex-lit-configurables)

Thanks a lot!

Answers

  • Options
    james_ashleyjames_ashley ✭✭✭✭

    Fiedel,

    It might ultimately be easier to have a second set of materials and change the materials rather than their shaders. This would make it easier to test that the shaders you choose actually do what you want, among other things.

    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

    Hey James,
    Thank you for you answer.

    I actually tried that. My goal is to create a test scene in which the user can test various shaders on arbitrary models they import.

    So I did something along the lines of

        private void CreateExchangeMaterialArray()
        {
            Material[] testMaterialArray = new Material[sharedMaterials.Count];
    
                for (int i = 0; i < sharedMaterials.Count; i++)
                {
                    Material testMaterial = new Material(sharedMaterials[i]);
    
                    testMaterial.shader = testShaders;
                    testMaterial.SetFloat("_UseColor", 1);
    
                    //testMaterial.EnableKeyword("_USECOLOR_ON");
                    //testMaterial.CopyPropertiesFromMaterial(sharedMaterials[i]);
    
                    testMaterialArray[i] = testMaterial;
                }
    
                testMaterialArrays.Add(materialArrayToAdd);
        }
    

    where sharedMaterials is a list of all shared materials on the test object and any of its children.

    Unfortunately, I got the same result.

    The lines that are commented out where attempts to get the original color somehow, but also didn't work.

    I want to avoid to have the user create all materials in Unity beforehand (which actually DOES work).

Sign In or Register to comment.