Skip to content

Commit 7799f8f

Browse files
authored
Merge pull request #10370 from keveleigh/improve-raycast-hit
[2.7.3] Improve raycast hit to prevent log spam on Unity 2021 and prevent a crash when using assetbundles
2 parents ec3b0ad + f70b0e7 commit 7799f8f

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

Assets/MRTK/Core/Definitions/InputSystem/MixedRealityRaycastHit.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,56 @@ public MixedRealityRaycastHit(bool raycastValid, RaycastHit hitInfo)
3232
barycentricCoordinate = hitInfo.barycentricCoordinate;
3333
distance = hitInfo.distance;
3434
triangleIndex = hitInfo.triangleIndex;
35-
textureCoord = hitInfo.textureCoord;
35+
3636
MeshCollider meshCollider = hitInfo.collider as MeshCollider;
37-
if (meshCollider == null || meshCollider.sharedMesh.isReadable)
37+
if (meshCollider == null)
3838
{
39+
textureCoord = hitInfo.textureCoord;
3940
textureCoord2 = hitInfo.textureCoord2;
41+
lightmapCoord = hitInfo.lightmapCoord;
4042
}
4143
else
4244
{
43-
textureCoord2 = Vector2.zero;
45+
Mesh sharedMesh = meshCollider.sharedMesh;
46+
if (sharedMesh != null && sharedMesh.isReadable)
47+
{
48+
#if UNITY_2019_4_OR_NEWER
49+
if (sharedMesh.HasVertexAttribute(UnityEngine.Rendering.VertexAttribute.TexCoord0))
50+
{
51+
textureCoord = hitInfo.textureCoord;
52+
}
53+
else
54+
{
55+
textureCoord = Vector2.zero;
56+
}
57+
58+
// This checks for TexCoord1, since textureCoord2 and lightmapCoord both query that index
59+
// via CalculateRaycastTexCoord(collider, m_UV, m_Point, m_FaceID, 1); (the last parameter is the index)
60+
if (sharedMesh.HasVertexAttribute(UnityEngine.Rendering.VertexAttribute.TexCoord1))
61+
{
62+
textureCoord2 = hitInfo.textureCoord2;
63+
lightmapCoord = hitInfo.lightmapCoord;
64+
}
65+
else
66+
{
67+
textureCoord2 = Vector2.zero;
68+
lightmapCoord = Vector2.zero;
69+
}
70+
#else
71+
textureCoord = hitInfo.textureCoord;
72+
textureCoord2 = hitInfo.textureCoord2;
73+
lightmapCoord = hitInfo.lightmapCoord;
74+
#endif
75+
}
76+
else
77+
{
78+
textureCoord = Vector2.zero;
79+
textureCoord2 = Vector2.zero;
80+
lightmapCoord = Vector2.zero;
81+
}
4482
}
83+
4584
transform = hitInfo.transform;
46-
lightmapCoord = hitInfo.lightmapCoord;
4785
collider = hitInfo.collider;
4886
}
4987
else

0 commit comments

Comments
 (0)