• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by PeterB · Mar 07, 2015 at 06:42 PM · animationunity 5shader

Animating Standard Shader Emission at runtime

I'm trying to animate the standard shader's emission property at runtime, but it seems only to work once and then stay at that value.

First, in the Awake function, I cache the material:

  private var m : Material;
 
  function Awake () {
       m = GetComponent.<Renderer>().material;
  }
     

Then, I use the property _EmissionScaleUI to change emission at runtime, using the function SetGlow:

 public function SetGlow (strength : float) {
   // Stop any ongoing transition
   StopCoroutine("Transition");
   // In transitionTime seconds, transition from the current strength to the new strength
   StartCoroutine("Transition", strength);
 }
 
 
 function Transition(to : float) {
   var from : float = m.GetFloat("_EmissionScaleUI");
   var startTime : float = Time.time;
   var endTime : float = startTime + (to < from ? transitionTimeDown : transitionTimeUp);
   while (Time.time <= endTime) {
     yield;
     var d : float = Mathf.InverseLerp(startTime, endTime, Time.time);
     var v : float = Mathf.Lerp(from, to, d);
     m.SetFloat("_EmissionScaleUI", v);
   }
 }
 

This works flawlessly in Unity 4, but with the new Standard Shader in Unity 5, only the first change takes.

Grateful for any input.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image col000r · Mar 30, 2015 at 10:34 AM 0
Share

I'm having the same issue. Did you report a bug?

avatar image Ryan-Gatts · Apr 15, 2015 at 10:56 PM 0
Share

I'm running into this too.

5 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by ChrisD · Apr 12, 2016 at 04:41 PM

11 months later.... I've been pondering the same problem, but the DynamicGI.SetEmissive call didn't work for me (at least in Unity 5.3.4p1)

I arrived at a solution by looking at what changed in the material ".mat" file when setting the emission color in the unity editor, and spotted the addition of the "_EMISSION" keyword. So my working dynamic emission setter looks like this:

     public void setHighlight(bool highlight)
     {
         Material material = getMaterial();
         if (highlight)
         {
             material.EnableKeyword("_EMISSION");
             material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
             material.SetColor("_EmissionColor", Color.green);
         }
         else
         {
             material.DisableKeyword("_EMISSION");
             material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.EmissiveIsBlack;
             material.SetColor("_EmissionColor", Color.black);
         }
     }
 

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dkwyss · Sep 23, 2016 at 07:18 PM 0
Share

Thank you, thank you, thank you! Been struggling to highlight meshes consistently, and this did the trick!

avatar image catiejojo · Oct 06, 2016 at 02:15 PM 0
Share

This was a life saver! Thanks; I've been trying to get this to work for hours. The only thing that didn't work for me was the get$$anonymous$$aterial() call, but replacing it with gameObject.GetComponent<Renderer>().material worked like a charm. Also, for those that want more of a glow effect (s$$anonymous$$dily increasing then decreasing emission intensity), I was able to modify this code into the following coroutine:

 private IEnumerator glowObject()
 {
 $$anonymous$$aterial material = gameObject.GetComponent().material;
         float curIntensity = 0.0f;
         material.Enable$$anonymous$$eyword("_E$$anonymous$$ISSION");
         material.globalIllu$$anonymous$$ationFlags = $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags.RealtimeEmissive;
         // Increase intensity (fade in)
         while (curIntensity < glowIntensity)
         {
             material.SetColor("_EmissionColor", glowColor * curIntensity);
             curIntensity += intensityIncrement;
             yield return new WaitForSeconds(0.05f);
         }
         // Peak reached. Now decrease intensity (fade out)
         while (curIntensity > 0)
         {
             material.SetColor("_EmissionColor", glowColor * curIntensity);
             curIntensity -= intensityIncrement;
             yield return new WaitForSeconds(0.05f);
         }
         // Cleanup
         material.Disable$$anonymous$$eyword("_E$$anonymous$$ISSION");
         material.globalIllu$$anonymous$$ationFlags = $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags.EmissiveIsBlack;
         material.SetColor("_EmissionColor", Color.black);
     }
 }
avatar image VA-CeoBohga catiejojo · Feb 02, 2019 at 05:03 PM 0
Share

Sorry to necro this, but I just wanted to throw this out there as another solution building on/taking away from yours:

      public Color colorStart; // just make sure the alpha is set to 0 if you want it to start with no glow
     public Color colorEnd; // set the glow to the color you want at it's brightest... alpha matters
      public float duration; // must be greater than 0 to pulse
      public Renderer rend;
      public float glowmultiplier; // must be greater than 0 or it's just black
      void Start()
      {
          rend = GetComponent();
          rend.material.Enable$$anonymous$$eyword("_E$$anonymous$$ISSION");
          rend.material.globalIllu$$anonymous$$ationFlags = $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags.RealtimeEmissive;
      }
    void Update()
      {
          float lerp = $$anonymous$$athf.PingPong(Time.time, duration) / duration;
          rend.material.color = Color.Lerp(colorStart, colorEnd, lerp);
          rend.material.SetColor("_EmissionColor", rend.material.color * glowmultiplier);
      }

From there, there the setup is fairly straight forward. The code can probably be simplified even further. but tethering it to the diffuse color just makes my life easier.

avatar image
3

Answer by chrismarch · May 15, 2015 at 06:29 PM

Unity's intended way to do this is like this:

 DynamicGI.SetEmissive(GetComponent<Renderer>(), yourcolor);

dev explanation in forums

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kilito · May 11, 2016 at 06:06 AM 0
Share

This is the way to do it, works perfectly, thank you very much!

avatar image
1

Answer by Ryan-Gatts · Apr 15, 2015 at 11:18 PM

I found a workable way of correcting this. I think that the Emissive Multiplier control that we see in the editor may just be there for UI and isn't actually checked at runtime. Just do your multiplications against the _EmissiveColor value, and it works just fine.

 using UnityEngine;
 using System.Collections;
 
 public class emissivePulse : MonoBehaviour 
 {
     public float frequency = 1f;
     public float amplitude = 1f;
     public float baseMult = 0.75f;
     public Material material;
     public Color emissionColor;
 
     // Use this for initialization
     void Start () 
     {
         emissionColor = material.GetColor("_EmissionColor") * baseMult * amplitude;
     }
     
     // Update is called once per frame
     void Update () 
     {
         float glow = (2 + Mathf.Cos(Time.time * frequency)) * amplitude;
         material.SetColor("_EmissionColor", emissionColor * glow);
     }
 }


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PeterB · Apr 16, 2015 at 12:01 AM 0
Share

Yes, that seems to be the only current option. Just arrived at the same conclusion. :)

avatar image
0

Answer by PeterB · Apr 15, 2015 at 11:15 PM

No, not yet, because I'm leaning towards the idea that another undocumented step may be necessary, since we can see several examples in Unity's own demos. I'm going to create a minimal project to try to replicate the problem in isolation and file a bug report. I'll let you know.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by DiscoFever · May 18, 2016 at 06:16 PM

I can't seem to make it work; it's just a standard material that you put there right ? I don't see any change; does the object need to be in static mode ?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiscoFever · May 18, 2016 at 06:28 PM 0
Share

oh sorry my bad; you forgot to assign the material, or maybe forgot to paste the Awake function :)

avatar image PeterB · May 19, 2016 at 10:41 AM 0
Share

Yes, the GameObject must be marked static. You'll also find that the GI resolution affects the end result.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the best way nowadays to record a film/movie(mp4) with Unity? 1 Answer

Button animator not playing. 0 Answers

Animation Event is won't be called 0 Answers

How can I create a seamless looping animation? 0 Answers

Create Unity2D Position Animation Without Any Smooting Transition. How? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges