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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Shader works perfect in the unity editor but fails on android

Discussion in 'Shaders' started by estratega2016, Oct 7, 2018.

  1. estratega2016

    estratega2016

    Joined:
    Dec 28, 2016
    Posts:
    10
    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 :)

    This is the shader that I am using:

    Code (CSharp):
    1. Shader "shader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         //_resta_tono("resta_tono", int) = 0
    7.     }
    8.     SubShader
    9.     {
    10.         // No culling or depth
    11.         Cull Off ZWrite Off ZTest Always
    12.  
    13.         Pass
    14.         {
    15.             CGPROGRAM
    16.             #pragma vertex vert
    17.             #pragma fragment frag
    18.             #pragma target 2.0
    19.             //#pragma shader_feature SHADER_API_MOBILE --> not work
    20.             //#pragma shader_feature UNITY_NO_LINEAR_COLORSPACE ---> not work
    21.             //#pragma only_renderers gles --> not work
    22.             //#pragma enable_d3d11_debug_symbols
    23.            
    24.             #include "UnityCG.cginc"
    25.  
    26.  
    27.             /*struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float2 uv : TEXCOORD0;
    31.             };*/
    32.  
    33.             struct v2f
    34.             {
    35.                 float2 uv : TEXCOORD0;
    36.                 float4 vertex : SV_POSITION;
    37.             };
    38.  
    39.             uniform sampler2D _MainTex;
    40.             float4 _MainTex_ST;
    41.             int _resta_tono;
    42.  
    43.             v2f vert (appdata_base v)
    44.             {
    45.                 v2f o;
    46.                 o.vertex = UnityObjectToClipPos(v.vertex);
    47.  
    48.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    49.                 return o;
    50.             }
    51.            
    52.            
    53.            
    54.  
    55.             float3 rgb_to_hsv(float r, float g, float b)
    56.             {
    57.                 float3 hsv;
    58.  
    59.              
    60.                 float max = r;
    61.                 if (g > max) max = g;
    62.                 if (b > max) max = b;
    63.  
    64.                
    65.                 float min = r;
    66.                 if (g < min) min = g;
    67.                 if (b < min) min = b;
    68.  
    69.                 // calculate H
    70.                 //caso 1
    71.                 if (max == r && g >= b) hsv.x = 60 * ((g - b) / (max - min));
    72.                 //caso 2
    73.                 if (max == r && g < b) hsv.x = 60 * ( (g - b) / (max - min) ) + 360;
    74.                 //caso 3
    75.                 if (max == g) hsv.x = 60 * ((b - r) / (max - min)) + 120;
    76.                 //caso 4
    77.                 if (max == b) hsv.x = 60 * ((r - g) / (max - min)) + 240;
    78.  
    79.                 //calcular S
    80.                 if (max == 0) hsv.y = 0;
    81.                 if (max != 0) hsv.y = 1 - (min / max);
    82.  
    83.                 //calculate V
    84.                 hsv.z = max;
    85.  
    86.                 return hsv;
    87.             }
    88.  
    89.             float3 hsv_to_rgb(float3 hsv)
    90.             {
    91.                 float hi = floor(hsv.x / 60) % 6;
    92.                 float f = ((hsv.x / 60) % 6) - hi;
    93.                 float p = hsv.z * (1 - hsv.y);
    94.                 float q = hsv.z * (1 - f * hsv.y);
    95.                 float t = hsv.z * (1 - (1 - f) * hsv.y);
    96.  
    97.                 float3 rgb;
    98.                 if (hi == 0)
    99.                 {
    100.                     rgb.x = hsv.z;
    101.                     rgb.y = t;
    102.                     rgb.z = p;
    103.                     return rgb;
    104.                 }
    105.                 if (hi == 1)
    106.                 {
    107.                     rgb.x = q;
    108.                     rgb.y = hsv.z;
    109.                     rgb.z = p;
    110.                     return rgb;
    111.                 }
    112.                 if (hi == 2)
    113.                 {
    114.                     rgb.x = p;
    115.                     rgb.y = hsv.z;
    116.                     rgb.z = t;
    117.                     return rgb;
    118.                 }
    119.                 if (hi == 3)
    120.                 {
    121.                     rgb.x = p;
    122.                     rgb.y = q;
    123.                     rgb.z = hsv.z;
    124.                     return rgb;
    125.                 }
    126.                 if (hi == 4)
    127.                 {
    128.                     rgb.x = t;
    129.                     rgb.y = p;
    130.                     rgb.z = hsv.z;
    131.                     return rgb;
    132.                 }
    133.                 if (hi == 5)
    134.                 {
    135.                     rgb.x = hsv.z;
    136.                     rgb.y = p;
    137.                     rgb.z = q;
    138.                     return rgb;
    139.                 }
    140.  
    141.                
    142.  
    143.                 return rgb;
    144.             }
    145.  
    146.        
    147.             half4 frag (v2f i) : Color //SV_Target
    148.             {
    149.                 half4 col = tex2D(_MainTex, i.uv);
    150.                 // just invert the colors
    151.                 //col *= 1.1f;
    152.                 if (col.r > 0 || col.g > 0 || col.b > 0)
    153.                 {
    154.                     float3 hsv = rgb_to_hsv(col.r, col.g, col.b);
    155.                    
    156.                     //  modify hue
    157.                     hsv.x += _resta_tono;
    158.                     if (hsv.x > 360) hsv.x -= 360;
    159.                     if (hsv.x < 0) hsv.x += 360;
    160.  
    161.                     // modify saturation
    162.                     hsv.z *= 1.03f;
    163.                     if (hsv.z > 1) hsv.z = 1;
    164.  
    165.                     float3 rgb = hsv_to_rgb(hsv);
    166.  
    167.                     col.r = rgb.x;
    168.                     col.g = rgb.y;
    169.                     col.b = rgb.z;
    170.                     col.a = 1;
    171.                 }
    172.  
    173.  
    174.                 return col;
    175.             }
    176.  
    177.             ENDCG
    178.         }
    179.     }
    180. }
    181.  
     
  2. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Probably too many branches. Generally speaking avoid branching until you understand the basics of shaders and what a branch entails.
     
    estratega2016 likes this.
  3. estratega2016

    estratega2016

    Joined:
    Dec 28, 2016
    Posts:
    10
    Thanks for answering, but could you please tell me what you mean with branches?
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,955
    estratega2016 likes this.
  5. bart_the_13th

    bart_the_13th

    Joined:
    Jan 16, 2012
    Posts:
    485
    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...
     
    estratega2016 likes this.
  6. estratega2016

    estratega2016

    Joined:
    Dec 28, 2016
    Posts:
    10
    OK thanks.