Getting Alpha from Grayscale on a Render Texture

I wasn’t sure whether to ask this in Shaders or General, but I guess I’ll put it here.

In short, here’s what I’m trying to do. I have two cameras: One that only looks at white shapes on a black background, and one that only looks at colored patterns. Both render to a RenderTexture. I have a shader that should be getting the alpha from the first one, then applying it to the second one, and thus giving me the color pattern on the second one in the shapes on the first one.

And it works, but only on some objects.

Here’s the masking RenderTexture (though with some shading stuff going on that’s not actually on the RenderTexture, just something I didn’t feel like fixing) and the color I’m trying to apply (just a flat red for now, but the idea is that I’ll be able to apply various graphical effects):

Here’s what I SHOULD be getting (again, minus the shading):

But here’s what I actually am getting:

Here’s the shader that I’m using:

Shader "Hidden/AlphaBlendText"
{
    Properties
    {
        _MainTex("Main Texture", 2D) = "white" {}
        _Alpha("Mask", 2D) = "white" {}

    }
   
    SubShader
    {
        Tags{ "RenderType" = "Transparent" "Queue" = "Transparent" }

        ZWrite Off
        Cull Off

        Blend SrcAlpha OneMinusSrcAlpha
       
        ColorMask RGB

        Pass
        {
            SetTexture[_MainTex]
            {
                Combine texture
            }
           
            SetTexture[_Alpha]
            {
                Combine previous * texture
            }
        }
    }
}

I don’t totally understand what’s going on in this shader, to be honest. I just found this somewhere on the internet. I’ve tried messing around with it, but every time I change something, it just breaks things even further, and I can’t seem to find any good documentation on programming shaders.

What seems to be happening is that the RenderTexture is only picking up the alpha from the paddles, which I made from untextured boxes, but not from the buttons, which are made from textured planes.

I just feel like there should be a way to tell this, instead of using the existing alpha on _Alpha, create a new alpha from the colors. Something like, _Alpha.a = (_Alpha.r + _Alpha.g + _Alpha.b) / 3. But I’ve been Googling around for like 4 hours and haven’t found any evidence of being able to do this. It seems like nobody else has tried to do something like this. Well, no, I did find one guy who had the exact same question as me, but then he just followed it up with, “Nevermind, figured it out,” and disappeared forever without sharing his solution. So that was great.

With all the crazy stuff I’ve seen shaders capable off, this simple thing has to be possible, right?

Oh my god, I’m about to soil myself out of sheer joy because I just figured it out.

In the script, take this line:

Blend SrcAlpha OneMinusSrcAlpha

And change it to this:

Blend SrcColor OneMinusSrcColor

I’m not 100% sure what this does, and I’d be willing to bet that it would mess stuff up if the mask layer wasn’t already grayscale. But hey, it works for my purposes.

The beautiful result, if anyone cares:

1 Like