Search Unity

Question overlay blend mode with shader graph

Discussion in 'Shader Graph' started by axlxi1, Dec 2, 2020.

  1. axlxi1

    axlxi1

    Joined:
    May 13, 2020
    Posts:
    2
    Hi I've just started working with shader graph and still trying to learn it but now I need overlay blend mode shader just like the one in photoshop so whenever I give that material to sprite it'll act like it has over blend mode in photoshop. something like this:
    normal blend mode:

    overlay blend mode:

    also it doesn't necessarily need to be shader graph
    I also searched and found some shaders but they mostly doesn't work like photoshop overlay blend mode e.g. this one is good but it's intensity is higher
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    In blend node 6ou have 3 inputs. A and B channels and opacity. Besides, do you have right blend effect enabled?

    In last one opacity feels to be set to 1. You probably want to reduce it a bit.
    Also, there is matter of alpha mask that u using as your flare effect.

    The last image is more pronounced, wlie previous examples are much more subtle.
     
  3. axlxi1

    axlxi1

    Joined:
    May 13, 2020
    Posts:
    2
    I tried to input my main texture as Base in blend node and leave the Blend and Opacity inputs empty and giving the output to master node but it doesn't blend it over other sprites

    also did some simple tweaks to the shader that I found and made it a bit more subtle but still not what I want

    and here's the shader:
    Code (CSharp):
    1.  Shader "Photoshop/Overlay"
    2. {
    3.      Properties
    4.      {
    5.          _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    6.      }
    7.  
    8.      SubShader
    9.      {
    10.          Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    11.          ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend DstColor SrcColor
    12.          LOD 110
    13.      
    14.          Pass
    15.          {
    16.              CGPROGRAM
    17.              #pragma vertex vert_vct
    18.              #pragma fragment frag_mult
    19.              #pragma fragmentoption ARB_precision_hint_fastest
    20.              #include "UnityCG.cginc"
    21.              sampler2D _MainTex;
    22.              float4 _MainTex_ST;
    23.              struct vin_vct
    24.              {
    25.                  float4 vertex : POSITION;
    26.                  float4 color : COLOR;
    27.                  float2 texcoord : TEXCOORD0;
    28.              };
    29.              struct v2f_vct
    30.              {
    31.                  float4 vertex : POSITION;
    32.                  fixed4 color : COLOR;
    33.                  half2 texcoord : TEXCOORD0;
    34.              };
    35.              v2f_vct vert_vct(vin_vct v)
    36.              {
    37.                  v2f_vct o;
    38.                  o.vertex = UnityObjectToClipPos(v.vertex);
    39.                  o.color = v.color;
    40.                  o.texcoord = v.texcoord;
    41.                  return o;
    42.              }
    43.              float4 frag_mult(v2f_vct i) : COLOR
    44.              {
    45.                  float4 tex = tex2D(_MainTex, i.texcoord);
    46.              
    47.                  float4 final;            
    48.                  final.rgb = i.color.rgb * tex.rgb * 2;
    49.                  final.a = i.color.a * tex.a;
    50.                  return lerp(float4(0.5f,0.5f,0.5f,0.5f), final, final.a);
    51.              
    52.              }
    53.              ENDCG
    54.          
    55.          }
    56.      
    57.        
    58.      }
    59.  
    60. }
    in line 48 (final.rgb = i.color.rgb * tex.rgb * 2) I multiplied final rgb by .75 (final.rgb = i.color.rgb * tex.rgb * .75f) so made it a bit more transparent
     
    Last edited: Dec 3, 2020
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    If you implement the Photoshop overlay blend mode, you need to also take into consideration the color mode you've set your textures to, that sRGB tickbox in the import settings affects the look. Same goes for the Color Space settings in the Player section of your project.
     
    axlxi1 likes this.
  5. JeffreyMoviebill

    JeffreyMoviebill

    Joined:
    Nov 14, 2019
    Posts:
    3
    This Shader works perfect! Do you happen to have one that does a Photoshop Screen blend?
     
  6. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @JeffreyMoviebill you need a shader or a Shader Graph? Just asking as this post was (and still is) posted to the Shader Graph section. And what would you want to use the screen for, blend a texture over another?
     
  7. jmrogne

    jmrogne

    Joined:
    Jun 25, 2021
    Posts:
    1
    Screen is like Multiply, just with the values flipped. So to get Screen in shader graph add the "One Minus" node to each of the inputs/outputs for the Multiply node. :)
     
  8. AedanCGraves

    AedanCGraves

    Joined:
    Jan 6, 2016
    Posts:
    14
    Hate to bring up an older thread but has anyone figured out how to do this in a shader graph yet?
     
  9. fleity

    fleity

    Joined:
    Oct 13, 2015
    Posts:
    345
    I'd suggest looking up the math used in photoshop just recreate that in shadergraph.
    This is from adobe but very mathy to read: https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdf_reference_archive/blend_modes.pdf
    this looks terribly old but is easier to read: https://www.deepskycolors.com/archivo/2010/04/21/formulas-for-Photoshop-blending-modes.html

    There is surely a better resource somewhere out there. Overlay is a bit annoying since it involves a conditional check, but that could be replaced with a step node or sth similar

    (Target > 0.5) * (1 - (1-2*(Target-0.5)) * (1-Blend)) + (Target <= 0.5) * ((2*Target) * Blend)

    (and I admit I have no idea if alpha is handled the same way here as rgb)
     
    Olmi likes this.
  10. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  11. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @fleity there's also the Nvidia documentation which has all the implementations of the blending modes.

    Overlay is listed there like this, their implementation:
    OVERLAY_NV
    (X,Y,Z) = (1,1,1)
    f(Cs,Cd) = 2*Cs*Cd, if Cd <= 0.5
    1-2*(1-Cs)*(1-Cd), otherwise
     
  12. AedanCGraves

    AedanCGraves

    Joined:
    Jan 6, 2016
    Posts:
    14
    I see, thanks all!