Search Unity

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

Discussion in 'Scripting' started by mkoser, Feb 14, 2008.

  1. mkoser

    mkoser

    Joined:
    Feb 27, 2007
    Posts:
    21
    how can i get renderer.material.lerp() to work? ... i've copied the code from the documentation:

    Code (csharp):
    1.  
    2. // Blends between two materials
    3. var material1 : Material;
    4. var material2 : Material;
    5. var duration = 2.0;
    6.  
    7. function Start () {
    8.     // At start, use the first material
    9.     renderer.material = material1;
    10. }
    11.  
    12. function Update () {
    13.     // Ping-pong between the materials over the duration
    14.     var lerp = Mathf.PingPong (Time.time, duration) / duration;
    15.     renderer.material.Lerp (material1, material2, lerp);
    16. }
    17.  
    ...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
     

    Attached Files:

  2. Doug

    Doug

    Joined:
    Oct 19, 2007
    Posts:
    36
    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.
     
  3. mkoser

    mkoser

    Joined:
    Feb 27, 2007
    Posts:
    21
    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)

    Code (csharp):
    1.  
    2. // Fade the color from red to green
    3. // back and forth over the defined duration
    4. var colorStart = Color.red;
    5. var colorEnd = Color.green;
    6. var duration = 1.0;
    7.  
    8. function Update () {
    9. var lerp = Mathf.PingPong (Time.time, duration) / duration;
    10. renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
    11. }
    12.  
    Hmmm...
     
  4. Bjerre

    Bjerre

    Joined:
    Nov 8, 2006
    Posts:
    108
    If you want to blend between two textures you should use a shader.
     
  5. Bjerre

    Bjerre

    Joined:
    Nov 8, 2006
    Posts:
    108
    I think the this shader should do the trick.


    Code (csharp):
    1.  
    2. Shader "Myshaders/ChangeMaterial" {
    3.     Properties {
    4.         _Tint ("Tint Color", Color) = (.9, .9, .9, 1.0)
    5.         _TexMat1 ("Base (RGB)", 2D) = "white" {}
    6.         _TexMat2 ("Base (RGB)", 2D) = "white" {}
    7.         _Blend ("Blend", Range(0.0,1.0)) = 0.0
    8.     }
    9.    
    10.     Category {
    11.         ZWrite On
    12.         Alphatest Greater 0
    13.         Tags {Queue=Transparent}
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.         ColorMask RGB
    16.     SubShader {
    17.         Pass {
    18.            
    19.             Material {
    20.                 Diffuse [_Tint]
    21.                 Ambient [_Tint]
    22.             }
    23.             Lighting On
    24.            
    25.             SetTexture [_TexMat1] { combine texture }
    26.             SetTexture [_TexMat2] { constantColor (0,0,0,[_Blend]) combine texture lerp(constant) previous }
    27.             SetTexture [_TexMat2] { combine previous +- primary, previous * primary }
    28.         }
    29.     }
    30.     FallBack " Diffuse", 1
    31. }
    32. }
    33.  
    34.  
     
    turnipxenon and wenderRondonia like this.
  6. mkoser

    mkoser

    Joined:
    Feb 27, 2007
    Posts:
    21
    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:

    Code (csharp):
    1.  
    2. var duration = 2.0;
    3.  
    4. function Update () {
    5.     var lerp = Mathf.PingPong (Time.time, duration) / duration;
    6.     Shader.Find("_shaders/ChangeMaterial")._Blend = lerp;
    7. }
    8.  
    ...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
     
    wenderRondonia likes this.
  7. Bjerre

    Bjerre

    Joined:
    Nov 8, 2006
    Posts:
    108
    Hi Mikkel

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

    so in you case it should be something like

    Code (csharp):
    1.  
    2. function Update () {
    3.     var lerp = Mathf.PingPong (Time.time, duration) / duration;
    4.     renderer.material.SetFloat( "_Blend", lerp );
    5. }
    6.  
    7.  
     
    shaktip2014 likes this.
  8. mkoser

    mkoser

    Joined:
    Feb 27, 2007
    Posts:
    21
    Sweet, works like a dream - thanks!
     
  9. hvilela

    hvilela

    Joined:
    Dec 6, 2010
    Posts:
    24
    Ok, that works for simple textures. But what about crossfade between complex shaders, with normal mapping for example? There's any soluction?
     
  10. cupsster

    cupsster

    Joined:
    Apr 14, 2009
    Posts:
    363
    +1 @HVilela
     
  11. kilik128

    kilik128

    Joined:
    Jul 15, 2013
    Posts:
    909
    same here +1 for normal map or decal
    please any idea ?
     
  12. sefiroths

    sefiroths

    Joined:
    Dec 13, 2014
    Posts:
    24
    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
     
  13. Privateer

    Privateer

    Joined:
    Jun 5, 2016
    Posts:
    23
    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.