Search Unity

Unity5 broke my frag shader.

Discussion in 'Shaders' started by HandyManStudios, Mar 16, 2015.

  1. HandyManStudios

    HandyManStudios

    Joined:
    May 29, 2014
    Posts:
    17
    I have a hueing shader that takes vertex colors (GL.Color()) to define the values, I just use the RGB as hue, brightness, saturation. It worked great before upgrading to unity 5, now the vertex color doesnt seem to get into the shader for some reason, was there a change that prevents this for some reason or something I have to do to make it work again? It seems really odd that this would break.
     
  2. WhiteSkullDev

    WhiteSkullDev

    Joined:
    Mar 14, 2015
    Posts:
    8
    post shader/screenshot/error log showing the issue?
     
  3. HandyManStudios

    HandyManStudios

    Joined:
    May 29, 2014
    Posts:
    17
    Well, the issue is contextual so a screenshot wouldnt really help, and there isnt an error.

    But as I've explained, the issue is the GL.Color(val) is never getting into the shader, the value always returns 'white' inside of the shader, when before it was the 'val' passed.
     
  4. HandyManStudios

    HandyManStudios

    Joined:
    May 29, 2014
    Posts:
    17
    if it helps to sort out the problem, heres the shader. but it worked totally fine in unity4


    Code (CSharp):
    1. Shader "Custom/HueShader" {
    2.  
    3.     Properties {
    4.         _MainTex ("Texture", 2D) = "white" {}
    5.         _Color ("Color", Color) = (0,0,0,0)
    6.     }
    7.  
    8.     SubShader {
    9.  
    10.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType" = "Transparent" }
    11.         ZWrite Off
    12.         Blend SrcAlpha OneMinusSrcAlpha
    13.         Cull Off
    14.  
    15.  
    16.         Pass
    17.         {
    18.  
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma target 2.0
    23.  
    24.             #include "UnityCG.cginc"
    25.  
    26.             sampler2D _MainTex;
    27.             float4 _MainTex_ST;
    28.             fixed4 _Color;
    29.  
    30.             float3 shift_col(float3 RGB, float3 shift)
    31.             {
    32.                 float3 RESULT = float3(RGB);
    33.                 float VSU = shift.z*shift.y*cos(shift.x*3.14159265/180);
    34.                 float VSW = shift.z*shift.y*sin(shift.x*3.14159265/180);
    35.  
    36.            
    37.  
    38.                 RESULT.x = (.299*shift.z+.701*VSU+.168*VSW)*RGB.x
    39.                         + (.587*shift.z-.587*VSU+.330*VSW)*RGB.y
    40.                         + (.114*shift.z-.114*VSU-.497*VSW)*RGB.z;
    41.  
    42.            
    43.  
    44.                 RESULT.y = (.299*shift.z-.299*VSU-.328*VSW)*RGB.x
    45.                         + (.587*shift.z+.413*VSU+.035*VSW)*RGB.y
    46.                         + (.114*shift.z-.114*VSU+.292*VSW)*RGB.z;
    47.  
    48.            
    49.  
    50.                 RESULT.z = (.299*shift.z-.3*VSU+1.25*VSW)*RGB.x
    51.                         + (.587*shift.z-.588*VSU-1.05*VSW)*RGB.y
    52.                         + (.114*shift.z+.886*VSU-.203*VSW)*RGB.z;
    53.  
    54.                 return (RESULT);
    55.             }
    56.  
    57.  
    58.             struct appdata_t
    59.             {
    60.                 float4 vertex : POSITION;
    61.                 fixed4 color : COLOR;
    62.                 float2 texcoord : TEXCOORD0;
    63.             };
    64.  
    65.             struct v2f {
    66.                 float4  pos : SV_POSITION;
    67.                 fixed4 color : COLOR;
    68.                 float2  uv : TEXCOORD0;
    69.             };
    70.  
    71.  
    72.             v2f vert (appdata_t v)
    73.             {
    74.                 v2f o;
    75.                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    76.                 o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    77.                 o.color = v.color;
    78.                 return o;
    79.             }
    80.  
    81.             half4 frag(v2f i) : COLOR
    82.             {
    83.                 half4 col = tex2D(_MainTex, i.uv);
    84.                 float3 shift =  float3(i.color.r * 360, i.color.g, i.color.b);
    85.                 return half4( half3(shift_col(col, shift)), col.a-(1-i.color.a)) + _Color;
    86.             }
    87.  
    88.             ENDCG
    89.  
    90.         }
    91.  
    92.     }
    93.  
    94.     Fallback "Particles/Alpha Blended"
    95.  
    96. }

    heres an example usage case as well
    Code (CSharp):
    1.         GL.Color(color);
    2.         GL.PushMatrix();
    3.         for (int i = 0; i < image.material.passCount; i++)
    4.         {
    5.             image.material.SetPass(i);
    6.             GL.LoadOrtho();
    7.             GL.Viewport(rect);
    8.  
    9.             GL.Begin(GL.QUADS);
    10.             GL.TexCoord2(0, 0);
    11.             GL.Vertex3(0.0F, 0.0F, 0);
    12.             GL.TexCoord2(0, 1);
    13.             GL.Vertex3(0.0F, 1.0F, 0);
    14.             GL.TexCoord2(1, 1);
    15.             GL.Vertex3(1.0F, 1.0F, 0);
    16.             GL.TexCoord2(1, 0);
    17.             GL.Vertex3(1.0F, 0.0F, 0);
    18.             GL.End();
    19.         }
    20.         GL.PopMatrix();
     
  5. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Does it work if you use SV_TARGET instead of COLOR for frag out?

    Failing that, try pragma target 3
     
  6. HandyManStudios

    HandyManStudios

    Joined:
    May 29, 2014
    Posts:
    17
    no dice on either sadly
     
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Pass in some manual values to test. Shader is probably working fine and its broken elsewhere?
     
  8. HandyManStudios

    HandyManStudios

    Joined:
    May 29, 2014
    Posts:
    17
    I did try to pass in some test values (IE just rendering vertex color) and it is just white.

    edit: Oh and manually passing a color, still white.
     
  9. HandyManStudios

    HandyManStudios

    Joined:
    May 29, 2014
    Posts:
    17
    So, it appears that the color is being cleared when GL.Begin is called, so I have to call GL.Color AFTERWORDS now...sadly this is a pretty massive issue to me because I was relying on being able to set the color BEFORE GL.Begin calls so I'm pretty peeved at the moment.
     
    Grassman7z7 likes this.
  10. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    Thats strange, doesn't GL.Color works like a flag or something? Asking out of curiosity.
     
  11. Grassman7z7

    Grassman7z7

    Joined:
    Jan 16, 2014
    Posts:
    2
    I had the exact same issue.
    Switched from Unity4 to Unity5 on a project, and GL.Color was not taking, and instead defaulting to white.
    I moved GL.Color() after GL.Begin() and it has now started using the proper color.

    Is this intended behavior of Unity5?