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

GI - script

Discussion in 'Global Illumination' started by topofsteel, Mar 15, 2015.

  1. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    999
    (posting here because I think I'll have a better chance of getting an answer)

    Originally I was just trying to modify the EmissionScale in the shader and was referred to the following link...

    http://answers.unity3d.com/question...ls-emission-scale-at-runtime.html?sort=oldest

    I'm just trying to toggle the emission on/off. The documentation (link below) says there are 3 parameters, but it will only accept 2. And I am puzzled as to why I can't control the emission with the emission value? Instead I have to jerk around with the color. In any case, it's not working, can someone give me a hand with this? Thanks.

    Code (CSharp):
    1. public class Lights : MonoBehaviour
    2. {
    3.     private Renderer renderer;
    4.     private Material material;
    5.     private float emissionValue;
    6.  
    7.     private Color white = new Color(1f,1f,1f,1f);
    8.     private Color black = new Color(0f,0f,0f,1f);
    9.  
    10.     private bool isOn = true;
    11.  
    12.     void Awake()
    13.     {
    14.         renderer = GetComponent<Renderer>();
    15.         material = renderer.sharedMaterial;
    16.  
    17.         emissionValue = material.GetFloat ("_EmissionScaleUI");
    18.  
    19.         Debug.Log (this.transform.name);
    20.         Debug.Log (emissionValue.ToString ());
    21.     }
    22.  
    23.     public void ToggleLights()
    24.     {
    25.         isOn = !isOn;
    26.  
    27.         if (!isOn)
    28.         {
    29.             Debug.Log ("Off");
    30.             DynamicGI.SetEmissive(renderer, (black * emissionValue));
    31.         }
    32.         else
    33.         {
    34.             Debug.Log ("On");
    35.             DynamicGI.SetEmissive(renderer, (white * emissionValue));
    36.         }
    37.  
    38.         DynamicGI.UpdateMaterials(renderer);
    39.         DynamicGI.UpdateEnvironment();
    40.     }
    41. }
    http://docs.unity3d.com/ScriptReference/DynamicGI.SetEmissive.html
     
  2. Bolbo13

    Bolbo13

    Joined:
    Apr 1, 2014
    Posts:
    7
    I believe you just have to set your emissionValue to 0 :

    1. Code (CSharp):
      1. DynamicGI.SetEmissive(renderer, (someArbitraryColor * 0));
     
  3. topofsteel

    topofsteel

    Joined:
    Dec 2, 2011
    Posts:
    999
    I was able to turn them on and off by just setting the color. But what I was really after was the emission value. Are you able modify it in runtime? Or does your code get to it through the color?

    Code (CSharp):
    1.     void Awake()
    2.     {
    3.         renderer = GetComponent<Renderer>();
    4.         material = renderer.sharedMaterial;
    5.     }
    6.  
    7.     public void ToggleLights()
    8.     {
    9.         isOn = !isOn;
    10.  
    11.         if (!isOn)
    12.         {  
    13.             DynamicGI.SetEmissive(renderer, black);
    14.         }
    15.         else
    16.         {
    17.             DynamicGI.SetEmissive(renderer, white);
    18.         }
    19.       }
    20.     }
     
  4. Bolbo13

    Bolbo13

    Joined:
    Apr 1, 2014
    Posts:
    7
    Sorry for getting back this late.
    You just have to multiply the intensity color by the desired emission value.

    Code (CSharp):
    1.  
    2.  
    3.         float desiredIntensity = 5f;
    4.  
    5.         void Awake()
    6.         {
    7.             renderer = GetComponent<Renderer>();
    8.             material = renderer.sharedMaterial;
    9.         }
    10.    
    11.         public void ToggleLights()
    12.         {
    13.             isOn = !isOn;
    14.    
    15.             if (!isOn)
    16.             {
    17.                 DynamicGI.SetEmissive(renderer, black * 0);
    18.             }
    19.             else
    20.             {
    21.                 DynamicGI.SetEmissive(renderer, white * desiredIntensity);
    22.             }
    23.           }
    24.         }
    25.  
     
    srmojuze likes this.