Unity-UI-Extensions/com.unity.uiextensions

Support for selection on UILineRenderer

QuantumCalzone opened this issue · 1 comments

My guess is we would need to override the IsRaycastLocationValid method and check if the screenPoint parameter is within any entry of Segments * lineThickness?

I ended up getting it to work by iterating through the triangles of the mesh.

		public override bool Raycast(Vector2 screenPoint, Camera eventCamera)
		{
			Mesh mesh = workerMesh;
			int triangleCount = mesh.triangles.Length;
			for (int i = 0; i < triangleCount; i += 3)
			{
				Vector3 pointA = mesh.vertices[mesh.triangles[i]];
				Vector3 pointB = mesh.vertices[mesh.triangles[i + 1]];
				Vector3 pointC = mesh.vertices[mesh.triangles[i + 2]];

				pointA = transform.TransformPoint(pointA);
				pointB = transform.TransformPoint(pointB);
				pointC = transform.TransformPoint(pointC);

				if (PointIsInTriangle(screenPoint, pointA, pointB, pointC))
                {
					return true;
                }
			}

			return false;
        }


		private bool PointIsInTriangle(Vector3 point, Vector3 trianglePointA, Vector3 trianglePointB, Vector3 trianglePointC)
		{
			double s1 = trianglePointC.y - trianglePointA.y;
			double s2 = trianglePointC.x - trianglePointA.x;
			double s3 = trianglePointB.y - trianglePointA.y;
			double s4 = point.y - trianglePointA.y;

			double w1 = (trianglePointA.x * s1 + s4 * s2 - point.x * s1) / (s3 * s2 - (trianglePointB.x - trianglePointA.x) * s1);
			double w2 = (s4 - w1 * s3) / s1;

			return w1 >= 0 && w2 >= 0 && (w1 + w2) <= 1;
		}