Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by Bolbo13 · Mar 10, 2015 at 04:20 AM · shader

Change material's emission scale at runtime

Is it possible to change the emission scale on unity 5's standard shader at runtime ? _EmissionScale can only be changed through the UI.

What i tried to do is to set the emission to a value higher than 0 and change the emissionColor through script, from black (no emission) to some arbitrary color.

The result is that the emission changes, but the glow and global illumitions remain absent.

Is there a way to change the emission at runtime ?

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Bolbo13 · Mar 12, 2015 at 01:43 AM

Changing the emission property using Material.SetColor() doesn't update the GI.

The way to go is using DynamicGI.SetEmissive (which also seems to be faster), also, the trick is to multiply the color by the desired intensity (ie emission scale) :

 DynamicGI.SetEmissive(renderer, new Color(1f, 0.1f, 0.5f, 1.0f) * intensity)

Thanks a lot mat ! : )

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 MaT227 · Mar 12, 2015 at 06:16 AM 0
Share

Your're welcome ! Glad to know that you found a solution. :)

avatar image
1

Answer by MaT227 · Mar 10, 2015 at 07:28 AM

I suggest you to take a look at the DynamicGI class. If I remember well, if you change the Albedo or the Emission of a material or whatever property that should affect GI, you need to perform an update on some materials.

Try to apply DynamicGI.UpdateMaterials and/or DynamicGI.UpdateEnvironment on your receiving and/or your emissive object.

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 Bolbo13 · Mar 10, 2015 at 04:08 PM 0
Share

At runtime, i get this message under the emission property in the material :

"emissive value is animated but the material has not been configured to support emissive. Please make sure the material itself has some amount of emissive."

I get that with the emission set to 4 and a bright color. Also, I'm now using DynamicGI.Update$$anonymous$$aterials but it doesnt seem to have any effect (no glow, no GI).

avatar image MaT227 · Mar 11, 2015 at 07:42 AM 0
Share

Did you try with a simple scene and did you apply DynamicGI.Update$$anonymous$$aterials on all materials ? Take also a look at $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags and especially at $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags.RealtimeEmissive

avatar image Bolbo13 · Mar 12, 2015 at 12:43 AM 1
Share

$$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags can be set in the inspector under the emission field (enabled only when emission is above 0).

I set up a new scene; no change. Still no global illu$$anonymous$$ation and the message (displayed on the material under the emission field) :

"emissive value is animated but the material has not been configured to support emissive. Please make sure the material itself has some amount of emissive."

Here is the relevant code :

     Renderer renderer;
 
     $$anonymous$$aterial material;
 
     public Color color;
 
     void Start()
     {
         renderer = GetComponent<Renderer>();
         material = renderer.shared$$anonymous$$aterial;
 
         //can be set in the inspector
         material.globalIllu$$anonymous$$ationFlags = $$anonymous$$aterialGlobalIllu$$anonymous$$ationFlags.RealtimeEmissive;
     }
 
     void Update()
     {
         material.SetColor("_EmissionColor", color);
         DynamicGI.Update$$anonymous$$aterials(renderer);
         DynamicGI.UpdateEnvironment();
     }
avatar image
1

Answer by DarkSinBad · Jul 02, 2016 at 02:44 PM

I know it might be too late for Bolbo now BUT for future generation like myself i found the answer in this link

http://answers.unity3d.com/questions/970290/emission-at-run-time-works-in-editor-but-not-in-bu.html

Basically just set the initial emission to something so small like 0.01 so the emissive shader can compile.

Hope this helps ;)

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
1

Answer by KarlKarl2000 · Oct 08, 2016 at 09:43 AM

I couldn't get the code above working.. at all.

But after digging around the Unity documentation. For anyone that's interested on a simple solution to help get them started on understanding emission changing

     [SerializeField] private Renderer _renderer;

     if(Input.GetKeyDown(KeyCode.K))  //for easy testing in Update
         {
             _renderer.material.SetColor ("_EmissionColor", Color.red);
             DynamicGI.UpdateMaterials (_renderer);
             DynamicGI.UpdateEnvironment ();
         }

Hope this helps others. Sharing is caring

https://madewith.unity.com/profiles/2dos

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 Monica_L22 · Oct 30, 2019 at 12:36 PM 0
Share

This works for me. For

 DynamicGI.Update$$anonymous$$aterials (_renderer);

it's been changed to

 _renderer.UpdateGI$$anonymous$$aterials();
avatar image
0

Answer by raincarrot · Jun 17, 2017 at 04:34 PM

Find the answer in unity shader file StandardShaderGUI.cs

Following is my test code.

 private void Test(GameObject obj)
 {
     MeshRenderer mr = obj.GetComponent<MeshRenderer>();
     mr.material.SetColor("_EmissionColor", Color.yellow);
     bool shouldEmissionBeEnabled = ShouldEmissionBeEnabled(mr.material.GetColor("_EmissionColor"));
     if (shouldEmissionBeEnabled)
     {
         mr.material.EnableKeyword("_EMISSION");
     }
     else
     {
         mr.material.DisableKeyword("_EMISSION");
     }
 }

 static bool ShouldEmissionBeEnabled(Color color)
 {
     return color.maxColorComponent > (0.1f / 255.0f);
 }


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

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.

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

How to force the compilation of a shader in Unity? 5 Answers

Is it possible to fake Global Illumination within textures? 1 Answer

Shader compilation fails for Wii-U 2 Answers

Unity 3 Problem: Legacy Lightmap Shaders don't seem to work. 1 Answer

Shader issue with a 170 upper angle 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