Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

How do i get the polygon(face) on the contact point by collider caster?

Discussion in 'Physics for ECS' started by amdroid135, Jun 21, 2021.

  1. amdroid135

    amdroid135

    Joined:
    Apr 20, 2021
    Posts:
    3
    1. How do I get the polygon(face) of the contact point (float3 by casting)?
    2. And how can I get the material information of polygon?
    • Unlike... "UnityEngine.Physics.RaycastHit", I found no triangle index for "Unity.Physics.ColliderCastHit". :confused:
    • So... I want to find out polygon, material, color in every way. o_O
    Code (CSharp):
    1.             if (Physics.Raycast(ray, out RaycastHit hit))
    2.             {
    3.                 int[] triangles = mesh.triangles;
    4.  
    5.                 var vertIndex1 = triangles[hit.triangleIndex * 3 + 0];
    6.                 var vertIndex2 = triangles[hit.triangleIndex * 3 + 1];
    7.                 var vertIndex3 = triangles[hit.triangleIndex * 3 + 2];
    8.  
    9.                 colorArray[vertIndex1] = Color.red;
    10.                 colorArray[vertIndex2] = Color.red;
    11.                 colorArray[vertIndex3] = Color.red;
    12.  
    13.                 mesh.colors = colorArray;
     
    Last edited: Jun 21, 2021
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    ColliderCastHit.ColliderKey gives you the information of the hit collider (provide it to Collider.GetLeaf). However, it won't give you a proper mapping to the original triangles, as physics mesh doesn't map 1-1 to graphics mesh (there are simplifications that happen so some triangles may be merged, others may be dismissed and so on). Material is actually global, per collider, there is currently no option to set a per triangle material.

    Sorry I couldn't be of more help. There were other topics in the forum that discussed this in more depth, you might want to take a closer look at them.
     
    amdroid135 likes this.
  3. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    @petarmHavok Collider.GetLeaf is internal (now?). What is the recommended way to do this currently?

    Edit: Found the answer. It is a bit cumbersome but essentially follow https://github.com/Unity-Technologies/EntityComponentSystemSamples/blob/f9043577ddc0d8667b6b4649a71454741075f4d1/UnityPhysicsSamples/Assets/Demos/3. Query/Scripts/QueryTester.cs

    The only thing I am unsure is, how to get the triangle index back, as this gives me the position in local or world space.

    I just want to highlight, there is a comment that the method should be made "internal". I am sure there are many use cases for triangle information and I hope you still keep some replacement!
     
    Last edited: Mar 1, 2022