-
How do I get the polygon(face) of the contact point (float3 by casting)?
-
And how can I get the material information of polygon?
-
Unlike… “UnityEngine.Physics.RaycastHit”, I found no triangle index for “Unity.Physics.ColliderCastHit”. 
-
So… I want to find out polygon, material, color in every way. 
if (Physics.Raycast(ray, out RaycastHit hit))
{
int[] triangles = mesh.triangles;
var vertIndex1 = triangles[hit.triangleIndex * 3 + 0];
var vertIndex2 = triangles[hit.triangleIndex * 3 + 1];
var vertIndex3 = triangles[hit.triangleIndex * 3 + 2];
colorArray[vertIndex1] = Color.red;
colorArray[vertIndex2] = Color.red;
colorArray[vertIndex3] = Color.red;
mesh.colors = colorArray;
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.
1 Like
@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!