- Home /
Find closest point in triangle to point?
Is there an easy way to find the closest point in triangle to triangle?
Step 1: Get the triangle index and add a plane to the three vertices.
Step 2: Use Vector3.ProjectOnPlane to put a point on the plane.
Step 3: Use cross products with the three vertices and the point.
Step 4: If the point is on a side inside the triangle then bool is true.
This seems to work, but I don't know how to get true if the point is in one triangle or false if none.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PointOnPlaneInTriangle : MonoBehaviour
 {
     public bool PointIn;
 
     private Vector3 Point;
 
     private Vector3 CamPos;
 
     public List<Plane> Planes = new List<Plane>();
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         CamPos = Camera.main.transform.position;
 
         Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
 
         var triangles = mesh.triangles;
         var vertices = mesh.vertices;
 
         for (int i = 0; i < triangles.Length; i += 3)
         {
             Vector3 p1 = vertices[triangles[i + 0]];
             Vector3 p2 = vertices[triangles[i + 1]];
             Vector3 p3 = vertices[triangles[i + 2]];
 
             Vector3 tp1 = transform.TransformPoint(p1);
             Vector3 tp2 = transform.TransformPoint(p2);
             Vector3 tp3 = transform.TransformPoint(p3);
 
             Planes.Add(new Plane(tp1, tp2, tp3));
 
             for (int e = 0; e < Planes.Count; ++e)
             {
                 Point = Vector3.ProjectOnPlane(CamPos, Planes[e].normal);
             }
 
             Vector3 Side1P1 = Vector3.Cross(tp1 - tp2, Point - tp1).normalized;
             Vector3 Side1P2 = Vector3.Cross(tp1 - tp2, tp3 - tp1).normalized;
 
             Vector3 Side2P1 = Vector3.Cross(tp2 - tp3, Point - tp2).normalized;
             Vector3 Side2P2 = Vector3.Cross(tp2 - tp3, tp1 - tp2).normalized;
 
             Vector3 Side3P1 = Vector3.Cross(tp3 - tp1, Point - tp3).normalized;
             Vector3 Side3P2 = Vector3.Cross(tp3 - tp1, tp2 - tp3).normalized;
 
             if (Vector3.Dot(Side1P1, Side1P2) >= 0 && Vector3.Dot(Side2P1, Side2P2) >= 0 && Vector3.Dot(Side3P1, Side3P2) >= 0)
             {
                 PointIn = true;
             }
             else
             {
                 PointIn = false;
             }
 
             Debug.Log(PointIn);
         }
     }
 }
 
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Tharzzat · Apr 30 at 03:57 PM
Found this.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PointOnPlaneInTriangle : MonoBehaviour
 {
     public bool PointIn;
 
     private Vector3 Point;
 
     private Vector3 CamPos;
 
     public List<Plane> Planes = new List<Plane>();
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         CamPos = Camera.main.transform.position;
 
         Mesh mesh = GetComponent<MeshFilter>().sharedMesh;
 
         int[] triangles = mesh.triangles;
         Vector3[] vertices = mesh.vertices;
 
         for (int i = 0; i < triangles.Length; i += 3)
         {
             Vector3 p1 = vertices[triangles[i + 0]];
             Vector3 p2 = vertices[triangles[i + 1]];
             Vector3 p3 = vertices[triangles[i + 2]];
 
             Vector3 tp1 = transform.TransformPoint(p1);
             Vector3 tp2 = transform.TransformPoint(p2);
             Vector3 tp3 = transform.TransformPoint(p3);
 
             Planes.Add(new Plane(tp1, tp2, tp3));
 
             for (int e = 0; e < Planes.Count; ++e)
             {
                 Point = Vector3.ProjectOnPlane(CamPos, Planes[e].normal);
             }
 
             //Point in triangle test: blackpawn.com/texts/pointinpoly
 
             // Compute vectors
             Vector3 v0 = tp3 - tp1;
             Vector3 v1 = tp2 - tp1;
             Vector3 v2 = Point - tp1;
 
             // Compute dot products
             float dot00 = Vector3.Dot(v0, v0);
             float dot01 = Vector3.Dot(v0, v1);
             float dot02 = Vector3.Dot(v0, v2);
             float dot11 = Vector3.Dot(v1, v1);
             float dot12 = Vector3.Dot(v1, v2);
 
             // Compute barycentric coordinates
             float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
             float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
             float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
         
             // Check if point is in triangle
             if (u >= 0 && v >= 0 && u + v < 1)
             {
                 PointIn = true;
             }
             else
             {
                 PointIn = false;
             }
 
             Debug.Log(PointIn);
         }
     }
 }
 
Your answer
 
 
             Follow this Question
Related Questions
Plane: vertices and triangles 2 Answers
Unity subdividing icosohedron script problem 1 Answer
Sketchup models import with 0 verts/tris? 0 Answers
Best way to optimize mesh updating through code? 1 Answer
Connecting flatshaded vertices 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                