- Home /
adapting the green screen shader from Kinect v2 package
I'm using the Kinect V2 (build 1409) unity package and have a question about the "green screen" shader. It works fine out of the box such that a game object that has the "CoordinateMapperView" script (from the package) can use the "GreenScreen" material (that uses the "GreenScreenShader" also in the package) to render just the player RGB image with a green background.
What I want is to make the green transparent. I have been able to do it by creating a render texture of the green screen (while setting the green pixels alpha to zero, see comment below in the code) and then applying the default transparent/cutout/diffuse shader to the render texture.
My question is whether this can be done in one step by adapting the "GreenScreenShader" shader (code pasted below)? I have looked at the cutout shader (code below) and just don't know anything about shader programming and whether the "surf" section in the cutout can be added to the green screen.
Thanks!
GreenScreenShader
Shader "DX11/GreenScreenShader" {
SubShader {
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, 1, 0, 1); //<-- 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);
}
}
return o;
}
ENDCG
}
}
Fallback Off
}
Transparent/Cutout/Diffuse
Shader "Transparent/Cutout/Diffuse" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
LOD 200
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff
sampler2D _MainTex;
fixed4 _Color;
struct Input {
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "Transparent/Cutout/VertexLit"
}
did you solve this? if you did would you please tell me how ?
Answer by Jim_Zee_King · Dec 10, 2014 at 06:49 PM
Hi, thanks, you helped me find the solution :). I don't know anything about shader too! Add this to the DX11 shader :
Blend SrcAlpha OneMinusSrcAlpha
Tags {"Queue"="AlphaTest" }
in
Shader "DX11/GreenScreenShader" {
SubShader {
did this work for you i tried it but it doesnt work, would you please put the whole shader here??