Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Custom/AlphaMapShader"
- {
- Properties
- {
- // Main Texture
- _MainTex ("Main Texture", 2D) = "white" {}
- // Alpha Map Texture
- _AlphaMapTex ("Alpha Texture", 2D) = "white" {}
- // Color to key out
- _KeyOutColor ("Key Out Color (BG Color)", Color) = (0,0,0,1)
- }
- SubShader
- {
- Tags
- {
- "Queue"="Transparent"
- "RenderType"="Transparent"
- }
- // These four lines below fixed my issue with different shaders behaving weird when on top of each other!
- LOD 100
- ZWrite Off
- ZTest Off
- Blend SrcAlpha OneMinusSrcAlpha
- CGPROGRAM
- #pragma surface surf Lambert alpha
- #pragma target 3.0
- #include "UnitySprites.cginc"
- struct Input
- {
- // The position of the vertex after being transformed into projection space: http://wiki.unity3d.com/index.php?title=Shader_Code
- float4 position : SV_POSITION;
- // 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
- // aka First texture coordinate, or UV: http://wiki.unity3d.com/index.php?title=Shader_Code
- float2 uv_MainTex : TEXCOORD0;
- // 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
- // 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
- float2 uv_AlphaMapTex : TEXCOORD1;
- };
- // sampler2D _MainTex;
- // 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
- sampler2D _AlphaMapTex;
- // uses the Key Out Color definied above (in properties)
- float4 _KeyOutColor;
- // IN is the input (struct of Input is defined above), and o is the output
- void surf (Input IN, inout SurfaceOutput o)
- {
- // 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
- o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
- // creates a variable that equals the color of the key out color subtracted from the color of the output
- float3 diff = o.Albedo.rgb - _KeyOutColor.rgb;
- /*
- if (diff.r < 0f)
- diff.r *= -1f;
- if (diff.g < 0f)
- diff.g *= -1f;
- if (diff.b < 0f)
- diff.b *= -1f;
- // if (all(o.Albedo.rgb == _KeyOutColor.rgb))
- // o.Alpha = 0;
- if (all(diff < 0.1f)
- o.Alpha = 0f;
- */
- // if(!any(diff > 0.1 || diff < -0.1))
- // 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
- // 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
- // map texture (I used the red channel instead of the alpha channel so that the alpha map texture could be from black to white
- // 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
- // to work with when making the actual alpha map textures, so basically white is full opacity and black is no opacity in the
- // alpha map texture)
- if (all(diff < 0.1) && all(diff > -0.1))
- o.Alpha = 0;
- else
- o.Alpha = tex2D (_AlphaMapTex, IN.uv_AlphaMapTex).r; // Could use .a, but this way we can use black to white for alpha
- }
- ENDCG
- }
- Fallback "Diffuse"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement