Search Unity

a shader that would scroll a multiline text from a texture

Discussion in 'Shaders' started by grobonom, Jun 19, 2019.

  1. grobonom

    grobonom

    Joined:
    Jun 23, 2018
    Posts:
    335
    Hmmm....

    Everything is in the title, and it's what i'd like to achieve...

    A multiline text on a texture that i need to draw on a banner ( the mesh ) in a scrolling manner.
    I believe i have to play with UV coordinates in the shader, but i really have no idea on how to achieve this :/

    Maybe ( surely ) it has already be done but i can find nothing about this ?

    Anyone got an idea ?

    Thanks in advance and happy unitying :)
     
  2. pitibonom

    pitibonom

    Joined:
    Aug 17, 2010
    Posts:
    220
    hm..... found the way :)

    it has to be done in the frag part of the shader with something like that:
    Code (CSharp):
    1.             fixed4 frag (v2f i) : SV_Target
    2.             {
    3.      
    4.                if(i.uv.x > 1 )
    5.                {
    6.                   float tmp;
    7.            
    8.                   tmp = i.uv.x;
    9.                   i.uv.x=frac(i.uv.x);
    10.                   tmp=tmp-i.uv.x;
    11.                   i.uv.y +=tmp*0.5;
    12.                   if(i.uv.y > 1)
    13.                   {
    14.                      i.uv.y=1;
    15.                      i.uv.x=1;
    16.                   }
    17.                }
    18.      
    19.                 // sample the texture
    20.                 fixed4 col = tex2D(_MainTex, i.uv);
    21.                 // apply fog
    22.                 UNITY_APPLY_FOG(i.fogCoord, col);
    23.                 return col;
    24.             }
    for a base texture like this one:
    bk.jpg

    giving this result:
    resoolt.gif


    The only S***ty part is the 'gray' limit generated by the non linear UV wrapping:
    here:
    shit.jpg
    looks like it's generated by some floating point accuracy.... ?
    am not sure tho :-/

    happy unitying !

    EDIT: thanks to @bgolus for his explainations in this post: https://forum.unity.com/threads/how-to-modify-uv-mapping-in-shader.537993/#post-3547684
     
    Last edited: Jun 19, 2019
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The color errors during wrapping is due to how GPUs calculate the mip level. Short version is the GPU is selecting a very small mip because the UVs are changing so much around those pixels. It can be fixed by removing mip maps, or by using a shader that sets the mip level.
    https://forum.unity.com/threads/til...shader-getting-artifacts.535793/#post-3529864


    Also, there's no reason why you couldn't just have a vertical strip of text that you pan across, unless you're looking to support old pre-Metal API iOS devices and can only use PVRTC. All other GPU texture formats don't need square textures.
     
    grobonom likes this.