- Home /
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.
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.
Thanks for explanation. I misunderstood how $$anonymous$$aterial.Lerp works. Now it's clearer!
Your answer
Follow this Question
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