How to computer the distance between the current pixel and the camera in the Pixel Shader (HLSL)?

Started by
9 comments, last by Key_C0de 1 year, 7 months ago

I have thought up of the following:

  1. pass the camera position to vertex shader
  2. distanceBetweenVertexAndCamera = length( cameraPosition - vertexPositionInObjectSpace );
  3. pass distanceBetweenVertexAndCamera to Pixel Shader

I'm slightly confused about this. Is distanceBetweenVertexAndCamera what I'm looking for? I believe there's something I'm missing..

Do you think my workflow would work?

None

Advertisement

Offhand, I'm not sure that working in object-space will be reliable--you might find that your distances incorporate things like scaling factors applied to the object. I'd suggest perhaps working in world-space, instead.

That said, I think that the basic idea is fine--I don't see anything missing. Distance is the length of the vector between two points, after all.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

I can't believe I wrote “object space”.

You're right. I think they should be in world space.

I guess view space isn't that reliable in this sense right?

Anyway, thank you for responding.

None

But my main concern is, will distanceBetweenVertexAndCamera be the distanceBetweenPixelAndCamera in the Pixel Shader?

None

Key_C0de said:
I guess view space isn't that reliable in this sense right?

That I don't know offhand, I'm afraid.

Key_C0de said:
But my main concern is, will distanceBetweenVertexAndCamera be the distanceBetweenPixelAndCamera in the Pixel Shader?

To the best of my knowledge, the name of the variable should be the same in both shaders.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Thaumaturge said:

To the best of my knowledge, the name of the variable should be the same in both shaders.

Yes, the name will be the same. Don't get caught up on that.

None

Key_C0de said:
Yes, the name will be the same. Don't get caught up on that.

Given that you gave them different names in the question, I had the impression that you were asking about the names.

Okay, so are you asking then whether their values will be the same? If so, then I think that the answer is: “More or less”. Specifically, I believe that you'll end up with pixel-values interpolated from the vertex-values, and which should therefore be related, but not an exact match.

You can likely get a more-accurate result (at the cost of calculation being a bit more expensive) by performing your calculation in the pixel-shader instead of the vertex-shader, I daresay.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Right, ignoring the interplation of the value (which will happen by passing the value from the VS → PS) then the values should be “related”. O.K. so far..

Let's consider the alternative you mentioned for a bit. If I perform the computation on the PS, then I will have to also carry along 2 more parameters to the PS:

  1. the World matrix
  2. as well as the Camera position.

What do you suggest?

None

Well, much depends on your purpose for this (including how often it'll be run), and what system resources you expect to have available to it.

In and of themselves, I would expect two extra inputs to be a fairly minor thing. However, if these are two more on top of a huge number already, and either your program, your target system, or your performance target leaves you heavily resource-constrained, then perhaps those two more are two too many.

But I do ask: how accurate does this have to be? Why not just implement it in the vertex shader, as you already have in mind, and have done with it?

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Thaumaturge said:

But I do ask: how accurate does this have to be? Why not just implement it in the vertex shader, as you already have in mind, and have done with it?

That's what I'll do.

Thanks for the support.

None

This topic is closed to new replies.

Advertisement