Search Unity

Shader based Non Atlas Image Scroller

Discussion in 'Shaders' started by GoRiLLa, Sep 23, 2015.

  1. GoRiLLa

    GoRiLLa

    Joined:
    Oct 8, 2013
    Posts:
    14
    Hi, dear Unity community.

    I'm going to make an world space ad scroller just like this one:


    Making this using an atlas is trivial by adjusting the texture offsets. However, I have to find a solution for doing it without an atlas, because I am going to download images on the fly and don't want to spend cpu cycles on combining them into one big texture atlas in real time.

    My shader knowledge is a bit lacking, but my guess is that this would be possible and probably best to do using some kind of a shader. However, I am sure there are other ways of doing what I want as well, like adding alpha pixels at the texture borders, set wrap mode to clamp and then adjust the offsets, but this sounds expensive fill rate wise and I'm not sure if this is the best solution.

    So, if anyone could nudge me in the right direction here it would be greatly appreciated!
     
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    Hi, I created a shader example for you that shows how you can scroll two separate textures to mimic the look of the video you posted. The example assumes the mesh has UV's from 0,0 to 1,1. At the bottom of this post you can find a download link of the example.

    Here is a video that shows what the shader does.


    The idea is that the material has two textures and a "progress" property. The progress is used to scroll the texture(s). Once it reached a progress of 1, you would need to make the 2nd texture the 1st one and reset the progress to 0. From there you can assign the next ad-texture to the 2nd slot and repeat.

    Code (CSharp):
    1. Shader "Custom/Diffuse Ad Scroller" {
    2. Properties {
    3.   _Color ("Main Color", Color) = (1,1,1,1)
    4.   _MainTex ("Base (RGB)", 2D) = "white" {}
    5.   _Main2Tex ("Base 2 (RGB)", 2D) = "black" {}
    6.   _Progress ("Progress", Range(0,1)) = 0
    7. }
    8. SubShader {
    9.   Tags { "RenderType"="Opaque" }
    10.   LOD 200
    11.  
    12. CGPROGRAM
    13. #pragma surface surf Lambert vertex:vert
    14.  
    15. sampler2D _MainTex;
    16. half4 _MainTex_ST;
    17. sampler2D _Main2Tex;
    18. fixed4 _Color;
    19. half _Progress;
    20.  
    21. struct Input {
    22.   fixed2 st_MainTex;
    23. };
    24.  
    25. void vert (inout appdata_full v, out Input o)
    26. {
    27.   UNITY_INITIALIZE_OUTPUT(Input,o);
    28.  
    29.   o.st_MainTex  = TRANSFORM_TEX(v.texcoord, _MainTex)  + half2(1,0) * _Progress;
    30. }
    31.  
    32. void surf (Input IN, inout SurfaceOutput o)
    33. {
    34.   fixed4 c0 = tex2D(_MainTex, IN.st_MainTex);
    35.   fixed4 c1 = tex2D(_Main2Tex, IN.st_MainTex);
    36.   fixed4 c = lerp(c0, c1, step(IN.st_MainTex.x, 1));
    37.  
    38.   o.Albedo = c.rgb * _Color.rgb;
    39.   o.Alpha = c.a * _Color.a;
    40. }
    41. ENDCG
    42. }
    43.  
    44. Fallback "Legacy Shaders/VertexLit"
    45. }
    46.  


    Hope it helps!

    Here is the download link
    http://www.console-dev.de/bin/Unity_Example_AdScroller.zip
     
    GoRiLLa likes this.
  3. GoRiLLa

    GoRiLLa

    Joined:
    Oct 8, 2013
    Posts:
    14
    Wow, thanks a bunch Peter77!

    That's perfect, exactly what I was looking for and I learned a trick or two as well.

    I really appreciate the effort, thanks again! :)
     
    Peter77 likes this.