Yes, that’s what I do, sorry I was not more clear. For a DX9 example, here’s what I do to get a greyscale image from a depth image with specifiable white and black limits, very similar I think to what you are talking about. For DX11 I actually use compute shaders for everything, but a similar idea.
DepthMin and Depthmax are integer millimeters, and you can see in the pixel shader where I multiply the red value by 65535 to get back to millimeters before doing the comparisons (and note the sampler states set to POINT):
//@author: mediadog
//@help: Convert Primesense depth map to inverted grayscale
//@tags:
//@credits:
// --------------------------------------------------------------------------------------------------
// PARAMETERS:
// --------------------------------------------------------------------------------------------------
//transforms
float4x4 tW: WORLD; //the models world matrix
float4x4 tV: VIEW; //view matrix as set via Renderer (EX9)
float4x4 tP: PROJECTION;
float4x4 tWVP: WORLDVIEWPROJECTION;
//texture
texture Tex <string uiname="Texture";>;
sampler Samp = sampler_state //sampler for doing the texture-lookup
{
Texture = (Tex); //apply a texture to the sampler
MipFilter = POINT; //sampler states
MinFilter = POINT;
MagFilter = POINT;
};
//texture transformation marked with semantic TEXTUREMATRIX to achieve symmetric transformations
float4x4 tTex: TEXTUREMATRIX <string uiname="Texture Transform";>;
int DepthMin <string uiname="DepthMin";>;
int DepthMax <string uiname="DepthMax";>;
float Pedestal <string uiname="Pedestal";>;
//the data structure: "vertexshader to pixelshader"
//used as output data with the VS function
//and as input data with the PS function
struct vs2ps
{
float4 Pos : POSITION;
float2 TexCd : TEXCOORD0;
};
// --------------------------------------------------------------------------------------------------
// VERTEXSHADERS
// --------------------------------------------------------------------------------------------------
vs2ps VS(
float4 PosO : POSITION,
float4 TexCd : TEXCOORD0)
{
//declare output struct
vs2ps Out;
//transform position
Out.Pos = mul(PosO, tWVP);
//transform texturecoordinates
Out.TexCd = mul(TexCd, tTex);
return Out;
}
// --------------------------------------------------------------------------------------------------
// PIXELSHADERS:
// --------------------------------------------------------------------------------------------------
float4 PS(vs2ps In): COLOR
{
unsigned int Depth;
float4 col = tex2D(Samp, In.TexCd);
// Get depth data from R
Depth = col.r * 65535;
bool good = [Depth >= DepthMin) && (Depth <= DepthMax](https://vvvv.org/documentation/Depth->=-DepthMin)-&&-(Depth-<=-DepthMax);
col.rgb = good * (Pedestal + [DepthMax - Depth) / (float)(DepthMax - DepthMin)](https://vvvv.org/documentation/DepthMax---Depth)-/-(float)(DepthMax---DepthMin));
col.a *= good;
return col;
}
// --------------------------------------------------------------------------------------------------
// TECHNIQUES:
// --------------------------------------------------------------------------------------------------
technique TSimpleShader
{
pass P0
{
//Wrap0 = U; // useful when mesh is round like a sphere
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_2_0 PS();
}
}
technique TFixedFunction
{
pass P0
{
//transforms
WorldTransform[0](0) = (tW);
ViewTransform = (tV);
ProjectionTransform = (tP);
//texturing
Sampler[0](0) = (Samp);
TextureTransform[0](0) = (tTex);
TexCoordIndex[0](0) = 0;
TextureTransformFlags[0](0) = COUNT2;
//Wrap0 = U; // useful when mesh is round like a sphere
Lighting = FALSE;
//shaders
VertexShader = NULL;
PixelShader = NULL;
}
}