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. Dismiss Notice

How do you use UNITY_REQUIRE_ADVANCED_BLEND?

Discussion in 'Shaders' started by melos_han_tani, Jul 11, 2018.

  1. melos_han_tani

    melos_han_tani

    Joined:
    Jan 11, 2018
    Posts:
    77
    I want to use the SoftLight BlendOp. The documentation here is unclear though. https://docs.unity3d.com/ScriptReference/Rendering.BlendOp.html

    (Also there's a typo which writes "ADVANDED")

    I type this into my shader after "Shader "SHADERNAME" { " :

    UNITY_REQUIRE_ADVANCED_BLEND(SoftLight)


    But I get this error "Shader error in 'BG_Layer_Advanced_2D': Parse error: syntax error, unexpected TVAL_ID at line 4"

    Followed by a warning:

    "Shader warning in 'BG_Layer_Advanced_2D': Shader is not supported on this GPU (none of subshaders/fallbacks are suitable)"


    I was kind of guessing my GPU wouldn't support this but I'm not sure if that's a result of me typing the UNITY_REQUIRE wrong or what.

    Here's the not working shader:

    Code (CSharp):
    1.  
    2. Shader "Unlit/TranspUnlitADVANCEDSpriteMesh"
    3. {
    4.     UNITY_REQUIRE_ADVANCED_BLEND(SoftLight)
    5.  
    6.     Properties
    7.     {
    8.         _MainTex ("Texture", 2D) = "white" {}
    9.         _Transparency ("Transparency", Range(0,1)) = 0
    10.         //SourceMode ("SourceMode",float) = 5
    11.         //DestinationMode ("DestinationMode",float) = 10
    12.         OpMode ("OpMode", float) = 0 //Add by default, set to advanced things in script. Overrides "Blend" if advanced
    13.     }
    14.     SubShader
    15.     {
    16.         Tags { "Queue" = "Transparent"}
    17.         BlendOp [OpMode]
    18.         Blend SrcAlpha OneMinusSrcAlpha // Regular blend by default
    19.         Pass
    20.         {
    21.        
    22.             CGPROGRAM
    23.             #pragma vertex vert
    24.             #pragma fragment frag
    25.            
    26.             #include "UnityCG.cginc"
    27.  
    28.             uniform float _Transparency;
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.                 float2 uv : TEXCOORD0;
    33.             };
    34.  
    35.             struct v2f
    36.             {
    37.                 float2 uv : TEXCOORD0;
    38.                 float4 vertex : SV_POSITION;
    39.             };
    40.  
    41.             sampler2D _MainTex;
    42.             float4 _MainTex_ST;
    43.            
    44.             v2f vert (appdata v)
    45.             {
    46.                 v2f o;
    47.                 o.vertex = UnityObjectToClipPos(v.vertex);
    48.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    49.                 return o;
    50.             }
    51.            
    52.             fixed4 frag (v2f i) : SV_Target
    53.             {
    54.                 fixed4 col = tex2D(_MainTex, i.uv);
    55.                 col.a = _Transparency;
    56.                 return col;
    57.             }
    58.             ENDCG
    59.         }
    60.     }
    61. }
    62.  
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    Hi!
    Try moving it to somewhere inside the CGPROGRAM block, like line 25.
     
  3. Sara_LightbulbCrew

    Sara_LightbulbCrew

    Joined:
    Jul 15, 2022
    Posts:
    3
    Hi! I'm having the same problem and moving it inside the CGPROGRAM block isn't fixing it. Did you get it to work eventually? Do i need to install any package or do something else outside the shader?
     
  4. melos_han_tani

    melos_han_tani

    Joined:
    Jan 11, 2018
    Posts:
    77
    I was never able to get it to work - I ended up having to code the softlight algorithm into the fragment shader:

    Code (CSharp):
    1. Shader "Unlit/TranspUnlitADVANCEDSpriteMesh"
    2. {
    3.  
    4.     Properties
    5.     {
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.         _Transparency ("Transparency", Range(0,1)) = 0
    8.         _OpMode ("OpMode", int) = 0 //Add by default, set to advanced things in script. Overrides "Blend" if advanced
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "Queue" = "Transparent"}
    13.        
    14.         GrabPass { }
    15.  
    16.         Blend One Zero
    17.         Pass
    18.         {
    19.        
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.            
    24.             #include "UnityCG.cginc"
    25.  
    26.             uniform float _Transparency;
    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; // NOT interpolated
    37.                 float4 screenPos: TEXCOORD1; // Interpolated!
    38.             };
    39.  
    40.             sampler2D _MainTex;
    41.             float4 _MainTex_ST;
    42.             sampler2D _GrabTexture;
    43.             uniform int _OpMode;
    44.  
    45.             v2f vert (appdata v)
    46.             {
    47.                 v2f o;
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);
    49.                 o.screenPos = o.vertex;
    50.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    51.                 return o;
    52.             }
    53.            
    54.             // a = bottom, b = top
    55.             fixed4 Overlay(fixed4 a, fixed4 b) {
    56.                 fixed4 r = a < .5 ? 2.0 * a * b : 1.0 - 2.0 * (1.0 - a) * (1.0 - b);
    57.                 return r;
    58.             }
    59.  
    60.             // Pegtop formula
    61.             fixed4 SoftLight(fixed4 a, fixed4 b) {
    62.                 fixed4 r = (1 - 2*b)*a*a + 2*b*a;
    63.                 return r;
    64.             }
    65.  
    66.             fixed4 frag (v2f i) : SV_Target
    67.             {
    68.                 fixed4 col = tex2D(_MainTex, i.uv);
    69.                 // Clip space, -1 to 1
    70.                 float2 grabTexcoord = (i.screenPos.xy + 1)/2; // Converts to UV coords from 0 to 1 that will sample from the grab tex2D
    71.                 // the conversion works because the clip space coords of stuff fit to the camera view
    72.                 #if UNITY_UV_STARTS_AT_TOP
    73.                 grabTexcoord.y = 1.0 - grabTexcoord.y;
    74.                 #endif
    75.                
    76.                 fixed4 grabColor = tex2D(_GrabTexture, grabTexcoord);
    77.  
    78.                
    79.                 col.a *= _Transparency;
    80.                 if (_OpMode == 29){ // SoftLight
    81.                     col *= col.a;
    82.                     col = SoftLight(grabColor,col);
    83.                 } else if (_OpMode == 23) { // Overlay
    84.                     col *= col.a;
    85.                     col = Overlay(grabColor,col);
    86.                 } else {
    87.                     col = col*col.a + grabColor*(1-col.a);
    88.                 }
    89.                 return col;
    90.             }
    91.             ENDCG
    92.         }
    93.  
    94.  
    95.  
    96.     }
    97. }
    98.  
     
    Sara_LightbulbCrew likes this.
  5. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Did you ever file a bug report?
     
  6. melos_han_tani

    melos_han_tani

    Joined:
    Jan 11, 2018
    Posts:
    77
    I did not.
     
  7. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,495
    Could you perhaps? That way it's in the pipeline to get fixed, improving the engine for all.
     
  8. Sara_LightbulbCrew

    Sara_LightbulbCrew

    Joined:
    Jul 15, 2022
    Posts:
    3
    Thank you! I guess I'll do the same inlcuding all the blend modes I need
     
  9. Sara_LightbulbCrew

    Sara_LightbulbCrew

    Joined:
    Jul 15, 2022
    Posts:
    3
    I tried copying the shader you posted before trying to do any modification to it and it gives some console errors that I can't figure out how to fix (it's the first time i use a GrabPass so I'm sorry if it's something obvious), I'd appreciate if you could tell me what's wrong
    *EDIT* I think it might be cause I'm on the URP pipeline and the GrabPass doesn't work here :(
    upload_2022-7-18_11-30-58.png
     

    Attached Files:

    Last edited: Jul 18, 2022