Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

need help with _SpecularHighlights and _GlossyReflections - can't access them via script

Discussion in 'Shaders' started by kbr0n, Sep 16, 2016.

  1. kbr0n

    kbr0n

    Joined:
    Aug 28, 2016
    Posts:
    24
    hello world

    I need to control these 2 properties from the Standard Shader, while the project is running:

    _SpecularHighlights -> which appears in the Inspector as checkbox named 'Specular Highlights'

    and

    _GlossyReflections -> which also appears in the Inspector as a checkbox named just 'Reflections'

    So I need a GUI checkbox that the user can toggle on/off while the project is running.

    I already managed to do a similar script that controls other properties of the shader, as _GlossMapScale, but the same approach is not working for _SpecularHighlights or _GlossyReflections

    so, this works:
    Code (csharp):
    1. shapes.GetComponent<Renderer> ().material.SetFloat ("_GlossMapScale", sldfind.value);
    but this does not:
    Code (csharp):
    1.  shapes.GetComponent<Renderer> ().material.SetFloat("_SpecularHighlights", 0);
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Those shader properties aren't what actually control those settings, they're kind of like placeholder properties to keep track of if that feature should be enabled or not. What actually lets you turn on or off that feature is a keyword set on the material. You'll either need to look at the StandardShaderCustomEditor.cs (I think that's what it's called) StandardShaderGUI.cs, or look at the shader to try and find a keyword that looks right, or try toggling the properties on the material and then use the debug inspector (right click on the inspector tab) and look at the keywords that are being set on the shader.

    The keywords are _SPECULARHIGHLIGHTS_OFF and _GLOSSYREFLECTIONS_OFF, and you set them with myMaterial.EnableKeyword("_SPECULARHIGHLIGHTS_OFF")
     
    Last edited: Sep 18, 2016
  3. kbr0n

    kbr0n

    Joined:
    Aug 28, 2016
    Posts:
    24
    mmm already tried EnableKeyword with no success

    gonna try your suggestion as soon as I get home

    thank you

    edited:

    it didn't work

    this will get the checkboxes to respond but the object is not updated

    Code (csharp):
    1. gameObject.GetComponent<Renderer>().material.SetFloat("_SpecularHighlights", 1 or 0);
     
    Last edited: Sep 21, 2016
  4. kbr0n

    kbr0n

    Joined:
    Aug 28, 2016
    Posts:
    24
    Finally! I manage to get this working using keywords and setfloat

    like this:

    to turn ON:

    Code (csharp):
    1.  shapes.GetComponent<Renderer>().material.DisableKeyword("_SPECULARHIGHLIGHTS_OFF");
    2. shapes.GetComponent<Renderer>().material.SetFloat("_SpecularHighlights",1f);
    to turn OFF:

    Code (csharp):
    1.  shapes.GetComponent<Renderer>().material.EnableKeyword("_SPECULARHIGHLIGHTS_OFF");
    2. shapes.GetComponent<Renderer>().material.SetFloat("_SpecularHighlights",0f);
    - it's tricky cause there's no "_specularhighlights_on", instead you disable or enable "_specularhightlights_off"
     
    Last edited: Sep 22, 2016
    Cuttlas-U, IgorAherne and mikelortega like this.
  5. Cuttlas-U

    Cuttlas-U

    Joined:
    Apr 11, 2017
    Posts:
    38



    New
    kbr0n
    u basically saved my project :) , thanks a lot ,
    I searched the whole web but could not find why its not working

    adding both lines of code fixed it for me ,
     
  6. StellarVeil

    StellarVeil

    Joined:
    Aug 31, 2018
    Posts:
    73
    Does it work in build for every materials though? It works fine in editor for me but in build some materials work whiles others not despite using the same Standard Unity Shader in Built-in, Unity 2022.1.12f1.
     
  7. Cuttlas-U

    Cuttlas-U

    Joined:
    Apr 11, 2017
    Posts:
    38
    you should create a material with the same details that you want to create from script and include it some where in your project with a reference , otherwise unity might strip that shader from your build and will not include it , so thats why it works on editor , you have to have a sample material with all the different details they might have
    I just created a list of materials and assigned different created materials to ti ,