码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-08-26 10:44:43      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:unity   kinect   绿屏   抠像   透明   

用的是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
} 


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:unity   kinect   绿屏   抠像   透明   

原文地址:http://blog.csdn.net/shenmifangke/article/details/47999373

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!