• 9
• 11
• 9
• 20
• 12

# C# SharpDX setting the depth for pixel shader

This topic is 402 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to give realistic depth for my rendered cube instead of one color depth. I have something like:

struct VS_IN
{
float4 pos : POSITION;
float3 Normal : NORMAL; // Normal - for lighting
float4 col : COLOR;
float2 TextureUV: TEXCOORD; // Texture UV coordinate
};

struct PS_IN
{
float4 pos : SV_POSITION;
float4 col : COLOR;
float2 TextureUV: TEXCOORD;
float3 WorldNormal : NORMAL;
float3 WorldPosition : WORLDPOS;
};

float4x4 worldViewProj;

PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN)0;

output.pos = mul(input.pos, worldViewProj);
output.col = input.col;
output.TextureUV = input.TextureUV;
return output;
}

float4 PS( PS_IN input ) : SV_Target
{
return input.col;
}

I want to obtain something like:

Edited by gomidas

##### Share on other sites

If I can remember, you can get the actual depth of Z in the screen with pos.z /= pos.w.

I haven't looked at my shadow code for a month so my memory maybe going.

This should give you a value in the range which is related to your viewport (usually 0 to 1).

There is a heavy bias in the way the Z depth is also done and as such the closer objects can be bunched up in values > 0.9f.

You can use your value between 0 and 1 to multiply out a colour, or your texture etc.  I assume you just want to shade your cube with depth.

##### Share on other sites

If I can remember, you can get the actual depth of Z in the screen with pos.z /= pos.w.

I haven't looked at my shadow code for a month so my memory maybe going.

This should give you a value in the range which is related to your viewport (usually 0 to 1).

There is a heavy bias in the way the Z depth is also done and as such the closer objects can be bunched up in values > 0.9f.

You can use your value between 0 and 1 to multiply out a colour, or your texture etc.  I assume you just want to shade your cube with depth.

Alright something like:

VS_OUTPUT vs_main( VS_INPUT input )
{
VS_OUTPUT output;
output.position = mul( input.position, matViewProjection );
output.depth = output.position.z / output.position.w;
return( output );
}

However, I could not get it work. Can you give any example suitable with my code at topic.

Last I get this: output.col = 1.0f-(input.pos.w*input.col)/ (input.pos.z*input.col); This seems working but when I rotate mesh I cannot see any change on that.

Edited by gomidas

##### Share on other sites
Will have a look later but just remember the z values are going to be very close to 1.0f.

To exacerbate these values i have in past done the following.

zvalue =zvalue - 0.9f;
zvalue *= 10.0f;

This will make 0.9f to 1.0f over 0 to 1.

See if that shows any difference

##### Share on other sites

Will have a look later but just remember the z values are going to be very close to 1.0f.

To exacerbate these values i have in past done the following.

zvalue =zvalue - 0.9f;
zvalue *= 10.0f;

This will make 0.9f to 1.0f over 0 to 1.

See if that shows any difference

Now I have this. I still concerned as I could not distinguish some faces that you see at right of the picture.