GI - script

(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/questions/920023/change-materials-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.

public class Lights : MonoBehaviour
{
    private Renderer renderer;
    private Material material;
    private float emissionValue;

    private Color white = new Color(1f,1f,1f,1f);
    private Color black = new Color(0f,0f,0f,1f);

    private bool isOn = true;

    void Awake()
    {
        renderer = GetComponent<Renderer>();
        material = renderer.sharedMaterial;

        emissionValue = material.GetFloat ("_EmissionScaleUI");

        Debug.Log (this.transform.name);
        Debug.Log (emissionValue.ToString ());
    }

    public void ToggleLights()
    {
        isOn = !isOn;

        if (!isOn)
        {
            Debug.Log ("Off");
            DynamicGI.SetEmissive(renderer, (black * emissionValue));
        }
        else
        {
            Debug.Log ("On");
            DynamicGI.SetEmissive(renderer, (white * emissionValue));
        }

        DynamicGI.UpdateMaterials(renderer);
        DynamicGI.UpdateEnvironment();
    }
}

I believe you just have to set your emissionValue to 0 :

DynamicGI.SetEmissive(renderer, (someArbitraryColor * 0));

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?

    void Awake()
    {
        renderer = GetComponent<Renderer>();
        material = renderer.sharedMaterial;
    }

    public void ToggleLights()
    {
        isOn = !isOn;

        if (!isOn)
        {  
            DynamicGI.SetEmissive(renderer, black);
        }
        else
        {
            DynamicGI.SetEmissive(renderer, white);
        }
      }
    }

Sorry for getting back this late.
You just have to multiply the intensity color by the desired emission value.

        float desiredIntensity = 5f;

        void Awake()
        {
            renderer = GetComponent<Renderer>();
            material = renderer.sharedMaterial;
        }
    
        public void ToggleLights()
        {
            isOn = !isOn;
    
            if (!isOn)
            { 
                DynamicGI.SetEmissive(renderer, black * 0);
            }
            else
            {
                DynamicGI.SetEmissive(renderer, white * desiredIntensity);
            }
          }
        }
1 Like