| 1 | fixed4 _ChromaKeyColor; |
| 2 | float _ChromaKeySimilarity; |
| 3 | float _ChromaKeySmoothRange; |
| 4 | float _ChromaKeySpillRange; |
| 5 | |
| 6 | // https://en.wikipedia.org/wiki/YUV |
| 7 | float3 RGB2YUV(float3 rgb) |
| 8 | { |
| 9 | float r = rgb.x; |
| 10 | float g = rgb.y; |
| 11 | float b = rgb.z; |
| 12 | float y = 0.2126 * r + 0.7152 * g + 0.0722 * b; |
| 13 | float u = -0.09991 * r - 0.33609 * g + 0.436 * b; |
| 14 | float v = 0.615 * r - 0.55861 * g - 0.05639 * b; |
| 15 | return float3(y, u, v); |
| 16 | } |
| 17 | |
| 18 | float3 YUV2RGB(float3 yuv) |
| 19 | { |
| 20 | float y = yuv.x; |
| 21 | float u = yuv.y; |
| 22 | float v = yuv.z; |
| 23 | float r = y + 1.28033 * v; |
| 24 | float g = y - 0.21482 * u - 0.38059 * v; |
| 25 | float b = y + 2.12798 * u; |
| 26 | return float3(r, g, b); |
| 27 | } |
| 28 | |
| 29 | fixed4 GetCameraColor(float2 colorUV); |
| 30 | |
| 31 | float ColorDistance_YUV_YUV(float3 yuv1, float3 yuv2) |
| 32 | { |
| 33 | float dist = distance(yuv1.yz, yuv2.yz); |
| 34 | // Increase the distance if the brightness of the first color is too high. |
| 35 | // It fixed the error on the over exposure areas |
| 36 | dist += saturate(yuv1.x - 0.9); |
| 37 | return dist; |
| 38 | } |
| 39 | |
| 40 | float ColorDistance_RGB_YUV(float3 rgb1, float3 yuv2) |
| 41 | { |
| 42 | float3 yuv1 = RGB2YUV(rgb1); |
| 43 | return ColorDistance_YUV_YUV(yuv1, yuv2); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | float ColorDistance_RGB_RGB(float3 rgb1, float3 rgb2) |
| 48 | { |
| 49 | float3 yuv1 = RGB2YUV(rgb1); |
| 50 | float3 yuv2 = RGB2YUV(rgb2); |
| 51 | return ColorDistance_YUV_YUV(yuv1, yuv2); |
| 52 | } |
| 53 | |
| 54 | float RGB2Gray(float3 rgb) |
| 55 | { |
| 56 | float r = rgb.x; |
| 57 | float g = rgb.y; |
| 58 | float b = rgb.z; |
| 59 | float y = 0.2126 * r + 0.7152 * g + 0.0722 * b; |
| 60 | return y; |
| 61 | } |
| 62 | |
| 63 | float GetAlphaFromDistance(float dist) |
| 64 | { |
| 65 | float result = smoothstep(_ChromaKeySimilarity, _ChromaKeySimilarity + _ChromaKeySmoothRange, dist); |
| 66 | result = result * result; |
| 67 | return result; |
| 68 | } |
| 69 | |
| 70 | float GetSpillFromDistance(float dist) |
| 71 | { |
| 72 | float result = smoothstep(_ChromaKeySimilarity, _ChromaKeySimilarity + _ChromaKeySpillRange, dist); |
| 73 | result = result * result * result; |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | float4 GetColorAfterChromaKey(float2 UV, float2 deltaUV, float step) |
| 79 | { |
| 80 | float3 chromaColor = _ChromaKeyColor.rgb; |
| 81 | float3 chromaYUV = RGB2YUV(chromaColor); |
| 82 | float dist = 0.0; |
| 83 | const int samples = 3; |
| 84 | float offset = ((float)samples - 1.0) / 2.0; |
| 85 | for (int i = 0; i < samples; ++i) |
| 86 | { |
| 87 | for (int j = 0; j < samples; ++j) |
| 88 | { |
| 89 | fixed4 color = GetCameraColor(UV + float2((float)i - offset, (float)j - offset) * deltaUV * step); |
| 90 | float d = ColorDistance_RGB_YUV(color, chromaYUV); |
| 91 | dist += d; |
| 92 | } |
| 93 | } |
| 94 | dist /= (samples * samples); |
| 95 | fixed4 centerColor = GetCameraColor(UV); |
| 96 | float alpha = GetAlphaFromDistance(dist); |
| 97 | float spill = GetSpillFromDistance(dist); |
| 98 | float gray = RGB2Gray(centerColor.rgb); |
| 99 | float4 outColor; |
| 100 | outColor.rgb = lerp(float3(gray, gray, gray), centerColor.rgb, spill); |
| 101 | outColor.a = alpha; |
| 102 | return outColor; |
| 103 | } |