- Home /
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
}
}
}
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
}
}
}
Your answer
Follow this Question
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