Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by JulienEmoko · Mar 29 at 09:22 AM · shaderrenderingmaterialeditor-scriptingglobal illumination

How to save emissive Material assets using "_EMISSION" keyword by script?

I have an editor script that generates emissive Material assets using the Standard shader. This works by first creating a temporary Material, then enabling the keyword "_EMISSION" and finally saving the asset using AssetDatabase (example code below).

  public static class EmissiveMaterialCreator
  {
      [MenuItem("Debug/Create Emissive Material Asset")]
      public static void CreateEmissiveMaterialAsset()
      {
          Material material = CreateEmissiveMaterial();
          AssetDatabase.CreateAsset(material, "Assets/EmissiveMaterial.asset");
      }
      private static Material CreateEmissiveMaterial()
      {
          Material material = new Material(Shader.Find("Standard"));
  
          material.name = "Emissive Material";
          material.color = Color.white;
  
          material.EnableKeyword("_EMISSION");
          material.SetColor("_EmissionColor", Color.blue);
  
          return material;
      }
  }

However when using this script, the asset created doesn't have an enabled toggle at the property "Emission", and the material does not render correctly in the scene and during GI baking.

I realized when playing with the asset file that toggling this property manually in the inspector, a property "m_LightmapFlags" in the file changes from 4 to 2, but when created by script this value is never set to 2, which would enable to "Emission" toggle.

  %YAML 1.1
  %TAG !u! tag:unity3d.com,2011:
  --- !u!21 &2100000
  Material:
    serializedVersion: 6
    m_ObjectHideFlags: 0
    m_CorrespondingSourceObject: {fileID: 0}
    m_PrefabInstance: {fileID: 0}
    m_PrefabAsset: {fileID: 0}
    m_Name: EmissiveMaterial
    m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
    m_ShaderKeywords: _EMISSION
    m_LightmapFlags: 4
    ...

My question now is how should I proceed to change this property so that the materials get saved properly ?

Comment
Add comment · Show 1
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 JulienEmoko · Mar 29 at 11:26 AM 0
Share

A quick and dirty fix for this issue would be to directly edit the asset file, here is an basic example of how this would work :

   public static class EmissiveMaterialCreator
   {
     public static void CreateEmissiveCubePrefab()
     {
         Material material = CreateEmissiveMaterial();
         CreateMaterialAsset(material, "Assets/EmissiveMaterial.asset");
     }
 
     public static void CreateMaterialAsset(Material material, string path)
     {
         // First, create normally the asset using AssetDatabase...
         AssetDatabase.CreateAsset(material, path);
 
         // ... then read the file content and edit it if the keyword for emission is found
         string text = File.ReadAllText(path);
         if (text.Contains("_EMISSION"))
         {
             text = text.Replace("m_LightmapFlags: 4", "m_LightmapFlags: 2");
             File.WriteAllText(path, text);
         }
 
         // ... finally refresh the asset to avoid any issue in the editor
         AssetDatabase.Refresh();
     }
   }

This doesn't account for the potential variation of the flag value (1, 3, 5, etc.), but a basic parsing improvement would take care of this detail. The point is in the end, the Material asset is now behaving as expected.

1 Reply

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

Answer by JulienEmoko · Mar 29 at 12:20 PM

The solution is to set the field globalIlluminationFlags of the Material to whichever value is required. In my case, the emission has to be baked, so the value to set is BakedEmissive.

Tip : if the material is not emissive, the value would be EmissiveIsBlack.

 private static Material CreateEmissiveMaterial()
 {
     Material material = new Material(Shader.Find("Standard"));
 
     material.name = "Emissive Material";
     material.color = Color.white;
 
     material.EnableKeyword("_EMISSION");
     material.globalIlluminationFlags = MaterialGlobalIlluminationFlags.BakedEmissive;
 
     material.SetColor("_EmissionColor", Color.blue);
 
     return material;
 }
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

240 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 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 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 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 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 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 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 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 apply my custom shader to scene lightmap/light probes 2 Answers

Why does the frame debugger say node uses different shader when it isn't? 0 Answers

How can i get my quad to only render my texture without stretching it? 1 Answer

Using Color.Lerp with Lightweight Render Pipeline 1 Answer

Standard Terrain Shader Upgrade to URP 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