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
} 


  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值