private void Update() { if (points.Count > 0) { polygonData polygon = new polygonData(); for (int i = 0; i < points.Count; i++) { polygon.points.Add(points[i].transform.position); } polygon.SetMaxMinXY(); lineRenderer.positionCount = polygon.GetDrawList().Length; lineRenderer.SetPositions(polygon.GetDrawList()); Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo)) { Debug.Log(pnpoly(polygon.V3ToV2(hitInfo.point), polygon) + " " + hitInfo.point); } } if (Input.GetKeyDown(KeyCode.P)) { GameObject go = new GameObject(points.Count.ToString()); go.transform.SetParent(transform); go.transform.localPosition = Vector3.zero; points.Add(go); } }
private bool pnpoly(Vector2 point, polygonData polygon) { if (polygon.maxX < point.x || polygon.maxY < point.y || polygon.minX > point.x || polygon.minY > point.y) { Debug.Log(polygon.maxX + " " + polygon.maxY + " " + polygon.minX + " " + polygon.minY); return(false); } List <Vector2> points = polygon.GetPointsV2(); bool isIn = false; for (int i = 0, j = points.Count - 1; i < points.Count; j = i++) { if (((points[i].y > point.y) != (points[j].y > point.y)) && (point.x < (points[j].x - points[i].x) * (point.y - points[i].y) / (points[j].y - points[i].y) + points[i].x)) { isIn = !isIn; } } return(isIn); }