Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Help with depth shader  (Read 1015 times)
serial
Customers
Community Member
*****
Posts: 380


« on: January 05, 2011, 08:56:52 PM »

Been trying to create a depth shader for a few weeks.  Just not getting it for some reason.  Followed several differant online examples.

I've been fiddling with HLSL code similar to this.  but everything is just completely white.  Makes me wonder if there is something I need to change in TV3D.

Any suggestions would be helpful. 

Thx. 

serial

Code:
float4x4 mvp: WORLDVIEWPROJ;


struct VS_OUTPUT
{
   float4 Pos: POSITION;
   float4 posInProjectedSpace: TEXCOORD0;
};
 
// vertex shader
VS_OUTPUT vs_main( float4 Pos: POSITION )
{
   VS_OUTPUT Out = (VS_OUTPUT) 0;
   Out.Pos = mul(Pos, mvp);
   Out.posInProjectedSpace = Out.Pos;
   return Out;
}
 
// pixel shader
float4 ps_main( VS_OUTPUT Input ) : COLOR
{
   float depth = Input.posInProjectedSpace.z / Input.posInProjectedSpace.w;
   return depth;
}

technique TSM1 {
    pass P0 {
        VertexShader = compile vs_2_0 vs_main();
        PixelShader  = compile ps_2_0 ps_main();
    }
}

 
Logged
AriusEso
Customers
Community Member
*****
Posts: 940

Esoteric


« Reply #1 on: January 05, 2011, 10:14:43 PM »

It will be white because your depth values are > 1. If you want visible scene depth for debugging purposes or something then maybe try something like this: http://developer.download.nvidia.com/shaderlibrary/packages/DepthColor.fx.zip
Logged

-...-

serial
Customers
Community Member
*****
Posts: 380


« Reply #2 on: January 05, 2011, 10:34:14 PM »

Thanks Arius,

I thought that dividing by Input.posInProjectedSpace.w was to supposed to normalize it. 
Does that mean it is normalizing it based on the distance from the near plane and far plane?

 
Logged
AriusEso
Customers
Community Member
*****
Posts: 940

Esoteric


« Reply #3 on: January 05, 2011, 10:49:59 PM »

I think you are storing linear depth, but I'm unsure. I don't do it in that way, I do it like this. Obviously ignore the RGB output, the A is linear depth and fFar is the far plane value.

Code:
float4x4 wvp  : WORLDVIEWPROJECTION;
float4x4 wv   : WORLDVIEW;
float4x4 w    : WORLD;

float fFar;

texture b: TEXTURE1;

sampler2D bs = sampler_state {
Texture   = (b);
MIPFILTER = LINEAR;
MINFILTER = LINEAR;
MAGFILTER = LINEAR;
};

struct a2v {
float4 p : POSITION0;
float3 t : TANGENT;
float3 b : BINORMAL;
float3 n : NORMAL;
float2 uv: TEXCOORD0;
};

struct v2f {
float4 p : POSITION0;
float2 uv: TEXCOORD0;
float3 t : TEXCOORD1;
float3 b : TEXCOORD2;
float3 n : TEXCOORD3;
float  z : TEXCOORD4;
};

v2f vp(in a2v _in) {
v2f _out;

_out.p    = mul(_in.p, wvp);
_out.uv   = _in.uv;
_out.t    = _in.t;
_out.b    = _in.b;
_out.n    = _in.n;
_out.z    = mul(_in.p, wv).z;

return _out;
}

float4 fp(in v2f _in): COLOR0 {
float3x3 tbn = float3x3(_in.t, _in.b, _in.n);
float3x3 ttw = mul(tbn, (float4x3)w);
float3   n   = 2 * tex2D(bs, _in.uv) - 1;

return float4(mul(n, ttw), _in.z / fFar);
}

technique render {
pass p0 {
VertexShader = compile vs_2_0 vp();
PixelShader  = compile ps_2_0 fp();
}
}
Logged

-...-

serial
Customers
Community Member
*****
Posts: 380


« Reply #4 on: January 06, 2011, 07:38:30 AM »

Thanks for the help.  That is the information I needed to understand the concept.  Getting the output I was expecting now :-D

For some reason I was expecting my output to look like the examples I was reading and it wasn't.  Now I understand why. 

Thx
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks