JJCUBER

Alpha Map Shader

Oct 29th, 2019
979
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/AlphaMapShader"
  2. {
  3.     Properties
  4.     {
  5.         // Main Texture
  6.         _MainTex ("Main Texture", 2D) = "white" {}
  7.         // Alpha Map Texture
  8.         _AlphaMapTex ("Alpha Texture", 2D) = "white" {}
  9.         // Color to key out
  10.         _KeyOutColor ("Key Out Color (BG Color)", Color) = (0,0,0,1)
  11.     }
  12.     SubShader
  13.     {
  14.         Tags
  15.         {
  16.             "Queue"="Transparent"
  17.             "RenderType"="Transparent"
  18.         }
  19.  
  20.         // These four lines below fixed my issue with different shaders behaving weird when on top of each other!
  21.         LOD 100
  22.         ZWrite Off
  23.         ZTest Off
  24.         Blend SrcAlpha OneMinusSrcAlpha
  25.  
  26.         CGPROGRAM
  27.         #pragma surface surf Lambert alpha
  28.         #pragma target 3.0
  29.         #include "UnitySprites.cginc"
  30.  
  31.         struct Input
  32.         {
  33.             // The position of the vertex after being transformed into projection space: http://wiki.unity3d.com/index.php?title=Shader_Code
  34.             float4 position  : SV_POSITION;
  35.             // The uv of the main texture (UV Coordinates, is xy values of texture between 0 and 1): http://wiki.winamp.com/wiki/Pixel_Shader_Basics
  36.             // aka First texture coordinate, or UV: http://wiki.unity3d.com/index.php?title=Shader_Code
  37.             float2 uv_MainTex : TEXCOORD0;
  38.             // The uv of the alpha map texture (UV Coordinates, is xy values of texture between 0 and 1): http://wiki.winamp.com/wiki/Pixel_Shader_Basics
  39.             // aka Second texture coordinate, or UV (only 2 UV coords currently supported, but there are ways to bypass): http://wiki.unity3d.com/index.php?title=Shader_Code
  40.             float2 uv_AlphaMapTex : TEXCOORD1;
  41.         };
  42.  
  43.         // sampler2D _MainTex;
  44.  
  45.         // I think this samples from the Alpha Map Texture but not 100% sure, check these sources to find out: https://www.khronos.org/opengl/wiki/Sampler_(GLSL) and https://stackoverflow.com/questions/10868958/what-does-sampler2d-store
  46.         sampler2D _AlphaMapTex;
  47.         // uses the Key Out Color definied above (in properties)
  48.         float4 _KeyOutColor;
  49.  
  50.         // IN is the input (struct of Input is defined above), and o is the output
  51.         void surf (Input IN, inout SurfaceOutput o)
  52.         {
  53.             // sets the color of the output texture (Albedo) to the rgb value of the main texture at the current uv point: https://forum.unity.com/threads/what-is-albedo-and-what-does-it-do.246182/ and https://developer.download.nvidia.com/cg/tex2D.html
  54.             o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
  55.             // creates a variable that equals the color of the key out color subtracted from the color of the output
  56.             float3 diff = o.Albedo.rgb - _KeyOutColor.rgb;
  57.  
  58.             /*
  59.             if (diff.r < 0f)
  60.                 diff.r *= -1f;
  61.             if (diff.g < 0f)
  62.                 diff.g *= -1f;
  63.             if (diff.b < 0f)
  64.                 diff.b *= -1f;
  65.  
  66.             // if (all(o.Albedo.rgb == _KeyOutColor.rgb))
  67.             //     o.Alpha = 0;
  68.             if (all(diff < 0.1f)
  69.                 o.Alpha = 0f;
  70.             */
  71.  
  72.             // if(!any(diff > 0.1 || diff < -0.1))
  73.  
  74.             // if the difference of all the values for this uv point is less than 0.1 from the key out color, then we set the alpha of
  75.             // the output to 0, otherwise the alpha is set to the "alpha" of the alpha map texture at the current uv point of the alpha
  76.             // map texture (I used the red channel instead of the alpha channel so that the alpha map texture could be from black to white
  77.             // instead of from 100% opacity to 0% opacity, since this is how most alpha maps are done that I have seen AND it is far easier
  78.             // to work with when making the actual alpha map textures, so basically white is full opacity and black is no opacity in the
  79.             // alpha map texture)
  80.             if (all(diff < 0.1) && all(diff > -0.1))
  81.                 o.Alpha = 0;
  82.             else
  83.                 o.Alpha = tex2D (_AlphaMapTex, IN.uv_AlphaMapTex).r; // Could use .a, but this way we can use black to white for alpha
  84.         }
  85.  
  86.         ENDCG
  87.     }
  88.     Fallback "Diffuse"
  89.  }
RAW Paste Data