Blend mode shader?

Okay, let me preface this by saying I don’t really know what I’m talking about. I don’t know where I should be posting this but shaders seemed like a good place. :???:

In another program I used for 2D games, there was a property for sprites called blend mode with options like source in, source out, destination over, etc, that alowed for effects like the picture below. The red square is the source, the one with the blend mode property, and the green shapes are the destination; basically the red square is invisible anywhere except where it overlaps the green shapes.

Here’s the url if the picture doesn’t show up for you: Imgur: The magic of the Internet

So now here I am making a 2D game in Unity (using 2D toolkit if that matters) trying to replicate that effect. Since the keywords involved are pretty generic, I couldn’t find any answers searching the forums or through Google, so sorry if this is a stupid question. But does anyone know how I can do this? :slight_smile:

It didn’t show up huh? I edited the url into the post, hopefully the forums aren’t just blocking imgur or something.


Thanks for the response. All of that is definitely going over my head, though…

The answer isn’t so much about Blending, refers more to Masks. Take a look at shader here:

In the fragment program you’ll find this comment and code line:

//assemble the RGB from _MainTex with the splatted channel from _Mask
//Note that XYZW is equivalent to RGBA so Mask.x is effectively Mask.r, change as needed.
float4 MaskedTex = float4(MainTex.x, MainTex.y, MainTex.z, Mask.x);

change MainTex’s alpha to equal the Mask’s green channel, by doing this:

float4 MaskedTex = float4(MainTex.x, MainTex.y, MainTex.z, Mask.y);

Ahh yeah, masking sounds like what I need. The other program called it blend modes, sorry for the mix up!

I can kinda get a general idea of what that shader’s doing, reeeally general. But the picture I posted is just an example of what I want to do, what I actually need to use it for is full textures and not just solid reds and greens. I literally know nothing about shaders though, so I’d be totally lost about how to modify that for my needs…

I found this thread about something similar, now I just need to figure out how to use it for myself:
http://answers.unity3d.com/questions/56564/Treating-an-animated-sprite-as-a-mask.html