ChromaCutoutShader.shader 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. _Feather ("Feathering", Range(0,1)) = 1
  8. }
  9. SubShader {
  10. Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
  11. LOD 100
  12. Lighting Off
  13. Pass {
  14. CGPROGRAM
  15. #pragma vertex vert
  16. #pragma fragment frag
  17. #pragma target 2.0
  18. #pragma multi_compile_fog
  19. #include "UnityCG.cginc"
  20. struct appdata_t {
  21. float4 vertex : POSITION;
  22. float2 texcoord : TEXCOORD0;
  23. UNITY_VERTEX_INPUT_INSTANCE_ID
  24. };
  25. struct v2f {
  26. float4 vertex : SV_POSITION;
  27. float2 texcoord : TEXCOORD0;
  28. UNITY_FOG_COORDS(1)
  29. UNITY_VERTEX_OUTPUT_STEREO
  30. };
  31. sampler2D _MainTex;
  32. float4 _MainTex_ST;
  33. fixed _Cutoff;
  34. fixed _Feather;
  35. float4 _MaskCol;
  36. float _Sensitivity;
  37. v2f vert (appdata_t v)
  38. {
  39. v2f o;
  40. UNITY_SETUP_INSTANCE_ID(v);
  41. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
  42. o.vertex = UnityObjectToClipPos(v.vertex);
  43. o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
  44. UNITY_TRANSFER_FOG(o,o.vertex);
  45. return o;
  46. }
  47. fixed4 frag (v2f i) : SV_Target
  48. {
  49. fixed4 c = tex2D(_MainTex, i.texcoord);
  50. // float maskY = 0.2989 * _MaskCol.r + 0.5866 * _MaskCol.g + 0.1145 * _MaskCol.b;
  51. // float maskCr = 0.7132 * (_MaskCol.r - maskY);
  52. // float maskCb = 0.5647 * (_MaskCol.b - maskY);
  53. // float Y = 0.2989 * c.r + 0.5866 * c.g + 0.1145 * c.b;
  54. // float Cr = 0.7132 * (c.r - Y);
  55. // float Cb = 0.5647 * (c.b - Y);
  56. float MY = 0.2989*_MaskCol.r + 0.5866*_MaskCol.g + 0.1145*_MaskCol.b;
  57. float MCr = 0.5 + 0.5*_MaskCol.r - 0.418688*_MaskCol.g - 0.081312*_MaskCol.b;
  58. float MCb = 0.5 + -0.168736*_MaskCol.r - 0.331264*_MaskCol.g + 0.5*_MaskCol.b;
  59. float Y = 0.2989 * c.r + 0.5866 * c.g + 0.1145 * c.b;
  60. float Cr = 0.5 + 0.5*c.r - 0.418688*c.g - 0.081312*c.b;
  61. float Cb = 0.5 + -0.168736*c.r - 0.331264*c.g + 0.5*c.b;
  62. // float dist = distance(float2(Cr, Cb), float2(MCr, MCb));
  63. float sqDist = (Cr - MCr)*(Cr - MCr) + (Cb - MCb)*(Cb - MCb);
  64. float S2 = _Sensitivity * _Sensitivity;
  65. float F2 = _Feather * _Feather;
  66. float d = 1;
  67. if(sqDist < S2) d = 0;
  68. else if(sqDist < F2) d = (sqDist-S2)/(F2-S2);
  69. // float blendValue = smoothstep(_Sensitivity, _Sensitivity, dist);
  70. clip(d - _Cutoff);
  71. return c;//float4(sqDist, sqDist, sqDist, 1);
  72. }
  73. ENDCG
  74. }
  75. }
  76. }