• Create Account

Vector3.Unproject return NAN [SharpDX]

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

16 replies to this topic

#1BlackJoker  Members

1201
Like
0Likes
Like

Posted 24 August 2014 - 09:59 AM

Hi, I am trying to setup picking in my project, but there is strange issue, which not let me do this. Vecto3.Unproject returns NAN always.

I found out that its because of Matrix.Inverse inside that method and as a result inversed matrix have 0 in all fields.

All transmitted parameters are correct.

So, I wonder isis that correct that from WVP matrix during inversion I always receive Zero matrix?

I leave here some code to be sure I transfer parameters correct:

ZNear = 0.01;

ZFar = 10000000000000;

mouse coords and ViewPort also correct;

But both vectors receive NAN as a result value;

private void Pick(int mouseX, int mouseY, ref Matrix wvpMatrix, BoundingBox boundingBox)
{

Vector3 mouseNearVector = new Vector3(mouseX, mouseY, basicCamera.ZNear);
Matrix m = Matrix.Identity;
m = Matrix.Invert(wvpMatrix);
Vector3 pointNear = Vector3.Unproject(mouseNearVector, 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, basicCamera.ZNear, basicCamera.ZFar,
wvpMatrix);

Vector3 mouseFarVector = new Vector3(mouseX, mouseY, basicCamera.ZFar);
Vector3 pointFar = Vector3.Unproject(mouseFarVector, 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, basicCamera.ZNear, basicCamera.ZFar,
wvpMatrix);

Ray ray = new Ray(pointNear, Vector3.Normalize(pointFar - pointNear));
float distance = 0;
bool intersect = boundingBox.Intersects(ref ray, out distance);
D2DText = "Intersect: " + intersect.ToString() + " distance = "+distance;
intersect = false;
}


Does Vector3.Unproject() method working correct or I am missing something?

Also it works really strange - sometime it returns true if no model under mouse and in the same time returns NAN for distance or return true and real distance value when no model under cursor is present.

It very strange that sometime picking is working at least somehow and the other time its not working at all.

Edited by BlackJoker, 24 August 2014 - 10:18 AM.

#2MJP  Moderators

18188
Like
2Likes
Like

Posted 24 August 2014 - 02:21 PM

Your far clip plane is way too big. It should probably be no bigger than 1000 or so.

#3TheChubu  Members

8945
Like
1Likes
Like

Posted 24 August 2014 - 03:45 PM

And your zNear is too small. Try 0.5 or 1.0 if you can.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

#4BlackJoker  Members

1201
Like
0Likes
Like

Posted 24 August 2014 - 10:04 PM

No effect - both points are still NAN with 1 and 1000. But with 0 and 100 far vector is calculated, but near vector still NAN with any value I give it.

#5 slalrbalr   Banned

2006
Like
0Likes
Like

Posted 25 August 2014 - 12:25 AM

So, I wonder isis that correct that from WVP matrix during inversion I always receive Zero matrix?

Not all matrices are invertible. The determinant of the matrix should be non-zero for it to be invertible. Try using Matrix.Inverse on the identity matrix - the result should be Identity. If that works, maybe your input matrix is the problem.

Also, your ZFar value is way too large. D3D (and maybe SharpDX as well?) converts 64-bot doubles to 32-bit floats internally, and the maximum integer that can be represented accurately as a 32-bit float, without precision loss is 16777216. Once you go above this value, weird things start to happen because of the precision loss.

Edited by tonemgub, 25 August 2014 - 12:39 AM.

#6BlackJoker  Members

1201
Like
0Likes
Like

Posted 26 August 2014 - 01:10 AM

Not all matrices are invertible. The determinant of the matrix should be non-zero for it to be invertible. Try using Matrix.Inverse on the identity matrix - the result should be Identity. If that works, maybe your input matrix is the problem.

Also, your ZFar value is way too large. D3D (and maybe SharpDX as well?) converts 64-bot doubles to 32-bit floats internally, and the maximum integer that can be represented accurately as a 32-bit float, without precision loss is 16777216. Once you go above this value, weird things start to happen because of the precision loss.

Ok, I tested identity matrix and it works correct, so the problem could be in my projection matrix where I set znear and zfar values.

And I have one question regarding all this:

does mouseNearVector and mouseFarVector needs to contain ZNear and ZFar values corresponding? and Unproject method also needs to keep the same ZNear and ZFar? Or values can differ?

And why I cant use for ex 100 000 as zFar or 0.01 as ZNear? In that case how I will create ray which could reach the far plane of camera view if this is possible by backbuffer and projection matrix?

#7 slalrbalr   Banned

2006
Like
1Likes
Like

Posted 26 August 2014 - 01:48 AM

does mouseNearVector and mouseFarVector needs to contain ZNear and ZFar values corresponding? and Unproject method also needs to keep the same ZNear and ZFar? Or values can differ?

Sorry, I can't tell you if your existing values are ok. You'll have to test that yourself.

IMHO yes, you should try to keep all your values between -16777216 and 16777216. If possible, you should use values even closer to 0 then that, so that any operations done by D3D on them don't go under/over these values either. There is a good explanation of this here: http://stackoverflow.com/questions/12596695/why-does-a-float-variable-stop-incrementing-at-16777216-in-c

Also, I think that your initial ZFar value is already NaN by the time you pass it to Unproject. There's no way such a large number can fit in a float. Maybe you also get a compiler warning if it's a float and the initial value is too large so it sets it to NaN? Or if it's a double, you'll get a warning about it being converted to float when you pass it to Unproject...

About ZNear: if you set it too small, you will get terrible aliasing. I don't remember why this is, but the recommentadion is that you set it to at least 0.5.

In that case how I will create ray which could reach the far plane of camera view if this is possible by backbuffer and projection matrix?

You don't need to create a vector (ray) that reaches that far by Unproject. Just create a normalized one, and then you can use scalar multiplication to make it whatever length you want. You can also convert your normalized vector to use doubles instead of floats for this if you need it to be longer than 16777216 (does SharpDX have a Vector class which uses doubles?). Just keep in mind that when you pass those double values back to D3D, they will be converted back to float...

Edited by tonemgub, 26 August 2014 - 01:59 AM.

#8BlackJoker  Members

1201
Like
0Likes
Like

Posted 26 August 2014 - 02:50 AM

Also, I think that your initial ZFar value is already NaN by the time you pass it to Unproject. There's no way such a large number can fit in a float. Maybe you also get a compiler warning if it's a float and the initial value is too large so it sets it to NaN?

No, ZFar is valid when I pass it to unproject and also my projection matrix works quite well without any problems, but i will try to descrease it to the valid float value.

#9BlackJoker  Members

1201
Like
0Likes
Like

Posted 26 August 2014 - 05:54 AM

OK, I tried and it works, but why noone points me that 6th and 7th parameters in Unproject() is not ZNear and ZFar, but Viewport.MinDepth and ViewPort.MaxDepth which is 0 and 1.0f correspondingly?

#10BlackJoker  Members

1201
Like
0Likes
Like

Posted 26 August 2014 - 10:44 AM

So, at last I coul unproject it and now it is not NAN, but I have the second question:

How to increase its length?

It return false when I far from the model and when I near - it always treu even if I not looking at the model. I suspect that I am inside bounding box and thats why it returns true.

SO, could you please tell how correctly make ray a maximum length possible?

#11 slalrbalr   Banned

2006
Like
1Likes
Like

Posted 26 August 2014 - 11:35 AM

why noone points me that 6th and 7th parameters in Unproject() is not ZNear and ZFar, but Viewport.MinDepth and ViewPort.MaxDepth

Sorry, I'm not very good with SharpDX...

SO, could you please tell how correctly make ray a maximum length possible?

Why don't you just use Ray.GetPickRay: http://sharpdx.org/documentation/api/m-sharpdx-ray-getpickray?

Note also that from the description of Ray.Direction (http://sharpdx.org/documentation/api/f-sharpdx-ray-direction), it seems that Ray actually always holds a normalized (length 1) vector for the direction of the ray, so there's no such thing as a "maximum length" ray - SharpDX Rays always have length 1, and they are probably always treated as "infinite length" in calculations.

Edited by tonemgub, 26 August 2014 - 11:42 AM.

#12BlackJoker  Members

1201
Like
0Likes
Like

Posted 26 August 2014 - 12:00 PM

Why don't you just use Ray.GetPickRay: http://sharpdx.org/documentation/api/m-sharpdx-ray-getpickray?

I dont know why, but this method almost always return direction as NAN and correct position. Its strange, but it is.

Unproject() work correct, but I cant peek object because intersection gives me false when I far from object and true when I am inside bounds.

It returns true ONLY when camera is inside the boundbox.

This is how I check intersection.

Vector3 point;
bool intersect = ray.Intersects(ref box, out point);


The same result for all other intersections. So, I dont know how to check intersection with the ray while camera is not inside the boundingbox...

#13 slalrbalr   Banned

2006
Like
1Likes
Like

Posted 26 August 2014 - 12:57 PM

As far as I can tell so far, you are testing if a view-space Ray intersects with a world-space BoundingBox. To get a world-space ray, you have to multiply the Ray with the inverse of the view matrix.

But GetPickRay should also work IMHO. If I were you, I'd try figuring out why it returns NaN, by changing the input values,rather than re-implementing it's functionality from scratch. You had the same NaN problem with your own implementation, remember? And now we also know that it's pointless to give a huge value for ZFar, since in the end, Ray.Direction is always length 1 -- ZFar doesn't affect it in any way, except when you use a value that is too big, which will cause Ray.Direction to be filled wtih NaNs. Maybe if you reduce your ZFar value to something more acceptable, GetPickRay will work properly... First, give up the assumption that you have to use huge numbers for things to work properly (or that the computer can even interpret those huge numbers), then check, ouble-check,and triple-check all your variables in debug, to make sure they really are what you want them to be.

Edited by tonemgub, 26 August 2014 - 12:57 PM.

#14BlackJoker  Members

1201
Like
0Likes
Like

Posted 26 August 2014 - 02:17 PM

OK, i found out the use case. Seems it works now. I have to increase ZNear to 10 to keep ZFar in my Projection Matrix with 16 000 000. In that case it works. Seems that than bigger ZFar, that zNear must also become bigger...

In near vector and far vector instead of ZNear, ZFar muste be 0 and 1 correspondingly. SO, both methods now working the same and at last picking is working correctly, but strange thing happens:

If i decrease ZNear in projection matrix <10 for ex, 8, ray becomes unstable and periodically looses boundingbox althought vectors are still NOT NAN. Some sort of magic happening here...

Edited by BlackJoker, 26 August 2014 - 02:39 PM.

#15 slalrbalr   Banned

2006
Like
0Likes
Like

Posted 27 August 2014 - 12:50 AM

I don't understand why after everything I've explained, you still try to use large values for ZFar. Fine, then... just use ZFar=1000, because I'm telling you to .

Some sort of magic happening here...

There's nothing magic about it. It's something like this: SharpDX takes your huge 16000000 ZFar value and multiplies it by 2 (best case), or even by itself (worst case)... The result of this multiplication will be a number that is way above 16777216, and like I've been trying to tell you, that's where the crazy happens. You have to use values that account for whatever SharpDX or D3D might do with them. 1000 is pretty reasonable. Or do you have any reason that I don't know of to stick with 16000000?

Edited by tonemgub, 27 August 2014 - 12:59 AM.

#16BlackJoker  Members

1201
Like
0Likes
Like

Posted 27 August 2014 - 01:54 AM

Or do you have any reason that I don't know of to stick with 16000000?

Yes. For now I use this huge numbers in my projection matrix to create projection for space simulator. If I use 1000, I wiil just don`t see nothing. I need at least 1 000 000 and more. IDK, maybe in future numbers will be changed, but for now I cannot do this.

But with 16 000 000 as zfar and 10 as znear works just fine.

BTW, ZFfar doesnt present in projection matrix in clear view. There is no huge numbers in result matrix, except position maybe. So, The only problem here is ration between ZFar and ZNear.

Edited by BlackJoker, 27 August 2014 - 02:27 AM.

#17 slalrbalr   Banned

2006
Like
1Likes
Like

Posted 27 August 2014 - 02:49 AM

There is no huge numbers in result matrix, except position maybe.

There don't have to be... For bad things to happen, it's sufficient that an intermediary calculation produces a large value (that maybe then gets divided, subtracted or square-rooted back to a small value).

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.