Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Mask Sprite With Another Sprite

Discussion in '2D' started by TabTaleTravis, Nov 12, 2015.

  1. TabTaleTravis

    TabTaleTravis

    Joined:
    Nov 12, 2015
    Posts:
    1
    I have 3 GameObjects using a SpriteRenderer to draw solid-colored quads behind a larger, transparent one as shown in the attached screenshot. In this case, the larger, transparent image is the rocket. The rocket image is also using a SpriteRenderer to draw the image. I want to mask my 3 large quads using that rocket image, but my knowledge of how to blend are simple. I have 4 SpriteRenderers on the same sorting layer (Default) where the 3 solid-color SpriteRenderers have an "Order in Layer" of 1, and the rocket SpriteRenderer has an "Order in Layer" of 2. I want to achieve the same effect as UI Mask, except with the SpriteRenderer. I'm pretty sure I have to create a custom shader, and attach it to each solid-colored SpriteRenderer, and reference the rocket's SpriteRenderer using the sorting layer (probably using "Queue" in the shader?)

    I'm really not sure where to go with this, but I did come across this thread earlier on blending. It's a fixed-function shader, so it'll have compatibility issues in Unity 5. I'm trying to convert it over to a regular vertex/fragment shader pair. Does anyone have any ideas on how to mask those quad sprites against the shape of the rocket sprite?

    EDIT: I forgot to mention that I went to this page to figure out what kind of blending I need to use. It's for OpenGL, but Unity's renderer runs on top of APIs like OGL, DX, etc. The blending mode I think I need is SrcAlpha, SrcAlpha, but no luck in my shader.

    Here's the example shader I've been trying to put together:
    Code (CSharp):
    1. Shader "Custom/SpriteMask"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    6.         _Cutoff ("Base Alpha cutoff", Range (0.0, 0.9)) = 0.5
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags {"Queue" = "Transparent+1"}
    12.         Offset 0, -1
    13.         ColorMask 0
    14.         ZWrite On
    15.         Blend SrcAlpha SrcAlpha
    16.  
    17.         Pass
    18.         {
    19.             AlphaTest Greater [_Cutoff]
    20.             SetTexture [_MainTex]
    21.             {
    22.                 combine texture * primary, texture
    23.             }
    24.         }
    25.     }
    26. }
    27.  
    It's fixed-function (should convert to vert/frag), but it's what the first linked post had. Since it's the closest thing I could find to a solution, I was going to keep it like that until I'm more familiar with CG as it's pretty different from GLSL.
     

    Attached Files:

    Last edited: Nov 12, 2015
  2. sandboxed

    sandboxed

    Unity Technologies

    Joined:
    Apr 6, 2015
    Posts:
    95
    theANMATOR2b likes this.