Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to make a scrolling texture shader with two textures

Discussion in 'Shaders' started by alevjen, May 25, 2023.

  1. alevjen

    alevjen

    Joined:
    Sep 11, 2020
    Posts:
    11
    I'm trying to figure out how to make a simple transition with two textures, but it seems to be proving harder than expected.

    I want the first texture to be on the quad and start scrolling down off the quad, while the second texture appears on top and scrolls down to replace it.

    I have this working, however, the lerp means that the textures are blended together mid way between the scroll. I assume there is a way to make the textures fully opaque for the whole transition, but I cant for the life of me figure out how!

    Here is the transition code which does what I want but also fades:


    Code (CSharp):
    1. incomingTexUV.y -= 1 - transitionProgress;
    2. incomingTexColor = tex2D(_IncomingTex, ncomingTexUV);
    3. mainTexUV.y += transitionProgress;
    4. mainTexColor = tex2D(_MainTex, mainTexUV);
    5. fixed4 finalColor = lerp(mainTexColor, incomingTexColor, transitionProgress);
    6. o.Albedo = finalColor.rgb;
     
  2. alevjen

    alevjen

    Joined:
    Sep 11, 2020
    Posts:
    11
    I figured it out!

    Code (csharp):
    1.  float scrollProgress = mainTexUV.y + transitionProgress;
    2.                  
    3. // Only display the main texture if it's within the scroll bounds
    4. if (scrollProgress >= 0 && scrollProgress <= 1)
    5. {
    6.       mainTexUV.y = scrollProgress;
    7. }
    8. mainTexColor = tex2D(_MainTex, mainTexUV);
    9. scrollProgress = incomingTexUV.y + (transitionProgress - 1);
    10. // Only display the scrolling texture if it's within the scroll bounds
    11. if (scrollProgress >= 0 && scrollProgress <= 1)
    12. {
    13.     incomingTexUV.y = scrollProgress;
    14. }
    15. incomingTexColor = tex2D(_IncomingTex,incomingTexUV);
    16. fixed4 finalColor = lerp(mainTexColor, incomingTexColor, step(0, scrollProgress));
    17. o.Albedo = finalColor.rgb;