Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Tharzzat · Apr 29 at 11:50 AM · script.vector3planeverticestriangles

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
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);
         }
     }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

171 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Plane: vertices and triangles 2 Answers

Unity subdividing icosohedron script problem 1 Answer

Change a plane's vertices positions in runtime using a 2D array 0 Answers

How to calculate normal direction for shared vertex position 1 Answer

Problem drawing a mesh with Graphics.DrawMeshNow 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges