can't get material.lerp() to work?

how can i get renderer.material.lerp() to work? … i’ve copied the code from the documentation:

// Blends between two materials
var material1 : Material;
var material2 : Material;
var duration = 2.0;

function Start () {
	// At start, use the first material
	renderer.material = material1;
}

function Update () {
	// Ping-pong between the materials over the duration
	var lerp = Mathf.PingPong (Time.time, duration) / duration;
	renderer.material.Lerp (material1, material2, lerp);
}

…I’ve made 2 simple materials and dumped them onto the script (which is placed on a simple cube).

when I run it I get a cube covered in the “dirt_mat” - which is my material no. 1 … but nothing else happens.

Have I misunderstood how to use this? or just missed something obvious?

(btw. using Unity 2.02).

Any ideas?

Cheers,
Mikkel

64893--2395--$picture_12_138.png
64893--2396--$picture_11_205.png
64893--2397--$picture_10_249.png

Try changing the diffuse color. That caused it to start fading between the 2 colors for me.

Do documentation states:

Not sure what they mean by ‘most often’, but it seems like it never interpolates the texture itself.

thanks - at least something is happening now … I changed the colors and I see the texture of material1 change color.

but… i was hoping to find a way to fade the entire material from one to another, not just the color.

So, if the code pasted above (in my first post) only lerps between the colors of the materials … it’s really no different than this:

(sample code from material.color documentation)

// Fade the color from red to green
// back and forth over the defined duration
var colorStart = Color.red;
var colorEnd = Color.green;
var duration = 1.0;

function Update () {
var lerp = Mathf.PingPong (Time.time, duration) / duration;
renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
}

Hmmm…

If you want to blend between two textures you should use a shader.

I think the this shader should do the trick.

Shader "Myshaders/ChangeMaterial" {
	Properties {
		_Tint ("Tint Color", Color) = (.9, .9, .9, 1.0)
		_TexMat1 ("Base (RGB)", 2D) = "white" {}
		_TexMat2 ("Base (RGB)", 2D) = "white" {}
		_Blend ("Blend", Range(0.0,1.0)) = 0.0
	}
	
	Category {
		ZWrite On
		Alphatest Greater 0
		Tags {Queue=Transparent}
		Blend SrcAlpha OneMinusSrcAlpha
		ColorMask RGB
	SubShader {
		Pass {
			
			Material {
				Diffuse [_Tint]
				Ambient [_Tint]
			}
			Lighting On
			
			SetTexture [_TexMat1] { combine texture }
			SetTexture [_TexMat2] { constantColor (0,0,0,[_Blend]) combine texture lerp(constant) previous }
			SetTexture [_TexMat2] { combine previous +- primary, previous * primary }
		}
	} 
	FallBack " Diffuse", 1
}
}
2 Likes

Thanks Bjerre it does exactly what I was looking for (and thought material.lerp() did!).

One quick question though - and this is where I yet again expose myself as a complete newbee - How can I access the properties of the shader from a javascript? I’d like to do a Mathf.PingPong on the _Blend property of the shader, my feeble attempt so far is this:

var duration = 2.0;

function Update () {
	var lerp = Mathf.PingPong (Time.time, duration) / duration;
	Shader.Find("_shaders/ChangeMaterial")._Blend = lerp;
}

…which throws the error: ‘_Blend’ is not a member of ‘UnityEngine.Shader’

…which makes sense, but how do I go about doing this?

Cheers,
Mikkel

1 Like

Hi Mikkel

You use the SetFloat function to access the the _Blend property.

so in you case it should be something like

function Update () {
    var lerp = Mathf.PingPong (Time.time, duration) / duration; 
    renderer.material.SetFloat( "_Blend", lerp );
}
1 Like

Sweet, works like a dream - thanks!

Ok, that works for simple textures. But what about crossfade between complex shaders, with normal mapping for example? There’s any soluction?

+1 @HVilela

same here +1 for normal map or decal
please any idea ?

Hi, I’m using unity 5.3.4 and material lerp still not work?
I have made a simple cube and a script with 2 textures as example.
Using the shaders, the resulting texture is a bit grayed…
any ideas?
thanks

I needed the same thing and tried this recommendation with the shader. Seems to work pretty good.
One more question thought, as I am no shader wizard, how do I make this unlit? I need it to not respond to lighting at all.