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. Dismiss Notice

Cutoff + AlphaBlend problems

Discussion in 'Shaders' started by oakshiro, Jul 4, 2012.

  1. oakshiro

    oakshiro

    Joined:
    Dec 9, 2008
    Posts:
    20
    Hi there,

    I'm trying to build a Shader that should allow me to perform a "clock animation" between two textures.
    The idea is to have a cutoff value and use a gradient texture to cut the texture but preserving the alpha value of the main texture.

    Here is the Shader
    Code (csharp):
    1.  
    2. Shader "MaskedTexture"
    3. {
    4.    Properties
    5.    {
    6.       _MainTex ("Base (RGB)", 2D) = "white" {}
    7.       _Mask ("Culling Mask", 2D) = "white" {}
    8.       _Cutoff ("Alpha cutoff", Range (0,1)) = 0.1
    9.    }
    10.    
    11.    SubShader
    12.    {
    13.       Tags {"Queue"="Transparent" }
    14.       Lighting Off
    15.       ZWrite Off
    16.       AlphaTest GEqual [_Cutoff]
    17.       Blend SrcAlpha OneMinusSrcAlpha    
    18.        
    19.      BindChannels {
    20.         Bind "Color", color
    21.         Bind "Vertex", vertex
    22.         Bind "TexCoord", texcoord
    23.     }    
    24.                      
    25.       Pass
    26.       {                                
    27.          SetTexture [_Mask] {combine texture}  
    28.          SetTexture [_MainTex] {matrix [_Rotation] combine texture, previous * texture }                                 
    29.       }                                  
    30.    }
    31. }
    32.  
    And this is what is happening...



    The cutted off gradient texture process is being done correctly, the problem is when I merge it, the blending is using the gray texture in the gradient and therefore it appears semitransparent. I suppose i should convert the cutted circle into a full-white-with-no-gray-at-all one, but don't know how!

    Anyone please?
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    It doesn't look like you're going about this well. In particular, you haven't shown what you're doing for desaturation, but rendering that, and then a colorized version in another pass, is wasteful. Fixed functions shaders are not good for your effect; it can be done, but your time will be better spent learning enough Cg to do this better.
     
  3. oakshiro

    oakshiro

    Joined:
    Dec 9, 2008
    Posts:
    20
    I think I failed to explain everything...
    There are TWO images, one is the moon and the other one is the sun. They are separate images and have separate shaders. The front one (the sun) is the one that should "erase" himself in a clock mood, letting you see the back graphic (the moon). If i deactivate blending it renders correctly, but with ugly borders...
     
  4. oakshiro

    oakshiro

    Joined:
    Dec 9, 2008
    Posts:
    20
    Hi there,

    In the end i found a solution.
    The problem was the cutoff was being applied, but also the alpha blending. Removing the "Blend SrcAlpha OneMinusSrcAlpha" was the key to all :)

    Thanks for the help anyway :)