Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Alpha based off texture color

Discussion in 'Shaders' started by ygliam, Mar 10, 2017.

  1. ygliam

    ygliam

    Joined:
    Jan 22, 2016
    Posts:
    6
    What would be the easiest way to base the alpha of a pixel on the green component of the texture color in a shader.
    So any green parts of the texture are transparent, and the rest is left alone.
     
  2. ygliam

    ygliam

    Joined:
    Jan 22, 2016
    Posts:
    6
    Solved it:

    Code (CSharp):
    1.      
    2. Tags{ "Queue" = "Transparent" "RenderType" = "Transparent"}
    3.  
    4. void surf(Input IN, inout SurfaceOutput o) {
    5.         o.Albedo = tex2D(_MainTex, IN.uv_MainTex);
    6.         o.Alpha = 1 - tex2D(_MainTex, IN.uv_MainTex).g;
    7.     }
     
  3. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    Don't do 2 samples... just do 1 - o.Albedo.g
     
  4. maximeb-valtech

    maximeb-valtech

    Joined:
    Feb 14, 2017
    Posts:
    1
    Hi, I am trying to do something very similar but lack any knowledge about shader programming. What I want is to use a .mp4 movie in a UI canvas and to make its background transparent. Such that for each pixel that is a given color, the shader will skip it.

    Would it be possible to adapt the previous answer for my need? Also, it would be nice to have more context given as to the previous code sample, I have no idea how it fits inside of a .shader file.

    Thanks.