WorldToScreenPoint does not work as expected!
Hello,
I get world positions of gaze points that are tracked by Tobii Pro VR Eye Tracker. I assign these positions to a game object called sphere (blue circles on the screenshots). After that I get screen positions of these world points by WorldToScreenPoint method. I screenshot of current frame by ScreenCapture.CaptureScreenshot method. Finally, I want to mark these gaze points on my screenshot by using the x and y pixel positions that I got from WorldToScreenPoint method (white circles on the screenshots). However, as you can see in the screenshots attached, I am not able to mark blue spheres by white spheres. They don't overlap. They tend to overlap when blue sphere is near to the center. But as it goes to the up, down, right or left, white sphere does not slip as much as the blue sphere does. I tried it with the camera in both perpective and orthographic modes Screen width is 1512 and screen height is 1680 and they are same with the VR height and width. I have been in stuck at that point for days. I look forward you to help me. Related screenshots are attached.
Screenshots: https://imgur.com/SESpUnc https://imgur.com/nzSTzQ0 https://imgur.com/4D9meJm https://imgur.com/EGIKuTn https://imgur.com/Bynbrzs
Code part to convert world position to screen position:
_spherePos = _sphere.transform.position;
x3 = _spherePos.x;
y3 = _spherePos.y;
z3 = _spherePos.z;
Vector3 _gazePos = cam.WorldToScreenPoint(_spherePos, Camera.MonoOrStereoscopicEye.Right);
x2 = _gazePos.x;
y2 = _gazePos.y;
z2 = _gazePos.z;
Code part to take screenshot and mark white points:
Texture2D texture = new Texture2D(1512, 1680);
texture = ScreenCapture.CaptureScreenshotAsTexture(ScreenCapture.StereoScreenCaptureMode.RightEye);
Circle(texture, (int)Mathf.Ceil(x2), (int)Mathf.Ceil(y2), 15, Color.white);
_bytes = texture.EncodeToJPG();
System.IO.File.WriteAllBytes("screenshots\\" + cc.ToString() + "asd" + x1.ToString() + "_" + y1.ToString() + "_" + z1.ToString() + "_" + x3.ToString() + "_" + y3.ToString() + "_" + z3.ToString() + "_.jpg", _bytes);
Circle method to draw the white circle:
void Circle(Texture2D tex, int cx, int cy, int r, Color col)
{
int x, y, px, nx, py, ny, d;
for (x = 0; x <= r; x++)
{
d = (int)Mathf.Ceil(Mathf.Sqrt(r * r - x * x));
for (y = 0; y <= d; y++)
{
px = cx + x;
nx = cx - x;
py = cy + y;
ny = cy - y;
tex.SetPixel(px, py, col);
tex.SetPixel(nx, py, col);
tex.SetPixel(px, ny, col);
tex.SetPixel(nx, ny, col);
}
}
}
Thanks.
alt text
Your answer
Follow this Question
Related Questions
How to convert the world co-ordinate to the co-ordinates that I can use on screen? 2 Answers
What is the matrix equivalent to Camera.WorldToScreenPoint? 1 Answer
c# RTS style snapping of game objects 0 Answers
Take XYZ of GameObject and convert that to canvas co-ordinates? 0 Answers
draw canvas/2D space Box Colliders on top of 3D objects that are located in world space? 0 Answers