Public Group

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

## Recommended Posts

Here's the page;

http://www.riemers.n...ding_detail.php

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="green"]
[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?

EDIT:
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;

I changed it to * 90 Yes it's a lot, but it does the trick.
[color="green"]

##### 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.

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633654
• Total Posts
3013168
×