Slide 54
Slide 54 text
#include
using namespace metal;
/// - SeeAlso: http://www.fundza.com/rman_shaders/smoothstep/index.html
kernel void ChromaKeyFilter(
// ೖྗը૾ใ
texture2d inTexture [[ texture(0) ]],
// ग़ྗը૾ใ
texture2d outTexture [[ texture(1) ]],
// ϚεΫରͷ
const device float *colorRed [[ buffer(0) ]],
// ϚεΫରͷ
const device float *colorGreen [[ buffer(1) ]],
// ϚεΫରͷ੨
const device float *colorBlue [[ buffer(2) ]],
// ಁա͕0-1ͷྖҬͷԼݶ
const device float *threshold [[ buffer(3) ]],
// ಁա͕0-1ͷྖҬͷԼݶ͔Β্ݶ·Ͱͷઈର
const device float *smoothing [[ buffer(4) ]],
// ࠲ඪใ
uint2 gid [[ thread_position_in_grid ]])
{
const float4 inColor = inTexture.read(gid);
// maskͷରͷ৭ɻ৭Λಁա͢ΔͨΊmaskColorfloat3(0, 1, 0)ʹͳΔ
const float3 maskColor = float3(*colorRed, *colorGreen, *colorBlue);
// RGB͔ΒYͷࣹ
const float3 YVector = float3(0.2989, 0.5866, 0.1145);
// maskColor(৭)ΛYCrCbม
const float maskY = dot(maskColor, YVector);
const float maskCr = 0.7131 * (maskColor.r - maskY);
const float maskCb = 0.5647 * (maskColor.b - maskY);
// ࠲ඪͷRGBΛYCrCbม
const float Y = dot(inColor.rgb, YVector);
const float Cr = 0.7131 * (inColor.r - Y);
const float Cb = 0.5647 * (inColor.b - Y);
// YCrCbɺᮢɺ৭ͱͷڑʹΑΓಁաΛࢉग़
const float alpha = smoothstep(
*threshold,
*threshold + *smoothing,
distance(float2(Cr, Cb), float2(maskCr, maskCb))
);
// ࠲ඪͷRGBΛࢉग़ͨ͠ಁաͰग़ྗ͢Δ৭Λܭࢉ
const float4 outColor = alpha * float4(inColor.r, inColor.g, inColor.b, 1.0);
outTexture.write(outColor, gid);
}
!54