protected virtual void OnDestroy()
 {
     if (activeSurface == this)
     {
         activeSurface = null;
     }
 }
    protected virtual void Awake()
    {
        menu          = FindObjectOfType <ContextualHandMenu>();
        activeSurface = this;
        Initialized   = false;

        SurfacePosition = surfaceTransform.position;
    }
예제 #3
0
        public async Task PlaceNewSurface()
        {
            await Task.Yield();

            // Find our finger surface
            FingerSurface fingerSurface       = GameObject.FindObjectOfType <FingerSurface>();
            float         fingerSurfaceRadius = fingerSurface.SurfaceRadius;

            // Use our camera to place the surface
            placementTransform.position = CameraCache.Main.transform.position;
            placementTransform.rotation = CameraCache.Main.transform.rotation;
            // Only rotate on y axis
            Vector3 eulerAngles = placementTransform.eulerAngles;

            eulerAngles.x = 0;
            eulerAngles.z = 0;
            placementTransform.eulerAngles = eulerAngles;

            Vector3 targetSurfacePosition = placementTransform.TransformPoint(defaultOffset);

            if (fingerSurface == null)
            {
                Debug.LogError("No surface found in surface placement.");
                return;
            }

            if (placedOnce)
            {   // If we've placed it once already, the current placement might still be valid.
                if (Vector3.Distance(CameraCache.Main.transform.position, currentSurfacePosition) < maxRepositionDistance)
                {
                    targetSurfacePosition = currentSurfacePosition;
                }
            }
            else if (useSpatialUnderstanding)
            {
                IMixedRealitySpatialAwarenessSystem spatial;
                if (!MixedRealityServiceRegistry.TryGetService <IMixedRealitySpatialAwarenessSystem>(out spatial))
                {
                    Debug.LogError("This component requires a IMixedRealitySpatialAwarenessSystem to be enabled.");
                    return;
                }

                // Start the observers creating meshes again
                spatial.ResumeObservers();

                // Wait until our spatial understanding has delivered some meshes
                timeStarted = Time.time;
                while (spatial.SpatialAwarenessObjectParent.transform.childCount == 0)
                {
                    if ((Time.time - timeStarted > timeOut))
                    {
                        break;
                    }

                    await Task.Yield();
                }

                // While our placement sphere overlaps with spatial awareness objects, move it progressively closer to the user
                bool collidesWithWalls = true;
                timeStarted = Time.time;
                while (collidesWithWalls)
                {
                    if (Time.time - timeStarted > timeOut)
                    {
                        break;
                    }

                    Collider[] colliders = Physics.OverlapSphere(targetSurfacePosition, fingerSurfaceRadius, spatialAwarenessMask);
                    if (colliders.Length == 0)
                    {
                        collidesWithWalls = false;
                    }
                    else
                    {
                        targetSurfacePosition = Vector3.MoveTowards(targetSurfacePosition, CameraCache.Main.transform.position, moveIncrement);
                    }

                    await Task.Yield();
                }

                // Suspeend observers now that we're all set
                spatial.SuspendObservers();
            }

            currentSurfacePosition = targetSurfacePosition;
            placedOnce             = true;

            // Tell the surface to initialize
            fingerSurface.Initialize(targetSurfacePosition);
        }