天天看點

unity Kinect v2 with MS-SDK20綠屏摳像shader修改 透明背景

用的是kinect2.0

 Kinect v2 with MS-SDK20插件

例子中的預設greenscreen裡面是綠色的,要求改成透明的,下面直接上代碼

改完後放背景看看吧 是不是透明了

Shader "DX11/GreenScreenShader" {
 SubShader {
 //透明就需要這個
  Blend SrcAlpha OneMinusSrcAlpha
 Tags {"Queue"="AlphaTest" }
 
 
 Pass {
 
 CGPROGRAM
 #pragma target 5.0
 
 #pragma vertex vert
 #pragma fragment frag
 
 #include "UnityCG.cginc"
 
 Texture2D _MainTex;
 
 sampler SampleType;
 
 struct vs_input {
     float4 pos : POSITION;
     float2 tex : TEXCOORD0;
 };
 
 StructuredBuffer<float2> depthCoordinates;
 StructuredBuffer<float> bodyIndexBuffer;
 
 struct ps_input {
     float4 pos : SV_POSITION;
     float2 tex : TEXCOORD0;
 };
 
 ps_input vert (vs_input v)
 {
     ps_input o;
     o.pos = mul (UNITY_MATRIX_MVP, v.pos);
     o.tex = v.tex;
     // Flip x texture coordinate to mimic mirror.
     o.tex.x = 1 - v.tex.x;
     return o;
 }
 
 float4 frag (ps_input i, in uint id : SV_InstanceID) :COLOR
 {
     float4 o;
     
     int colorWidth = (int)(i.tex.x * (float)1920);
     int colorHeight = (int)(i.tex.y * (float)1080);
     int colorIndex = (int)(colorWidth + colorHeight * (float)1920);
     
     o = float4(0, 0, 0, 0);  //<-- Here I set alpha to zero in my version 
                              // to feed into Transparent/cutout/diffuse
     
     if ((!isinf(depthCoordinates[colorIndex].x) && !isnan(depthCoordinates[colorIndex].x) && depthCoordinates[colorIndex].x != 0) || 
         !isinf(depthCoordinates[colorIndex].y) && !isnan(depthCoordinates[colorIndex].y) && depthCoordinates[colorIndex].y != 0)
     {
         // We have valid depth data coordinates from our coordinate mapper.  Find player mask from corresponding depth points.
         float player = bodyIndexBuffer[(int)depthCoordinates[colorIndex].x + (int)(depthCoordinates[colorIndex].y * 512)];
         if (player != 255)
         {
             o = _MainTex.Sample(SampleType, i.tex);
         }else o.a = o.rgb; 
     }
      
      
//    float4 sampler2[9];
//    float4 minValue = float4(255,255,255,255);
//    for (int i = 0; i < 9; ++i)
//    {
        //sampler2[i] = texture2D(SampleType, gl_TexCoord[0].st + tc_offset[i]);
        //minValue = min(minValue, sampler2[i]);
//    }
     return o;
 }

 ENDCG
 
 }
 }
 
 Fallback Off
}