Projecting a Masked Image

Hi friends,

I’m new to shader writing and I’ve been having a difficult time. I’m trying to create a shader that projects a portion of one texture according to an alpha map in a second texture onto a surface:

=

Here’s my pitiful attempt which seems to render the decal ok, but doesn’t mask it with the second texture:

Shader "Projector/MaskedImage" {
  Properties {
     _Decal ("Decal", 2D) = "" { TexGen ObjectLinear }
     _Mask ("Alpha Mask", 2D) = "" { TexGen ObjectLinear }
  }
  Subshader {
     Pass {
        Tags {"Queue"="Transparent"}
        ZWrite off
        Blend SrcAlpha OneMinusSrcAlpha
        Offset -1, -1

        SetTexture [_Decal] {
           combine texture
           Matrix [_Projector]
        }

        SetTexture [_Mask] {
           combine texture, previous
           Matrix [_Projector]
        }


     }
  }
}

Anyone have any suggestions? Sorry for the trouble

Got it to work:

Shader "Projector/MaskedImage" {
  Properties {
     _Decal ("Decal", 2D) = "" { TexGen ObjectLinear }
     _Mask ("Alpha Mask", 2D) = "" { TexGen ObjectLinear }
  }
  Subshader {
     Pass {
        Tags {"Queue"="Transparent"}
        ZWrite off
        Blend SrcAlpha OneMinusSrcAlpha
        Offset -1, -1

        SetTexture [_Decal] {
           combine texture
           Matrix [_Projector]
        }

        SetTexture [_Mask] {
           constantColor (0,0,0,0)
           combine previous lerp(texture) constant
           Matrix [_Projector]
        }


     }
  }
}

The primary texture must have an alpha channel (go to channels tab next to layers in Photoshop and hit the “Create new Channel” icon
The masking texture can be a greyscale image, just select “Alpha from Grayscale” checkbox in the texture import settings when selecting the “Advanced” option from the Texture Type dropdown.

You never need to type “combine texture”; that’s the default behavior. Lerping is overkill if the lerp is to black; this is clearer and possibly faster:

Combine previous * texture alpha

There may be something else to simplify, but I don’t know what you’re doing with blending.