ChromaCutoutShader.shader 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. Shader "Imagine/ChromaKeyCutout" {
  2. Properties {
  3. _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
  4. _MaskCol ("Mask Color", Color) = (1.0, 0.0, 0.0, 1.0)
  5. _Sensitivity ("Threshold Sensitivity", Range(0,1)) = 0.5
  6. _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
  7. }
  8. SubShader {
  9. Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
  10. LOD 100
  11. Lighting Off
  12. Pass {
  13. CGPROGRAM
  14. #pragma vertex vert
  15. #pragma fragment frag
  16. #pragma target 2.0
  17. #pragma multi_compile_fog
  18. #include "UnityCG.cginc"
  19. struct appdata_t {
  20. float4 vertex : POSITION;
  21. float2 texcoord : TEXCOORD0;
  22. UNITY_VERTEX_INPUT_INSTANCE_ID
  23. };
  24. struct v2f {
  25. float4 vertex : SV_POSITION;
  26. float2 texcoord : TEXCOORD0;
  27. UNITY_FOG_COORDS(1)
  28. UNITY_VERTEX_OUTPUT_STEREO
  29. };
  30. sampler2D _MainTex;
  31. float4 _MainTex_ST;
  32. fixed _Cutoff;
  33. float4 _MaskCol;
  34. float _Sensitivity;
  35. v2f vert (appdata_t v)
  36. {
  37. v2f o;
  38. UNITY_SETUP_INSTANCE_ID(v);
  39. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  40. o.vertex = UnityObjectToClipPos(v.vertex);
  41. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  42. UNITY_TRANSFER_FOG(o,o.vertex);
  43. return o;
  44. }
  45. fixed4 frag (v2f i) : SV_Target
  46. {
  47. fixed4 c = tex2D(_MainTex, i.texcoord);
  48. float maskY = 0.2989 * _MaskCol.r + 0.5866 * _MaskCol.g + 0.1145 * _MaskCol.b;
  49. float maskCr = 0.7132 * (_MaskCol.r - maskY);
  50. float maskCb = 0.5647 * (_MaskCol.b - maskY);
  51. float Y = 0.2989 * c.r + 0.5866 * c.g + 0.1145 * c.b;
  52. float Cr = 0.7132 * (c.r - Y);
  53. float Cb = 0.5647 * (c.b - Y);
  54. float blendValue = smoothstep(_Sensitivity, _Sensitivity, distance(float2(Cr, Cb), float2(maskCr, maskCb)));
  55. clip(blendValue - _Cutoff);
  56. UNITY_APPLY_FOG(i.fogCoord, c);
  57. return c;
  58. }
  59. ENDCG
  60. }
  61. }
  62. }