Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheSky · Jul 06, 2017 at 02:05 AM · shaderspritessubtract

Subtractive Shader

Hello

I would like to achieve the following effect:
If two sprites overlap, the colors in the overlapping area should blend subtractive, as shown in the following image. alt text

Is there a shader that achieves this effect or can you modify the standard shaders accordingly?

wuhgus.jpg (8.8 kB)
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

2 Replies

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

Answer by Bunny83 · Jul 06, 2017 at 02:58 AM

Well you can subtract colors by choosing a proper blend mode / blend factor combination. Instead of the usual alpha blending which is

 BlendOp Add
 Blend SrcAlpha OneMinusSrcAlpha 

You can use:

 BlendOp RevSub
 Blend One One

Keep in mind that subtraction happens on each channel seperately and the result is of course clamped between 0 and 1. Also keep in mind that the order matters. subtracting 100% red from 100% yellow would result in 100% green. Though the reverse would be black since:

   (1,1,0) yellow
  -(1,0,0) red
 --------------
   (0,1,0) green

   (1,0,0) red
  -(1,1,0) yellow
 --------------
   (0,-1,0) --clamped to 0 --> (0,0,0) black

If you ment that you want to perform subtractive color mixing that's not really possible in a shader as computer graphics work in an RGB color space (additive color space) while real-world paint mixing happens in a subtactive color space. In theory all you have to do is converting all colors from RGB into CMY, do the mixing and convert them back to RGB. However this is not possible with the available blendmodes. The main problem is the destination color in the frame buffer. It can't be simply read into the shader. If that would be possible it would be simple

To convert an RGB value into CMY all you have to do is to invert all channels:

 RGB <-> CMY
 ----------------------
 000 <-> 111 --> black
 100 <-> 011 --> red
 010 <-> 101 --> green
 001 <-> 110 --> blue
 011 <-> 100 --> cyan
 101 <-> 010 --> magenta
 110 <-> 001 --> yellow
 111 <-> 000 --> white


Comment
Add comment · Show 1 · 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 koboldskeep · Apr 26, 2020 at 12:47 AM 0
Share

Ah, that works for me. I've been looking for a simple subtractive color shader. Unity's standard sprite shader with "BlendOp RevSub" and "Blend One One" does exactly what I want it to!

avatar image
0

Answer by Jwizard93 · Jul 06, 2017 at 02:40 AM

ShaderLab

Just like you messed with the blend mode in your last question you can mess with it here and add the Op "sub".

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

104 People are following this question.

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

Related Questions

Shaders - offset texture coordinates by a single pixel 2 Answers

Why not tiling on Shader (Sprite/Diffuse)? Unity 5.4.1 f1 1 Answer

How to get correct UV's for second texture in a sprite shader 1 Answer

How to edit a shader to make it show sprites it's on without lighting? 0 Answers

How to make transparent shader (2D)? 1 Answer


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