• 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
12
Question by Luca Belluccini · Mar 04, 2015 at 02:28 PM · shaderunity5

Standard Shader Emission control via Script

Hello, I wanted to try out Unity5 Standard Shader feature to re-create one of my custom shaders.

It is basically an emitting shader with an alpha parameter to control the amount of emission at runtime.

Is it possible to control the emission of the standard shader at runtime?

Comment
Add comment · Show 3
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 meat5000 ♦ · Mar 04, 2015 at 03:25 AM 0
Share

Something like this?

http://docs.unity3d.com/ScriptReference/$$anonymous$$aterial.SetFloat.html

avatar image Luca Belluccini · Mar 13, 2015 at 09:42 AM 0
Share

Yes meat5000, the only problem was the fact I did not know the "parameter name" of the emission.

avatar image duck ♦♦ · Jul 10, 2015 at 04:05 PM 0
Share

Also read this if you're setting values on a material using the Standard Shader:

http://docs.unity3d.com/$$anonymous$$anual/$$anonymous$$aterialsAccessingViaScript.html

Important info.

5 Replies

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

Answer by dangerdex · Mar 13, 2015 at 12:31 AM

Hi Luca,

I was finally able to get this to work after about an hour and a half of research.

Here is my code:

 void Update () {
         Renderer renderer = GetComponent<Renderer> ();
         Material mat = renderer.material;
 
         float emission = Mathf.PingPong (Time.time, 1.0f);
         Color baseColor = Color.yellow; //Replace this with whatever you want for your base color at emission level '1'
 
         Color finalColor = baseColor * Mathf.LinearToGammaSpace (emission);
 
         mat.SetColor ("_EmissionColor", finalColor);
     }

This should be all you need.

If you want to oscillate between something other than 0 and your ceiling (1.0 in this case) just declare emission as such:

 float floor = 0.3f;
 float ceiling = 1.0f;
 float emission = floor + Mathf.PingPong (Time.time, ceiling - floor);

If you want to slow down or speed up the oscillation, just multiply Time.time by a constant. Multiplying Time.time by a constant greater than 1 will speed up the oscillation. Multiplying Time.time by a constant less than 1 will slow down the oscillation.

Best of luck in your development! Make good things!

Comment
Add comment · Show 7 · 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 Luca Belluccini · Mar 13, 2015 at 09:42 AM 0
Share

I'll try it... Where did you found the "_EmissionColor" name?

avatar image dangerdex · Mar 13, 2015 at 03:40 PM 0
Share

Someone else's forum post in my research led me to try to find the standard shader source. I couldn't find it in the standard resources that come with Unity but I eventually found it in the download archive

https://unity3d.com/get-unity/download/archive

In the Unity5 shader source materials you can find the actual names that you need to reference by searching for <<"_>> to give you all strings starting with a double-quote and an underscore. I knew I wanted a name beginning with an underscore because of other posts I'd seen about the subject, but just didn't know which one. This one finally prevailed!

avatar image St0fF · Mar 30, 2015 at 10:28 PM 0
Share

To find the Properties of any Shader I recommend you put the following script temporarily into the object that has the shader attached:

 using UnityEngine;
 using System;
 using UnityEditor;
 using System.Collections.Generic;
 
 [ExecuteInEdit$$anonymous$$ode]
 public class CheckShader: $$anonymous$$onoBehaviour
 {
     void OnEnable ()
     {
         Shader s = GetComponent<$$anonymous$$eshRenderer>().shared$$anonymous$$aterial.shader;
         List<String> props = new List<String>();
         for ( int i = 0; i < ShaderUtil.GetPropertyCount( s ); ++i )
             props.Add( ShaderUtil.GetPropertyName( s, i ) );
         Debug.Log( String.Join("\n", props.ToArray()) );
     }
 }

And have a look at the console after re-enabling the object.

avatar image xanat0s St0fF · Jan 19, 2016 at 02:48 PM 1
Share

Not to di$$anonymous$$ish your hard work, but all this data is available right in the editor if you switch Inspector to debug mode. Implementing a script for it is entirely unnecessary (and will add useless overhead to your build.)

avatar image Ascensi · Mar 10, 2018 at 01:16 PM 0
Share

@dangerdex Would you by any chance know about how to get the emissive state to switch to off or on with no levels of dim$$anonymous$$g inbetween? I've tried the above method and while adding it to a button/switch does toggle it, the emission doesn't turn off completely until about the third button press.

I understand there is another method as well - material swap but haven't got this to work correctly. Ultimately I'm trying to set emissive toggle for VR, I have physical switches/transforms that act as the trigger.

avatar image RendCycle · Sep 02, 2018 at 10:29 AM 0
Share

Worked nicely! Thank you! $$anonymous$$y version didn't actually need to use a Renderer Component but requires dragging the gameobject $$anonymous$$aterial to a public field. I also have a speed and maxdistance variables to have extra control in the pulsate effect. This also just uses the color specified in the material Inspector. Here is a snippet of the function.

 public $$anonymous$$aterial lightBulbOn$$anonymous$$aterial;
 
 void StartPulsating (float $$anonymous$$Intensity, float maxIntensity, float pulsateSpeed, float pulsate$$anonymous$$axDistance)
      float emission = $$anonymous$$athf.Lerp ($$anonymous$$Intensity, maxIntensity, $$anonymous$$athf.PingPong (Time.time * pulsateSpeed, pulsate$$anonymous$$axDistance));
      Color baseColor = lightBulbOn$$anonymous$$aterial.color;
      Color finalColor = baseColor * $$anonymous$$athf.LinearToGammaSpace (emission);
      lightBulbOn$$anonymous$$aterial.SetColor ("_EmissionColor", finalColor);
 }
Show more comments
avatar image
33

Answer by ikriz · May 06, 2015 at 01:22 PM

Standard Shader Properties Here for anyone interested

 _Color
 _MainTex
 _Cutoff
 _Glossiness
 _Metallic
 _MetallicGlossMap
 _BumpScale
 _BumpMap
 _Parallax
 _ParallaxMap
 _OcclusionStrength
 _OcclusionMap
 _EmissionColor
 _EmissionMap
 _DetailMask
 _DetailAlbedoMap
 _DetailNormalMapScale
 _DetailNormalMap
 _UVSec
 _EmissionScaleUI
 _EmissionColorUI
 _Mode
 _SrcBlend
 _DstBlend
 _ZWrite
Comment
Add comment · Show 6 · 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 rustofelees · Jun 03, 2015 at 07:25 AM 0
Share

Hello,

Thanks for listing the properties, that was most helpful.

I recently tried something like this:

mr.materials[1].SetFloat("_EmissionScaleUI", 5f);

While the value changed in the UI, the emission value does not update until I manually go and change the shader in the UI. Is this a bug? Is there another means of changing the emission value?

Thanks!

avatar image Magnumstar · May 21, 2016 at 01:24 PM 0
Share

Same here. Emission doesn't pulse using set Emission until I click in the inspector. Weird bug, or feature?

avatar image naftalibeder Magnumstar · Dec 07, 2017 at 07:37 PM 1
Share

For anyone confused by the need to click on the inspector to make the emission value display, @vostok4's answer below solves it:

 material.Enable$$anonymous$$eyword("_E$$anonymous$$ISSION");
avatar image nsfnotthrowingaway · Jun 13, 2016 at 07:44 PM 0
Share

Hi. Thank you so much for listing these. $$anonymous$$ay I ask where you found this information? Was it in the documentation, or in source files?

avatar image sun_stealer · Jan 20, 2017 at 03:43 PM 0
Share

How do I change metallic smoothness?

avatar image AtomR · Oct 31, 2017 at 03:09 PM 0
Share

To control emissiveness in real time you need to change the _EmissionColor using script.

avatar image
1

Answer by Tonks · May 13, 2015 at 07:36 PM

Or, you can select a material, right click, "select shader" and they will all be listed in your inspector. This DOES NOT WORK if you have Shaderforge installed, however. So St0Ff's little script will come in handy for those who use it.

Incidentally, I am attempting to turn on emmissive on my materials, the following doesn't work.

     private const string EmissiveValue = "_EmissionScaleUI";
     private const string EmissiveColour = "_EmissionColor";
 if (NightMaterials != null) {
                 foreach (Material nightMaterial in NightMaterials) {
                     //Color colour = nightMaterial.GetColor(EmissiveColour);
                     nightMaterial.SetFloat(EmissiveValue, 1);
                     //nightMaterial.SetColor(EmissiveColour, colour);
                     nightMaterial.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
                     Debug.Log(nightMaterial.name + " Emmisive Value = " + nightMaterial.GetFloat(EmissiveValue));
                 }
             }


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 vostok4 · Jun 18, 2017 at 03:24 PM 5
Share

To change if Emissive is on or off in your material at runtime, use the "_E$$anonymous$$ISSION" shader keyword.

 // Turn on emission
 material.Enable$$anonymous$$eyword("_E$$anonymous$$ISSION");
 
 // Turn off emission
 material.Disable$$anonymous$$eyword("_E$$anonymous$$ISSION");
avatar image astracat111 vostok4 · Feb 23, 2018 at 03:36 AM 0
Share

Thanks! This is greatly helpful!

avatar image xCyborg · Feb 02, 2018 at 09:28 PM 0
Share

@vostok4 That's what I've been looking for, thanks. Do you know where we could get the list of keywords?

avatar image
0

Answer by FableStudio · Jun 04, 2020 at 03:51 PM

alt text @xCyborg The keywords are found in the Edit Shader panel. Click on the three-dot button to expand the list.


keywords.png (117.2 kB)
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 dave_sf_42 · Jul 08, 2020 at 06:19 AM

// turn on emission shader flag material.EnableKeyword("_EMISSION");
// turn on realtime emissive GI material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive; material.SetColor("_EmissionColor", new Color(1.0f,1.0f,1.0f)); material.SetTexture("_EmissionMap"), TryLoadTexture(texturePath)); , // turn on emission shader flag material.EnableKeyword("_EMISSION");
// turn on realtime emissive GI material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive; material.SetColor("_EmissionColor", new Color(1.0f,1.0f,1.0f)); material.SetTexture("_EmissionMap"), TryLoadTexture(texturePath));

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.

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

22 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

Related Questions

Unity 5 Fog not working on Standard Shader Objects 3 Answers

NavMeshAgent dont find the right way on runtime build NavMesh ,Agents didn´t walk right on a runtime generated NavMesh 0 Answers

Unity 5 prefab with FBX and camera as child renders in black 0 Answers

Writing a shader for a terrain? 0 Answers

Unity5 CG Shader get model position. 1 Answer


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