Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by maho125 · Jul 29, 2015 at 01:51 PM · shadersblending

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:

​IMG

Output from my shader:

​IMG

Comment
Add comment · Show 4
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
avatar image tanoshimi · Jul 29, 2015 at 02:40 PM 0
Share

Wikipedia gives a different formula for hard light than that which you're using - have you tried this ins$$anonymous$$d?

alt text

eee8e5b09fc6c65fd212c1b17adb6b42.png (2.1 kB)
avatar image Bunny83 · Jul 29, 2015 at 03:11 PM 0
Share

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

avatar image Bunny83 · Jul 29, 2015 at 03:34 PM 0
Share

Btw: since you use GrabPass, do you actually have Unity pro?

avatar image maho125 · Jul 29, 2015 at 05:14 PM 0
Share

@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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · 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
avatar image maho125 · Jul 30, 2015 at 07:35 PM 0
Share

@BJO Thank for your advices. After a few tweaks finally output of my shader is equal to hard light blend mode in photoshop. Here is my solution maybe it helps also somebody else.

This is output of my shader: alt text

avatar image BJO · Jul 30, 2015 at 07:50 PM 0
Share

@maho125 Glad to hear it's working. Looks good!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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