11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License. See LICENSE in the project root for license information.
33
4- using Microsoft . MixedReality . Toolkit . Internal . Interfaces ;
4+ using Microsoft . MixedReality . Toolkit . Internal . Definitions . BoundarySystem ;
5+ using Microsoft . MixedReality . Toolkit . Internal . EventDatum . Boundary ;
6+ using Microsoft . MixedReality . Toolkit . Internal . Interfaces . BoundarySystem ;
57using Microsoft . MixedReality . Toolkit . Internal . Managers ;
6- using Microsoft . MixedReality . Toolkit . Internal . Utilities ;
8+ using System . Collections . Generic ;
79using UnityEngine ;
810using UnityEngine . Experimental . XR ;
911
1012namespace Microsoft . MixedReality . Toolkit . Examples . Demos
1113{
1214 /// <summary>
13- /// Demo class to show different ways of using the boundary API .
15+ /// Demo class to show different ways of using the boundary system and visualizing the data .
1416 /// </summary>
15- public class BoundaryVisualizationDemo : MonoBehaviour
17+ public class BoundaryVisualizationDemo : MonoBehaviour , IMixedRealityBoundaryHandler
1618 {
17- /// <summary>
18- /// The material used to display indicators that are within the boundary geometry.
19- /// </summary>
20- [ SerializeField ]
21- [ Tooltip ( "Material used to display indicators that are within the boundary geometry." ) ]
22- private Material boundsMaterial = null ;
19+ private IMixedRealityBoundarySystem BoundaryManager => boundaryManager ?? ( boundaryManager = MixedRealityManager . Instance . GetManager < IMixedRealityBoundarySystem > ( ) ) ;
20+ private IMixedRealityBoundarySystem boundaryManager = null ;
21+
22+ private readonly List < GameObject > markers = new List < GameObject > ( ) ;
2323
24- /// <summary>
25- /// The material used to display indicators that are outside of the boundary geometry.
26- /// </summary>
2724 [ SerializeField ]
28- [ Tooltip ( "Material used to display indicators that are outside of the boundary geometry." ) ]
29- private Material outOfBoundsMaterial = null ;
25+ private bool showFloor = true ;
3026
31- /// <summary>
32- /// The material used to display the inscribed rectangle and the indicators that are within it.
33- /// </summary>
3427 [ SerializeField ]
35- [ Tooltip ( "Material used to display the inscribed rectangle and the indicators that are within it." ) ]
36- private Material inscribedRectangleMaterial = null ;
28+ private bool showPlayArea = true ;
3729
38- /// <summary>
39- /// Boundary system implementation.
40- /// </summary
41- private IMixedRealityBoundarySystem boundaryManager = null ;
42- private IMixedRealityBoundarySystem BoundaryManager => boundaryManager ?? ( boundaryManager = MixedRealityManager . Instance . GetManager < IMixedRealityBoundarySystem > ( ) ) ;
30+ #region MonoBehaviour Implementation
4331
4432 private void Start ( )
4533 {
46- if ( MixedRealityManager . HasActiveProfile && MixedRealityManager . Instance . ActiveProfile . IsBoundarySystemEnabled )
34+ if ( BoundaryManager != null )
4735 {
48- AddQuad ( ) ;
49- AddIndicators ( ) ;
36+ if ( markers . Count == 0 )
37+ {
38+ AddMarkers ( ) ;
39+ }
5040 }
5141 }
5242
53- /// <summary>
54- /// Displays the boundary as a quad primitive.
55- /// </summary>
56- private void AddQuad ( )
43+ private void Update ( )
5744 {
58- // Get the rectangular bounds.
59- Vector2 center ;
60- float angle ;
61- float width ;
62- float height ;
63- if ( ( BoundaryManager == null ) || ! BoundaryManager . TryGetRectangularBoundsParams ( out center , out angle , out width , out height ) )
45+ if ( BoundaryManager != null )
6446 {
65- // No rectangular bounds, therefore do not render the quad.
66- return ;
47+ BoundaryManager . ShowFloor = showFloor ;
48+ BoundaryManager . ShowPlayArea = showPlayArea ;
6749 }
50+ }
6851
69- // Render the rectangular bounds.
70- if ( EdgeUtilities . IsValidPoint ( center ) )
71- {
72- GameObject quad = GameObject . CreatePrimitive ( PrimitiveType . Quad ) ;
73- quad . transform . SetParent ( transform ) ;
74- quad . transform . Translate ( new Vector3 ( center . x , 0.005f , center . y ) ) ; // Add fudge factor to avoid z-fighting
75- quad . transform . Rotate ( new Vector3 ( 90 , - angle , 0 ) ) ;
76- quad . transform . localScale = new Vector3 ( width , height , 1.0f ) ;
77- quad . GetComponent < Renderer > ( ) . sharedMaterial = inscribedRectangleMaterial ;
78- }
52+ private void OnEnable ( )
53+ {
54+ BoundaryManager . Register ( gameObject ) ;
55+ }
56+
57+ private void OnDisable ( )
58+ {
59+ BoundaryManager . Unregister ( gameObject ) ;
60+ }
61+
62+ #endregion MonoBehaviour Implementation
63+
64+ #region IMixedRealityBoundaryHandler Implementation
65+
66+ /// <inheritdoc />
67+ public void OnBoundaryVisualizationChanged ( BoundaryEventData eventData )
68+ {
69+ Debug . Log ( "[BoundaryVisualizationDemo] Boundary visualization changed." ) ;
7970 }
8071
72+ #endregion IMixedRealityBoundaryHandler Implementation
73+
8174 /// <summary>
8275 /// Displays the boundary as an array of spheres where spheres in the
8376 /// bounds are a different color.
8477 /// </summary>
85- private void AddIndicators ( )
78+ private void AddMarkers ( )
8679 {
8780 // Get the rectangular bounds.
8881 Vector2 centerRect ;
8982 float angleRect ;
9083 float widthRect ;
9184 float heightRect ;
92- if ( ( BoundaryManager == null ) || ! BoundaryManager . TryGetRectangularBoundsParams ( out centerRect , out angleRect , out widthRect , out heightRect ) )
85+
86+ if ( ! BoundaryManager . TryGetRectangularBoundsParams ( out centerRect , out angleRect , out widthRect , out heightRect ) )
9387 {
9488 // If we have no boundary manager or rectangular bounds we will show no indicators
9589 return ;
9690 }
9791
92+ MixedRealityBoundaryVisualizationProfile visualizationProfile = MixedRealityManager . Instance . ActiveProfile . BoundaryVisualizationProfile ;
93+ if ( visualizationProfile == null )
94+ {
95+ // We do not have a visualization profile configured, therefore do not render the indicators.
96+ return ;
97+ }
98+
9899 const int indicatorCount = 20 ;
99100 const float indicatorDistance = 0.2f ;
100101 const float indicatorScale = 0.1f ;
@@ -110,26 +111,29 @@ private void AddIndicators()
110111 {
111112 Vector3 offset = new Vector3 ( xIndex * indicatorDistance , 0.0f , yIndex * indicatorDistance ) ;
112113 Vector3 position = corner + offset ;
113- GameObject marker = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
114- marker . transform . SetParent ( transform ) ;
115- marker . transform . position = position ;
116- marker . transform . localScale = Vector3 . one * indicatorScale ;
117-
118- // Get the desired material for the marker.
119- Material material = outOfBoundsMaterial ;
120114
115+ Material material = null ;
121116 // Check inscribed rectangle first
122117 if ( BoundaryManager . Contains ( position , Boundary . Type . PlayArea ) )
123118 {
124- material = inscribedRectangleMaterial ;
119+ material = visualizationProfile . PlayAreaMaterial ;
125120 }
126121 // Then check geometry
127122 else if ( BoundaryManager . Contains ( position , Boundary . Type . TrackedArea ) )
128123 {
129- material = boundsMaterial ;
124+ material = visualizationProfile . TrackedAreaMaterial ;
130125 }
131126
132- marker . GetComponent < MeshRenderer > ( ) . sharedMaterial = material ;
127+ if ( material != null )
128+ {
129+ GameObject marker = GameObject . CreatePrimitive ( PrimitiveType . Sphere ) ;
130+ marker . name = "Boundary Demo Marker" ;
131+ marker . transform . SetParent ( transform ) ;
132+ marker . transform . position = position ;
133+ marker . transform . localScale = Vector3 . one * indicatorScale ;
134+ marker . GetComponent < MeshRenderer > ( ) . sharedMaterial = material ;
135+ markers . Add ( marker ) ;
136+ }
133137 }
134138 }
135139 }
0 commit comments