Search Unity

Tint Additive shader help for iOS!

Discussion in 'Shaders' started by bobcat53, Feb 9, 2012.

  1. bobcat53

    bobcat53

    Joined:
    Jul 12, 2010
    Posts:
    26
    Hi I am using Jessy's additive shader on iOS for the shadow material of my objects. However the shader currently additively blends out the black areas of my texture and I would like my shadow to be black as well. How can I add a tint color to this shader?

    Thanks!

    Jessy's shader is as follows:

    Code (csharp):
    1. Shader "Additive Texture" {
    2.  
    3. Properties {
    4.     _MainTex ("Texture", 2D) = ""
    5. }
    6.  
    7. SubShader {
    8.     Tags {Queue = Transparent}
    9.     Blend One One
    10.     ZWrite Off
    11.     Pass {
    12.         SetTexture[_MainTex]
    13.     }
    14. }
    15.  
    16. }
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,792
    Additive means... what it says. It adds the color of the shader to whatever is in the background. So if you have black it basically does Background(RGB) + (0,0,0)... Which results in the same RGB values being returned. In short it does nothing (visually). So unless you are using negative values for some reason, using an additive blend mode will always result in things getting brighter, not darker.

    So it's not additive you want. Try different blend modes : http://unity3d.com/support/documentation/Components/SL-Blend.html
     
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Except, the values for blending are clamped 0-1. Unity 3.5 does allow you to use subtractive blending, though.
     
  4. bobcat53

    bobcat53

    Joined:
    Jul 12, 2010
    Posts:
    26
    Thnx for the hint acidarrow, I changed to multiplicative blending and got what I wanted now.

    Cheers
     
  5. WarpZone

    WarpZone

    Joined:
    Oct 29, 2007
    Posts:
    326
    Sorry for resurrecting an old topic, but I can't find this anywhere. What are the legal values you can use for Sub and RevSub? Do you need to use both? Are they numbers? Strings indicating Blend Modes like "Multiply," "Subtract," etc?? What does each variable actually do?

    Everyone keeps copypasting the line "BlendOp Min|Max|Sub|RevSub," but I can't find Sub and RevSub documented anywhere.

    Edit: I did see a promising link to this page: https://www.opengl.org/wiki/Blending#Blend_Equations which of course is a broken link.

    Edit: Whoops. HTTPS Everywhere was preventing the wiki from loading properly. Had to disable it temporarily. Here's the relevant bit:

    Actually, wait... If these are on the OpenGL wiki, does that mean we need to implement a different version of BlendOp for each hardware manufacturer? And I still don't understand BlendOp's syntax exactly. Please help.

    Also, Jessy, I think I read somewhere that it's only clamped between 0 and 1.0 on one manufacturer's cards. On the other manufacturer's cards, it will return higher values. This means if you're counting on it to clamp or not clamp just because it looks right on your card, it'll be broken on other users' cards. :(
     
    Last edited: Dec 6, 2012
  6. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    why not use a cg shader? it lets you use normal maths then, such as multiply, plus, minus, or whatever - and works on everything? it is not as hard as you might suspect...
     
  7. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    http://docs.unity3d.com/Documentation/Components/SL-Blend.html says:

    Code (csharp):
    1. BlendOp Min | Max | Sub | RevSub
    Thus, either you write "BlendOp Min" or "BlendOp Max" or "BlendOp Sub" or "BlendOp RevSub".

    There isn't more syntax. And I'm pretty sure they are also supported on Direct3D.
     
    Last edited: Dec 6, 2012
  8. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    I don't understand. Isn't the question about ShaderLab, which is the same for Cg and GLSL?
     
  9. Daniel_Brauer

    Daniel_Brauer

    Unity Technologies

    Joined:
    Aug 11, 2006
    Posts:
    3,355
    When documenting commands, the | (pipe) character means "or". So the bit that everyone keeps pasting means you can write:

    BlendOp Min
    or
    BlendOp Max
    or
    BlendOp Sub
    ...

    Blending operations are limited by hardware, so instead of letting you write an arbitrary function to do your blending, graphics drivers require you to pick from a set of supported functions, and then fill in the blanks. The examples above are complete usages of the BlendOp command. There are no other parameters, because all it does is choose the blending function. You can only choose one blending function.

    The other half of blending is setting the variables that will be used in the function using the Blend command. This sets the lower-case s and d parameters in the document you quoted. S is the colour coming from your shader, and D is the colour already occupying the target pixel.


    Now, some examples are in order. First, straight additive blending:

    Blend One One

    Since the default blend operation is addition, we are using this formula from the page you quoted:

    s*S + d*D

    ...and our Blend command has set s and d both to one, which reduces to:

    S + D

    The source colour is added to the destination, and that's it.


    Another common example would be alpha blending:

    Blend SrcAlpha OneMinusSrcAlpha

    Again, there is no need to use the BlendOp command for this:

    s*S + d*D

    ...becomes:

    S.alpha * S + (1 - S.alpha) * D

    This is slightly more complicated than the previous equation, but it's a pretty straightforward linear interpolation of S and D by S.alpha.


    Multiplicative blending is also done using the additive blend function:

    Blend DstColor Zero

    ...means:

    D*S + 0*D

    ...which is just:

    D*S


    Finally, let's do what I would call the intuitive order for subtractive blending, but the hardware (and thus OpenGL) calls reversed:

    BlendOp RevSub
    Blend One One


    This gives us the reversed subtractive function:

    dD - sS

    ...and the factors are both one, so you get:

    D - S

    ...which means that whatever colour your shader outputs will be subtracted from what is already in the destination pixel.

    OpenGL is just an API. Direct3D is a competing API. With the right drivers, both can be used to talk to the same hardware. For the most part, both APIs let you do similar things, because that's what's supported by the hardware.

    This may have been true in the past, but I think that at this point you can safely consider cards that don't clamp to be broken, and ignore them.

    This thread is about blending operations, which are (perhaps surprisingly) still fixed function.
     
    Last edited: Dec 6, 2012