Shader works perfect in the unity editor but fails on android

Hi, I’m quite new to the shaders, but I’ve managed to make this simple shader, the problem is that although it works perfectly in the editor, compiling android does not change the hue correctly.

The shader works with “material.setInt” to change the value of _resta_tono
and thus be able to modify the hue of the pixel based on the hue that the pixel already had.

Thank you very much for reading and I hope you can help me :slight_smile:

This is the shader that I am using:

Shader "shader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        //_resta_tono("resta_tono", int) = 0
    }
    SubShader
    {
        // No culling or depth
        Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0
            //#pragma shader_feature SHADER_API_MOBILE --> not work
            //#pragma shader_feature UNITY_NO_LINEAR_COLORSPACE ---> not work
            //#pragma only_renderers gles --> not work
            //#pragma enable_d3d11_debug_symbols
           
            #include "UnityCG.cginc"


            /*struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };*/

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            uniform sampler2D _MainTex;
            float4 _MainTex_ST;
            int _resta_tono;

            v2f vert (appdata_base v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);

                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }
           
           
           

            float3 rgb_to_hsv(float r, float g, float b)
            {
                float3 hsv;

              
                float max = r;
                if (g > max) max = g;
                if (b > max) max = b;

               
                float min = r;
                if (g < min) min = g;
                if (b < min) min = b;

                // calculate H
                //caso 1
                if (max == r && g >= b) hsv.x = 60 * ((g - b) / (max - min));
                //caso 2
                if (max == r && g < b) hsv.x = 60 * ( (g - b) / (max - min) ) + 360;
                //caso 3
                if (max == g) hsv.x = 60 * ((b - r) / (max - min)) + 120;
                //caso 4
                if (max == b) hsv.x = 60 * ((r - g) / (max - min)) + 240;

                //calcular S
                if (max == 0) hsv.y = 0;
                if (max != 0) hsv.y = 1 - (min / max);

                //calculate V
                hsv.z = max;

                return hsv;
            }

            float3 hsv_to_rgb(float3 hsv)
            {
                float hi = floor(hsv.x / 60) % 6;
                float f = ((hsv.x / 60) % 6) - hi;
                float p = hsv.z * (1 - hsv.y);
                float q = hsv.z * (1 - f * hsv.y);
                float t = hsv.z * (1 - (1 - f) * hsv.y);

                float3 rgb;
                if (hi == 0)
                {
                    rgb.x = hsv.z;
                    rgb.y = t;
                    rgb.z = p;
                    return rgb;
                }
                if (hi == 1)
                {
                    rgb.x = q;
                    rgb.y = hsv.z;
                    rgb.z = p;
                    return rgb;
                }
                if (hi == 2)
                {
                    rgb.x = p;
                    rgb.y = hsv.z;
                    rgb.z = t;
                    return rgb;
                }
                if (hi == 3)
                {
                    rgb.x = p;
                    rgb.y = q;
                    rgb.z = hsv.z;
                    return rgb;
                }
                if (hi == 4)
                {
                    rgb.x = t;
                    rgb.y = p;
                    rgb.z = hsv.z;
                    return rgb;
                }
                if (hi == 5)
                {
                    rgb.x = hsv.z;
                    rgb.y = p;
                    rgb.z = q;
                    return rgb;
                }

               

                return rgb;
            }

       
            half4 frag (v2f i) : Color //SV_Target
            {
                half4 col = tex2D(_MainTex, i.uv);
                // just invert the colors
                //col *= 1.1f;
                if (col.r > 0 || col.g > 0 || col.b > 0)
                {
                    float3 hsv = rgb_to_hsv(col.r, col.g, col.b);
                   
                    //  modify hue
                    hsv.x += _resta_tono;
                    if (hsv.x > 360) hsv.x -= 360;
                    if (hsv.x < 0) hsv.x += 360;

                    // modify saturation
                    hsv.z *= 1.03f;
                    if (hsv.z > 1) hsv.z = 1;

                    float3 rgb = hsv_to_rgb(hsv);

                    col.r = rgb.x;
                    col.g = rgb.y;
                    col.b = rgb.z;
                    col.a = 1;
                }


                return col;
            }

            ENDCG
        }
    }
}

Probably too many branches. Generally speaking avoid branching until you understand the basics of shaders and what a branch entails.

1 Like

Thanks for answering, but could you please tell me what you mean with branches?

also check device log for possible errors/warnings

1 Like

if()…
shader especially on mobile doesnt really like if or if else (this should includes for and while loop iirc)
you can replace those ifs using saturate, min, max, lerps etc etc…

1 Like

OK thanks.