C# SharpDX setting the depth for pixel shader

Started by
4 comments, last by ErnieDingo 7 years, 1 month ago

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:

ind3q9.jpg

Advertisement

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.

Indie game developer - Game WIP

Strafe (Working Title) - Currently in need of another developer and modeler/graphic artist (professional & amateur's artists welcome)

Insane Software Facebook

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.

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

Indie game developer - Game WIP

Strafe (Working Title) - Currently in need of another developer and modeler/graphic artist (professional & amateur's artists welcome)

Insane Software Facebook

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

5arpkh.jpg

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

To distinguish the different faces. How about using the normal for a hack to see a different color on each face. The issue you will get for the cube on the right is that the edge is shared and so the depth.

Indie game developer - Game WIP

Strafe (Working Title) - Currently in need of another developer and modeler/graphic artist (professional & amateur's artists welcome)

Insane Software Facebook

This topic is closed to new replies.

Advertisement