Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.02k stars 1.12k forks source link

[Bug] ARRaycastManager.RaycastFallback() produces wrong results if more than one m_Raycasters hit something #963

Closed KyryloKuzyk closed 2 years ago

KyryloKuzyk commented 2 years ago

Describe the bug ARRaycastManager.RaycastFallback() produces wrong results if more than one m_Raycasters hit something (lines producing the bug). Please read the comments below:

int dstIndex = 0;
foreach (var hitArray in s_NativeRaycastHits)
{
    NativeArray<XRRaycastHit>.Copy(hitArray, 0, allHits, dstIndex, hitArray.Length);
    hitArray.Dispose(); // hitArray is disposed before accessing the hitArray.Length
    dstIndex += hitArray.Length; // resulting in hitArray.Length being always zero. This ovewrites the hitArray if s_NativeRaycastHits > 1
}

To Reproduce Let the ARRaycastManager.RaycastFallback() hit both ARPointCloud and ARPlane simultaneously.

Expected behavior ARRaycastManager.RaycastFallback() returns an array containing all ARPointClouds and ARPlanes that were hit.

Actual behavior Hits from the m_Raycasters[1] overwrite hits from the m_Raycasters[0].

Reproducible with AR Foundation [3.0.1, 5.0.0-pre.8]

Proposed solution Rearrange the code as follows:

dstIndex += hitArray.Length;
hitArray.Dispose();
andyb-unity commented 2 years ago

Fixed in AR Foundation 5.0.0-pre.12 https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.0/changelog/CHANGELOG.html