KeldorKatarn
Topic Author
Posts: 192
Joined: 30 May 2014, 10:26

Visual.PointFromScreen from actual screen

25 May 2021, 08:47

So a Visual has the method Visual.PointFromScreen. Now I'm assuming that the parameter for this in Noesis would be a point on whatever area the View is using. Meaning its own 'screen' not the actual physical one the game is rendering, considering Noesis could be on some texture somewhere in 3D space.

Is there actually a way to map a screen point on the current main camera to a point on the NoesisView area (might of course be something like NaN, NaN if the noesis view is not even on the screen, so there's no mapping. But is there such a funtion at all? Or would that have to be custom implemented?

I'm assuming if a NoesisView sits on the Camera, the Unity screen point coordinates would be identical to the Noesis internal screen coordinates for a point?

Tags:
 
User avatar
jsantos
Site Admin
Posts: 3117
Joined: 20 Jan 2012, 17:18
Contact:

Re: Visual.PointFromScreen from actual screen

25 May 2021, 12:29

NoesisView.cs is using UnityEngine.Camera.main.ScreenPointToRay for exactly that, mapping from screen to a view in a render texture. A MeshCollider is necessary for this. Probably we could expose this functionality as a public method...
 
KeldorKatarn
Topic Author
Posts: 192
Joined: 30 May 2014, 10:26

Re: Visual.PointFromScreen from actual screen

25 May 2021, 13:23

I was thinking about this regarding the Unit-Health bar. If such a public API existed, positioning the health bars correctly should be trivial.
I was imagining the Noesis UI being projected onto the inside of a helmet and the character taking the helmet off, and you can see that all the projections of the HUD are always correct, even while the "putting the helmet on" animation is still going on... just a thought :)
 
MickV8
Posts: 1
Joined: 22 May 2021, 05:58

Re: Visual.PointFromScreen from actual screen

27 May 2021, 05:15

So a Visual has the method Visual.PointFromScreen. Now I'm assuming that the parameter for this in Noesis would be a point on whatever area the View is using. Meaning its own 'screen' not the actual physical one the game is rendering, considering Noesis could be on some texture somewhere in 3D space.

Is there actually a way to map a screen point on the current main camera to a point on the NoesisView area (might of course be something like NaN, NaN if the noesis view is not even on the screen, so there's no mapping. But is there such a funtion at all? Or would that have to be custom implemented?

I'm assuming if a NoesisView sits on the Camera, the Unity screen point coordinates would be identical to the Noesis internal screen coordinates for a point?
Hi KeldorKatarn, I'm not sure if that's what you want, and by the way I'm playing around with Noesis in my own engine using OpenGL and C#, so let me know if this helps. THis is what I'm doing (likely there are other ways of doing it but this was quick and works really good):
 
//  here I get the 3D position of mouse pointer
Vector3 target = camera.GetWorldPositionFromScreenSpaceCoordinates(new Vector3(e.X, e.Y, 0));
// now get a vector from the camera in direction of the mouse pointer
Vector3 dir = (target - camera.Position).Normalized();
// A ray from camera to the direction 
Ray ray = new Ray(camera.Position, dir);
// Now I create a plane that faces the same direction as my UI object (in my case I've created a Neosis Grid object with a button inside)
Plane plane = new Plane(button1.Position, button1.Direction);
float? result = ray.Intersects(plane);
if (result.HasValue)
{ 
 	// If my ray intersects the plane then I check if I'm hitting the button, first I get the exact position that my ray hit the plane
	Vector3 pointInWorld = camera.Position + dir * result.Value;
	// Now I convert this position to a local position relative to the button
	Vector3 relativePoint = Vector3.TransformPosition(pointInWorld, button1.Transform.Inverted());
	// Get the 3D button dimensions and calculate normalized values for X and Y (meaning a value that goes from 0 to 1). Actually they can be negatives, which means the mouse click happened either more to the left or more to the top (or both) than the button position.
	// The x value goes from left to right and y value from top to botton
	Vector3 buttonDimensions = button1.GetDimensions();
	float x = (relativePoint.X + buttonDimensions.X * 0.5f) / buttonDimensions.X;
	float y = 1f - ((relativePoint.Y + buttonDimensions.Y * 0.5f) / buttonDimensions.Y);
	// Now I get the actual 2D position in the button and trigger the MouseMove method in Neosis view object. It might result in negative values, meaning the click position is outside the button bounds. 
	Vector2 v = new Vector2(button1.Texture.Width * x, button1.Texture.Height * y);
	button1.MouseMove((int)v.X, (int)v.Y);
}
Michael.
 
KeldorKatarn
Topic Author
Posts: 192
Joined: 30 May 2014, 10:26

Re: Visual.PointFromScreen from actual screen

27 May 2021, 11:53

Something like this should work yes, thanks :)

Who is online

Users browsing this forum: Google [Bot] and 3 guests