Jump to content
  • Advertisement
Sign in to follow this  

xna Adding Detail tut

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here's the page;


I tried posting there but no joy, so here i am.

Problem im having, is how on earth does the shader know how close the camera is, relative to the terrain?

[color="green"][color="#000000"]And how can i provide this info for the shader?

He says;

[color="#030752"]As you can see, for each pixel we’ll need the distance to our camera. So add this line to the struct MTVertexToPixel struct:

[color="green"] float Depth : TEXCOORD4;

[color="#030752"]This distance is nothing more than the z coordinate of the position in camera space. Remember, because this was the result of a 4x4 matrix multiplication, we first need to divide it by the w component before we can use it:

[color="green"] Output.Depth = Output.Position.z/Output.Position.w;
[color="#000000"]Ive tried the code, but it's entirely static, and does not respond to the position of the camera.

[color="#000000"]I imagine the pixel shader could test the depth of each pixel, but it's not working at present. Any ideas?

Im terribly sorry it's been a long night, and spent 4 hours troubleshooting a bug in tutorial code.

Let me explain what solved this for me.....
It's a question of scale.

It does work, but for me, i had to change the value;

[color="#030752"][color="red"][color="red"][color="#31659c"][color="red"][color="#31659c"]float2 nearTextureCoords = PSIn.TextureCoords*3;

Instead of * 3
I changed it to * 90 :) Yes it's a lot, but it does the trick.

Share this post

Link to post
Share on other sites
After you multiplied the position of the vertex with your WorldViewProjection-Matrix, the z-component equals the distance to the camera. So Output.Depth = Output.Position.z
would be the real distance, between the near and far clip plane. We need to get it to thr range 0.0-1.0, so you devide it by w (or alternatively the far plane value).

I don't know why your code is static, just some suggestions:

- You only asign the view-matrix once on camera creation

-You use the wrong position values (you need to use the WVP-transforned vertex position).

- You asign the Matrices incorrectly. World/View/Projection should be declared as float 4x3 in shader, while the combined WVP matrix needs to be a 4x4-Matrix.

Hope this helped.

Edit: Never mind, didn't read your edit fast enough. Glad to hear you got it working!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!