- Home /
How to write shader for hard light blend mode in unity
Hi.
I'm trying to implement hard light blend mode similar to photoshop hard light blend mode. Bellow you can see example output from photoshop and output from my shader. As you can see outputs aren't equal. I know that I'm doing something wrong but I don't know what because I'm newbie in shader programming. For hard light blend mode I used formula from here. So I will be very thankful for any help or idea. Thanks.
Source code of my shader: https://bitbucket.org/maho125/hardlight/src/ca0db7486768e0e157cde48c1cd590ff43fe1bb8/Assets/Shaders/Hard%20Light.shader
Hard Light in Photoshop:
Output from my shader:
Wikipedia gives a different formula for hard light than that which you're using - have you tried this ins$$anonymous$$d?

Right, this also looks strange:
(1.0 - 2.0 * (b - .5))
This is essentially just
(-2.0*b)
because:
(1.0 - 2.0 * b - 2.0 * .5)
(1.0 - 2.0 * b - 1.0))
(-2.0 * b)
edit
Apart from that you don't seem to specify any blend mode in the shader...
Btw: since you use GrabPass, do you actually have Unity pro?
@tanoshimi Thanks you. You were right I used wrong formula but when I updated formula the result still isn't equal to photoshop output. Still I have something wrong or something is missing. Here is updated shader https://bitbucket.org/maho125/hardlight/src/83c7385b6b542941bc528e8b713ab5f149d58eb7/Assets/Shaders/Hard%20Light.shader
@Bunny83 Thank you for your advices. I'm using unity 5.1 Personal Edition and on unity official site is written that it contains all features of unity engine so GrabPass should be included too. Your link was related to older version of unity 4 or less.
I experimented with unity Blend operators and Blend Factors but I'm quite confused with using it. When you want some basic blend mode like additive you can use Blend One One but in my case I need to use formula mentioned above. So I thought when I used this formula in fragment shader I'dont need use also predefined Blend factors or operators from unity. Or I'm not right?
Answer by BJO · Jul 30, 2015 at 04:34 AM
I recently wrote an overlay shader. According to Wikipedia, it's the same as hard light, except a and b are swapped.
My fragment is as follows...
fixed4 frag(vertexOutput input) : COLOR
{
fixed4 output = 0;
fixed4 inputA = tex2D(_MainTex, input.tex);
fixed4 inputB = _Color;
output = (inputA < 0.5) ? 2*inputA*inputB : 1-2*(1-inputA)*(1 - inputB);
output = lerp(inputA, output, _Blend);
output.a = inputA.a * inputB.a * _Transparency;
output.a = inputA.a * inputB.a * _Transparency;
}
Note, I'm doing a couple things different here than what you'll ultimately need. First, this is an "Overlay" not "Hard Light" blend mode, but it should be a matter of swapping which color is assigned to "a" and "b" https://en.wikipedia.org/wiki/Blend_modes#Overlay. Second, I'm blending a texture and a color. It sounds like you want to blend a texture with the scene, so your use of a grab pass appears necessary.
As @tanoshimi mentioned, you don't have a blend mode specified. Since the blending has already happened in the shader,...
Blend SrcAlpha OneMinusSrcAlpha
...should work (it did for me).
Lastly, this line...
output = lerp(inputA, output, _Blend);
...functions the same as the "Opacity" property of a layer in photoshop.
The "_Transparency" property...
output.a = inputA.a * inputB.a * _Transparency;
...was just to allow me to fade out the object being rendered altogether.
Your answer
Follow this Question
Related Questions
Simple texture blender shader not working properly 0 Answers
How can I get shadows in a transparent Surface Shader? 0 Answers
How to get rid of this Bleeding in my baked scene? 1 Answer
Deformation Maps Possible? 0 Answers
Silhouette overlay shader 0 Answers