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 /
avatar image
0
Question by backer_s · Mar 16, 2017 at 08:36 PM · shadermaterialshadersshader programming

Material.Lerp() with different shaders problem!

Hi,

I'm trying to reach something similar to the Unity's hierarchy gray-scale search feature using Material.Lerp() to lerp between 2 materials, Material1 and Material2. Both materials have same textures but different shaders. Material1 uses the Standard shader and Material2 uses 'GrayScale' shader that turns the colors(without loosing textures) to gray-scale. When I use Material.Lerp() function it doesn't work! As I understand from the documentation that in order this function to work, I should use the same textures on both materials witch I do. I went though all the precess of learning shader scripting to be able to solve this! I created a custom shader that is very similar to the standard shader. When I use the custom shader on Material1 and the grey-scale shader on Material2 the Material.Lerp() works but it lerps from the color on Material1 to the color on Material2 overriding the gray-scale effect. I could not understand how it does this. I suppose that Material.Lerp() should lerp to the color that is provided by the shdader (the gray-scalse shader). in other words, the gray-scaled color, but it's not. Why it does that and why it doesn't work with the standard shader but works on my custom shader? Here's all custom shaders I use

 GrayScale.shader
 
 Shader "Custom/GrayScale" {
     Properties{
         _Color("Color", Color) = (1,1,1,1)
         _MainTex("Albedo (RGB)", 2D) = "white" {}
     _Glossiness("Smoothness", Range(0,1)) = 0.5
         _Metallic("Metallic", Range(0,1)) = 0.0
     }
         SubShader{
         Tags{ "RenderType" = "Opaque" }
         LOD 200
 
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
 #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
 #pragma target 3.0
 
         sampler2D _MainTex;
 
     struct Input {
         float2 uv_MainTex;
     };
 
     half _Glossiness;
     half _Metallic;
     fixed4 _Color;
 
     void surf(Input IN, inout SurfaceOutputStandard o) {
         // Albedo comes from a texture tinted by color
         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
         //o.Albedo = c.rgb;
         o.Albedo = (c.r + c.g + c.b)/2;
         // Metallic and smoothness come from slider variables
         o.Metallic = _Metallic;
         o.Smoothness = _Glossiness;
         o.Alpha = c.a;
     }
     ENDCG
     }
         FallBack "Diffuse"
 }
 

And this is the custom shader I created

 Shader "Custom/1" 
 {
     Properties 
     {
         _Color ("Color Tint", Color) = (1,1,1,1)
         _SpecColor("Specular Color", Color) = (1,1,1,1)
         _Shininess("Speq Power", Range(-1,10)) = 0.5
         _MainTex("Main Texture", 2D) = "white"{}
         _BumpMap("Normal Map", 2D) = "bump"{}
         _RimColor("Rim Color", Color) = (1,1,1,1)
         _RimPower("Rim Power", Range(0.0, 10.0)) = 3.0
             
     }
 
     SubShader 
     {
         Tags { "RenderType"="Opaque" }
         
         CGPROGRAM
         #pragma surface surf BlinnPhong
 
 
         struct Input 
         {
             float4 color : COLOR;
             float2 uv_MainTex;
             float2 uv_BumpMap;
             float3 viewDir;
         };
         
         sampler2D _MainTex;
         sampler2D _BumpMap;
         float4 _Color;
         float4 _RimColor;
         float _RimPower;
         float _Shininess;
 
         void surf(Input IN, inout SurfaceOutput o)
         {
             //Handle texture
             fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
             float3 bump = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
 
             //Apply normal map
             o.Normal = bump.rgb;
 
             //Apply diffuse texture
             o.Albedo = tex.rgb * _Color.rgb;
             
             //Specular
             o.Specular = _Shininess;
             o.Gloss = tex.a;
 
             //Rim lights
             half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
             o.Emission = _RimColor.rgb * pow(rim, _RimPower);
 
         }
         ENDCG
     }
     FallBack "Diffuse"
 }
 

Any help is much appreciated.

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

1 Reply

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

Answer by Namey5 · Mar 17, 2017 at 08:17 AM

I don't think you understand how Material.Lerp works. It doesn't interpolate between two materials/shaders, rather it simply interpolates the common properties. As such, if the shaders are different it won't do particularly much. This is stated in the same section as the similar textures, as you mentioned previously;

"Most often you want the materials that are interpolated between to be the same (use the same shaders and textures) except for colors and floats."

Your best bet would be to make some form of uber shader that handles the interpolations internally.

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 backer_s · Mar 17, 2017 at 10:16 AM 0
Share

Thanks for explanation. I misunderstood how $$anonymous$$aterial.Lerp works. Now it's clearer!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

RimLight Shader 0 Answers

Unlit Transparent With Color Support 1 Answer

create a texture with shader code? ( without using a texture ) 1 Answer

How to add Emission to my custom shader? 2 Answers

The Best Way To Make Stylised Grass in Unity? 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