Search Unity

how to migrate combine +-

Discussion in 'Shaders' started by creat327, Feb 25, 2015.

  1. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Hi

    I have no clue about shaders, and when I was finally getting to understand them a bit, the system has changed completely (for me) on Unity 5.

    I have this on a shader:

    Properties {
    _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    _Blend ("Blend", Range(0.0,1.0)) = 0.5
    _FrontTex ("Front (+Z)", 2D) = "white" {}
    _FrontTex2("2 Front (+Z)", 2D) = "white" {}
    }

    SubShader {
    Tags { "Queue" = "Background" }
    Cull Off
    ZTest Less
    ZWrite Off
    Fog { Mode Off }
    Lighting Off
    Color [_Tint]
    Pass {
    SetTexture [_FrontTex] { combine texture }
    SetTexture [_FrontTex2] { constantColor (0,0,0,[_Blend]) combine texture lerp(constant) previous }
    SetTexture [_FrontTex2] { combine previous +- primary, previous * primary }
    }
    }


    but Unity5 doesn't like it because it's using SetTexture combine +-

    How can this be rewritten so that it supports it? I thought it would be a 1 line change but apparently it requires all kind of fancy new things in cg that I have no idea about :(
     
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Well... the reason for this is apparently due to some functions being removed for 5.0, since FFP is technically outdated... :D

    Another reason was the sheer amount of shader emulation code required to do such shaders in DX11 mode. This is due to DX11 being fully shader-based, and requiring the user knows HLSL. By removing those parts, it meant that now there's less code to maintain.

    Of course, that meant removal of the dot3 operation... sad! :D

    But I don't have any idea why a signed add (+-, as you pointed out) would be such an issue that it was removed... Would have to ask Aras that question!

    EDIT: Also, could you show me at least the whole error log?
     
  3. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    What I'm asking is how to rewrite this shader to perform the same. There is no log, it says "SetTexture combine +- is obsolete" and that's about it :(
     
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,610
    The fixed-function pipeline has been removed in Unity 5 if I remember correctly, but the shader you posted is using it.

    The following code should be doing the same (hopefully) as the code you posted, but using the programmable pipeline, which works with Unity 5:
    Code (CSharp):
    1. Shader "Custom/how to migrate combine new" {
    2.     Properties {
    3.         _Tint ("Tint Color", Color) = (.5, .5, .5, .5)
    4.         _Blend ("Blend", Range(0.0,1.0)) = 0.5
    5.         _FrontTex ("Front (+Z)", 2D) = "white" {}
    6.         _FrontTex2("2 Front (+Z)", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader {
    10.         Tags { "Queue" = "Background" }
    11.         Cull Off
    12.         ZTest Less
    13.         ZWrite Off
    14.         Fog { Mode Off }
    15.         Lighting Off
    16.  
    17.     CGPROGRAM
    18.     #pragma surface surf Lambert
    19.  
    20.     sampler2D _FrontTex;
    21.     sampler2D _FrontTex2;
    22.     float _Blend;
    23.     float4 _Tint;
    24.  
    25.     struct Input {
    26.         float2 uv_FrontTex;
    27.     };
    28.  
    29.     void surf (Input IN, inout SurfaceOutput o)
    30.     {
    31.         half4 c1 = tex2D (_FrontTex, IN.uv_FrontTex);
    32.         half4 c2 = tex2D (_FrontTex2, IN.uv_FrontTex);
    33.    
    34.         // SetTexture [_FrontTex2] { constantColor (0,0,0,[_Blend]) combine texture lerp(constant) previous }
    35.         // combine src1 lerp (src2) src3: Interpolates between src3 and src1, using the alpha of src2.
    36.         half4 c = lerp(c1, c2, _Blend);
    37.  
    38.         // SetTexture [_FrontTex2] { combine previous +- primary, previous * primary }
    39.         // combine src1 +- src2: Adds src1 to src2, then subtracts 0.5 (a signed add).
    40.         c.rgb += _Tint.rgb - 0.5;
    41.            
    42.         o.Emission = c.rgb;
    43.         o.Alpha = c.a;
    44.     }
    45.     ENDCG
    46.     }
    47.     FallBack "Diffuse"
    48. }
    49.  
    What each of the commands is doing in the fixed function pipeline is documented here btw.
     
    Last edited: Feb 25, 2015
  5. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Thanks Peter77
    I have a question though, about this line
    c.rgb += _Tint.rgb - 0.5;

    I don't see the Tint used anywhere on the original shader, how do you figure Tint is used there? Just wondering because I'm surprised not to see it on the SetTexture but used on the surface shader
     
  6. creat327

    creat327

    Joined:
    Mar 19, 2009
    Posts:
    1,756
    Btw I tried it and it failed...
    it doesn't like float4 _Tint;
     
  7. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,610
    It's this command:
    Code (CSharp):
    1. SetTexture [_FrontTex2] { combine previous +- primary, previous * primary }
    primary refers to the color from the lighting calculation or the vertex color if it is bound (as stated here), so it should be _Tint, I think, because of this binding:
    Code (CSharp):
    1. Color [_Tint]
    Hmm not sure what failed exactly means. I did test the original shader code and the one I posted side-by-side on a Plane GameObject in Unity 4.6.3 and it looks identical to me there and the code I posted looks the same in Unity 5.0b20.
     
  8. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Perhaps changing the blend mode (if it's meant to let other objects through) would help? o_O