77
88namespace HoloToolkit . Unity
99{
10- public class RaycastResultComparer : IComparer < RaycastResult >
10+ public struct ComparableRaycastResult
1111 {
12- private static readonly List < Func < RaycastResult , RaycastResult , int > > Comparers = new List < Func < RaycastResult , RaycastResult , int > >
12+ public readonly int LayerMaskIndex ;
13+ public readonly RaycastResult RaycastResult ;
14+
15+ public ComparableRaycastResult ( RaycastResult raycastResult , int layerMaskIndex = 0 )
1316 {
17+ RaycastResult = raycastResult ;
18+ LayerMaskIndex = layerMaskIndex ;
19+ }
20+ }
21+
22+ public class RaycastResultComparer : IComparer < ComparableRaycastResult >
23+ {
24+ private static readonly List < Func < ComparableRaycastResult , ComparableRaycastResult , int > > Comparers = new List < Func < ComparableRaycastResult , ComparableRaycastResult , int > >
25+ {
26+ CompareRaycastsByLayerMaskPrioritization ,
1427 CompareRaycastsBySortingLayer ,
1528 CompareRaycastsBySortingOrder ,
1629 CompareRaycastsByCanvasDepth ,
1730 CompareRaycastsByDistance ,
1831 } ;
1932
20- public int Compare ( RaycastResult left , RaycastResult right )
33+ public int Compare ( ComparableRaycastResult left , ComparableRaycastResult right )
2134 {
2235 for ( var i = 0 ; i < Comparers . Count ; i ++ )
2336 {
@@ -30,33 +43,39 @@ public int Compare(RaycastResult left, RaycastResult right)
3043 return 0 ;
3144 }
3245
33- private static int CompareRaycastsBySortingOrder ( RaycastResult left , RaycastResult right )
46+ private static int CompareRaycastsByLayerMaskPrioritization ( ComparableRaycastResult left , ComparableRaycastResult right )
47+ {
48+ //Lower is better, -1 is not relevant
49+ return right . LayerMaskIndex . CompareTo ( left . LayerMaskIndex ) ;
50+ }
51+
52+ private static int CompareRaycastsBySortingLayer ( ComparableRaycastResult left , ComparableRaycastResult right )
3453 {
3554 //Higher is better
36- return right . sortingOrder . CompareTo ( left . sortingOrder ) ;
55+ return left . RaycastResult . sortingLayer . CompareTo ( right . RaycastResult . sortingLayer ) ;
3756 }
3857
39- private static int CompareRaycastsBySortingLayer ( RaycastResult left , RaycastResult right )
58+ private static int CompareRaycastsBySortingOrder ( ComparableRaycastResult left , ComparableRaycastResult right )
4059 {
4160 //Higher is better
42- return right . sortingLayer . CompareTo ( left . sortingLayer ) ;
61+ return left . RaycastResult . sortingOrder . CompareTo ( right . RaycastResult . sortingOrder ) ;
4362 }
4463
45- private static int CompareRaycastsByCanvasDepth ( RaycastResult left , RaycastResult right )
64+ private static int CompareRaycastsByCanvasDepth ( ComparableRaycastResult left , ComparableRaycastResult right )
4665 {
4766 //Module is the graphic raycaster on the canvases.
48- if ( left . module . transform . IsParentOrChildOf ( right . module . transform ) )
67+ if ( left . RaycastResult . module . transform . IsParentOrChildOf ( right . RaycastResult . module . transform ) )
4968 {
5069 //Higher is better
51- return right . depth . CompareTo ( left . depth ) ;
70+ return left . RaycastResult . depth . CompareTo ( right . RaycastResult . depth ) ;
5271 }
5372 return 0 ;
5473 }
5574
56- private static int CompareRaycastsByDistance ( RaycastResult left , RaycastResult right )
75+ private static int CompareRaycastsByDistance ( ComparableRaycastResult left , ComparableRaycastResult right )
5776 {
5877 //Lower is better
59- return left . distance . CompareTo ( right . distance ) ;
78+ return right . RaycastResult . distance . CompareTo ( left . RaycastResult . distance ) ;
6079 }
6180 }
6281}
0 commit comments