我已经编写了一个HLSL代码,用于在背景图像上使用Kinect的深度视图来提高玩家的透明度。这是我的HLSL代码:
sampler stream : register(s0);
sampler back : register(s1);
sampler middle : register(s2);
sampler character : register(s3);
float4 DepthToRGB(float2 texCoord : TEXCOORD0) : COLOR0
{
float4 color = tex2D(stream, texCoord);
float4 backColor = tex2D(back, texCoord);
float4 middleColor = tex2D(middle, texCoord);
float4 characterColor = tex2D(character, texCoord);
// convert from [0,1] to [0,15]
color *= 15.01f;
// unpack the individual components into a single int
int4 iColor = (int4)color;
int depthV = iColor.w * 4096 + iColor.x * 256 + iColor.y * 16 + iColor.z;
// player mask is in the lower 8 bits
int playerId = depthV % 8;
if(playerId == 0)
{
//Not yet recognized
return backColor * middleColor;
}
else
{
return characterColor;
}
}
technique KinectDepth
{
pass KinectDepth
{
PixelShader = compile ps_2_0 DepthToRGB();
}
}
这是我在C#中的Draw方法:
public void Draw(GameTime gameTime, SpriteBatch SharedSpriteBatch)
{
// If we don't have a depth target, exit
if (this.depthTexture == null) return;
if (this.needToRedrawBackBuffer)
{
// Set the backbuffer and clear
this.GraphicsDevice.SetRenderTarget(this.backBuffer);
this.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 1.0f, 0);
this.depthTexture.SetData<short>(this.depthData);
GraphicsDevice.Textures[0] = this.depthTexture;
GraphicsDevice.Textures[1] = back;
GraphicsDevice.Textures[2] = middle;
GraphicsDevice.Textures[3] = character;
// Draw the depth image
SharedSpriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, this.kinectDepthVisualizer);
SharedSpriteBatch.Draw(this.depthTexture, Vector2.Zero, Color.White);
SharedSpriteBatch.End();
// Reset the render target and prepare to draw scaled image
this.GraphicsDevice.SetRenderTarget(null);
// No need to re-render the back buffer until we get new data
this.needToRedrawBackBuffer = false;
}
// Draw scaled image
SharedSpriteBatch.Begin();
SharedSpriteBatch.Draw(
this.backBuffer,
new Rectangle((int)Position.X, (int)Position.Y, (int)Size.X, (int)Size.Y),
null,
Color.White);
SharedSpriteBatch.End();
}
背面和角色是我的前景和背景图像。 middle用于在前景和背景图像之间放置对象。 当我在物体上移动身体时,物体就会出现。
我想问一下,当物体出现时,我怎样才能在2秒后发出一个事件?
由于