Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by tancas · May 14, 2020 at 11:49 AM · shadermaterialeditor-scripting

Saving changes made on a material from an editor script

I have an editor script generating a mesh, applying a material to that mesh, and updating some values on the material to pass on to a custom shader (The shader basically displays different colors according to mesh height) My problem is that the changes made on the material aren't saved. If I save the scene where my mesh is generated, the materials values are lost.

Here is a code snippet executed in my editor scripts that sets the materials values : ("material " is a public variable, set in unity inspector)

         material.SetInt("baseColorCount", baseColors.Length);
         material.SetColorArray("baseColors", baseColors);
         material.SetFloatArray("baseStartHeights", baseStartHeights);
         material.SetFloatArray("baseBlends", baseBlends);
         meshGameObject.GetComponent<MeshRenderer>().sharedMaterial = material;
         

And here is the shader :

 Shader "Custom/TerrainShader"
 {
     Properties
     {
     }
     SubShader
     {
         Tags { "RenderType"="Opaque" }
         LOD 200
 
         CGPROGRAM
         #pragma surface surf Standard fullforwardshadows
         #pragma target 3.0
         
         const static int maxColorCount = 10;
         // To avoid potential div / 0 problems in inverseLerp
         const static float epsilon = 1E-4;
          
         int baseColorCount;
         float3 baseColors[maxColorCount];
         float baseStartHeights[maxColorCount];
         float baseBlends[maxColorCount];
         
         float minHeight;
         float maxHeight;
         
         struct Input
         {
             float3 worldPos;
         };
 
         float inverseLerp(float a, float b, float value) {
             return saturate((value-a)/(b-a));
         }
 
         void surf (Input IN, inout SurfaceOutputStandard o)
         {
             float heightPercent = inverseLerp(minHeight, maxHeight, IN.worldPos.y);
             for (int i = 0; i < baseColorCount; i++) 
             {
                 float drawStrength = inverseLerp(-baseBlends[i]/2 - epsilon, baseBlends[i]/2 - epsilon, heightPercent - baseStartHeights[i]);
                 o.Albedo = o.Albedo * (1 - drawStrength) + baseColors[i] * drawStrength;
             }
         }
         ENDCG
     }
     FallBack "Diffuse"
 }

Now after my code is executed, everything is ok : alt text

But if I save the scene for example, the material loses its values :

alt text

I tried loading the material from AssetsDatabase / Saving it with SaveAssets() but that didn't change anything.

I feel like I'm missing something but I can't figure it out.

What should I do so that the changes I made on a material in my editor script are actually saved on the material ?

material-ok.png (348.4 kB)
material-ko.png (200.8 kB)
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 tancas · May 14, 2020 at 12:58 PM 0
Share

I found a begining of a solution soon after posting this : Apparently I have to use Shader properties. But as I understand it, it's not possible to use arrays in shader properties.

1 Reply

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

Answer by tancas · May 14, 2020 at 01:12 PM

The solution is to use Shader properties, they will be saved, unlike the values set with SetFloatArray, SetInt etc. But you can't use arrays in shader properties, which is sad :/

More info here : https://docs.unity3d.com/Manual/SL-Properties.html

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

290 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 know whether material's emission is enabled or not by C# script? 0 Answers

Problem with fresnel materials 0 Answers

Unity 2018.1.2f1 doesn't apply textures to material if I use HDRenderPipeline. 0 Answers

Using Texture as HDR color? 2 Answers

Shader not working properly in a build 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