Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

saving scene resets shader property

Discussion in 'Shaders' started by Airship, Dec 15, 2011.

  1. Airship

    Airship

    Joined:
    Sep 10, 2011
    Posts:
    260
    Hi,

    I have a shader made in SSE that has 2 different float properties (Intensity and Mod). When I save the scene the "mod" float property always resets to 2. I do not know why. The other float property stays at whatever it is set too.


    Why might this be occuring?




    Code (csharp):
    1. Shader "LightControlReflective"
    2. {
    3.     Properties
    4.     {
    5. _MainTex("Base (RGB) Gloss (A)", 2D) = "white" {}
    6. _Color("Main Color", Color) = (1,1,1,1)
    7. _LightColor("_LightColor", Color) = (1,1,1,1)
    8. _Intensity("_Intensity", Float) = 1.5
    9. _Mod("_Mod", Float) = 2
    10. _Lightmap("_Lightmap", 2D) = "black" {}
    11. _Cube("Reflection Cubemap", Cube) = "black" {}
    12.  
    13.     }
    14.    
    15.     SubShader
    16.     {
    17.         Tags
    18.         {
    19. "Queue"="Geometry"
    20. "IgnoreProjector"="False"
    21. "RenderType"="Opaque"
    22.  
    23.         }
    24.  
    25.        
    26. Cull Back
    27. ZWrite On
    28. ZTest LEqual
    29. ColorMask RGBA
    30. Fog{
    31. }
    32.  
    33.  
    34.         CGPROGRAM
    35. #pragma surface surf BlinnPhongEditor  vertex:vert
    36. #pragma target 2.0
    37.  
    38.  
    39. sampler2D _MainTex;
    40. float4 _Color;
    41. float4 _LightColor;
    42. float _Intensity;
    43. float _Mod;
    44. sampler2D _Lightmap;
    45. samplerCUBE _Cube;
    46.  
    47.             struct EditorSurfaceOutput {
    48.                 half3 Albedo;
    49.                 half3 Normal;
    50.                 half3 Emission;
    51.                 half3 Gloss;
    52.                 half Specular;
    53.                 half Alpha;
    54.                 half4 Custom;
    55.             };
    56.            
    57.             inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
    58.             {
    59. half3 spec = light.a * s.Gloss;
    60. half4 c;
    61. c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
    62. c.a = s.Alpha;
    63. return c;
    64.  
    65.             }
    66.  
    67.             inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    68.             {
    69.                 half3 h = normalize (lightDir + viewDir);
    70.                
    71.                 half diff = max (0, dot ( lightDir, s.Normal ));
    72.                
    73.                 float nh = max (0, dot (s.Normal, h));
    74.                 float spec = pow (nh, s.Specular*128.0);
    75.                
    76.                 half4 res;
    77.                 res.rgb = _LightColor0.rgb * diff;
    78.                 res.w = spec * Luminance (_LightColor0.rgb);
    79.                 res *= atten * 2.0;
    80.  
    81.                 return LightingBlinnPhongEditor_PrePass( s, res );
    82.             }
    83.            
    84.             struct Input {
    85.                 float4 color : COLOR;
    86. float2 uv_MainTex;
    87. float4 meshUV;
    88. float3 viewDir;
    89.  
    90.             };
    91.  
    92.             void vert (inout appdata_full v, out Input o) {
    93. float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
    94. float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
    95. float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
    96. float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
    97.  
    98. o.meshUV.xy = v.texcoord.xy;
    99. o.meshUV.zw = v.texcoord1.xy;
    100.  
    101.             }
    102.            
    103.  
    104.             void surf (Input IN, inout EditorSurfaceOutput o) {
    105.                 o.Normal = float3(0.0,0.0,1.0);
    106.                 o.Alpha = 1.0;
    107.                 o.Albedo = 0.0;
    108.                 o.Emission = 0.0;
    109.                 o.Gloss = 0.0;
    110.                 o.Specular = 0.0;
    111.                 o.Custom = 0.0;
    112.                
    113. float4 Split0=IN.color;
    114. float4 Assemble0_3_NoInput = float4(0,0,0,0);
    115. float4 Assemble0=float4(float4( Split0.x, Split0.x, Split0.x, Split0.x).x, float4( Split0.y, Split0.y, Split0.y, Split0.y).y, float4( Split0.z, Split0.z, Split0.z, Split0.z).z, Assemble0_3_NoInput.w);
    116. float4 Sampled2D0=tex2D(_MainTex,IN.uv_MainTex.xy);
    117. float4 Multiply0=Sampled2D0 * _Color;
    118. float4 Tex2D0=tex2D(_Lightmap,(IN.meshUV.zwzw).xy);
    119. float4 Multiply8=Tex2D0 * float4( Split0.w, Split0.w, Split0.w, Split0.w);
    120. float4 Multiply2=_Intensity.xxxx * Multiply8;
    121. float4 Multiply1=_LightColor * Multiply2;
    122. float4 Multiply3=Sampled2D0 * Multiply1;
    123. float4 Add0=Multiply0 + Multiply3;
    124. float4 TexCUBE0=texCUBE(_Cube,float4( IN.viewDir.x, IN.viewDir.y,IN.viewDir.z,1.0 ));
    125. float4 Multiply5=Sampled2D0.aaaa * TexCUBE0;
    126. float4 Multiply6=_LightColor * Multiply5;
    127. float4 Add2=Add0 + Multiply6;
    128. float4 Multiply4=Assemble0 * Add2;
    129. float4 Multiply7=_Mod.xxxx * Multiply4;
    130. float4 Master0_1_NoInput = float4(0,0,1,1);
    131. float4 Master0_2_NoInput = float4(0,0,0,0);
    132. float4 Master0_3_NoInput = float4(0,0,0,0);
    133. float4 Master0_4_NoInput = float4(0,0,0,0);
    134. float4 Master0_5_NoInput = float4(1,1,1,1);
    135. float4 Master0_7_NoInput = float4(0,0,0,0);
    136. float4 Master0_6_NoInput = float4(1,1,1,1);
    137. o.Albedo = Multiply7;
    138.  
    139.                 o.Normal = normalize(o.Normal);
    140.             }
    141.         ENDCG
    142.     }
    143.     Fallback "Diffuse"
    144. }
     
  2. Airship

    Airship

    Joined:
    Sep 10, 2011
    Posts:
    260
    I dont know why but, I renamed the _Mod property to _Modulate and it works now...
     
  3. fox

    fox

    Joined:
    Jan 14, 2009
    Posts:
    118
    Have the exact same problem now using the latest Unity release. Random float values defaults back to the written value in the shader upon save. Anyone know anything more about this?
     
  4. Kimau

    Kimau

    Joined:
    Jan 7, 2011
    Posts:
    7
    Same problem here its driving me nuts.

    The code below will only on SOME materials cause the colour data to be lost. Setting the texture or float properties seem to be fine. Only confined to vectors.

    Code (csharp):
    1.  
    2. Debug.Log("Set Colour on " + targetMat.name);
    3.                 targetMat.SetColor("_DarkColour", dark);
    4.                 targetMat.SetColor("_MidColour", mid);
    5.                 targetMat.SetColor("_LightColour", light);
    6.                 EditorUtility.SetDirty(targetMat);
    7.                 Debug.Log("Set Colour on "
    8.                     + " -- " + targetMat.name
    9.                     + " -- " + AssetDatabase.GetAssetPath(targetMat)
    10.                     + " -- " + mid
    11.                     + " -- " + targetMat.hideFlags
    12.                     + " -- " + targetMat.shader.hideFlags);
    13.                
    14.                 AssetDatabase.SaveAssets();
    15.                 mid = targetMat.GetColor("_MidColour");
    16.                 Debug.Log(mid);
    17.  
     
  5. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    795
    I know this is an old thread, but I'm getting the same problem in Unity 5.3.1p3. Also, switching applications and coming back to the editor seems to reset them too.

    This happens on materials instances created within the editor at runtime (i.e. through extending the editor) for me.

    Did anybody ever figure it out?
     
  6. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    795
    Bumping this 7 years later...
     
  7. POOKSHANK

    POOKSHANK

    Joined:
    Feb 8, 2022
    Posts:
    73
    instances are disposed of when no longer needed so i figure the garbage collector took your materials away

    ie material instances in playmode are destroyed when exiting playmode