Hi y’all!
I experimented with instantiating behind a detected category e.g. shoot a raycast from the rectangle position that detected a chair to spawn a 3D object at the exact chair position. However I found a weird issue that I don’t quite understand.
The code below draws a rect using the lightship provided methods, as shown in the how tos and passes the position of the rect on with an event.
if (listOfCategories.Contains(categoryToDisplay.CategoryName))
{
_confidence = categoryToDisplay.Confidence;
_name = categoryToDisplay.CategoryName;
int h = Mathf.FloorToInt(_canvas.GetComponent<RectTransform>().rect.height);
int w = Mathf.FloorToInt(_canvas.GetComponent<RectTransform>().rect.width);
var rect = result[j].CalculateRect(w,h, Screen.orientation);
_drawRect.CreateRect(rect, _colors[j % _colors.Length], _name);
OnFoundItemAtPosition?.Invoke((categoryToDisplay.CategoryName, rect.position, rect));
}
This other method will receive the rect position (“rectUpperRightPosition”) which usually should be the upper right or left corner of the rect. However, only with some testing I found out that the value (Vector 2) of the rect (width and height) does not match with the actual position on the screen, but rather is ~2x of the actual screen position of the object.
Only if you divide the position by “2”, it will shoot a ray from exactly the position of the rectangle forward. Can anyone tell me if this if I’m just stupid to wonder about this or is this an issue with Lightship or the rects?
(Vector3 point, Vector3 normal) ShootRaycastFromDetectedPosition(Vector2 rectUpperRightPosition, Rect rect)
{
Ray ray;
Vector2 convertedScreenToRay = new Vector2(rectUpperRightPosition.x /2 , rectUpperRightPosition.y/2 );
ray = mainCam.ScreenPointToRay(convertedScreenToRay);
debugRay = ray;
RaycastHit[] raycastHits = Physics.RaycastAll(ray, 100f);
if (raycastHits.Length > 0)
{
foreach (var raycastHit in raycastHits)
{
if (raycastHit.collider.gameObject.layer == meshLayer)
{
return (raycastHit.point, raycastHit.normal);
}
}
}
return (Vector3.zero, Vector3.zero);
}
Code used to draw the rects: How to Enable Object Detection | Niantic Lightship