Search Unity

Specular Map only works after expanding Shader in Editor

Discussion in 'Scripting' started by Dinin, Oct 8, 2015.

  1. Dinin

    Dinin

    Joined:
    Apr 1, 2015
    Posts:
    6
    Hi,

    In code i'm creating a Material with 'Standard (Specular setup)' shader.
    I'm setting mainTexture, normalmap and specularmap and assign the material to a skinned mesh renderer.

    Code (CSharp):
    1. Material combinedMat = new Material(Shader.Find("Standard (Specular setup)"));
    2.             Texture2D myTexture = (Texture2D)Resources.Load(String.Format("Textures/Characters/{0}", texName));
    3.             Texture2D myTextureNorm = (Texture2D)Resources.Load(String.Format("Textures/Characters/{0}", texNormName));
    4.             Texture2D myTextureSpec = (Texture2D)Resources.Load(String.Format("Textures/Characters/{0}", texNormName.Replace("_normal", "_specular")));
    5.  
    6.             combinedMat.mainTexture = myTexture;
    7.             combinedMat.SetTexture("_BumpMap", myTextureNorm);
    8.             combinedMat.SetTexture("_SpecGlossMap", myTextureSpec);
    9.            
    10.             smr.sharedMaterial = combinedMat;
    But specular map does not work after execution of code.
    But if i expand the shader panel - while game is running in unity editor - the specular map suddenly is working correct.

    Any suggestions ?

    Thanks and greeting
    Dinin
     
  2. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Hi @Dinin,

    You must enable the Standard Shader variant by using the EnableKeyword function. A different variant would be required if you start using a shader feature that was not initially in use by the material. For example assigning a Normal Map to a Material that did not have one, or setting the Emissive level to a value greater than zero, when it was previously zero.

    The specific Keywords required to enable the Standard Shader features are as follows:
    _NORMALMAP (Normal Mapping)
    _ALPHATEST_ON (“Cut out” Transparency Rendering Mode)
    _ALPHABLEND_ON (“Fade” Transparency Rendering Mode)
    _ALPHAPREMULTIPLY_ON (“Transparent” Transparency Rendering Mode)
    _EMISSION (Emission Colour or Emission Mapping)
    _PARALLAXMAP (Height Mapping)
    _DETAIL_MULX2 (Secondary “Detail” Maps (Albedo & Normal Map)
    _METALLICGLOSSMAP (Metallic/Smoothness Mapping in Metallic Workflow)
    _SPECCGLOSSMAP (Specular/Smoothness Mapping in Specular Workflow)

    More info here: http://docs.unity3d.com/Manual/MaterialsAccessingViaScript.html
     
  3. Dinin

    Dinin

    Joined:
    Apr 1, 2015
    Posts:
    6
    Hi Jordi Bonastre,

    Thanks - it worked.
    But the enable keyword is '_SPECGLOSSMAP' - without double C
    Is also wrong in docs!
     
  4. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Thanks @Dinin! I will create a bug for this issue.
     
  5. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102