Search Unity

Weird shader problem on mobile

Discussion in 'Shaders' started by Edgaras-Randis, Dec 22, 2014.

  1. Edgaras-Randis

    Edgaras-Randis

    Joined:
    Jul 8, 2014
    Posts:
    20
    Here's the deal. Each "animated line" is a material with shader attached to ugui image component. For some reason, on android (haven't tested on other devices) after few seconds it starts to be noticeable that line segments increases constantly.

    Editor:




    Android after a half a minute in and it gets worse:



    A second look from someone would be awesome.

    Code (CSharp):
    1.  
    2. Shader "Custom/GlowLine"
    3. {
    4.     Properties
    5.     {
    6.         _Speed("Speed", float) = 5
    7.         _Amplitude("Amplitude", float) = 0.025
    8.         _Offset("Offset", float) = 0.0
    9.         _Width("Width", float) = 100
    10.         _Color("Color", color) = (1,1,1,1)
    11.     }
    12.    
    13.     SubShader
    14.     {
    15.         Tags { "Queue" = "Transparent" }
    16.        
    17.         Pass
    18.         {
    19.             Blend SrcAlpha OneMinusSrcAlpha  
    20.             Cull off
    21.        
    22.             CGPROGRAM
    23.             #pragma target 2.0
    24.             #pragma vertex Vert
    25.             #pragma fragment Frag
    26.  
    27.             #include "UnityCG.cginc"
    28.            
    29.             float  _Speed;
    30.             float  _Amplitude;
    31.             float  _Offset;
    32.             float  _Width;
    33.             float4 _Color;
    34.                
    35.             struct VertexInput
    36.             {
    37.                 float4 pos : POSITION;
    38.                 float2 uv  : TEXCOORD0;
    39.             };
    40.            
    41.             struct FragmentInput
    42.             {
    43.                 float4 pos : POSITION;
    44.                 float2 uv  : TEXCOORD0;
    45.             };
    46.            
    47.             FragmentInput Vert(VertexInput Input)
    48.             {
    49.                 FragmentInput Result;
    50.                
    51.                 Result.pos = mul(UNITY_MATRIX_MVP, Input.pos);
    52.                 Result.uv  = Input.uv;
    53.                
    54.                 return Result;
    55.             }
    56.            
    57.             float4 Frag(FragmentInput Input) : COLOR
    58.             {
    59.                 float y = -1 + Input.uv.y * 2;
    60.                 float t = _Time.y;
    61.                
    62.                 y += sin(Input.uv.x + (_Offset + t) * _Speed) * _Amplitude;
    63.                
    64.                 return float4(_Color.rgb, abs(1.0 / (_Width * y)) * _Color.a);
    65.             }
    66.            
    67.             ENDCG
    68.         }
    69.     }
    70.     FallBack "Unlit/Diffuse"
    71. }
    72.  
     
  2. Alexey

    Alexey

    Unity Technologies

    Joined:
    May 10, 2010
    Posts:
    1,624
    >>sin(Input.uv.x + (_Offset + t) * _Speed)
    i would suggest you "clamp" or otherwise bring sin argument closer to [-pi...pi] yourself - otherwise you are bound to have precision issues. The easiest would be to move time calc into vprog as you have way more instructions to spare there