Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question _SPECULARHIGHLIGHTS_OFF in final build

Discussion in 'Shaders' started by donatdeva, Aug 16, 2023.

  1. donatdeva

    donatdeva

    Joined:
    Dec 17, 2022
    Posts:
    4
    Hello everyone, this is my situation: I create a plane game object at runtime and then set up its material, something like this:

    Code (CSharp):
    1. GameObject canvas = GameObject.CreatePrimitive(PrimitiveType.Plane);
    2. ...
    3. Renderer renderer = canvas.GetComponent<Renderer>();
    4. renderer.material.SetTexture("_MainTex", MyTexture);
    5. renderer.material.EnableKeyword("_NORMALMAP");
    6. renderer.material.SetTexture("_BumpMap", MyNormalMap);
    7. renderer.material.EnableKeyword("_SPECULARHIGHLIGHTS_OFF");
    This works when I debug my program in the editor, problem comes when I do the final build - I just can't get "_SPECULARHIGHLIGHTS_OFF" to have an effect.

    In the docs it says, "Use shader variant collections to prewarm shader variants, or to ensure that shader variants that are required at runtime but not referenced in a scene are not excluded (“stripped”) from your build." So I created a shader variant collection and put it in my Resources folder. Then, in the inspector I added the Standard shader and clicked the "+" to open the "Add shader Standard variants to collection" menu. Here I selected my keywords "_NORMALMAP" and "_SPECULARHIGHLIGHTS_OFF" and clicked one of the shaders in the "Shader variants with these keywords (click to select):" list.

    This way my normal map works in the final build, however specular highlights are still on.

    Also: Is there a better way of creating the shader variant collection? In the docs it also says: "The Unity Editor can track which shader variants your application uses when it runs, and automatically create a shader variant collection asset that contains them." However, when I export this shader variant collection and put it in my Resources folder, not even the normal maps work in the final build.

    Help would be much appreciated
     
  2. donatdeva

    donatdeva

    Joined:
    Dec 17, 2022
    Posts:
    4
    Is there really no one having a solution for this issue?
     
  3. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,046
    You can enable "strict variant matching" and check the player logs to see which variant it actually tries to use.
     
    donatdeva likes this.
  4. donatdeva

    donatdeva

    Joined:
    Dec 17, 2022
    Posts:
    4
    Thanks for your help! It turns out "strict variant matching" is only available in Unity 2022 - before I was using 2021.3.3f1. Funny thing is, if I manually create the shader variant collection in 2022.3.3f1, specular lights are off in the final build. Automatically generating the shader variant collection still doesn't work, but having the other solution is good enough for me right now