Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issue with shader in iOS

Discussion in 'Shaders' started by Martin_Gonzalez, Feb 22, 2016.

  1. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    Hi everyone. I did a simple Unlit Texture shader with a slider where i control the saturation of the texture. In Editor works great, also i'm updating the saturation value from Material.SetFloat and also works.

    The problem is when i build to iOS. The shader seems to be doing what it wants.
    The effect is to go from the texture color to a gray scale reducing the saturation, as i said in the editor works but in iOS nope.

    I am not a shader dev so am i missiing something?

    Thanks!
     
  2. echo4papa

    echo4papa

    Joined:
    Mar 26, 2015
    Posts:
    158
    Can you post the shader code?
     
  3. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    Sure, here it is.

    Code (CSharp):
    1. Shader "Unlit/Saturation"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Saturation ("Saturation", Range(0, 1)) = 1
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.  
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert
    16.             #pragma fragment frag
    17.  
    18.             sampler2D _MainTex;
    19.             float4 _MainTex_ST;
    20.             float _Saturation;
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 float4 vertex : SV_POSITION;
    32.             };
    33.          
    34.             v2f vert (appdata v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    38.                 o.uv = v.uv;
    39.                 return o;
    40.             }
    41.          
    42.             fixed4 frag (v2f i) : SV_Target
    43.             {
    44.                 fixed4 col = tex2D(_MainTex, i.uv);
    45.                 col.rgb = lerp( dot(col.rgb, fixed3(0.22, 0.707, 0.071)), col.rgb , _Saturation );
    46.                 return col;
    47.             }
    48.             ENDCG
    49.         }
    50.     }
    51. }
    52.  
     
  4. Owers

    Owers

    Joined:
    Jul 7, 2012
    Posts:
    39
    I think it's because you're using a dot product, which converts a vector to a single float, in a vector3. Maybe try this:
    Code (CSharp):
    1. Shader "Unlit/Saturation"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Saturation ("Saturation", Range(0, 1)) = 1
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             sampler2D _MainTex;
    17.             float4 _MainTex_ST;
    18.             float _Saturation;
    19.             struct appdata
    20.             {
    21.                 float4 vertex : POSITION;
    22.                 float2 uv : TEXCOORD0;
    23.             };
    24.             struct v2f
    25.             {
    26.                 float2 uv : TEXCOORD0;
    27.                 float4 vertex : SV_POSITION;
    28.             };
    29.      
    30.             v2f vert (appdata v)
    31.             {
    32.                 v2f o;
    33.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    34.                 o.uv = v.uv;
    35.                 return o;
    36.             }
    37.      
    38.             fixed4 frag (v2f i) : SV_Target
    39.             {
    40.                 fixed4 col = tex2D(_MainTex, i.uv);
    41.                 fixed3 desaturate = dot(col.rgb, fixed3(0.22, 0.707, 0.071));
    42.                 col.rgb = lerp( desaturate, col.rgb , _Saturation );
    43.                 return col;
    44.             }
    45.             ENDCG
    46.         }
    47.     }
    48. }
     
  5. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361

    You are saying that the way a did the dot function is the problem?
    Now i cannot test it but the behaviour in iOS is that the object is in gray scale but i can see how the intense of the gray scale changes when it has to change to color again. Its a ping pong from color to gray scale.
    But i also had a problem animating the normal map value (the normal scale/depth) in IOS is not animating and also in pc works fine.
    Just to check that i used the standar shader set a texture and normal and with a script change the NormalDepth.
    I dont know whats going on
     
  6. Owers

    Owers

    Joined:
    Jul 7, 2012
    Posts:
    39
    Yeah, so by putting a dot product in a vector you confused the compiler into thinking the shader was lerping a float. The end result was the shader lerping between your dot result and the .x value of col (which looks greyscale). This only happened in iOS because different graphics platforms are more strict with their syntax than others, so something that works fine on PC with DirectX will probably not work with Metal on iOS. It can be frustrating, but eventually you start to develop techniques to avoid it (i.e. don't mix different variable types together in math, clamp colour values with saturate(), etc). It would be great if Unity could better emulate different graphics platforms on desktops to help diagnose problems like this.
     
  7. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    Tomorrow i will test it!
    Thanks a lot for your time!
     
  8. Martin_Gonzalez

    Martin_Gonzalez

    Joined:
    Mar 25, 2012
    Posts:
    361
    I test it and works. Thanks a lot for your answer.
    I will show another issue in other post later.