Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheFudster · Jan 21, 2013 at 10:22 AM · shadervertex shaderoverlayphotoshop

Shader: Photoshop Overlay Effect

So I'm trying to reproduce the same effect that's used for photoshop overlay layers in Unity by writing a shader. Problem is I have limited knowledge of shaders so I could use some help.

I have a function called overlay_filter(float4 lower, float4 upper). The lower value should be the 'lower' layer or the pixel color that's currently in the render buffer. 'upper' is the top layer or basically it's just the pixel color from the _MainTex that we are overlaying on top of the screen. I know how to get the pixel data from the _MainText but what I don't know is where to get the current render buffer pixel data so that I can pass it to overlay_filter.

Here is what I currently have:

 Shader "photoshop/TestShader" 
 {
     Properties 
     {
         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
     }
     
     SubShader
     {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend SrcAlpha OneMinusSrcAlpha
         LOD 110
         
         Pass 
         {
             CGPROGRAM
             #pragma vertex vert_vct
             #pragma fragment frag_mult 
             #pragma fragmentoption ARB_precision_hint_fastest
             #include "UnityCG.cginc"
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
 
             struct vin_vct 
             {
                 float4 vertex : POSITION;
                 float4 color : COLOR;
                 float2 texcoord : TEXCOORD0;
             };
 
             struct v2f_vct
             {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
                 half2 texcoord : TEXCOORD0;
             };
 
             float4 overlay_filter(float4 lower, float4 upper) {
             
                 float4 overlay = upper;
                 float valueUnit = 0;
                 float minValue = 0;
             
                 if ( lower.r > 0.5 ) {
                     valueUnit = (1.0-lower.r)/0.5;
                     minValue = lower.r - (1.0 - lower.r);
                     overlay.r = (upper.r * valueUnit) + minValue;
                     //overlay.r = (upper.r * ((1.0-lower.r)/0.5)) + (lower.r - (1.0 - lower.r));
                 } else if ( lower.r < 0.5 ) {
                     valueUnit = (1.0-lower.r)/0.5;
                     overlay.r = upper.r * valueUnit;
                 }
                 
                 if ( lower.g > 0.5 ) {
                     valueUnit = (1.0-lower.g)/0.5;
                     minValue = lower.g - (1.0 - lower.g);
                     overlay.g = (upper.g * valueUnit) + minValue;
                 } else if ( lower.g < 0.5 ) {
                     valueUnit = (1.0-lower.g)/0.5;
                     overlay.g = upper.g * valueUnit;
                 }
 
                 if ( lower.b > 0.5 ) {
                     valueUnit = (1.0-lower.b)/0.5;
                     minValue = lower.b - (1.0 - lower.b);
                     overlay.b = (upper.b * valueUnit) + minValue;
                 } else if ( lower.b < 0.5 ) {
                     valueUnit = (1.0-lower.b)/0.5;
                     overlay.b = upper.b * valueUnit;
                 }
 
                 return overlay;
             
             }
 
             v2f_vct vert_vct(vin_vct v)
             {
                 v2f_vct o;
                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.color = v.color;
                 o.texcoord = v.texcoord;
                 return o;
             }
 
             float4 frag_mult(v2f_vct i) : COLOR
             {
                 float4 col1 = tex2D(_MainTex, i.texcoord);
                 float4 col2 = i.color;
                 return (col1 * col2);
             }
 
             ENDCG
             
         }
         
           
     }
  
     
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by TheFudster · Jan 21, 2013 at 11:30 AM

In the end I found this shader most closely matched the effect I wanted as long as I used all gray vertex colors. This is a modified version of the Particle/Multiply (Double) shader. The result is very close if not exactly what I get in photoshop when I set a layer to "overlay" instead of "normal".

 //Somehow achieves an effect similar to this:
 //#define BlendOverlayf(base, blend)     (base < 0.5 ? (2.0 * base * blend) : (1.0 - 2.0 * (1.0 - base) * (1.0 - blend)))
 
 Shader "Photoshop/Overlay" 
 {
     Properties 
     {
         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
     }
     
     SubShader
     {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         ZWrite Off Lighting Off Cull Off Fog { Mode Off } Blend DstColor SrcColor
         LOD 110
         
         Pass 
         {
             CGPROGRAM
             #pragma vertex vert_vct
             #pragma fragment frag_mult 
             #pragma fragmentoption ARB_precision_hint_fastest
             #include "UnityCG.cginc"
 
             sampler2D _MainTex;
             float4 _MainTex_ST;
 
             struct vin_vct 
             {
                 float4 vertex : POSITION;
                 float4 color : COLOR;
                 float2 texcoord : TEXCOORD0;
             };
 
             struct v2f_vct
             {
                 float4 vertex : POSITION;
                 fixed4 color : COLOR;
                 half2 texcoord : TEXCOORD0;
             };
 
             v2f_vct vert_vct(vin_vct v)
             {
                 v2f_vct o;
                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                 o.color = v.color;
                 o.texcoord = v.texcoord;
                 return o;
             }
 
             float4 frag_mult(v2f_vct i) : COLOR
             {
                 float4 tex = tex2D(_MainTex, i.texcoord);
                 
                 float4 final;                
                 final.rgb = i.color.rgb * tex.rgb * 2;
                 final.a = i.color.a * tex.a;
                 return lerp(float4(0.5f,0.5f,0.5f,0.5f), final, final.a);
                 
             }
 
             ENDCG
             
         }
         
           
     }
  
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vertex displacement shader not working 2 Answers

Apply Shader to overlay GUI 0 Answers

Modify vertex position using shaders on Windows Phone 8 0 Answers

How do I render a scene's depth buffer when I have custom vertex shaders? 0 Answers

Mobile performance of splat map shader with distance blending 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges