Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Standard Shader Emission Value

Discussion in 'Unity 5 Pre-order Beta' started by nickw4rd, Nov 14, 2014.

  1. nickw4rd

    nickw4rd

    Joined:
    Nov 14, 2014
    Posts:
    5
    How can I change the Emission value of a standard shader using c#?
     
  2. nickw4rd

    nickw4rd

    Joined:
    Nov 14, 2014
    Posts:
    5
    Alright starting over. I have a model that came from studio max with a multi-sub object material. The model has 6 materials all together. How can I go in and change the Emission Color of one of those materials?

    I can do it with a model with a single material on it with:

    GetComponent<Renderer>().material.SetColor ("_EmissionColor", Color.black);

    Any help would be great. Thanks!
     
    Last edited: Nov 14, 2014
  3. one_one

    one_one

    Joined:
    May 20, 2013
    Posts:
    621
    So where's the difficulty then?
     
  4. noneee

    noneee

    Joined:
    Nov 26, 2014
    Posts:
    3
    looks like no one can halp, so problem is actual for me too
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (CSharp):
    1. // First you want to use the API with multiple materials
    2. // where 3 should be the index you want to use...
    3. var materials = GetComponent<Renderer>().materials;
    4. materials[3].SetColor ("_EmissionColor", Color.blue);
    5.  
    6. // Another thing to note is that Unity 5 uses the concept of shader keywords extensively.
    7. // So if your material is initially configured to be without emission, then in order to enable emission, you need to enable // the keyword.
    8. materials[3].EnableKeyword ("_EMISSION");
    9.  
    Oh and if you actually want to set it to black, then of course EnableKeyword is not necessary. In fact DisableKeyword in that case will make your shader run faster, because it will define out the math that deals with emissive surfaces, but of course the difference may or may not be huge depending on what you do, it may very well not be worth it to do that kind of optimization with switching enable / disable keywords.
     
    jakob-, arufolo, AAAAAAAAAE and 3 others like this.
  6. noneee

    noneee

    Joined:
    Nov 26, 2014
    Posts:
    3
    i tried, but unfortunately nothing changed, looks like emission color do not work for unity's standard shader
     
  7. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    659
    You might have to call DynamicGI.UpdateMaterials as well.
     
  8. elbows

    elbows

    Joined:
    Nov 28, 2009
    Posts:
    2,502
    You are giving up too easily. They work, I use them all over the place.
     
  9. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    This is only necessary if you actually want to animate emissive lighting. Do you want to animate emissive lighting?
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    As a starting point. I would suggest first trying it out in the material editor. So you can quickly see the results of changing the colors / textures assigned etc. This way you can prove to yourself or others that the rendering feature does indeed work.

    Once you know that the rendering actually works and the content looks as you expect it, then it makes sense to script it. As explained above.
     
  11. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289
    The correct answer is
    Color finalValue=Color.White*someValue; // someValue adjust the scale of emission
    DynamicGI.SetEmissive(GetComponent<Renderer>(), finalValue); // Pass your objet's renderer which emissive material attached
    if you want to change emissive value at run time and affect GI. I have tested it and it works.
     
    the_motionblur likes this.
  12. Gokcan

    Gokcan

    Joined:
    Aug 15, 2013
    Posts:
    289