// invoked by AnchorRemoved-event
    private void PlaneAnchorRemoved(ARPlaneAnchor arPlaneAnchor)
    {
        string surfId = arPlaneAnchor.identifier;

        Debug.Log("Plane removed: " + surfId);

        // remove plane anchor
        if (planeAnchorDict.ContainsKey(surfId))
        {
            ARPlaneAnchorGameObject arpag = planeAnchorDict[surfId];

            if (arpag != null && arpag.gameObject)
            {
                GameObject.Destroy(arpag.gameObject);
            }

            planeAnchorDict.Remove(surfId);
            trackedPlanesTimestamp = GetLastFrameTimestamp();
        }

        // remove overlay surface
        MultiARInterop.MultiARData arData = arManager.GetARData();
        if (arData.dictOverlaySurfaces.ContainsKey(surfId))
        {
            OverlaySurfaceUpdater overlaySurface = arData.dictOverlaySurfaces[surfId];
            arData.dictOverlaySurfaces.Remove(surfId);

            Destroy(overlaySurface.gameObject);
        }
    }
    // invoked by UserAnchorRemoved-event
    private void UserAnchorRemoved(ARUserAnchor anchor)
    {
        if (!arManager)
        {
            return;
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        if (arData.allAnchorsDict.ContainsKey(anchor.identifier))
        {
            List <GameObject> anchoredObjs = arData.allAnchorsDict[anchor.identifier];
            arData.allAnchorsDict.Remove(anchor.identifier);

            foreach (GameObject anchoredObj in anchoredObjs)
            {
                if (anchoredObj && anchoredObj.transform.parent)
                {
                    GameObject parentObj = anchoredObj.transform.parent.gameObject;
                    anchoredObj.transform.parent = null;
                    anchoredObj.SetActive(false);

                    Destroy(parentObj);
                }
            }

            Debug.Log("Anchor removed: " + anchor.identifier);
        }
    }
    // invoked by AnchorUpdated-event
    private void PlaneAnchorUpdated(ARPlaneAnchor arPlaneAnchor)
    {
        string surfId = arPlaneAnchor.identifier;

        // update plane anchor
        if (planeAnchorDict.ContainsKey(surfId))
        {
            ARPlaneAnchorGameObject arpag = planeAnchorDict[surfId];
            arpag.planeAnchor = arPlaneAnchor;

            if (arpag.gameObject)
            {
                UnityARUtility.UpdatePlaneWithAnchorTransform(arpag.gameObject, arPlaneAnchor);
            }

            planeAnchorDict[surfId] = arpag;
            trackedPlanesTimestamp  = GetLastFrameTimestamp();
        }

        // update overlay surface
        MultiARInterop.MultiARData arData = arManager.GetARData();
        if (arData.dictOverlaySurfaces.ContainsKey(surfId))
        {
            UpdateOverlaySurface(arData.dictOverlaySurfaces[surfId], arPlaneAnchor);
        }
    }
    /// <summary>
    /// Anchors the game object to anchor object.
    /// </summary>
    /// <returns><c>true</c>, if game object was anchored, <c>false</c> otherwise.</returns>
    /// <param name="gameObj">Game object.</param>
    /// <param name="anchorObj">Anchor object.</param>
    public override bool AnchorGameObject(GameObject gameObj, GameObject anchorObj)
    {
        if (!isInitialized || anchorObj == null)
        {
            return(false);
        }

        if (arManager)
        {
            string anchorId = anchorObj.name;
            DontDestroyOnLoad(anchorObj);              // don't destroy it accross scenes

            if (gameObj)
            {
                gameObj.transform.SetParent(anchorObj.transform, true);
                gameObj.transform.localPosition = Vector3.zero;
                gameObj.transform.localRotation = Quaternion.identity;
            }

            MultiARInterop.MultiARData arData = arManager.GetARData();
            arData.allAnchorsDict[anchorId] = new List <GameObject>();

            if (gameObj)
            {
                arData.allAnchorsDict[anchorId].Add(gameObj);
            }

            return(true);
        }

        return(false);
    }
Ejemplo n.º 5
0
    /// <summary>
    /// Gets reference to the background render texture. Creates or recreates it, if needed.
    /// </summary>
    /// <param name="arData">AR data</param>
    /// <returns>The background render texture, or null</returns>
    protected RenderTexture GetBackgroundTexureRef(MultiARInterop.MultiARData arData)
    {
        if (arData != null)
        {
            int currentScreenW = arData.isFixedBackTexSize ? arData.fixedBackTexW : Screen.width;
            int currentScreenH = arData.isFixedBackTexSize ? arData.fixedBackTexH : Screen.height;

            if (arData.backgroundTex == null || arData.backScreenW != currentScreenW || arData.backScreenH != currentScreenH)
            {
                if (arData.backgroundTex != null)
                {
                    arData.backgroundTex.Release();
                    arData.backgroundTex = null;
                }

                arData.backScreenW = currentScreenW;
                arData.backScreenH = currentScreenH;

                arData.backgroundTex = new RenderTexture(arData.backScreenW, arData.backScreenH, 0);
                arData.backTexTime   = 0.0;
            }

            return(arData.backgroundTex);
        }

        return(null);
    }
Ejemplo n.º 6
0
 /// <summary>
 /// Sets or clears fixed background texture size
 /// </summary>
 /// <param name="arData">AR data</param>
 /// <param name="isFixedSize">Whether the background texture has fixed size</param>
 /// <param name="fixedSizeW">Fixed size width</param>
 /// <param name="fixedSizeH">Fixed size height</param>
 public void SetFixedBackTexSize(MultiARInterop.MultiARData arData, bool isFixedSize, int fixedSizeW, int fixedSizeH)
 {
     if (arData != null)
     {
         arData.isFixedBackTexSize = isFixedSize;
         arData.fixedBackTexW      = fixedSizeW;
         arData.fixedBackTexH      = fixedSizeH;
     }
 }
    // invoked by AnchorAdded-event
    private void PlaneAnchorAdded(ARPlaneAnchor arPlaneAnchor)
    {
        Debug.Log("Plane added: " + arPlaneAnchor.identifier);

        GameObject go = null;
//		if(arManager.displayTrackedSurfaces)
//		{
//			go = UnityARUtility.CreatePlaneInScene(arPlaneAnchor);
//			go.AddComponent<DontDestroyOnLoad>();  // these GOs persist across scene loads
//		}

        ARPlaneAnchorGameObject arpag = new ARPlaneAnchorGameObject();

        arpag.planeAnchor = arPlaneAnchor;
        arpag.gameObject  = go;

        planeAnchorDict.Add(arPlaneAnchor.identifier, arpag);
        trackedPlanesTimestamp = GetLastFrameTimestamp();

        // create overlay surfaces as needed
        if (arManager.useOverlaySurface != MultiARManager.SurfaceRenderEnum.None)
        {
            // estimate the material
            Material surfaceMat   = arManager.GetSurfaceMaterial();
            int      surfaceLayer = MultiARInterop.GetSurfaceLayer();

            MultiARInterop.MultiARData arData = arManager.GetARData();

            string surfId = arPlaneAnchor.identifier;
            if (!arData.dictOverlaySurfaces.ContainsKey(surfId))
            {
                GameObject overlaySurfaceObj = new GameObject();
                overlaySurfaceObj.name = "surface-" + surfId;

                overlaySurfaceObj.layer = surfaceLayer;
                overlaySurfaceObj.transform.SetParent(arData.surfaceRendererRoot ? arData.surfaceRendererRoot.transform : null);

//				GameObject overlayCubeObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
//				overlayCubeObj.name = "surface-cube-" + surfId;
//				overlayCubeObj.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
//				overlayCubeObj.transform.SetParent(overlaySurfaceObj.transform);

                OverlaySurfaceUpdater overlaySurface = overlaySurfaceObj.AddComponent <OverlaySurfaceUpdater>();
                overlaySurface.SetSurfaceMaterial(surfaceMat);
                overlaySurface.SetSurfaceCollider(arManager.surfaceCollider, arManager.colliderMaterial);

                arData.dictOverlaySurfaces.Add(surfId, overlaySurface);
            }

            // update the surface mesh
            UpdateOverlaySurface(arData.dictOverlaySurfaces[surfId], arPlaneAnchor);
        }
    }
    /// <summary>
    /// Unparents the game object and removes the anchor from the system (if possible).
    /// </summary>
    /// <returns><c>true</c>, if game object anchor was removed, <c>false</c> otherwise.</returns>
    /// <param name="anchorId">Anchor identifier.</param>
    /// <param name="keepObjActive">If set to <c>true</c> keeps the object active afterwards.</param>
    public bool RemoveGameObjectAnchor(string anchorId, bool keepObjActive)
    {
        if (!isInitialized || !arManager)
        {
            return(false);
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        if (arData.allAnchorsDict.ContainsKey(anchorId))
        {
            // get the child game objects
            List <GameObject> anchoredObjs = arData.allAnchorsDict[anchorId];
            arData.allAnchorsDict.Remove(anchorId);

            foreach (GameObject anchoredObj in anchoredObjs)
            {
                if (anchoredObj && anchoredObj.transform.parent)
                {
                    //GameObject parentObj = anchoredObj.transform.parent.gameObject;
                    anchoredObj.transform.parent = null;

                    if (!keepObjActive)
                    {
                        anchoredObj.SetActive(false);
                    }
                }
            }

            // remove the anchor from the system
            GameObject anchorObj = GameObject.Find(anchorId);
            if (anchorObj)
            {
                WorldAnchor anchor = anchorObj.GetComponent <WorldAnchor>();

                if (anchor)
                {
                    anchor.OnTrackingChanged -= Anchor_OnTrackingChanged;
                    //Destroy(anchor);
                    Destroy(anchor.gameObject);
                }
            }

            return(true);
        }

        return(false);
    }
    /// <summary>
    /// Gets the background (reality) texture
    /// </summary>
    /// <param name="arData">AR data</param>
    /// <returns>The background texture, or null</returns>
    public override Texture GetBackgroundTex(MultiARInterop.MultiARData arData)
    {
        if (arData != null)
        {
            RenderTexture backTex = GetBackgroundTexureRef(arData);

            if (backTex != null && backgroundMat != null &&
                cameraTrackingState != ARTrackingState.ARTrackingStateNotAvailable && arData.backTexTime != lastFrameTimestamp)
            {
                arData.backTexTime = lastFrameTimestamp;
                Graphics.Blit(null, backTex, backgroundMat);
            }

            return(backTex);
        }

        return(null);
    }
Ejemplo n.º 10
0
    /// <summary>
    /// Gets the background (reality) texture
    /// </summary>
    /// <param name="arData">AR data</param>
    /// <returns>The background texture, or null</returns>
    public override Texture GetBackgroundTex(MultiARInterop.MultiARData arData)
    {
        if (arData != null)
        {
            RenderTexture backTex = GetBackgroundTexureRef(arData);

            if (backTex != null && backgroundMat != null &&
                Session.Status.IsValid() && arData.backTexTime != lastFrameTimestamp)
            {
                arData.backTexTime = lastFrameTimestamp;
                Graphics.Blit(null, backTex, backgroundMat);
            }

            return(backTex);
        }

        return(null);
    }
    /// <summary>
    /// Anchors the game object to world.
    /// </summary>
    /// <returns>The anchor Id, or empty string.</returns>
    /// <param name="gameObj">Game object.</param>
    /// <param name="worldPosition">World position.</param>
    /// <param name="worldRotation">World rotation.</param>
    public string AnchorGameObjectToWorld(GameObject gameObj, Vector3 worldPosition, Quaternion worldRotation)
    {
        string sAnchorId = string.Empty;

        if (!isInitialized || !arManager)
        {
            return(sAnchorId);
        }

        GameObject anchorObj = new GameObject();

        //GameObject anchorObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        sAnchorId      = System.Guid.NewGuid().ToString();
        anchorObj.name = sAnchorId;

        anchorObj.transform.position = worldPosition;
        anchorObj.transform.rotation = worldRotation;
        //anchorObj.transform.localScale = new Vector3(0.1f, 0.2f, 0.1f);  // for debug only

        WorldAnchor anchor = anchorObj.AddComponent <WorldAnchor>();

        anchor.OnTrackingChanged += Anchor_OnTrackingChanged;

        // don't destroy it accross scenes
        DontDestroyOnLoad(anchorObj);

        if (gameObj)
        {
            gameObj.transform.SetParent(anchorObj.transform, true);
            gameObj.transform.localPosition = Vector3.zero;
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        arData.allAnchorsDict[sAnchorId] = new List <GameObject>();

        if (gameObj)
        {
            arData.allAnchorsDict[sAnchorId].Add(gameObj);
        }

        return(sAnchorId);
    }
Ejemplo n.º 12
0
    /// <summary>
    /// Anchors the game object to world.
    /// </summary>
    /// <returns>The anchor Id, or empty string.</returns>
    /// <param name="gameObj">Game object.</param>
    /// <param name="worldPosition">World position.</param>
    /// <param name="worldRotation">World rotation.</param>
    public string AnchorGameObjectToWorld(GameObject gameObj, Vector3 worldPosition, Quaternion worldRotation)
    {
        if (!isInitialized || (cameraTrackingState == TrackingState.Stopped))
        {
            return(string.Empty);
        }

        if (arManager)
        {
            Pose pose = new Pose();
            pose.position = worldPosition;
            pose.rotation = worldRotation;

            Anchor anchor = Session.CreateAnchor(pose);
            if (anchor == null)
            {
                return(string.Empty);
            }

            string anchorId = anchor.m_NativeHandle.ToString();
            DontDestroyOnLoad(anchor.gameObject);              // don't destroy it accross scenes

            if (gameObj)
            {
                gameObj.transform.SetParent(anchor.transform, true);
                gameObj.transform.localPosition = Vector3.zero;
                gameObj.transform.localRotation = Quaternion.identity;
            }

            MultiARInterop.MultiARData arData = arManager.GetARData();
            arData.allAnchorsDict[anchorId] = new List <GameObject>();

            if (gameObj)
            {
                arData.allAnchorsDict[anchorId].Add(gameObj);
            }

            return(anchorId);
        }

        return(string.Empty);
    }
Ejemplo n.º 13
0
    /// <summary>
    /// Unparents the game object and removes the anchor from the system (if possible).
    /// </summary>
    /// <returns><c>true</c>, if game object anchor was removed, <c>false</c> otherwise.</returns>
    /// <param name="anchorId">Anchor identifier.</param>
    /// <param name="keepObjActive">If set to <c>true</c> keeps the object active afterwards.</param>
    public bool RemoveGameObjectAnchor(string anchorId, bool keepObjActive)
    {
        if (!isInitialized || !arManager)
        {
            return(false);
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        if (arData.allAnchorsDict.ContainsKey(anchorId))
        {
            // remove the anchor from the system
            // ARCore doesn't provide API for removal (as of preview version)
            // all anchor game objects remain in inactive state

            // get the child game object
            List <GameObject> anchoredObjs = arData.allAnchorsDict[anchorId];
            arData.allAnchorsDict.Remove(anchorId);

            foreach (GameObject anchoredObj in anchoredObjs)
            {
                // detach the parent
                if (anchoredObj && anchoredObj.transform.parent)
                {
                    GameObject parentObj = anchoredObj.transform.parent.gameObject;
                    anchoredObj.transform.parent = null;

                    if (!keepObjActive)
                    {
                        anchoredObj.SetActive(false);
                    }

                    Destroy(parentObj);                      // todo: check if thus works in Preview2
                }
            }

            return(true);
        }

        return(false);
    }
    /// <summary>
    /// Anchors the game object to world.
    /// </summary>
    /// <returns>The anchor Id, or empty string.</returns>
    /// <param name="gameObj">Game object.</param>
    /// <param name="worldPosition">World position.</param>
    /// <param name="worldRotation">World rotation.</param>
    public string AnchorGameObjectToWorld(GameObject gameObj, Vector3 worldPosition, Quaternion worldRotation)
    {
        string sAnchorId = string.Empty;

        if (!isInitialized || !arManager)
        {
            return(sAnchorId);
        }

        GameObject anchorObj = new GameObject();

        //GameObject anchorObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        anchorObj.transform.position = worldPosition;
        anchorObj.transform.rotation = worldRotation;
        //anchorObj.transform.localScale = new Vector3(0.1f, 0.2f, 0.1f);  // for debug only

        UnityARUserAnchorData anchorData = UnityARSessionNativeInterface.GetARSessionNativeInterface().AddUserAnchorFromGameObject(anchorObj);

        sAnchorId = anchorData.identifierStr;

        anchorObj.name = sAnchorId;
        DontDestroyOnLoad(anchorObj);          // don't destroy it accross scenes

        if (gameObj)
        {
            gameObj.transform.SetParent(anchorObj.transform, true);
            gameObj.transform.localPosition = Vector3.zero;
            gameObj.transform.localRotation = Quaternion.identity;
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        arData.allAnchorsDict[sAnchorId] = new List <GameObject>();

        if (gameObj)
        {
            arData.allAnchorsDict[sAnchorId].Add(gameObj);
        }

        return(sAnchorId);
    }
    // invoked by Anchor.OnTrackingChanged-event
    void Anchor_OnTrackingChanged(WorldAnchor self, bool located)
    {
        if (!arManager || !self)
        {
            return;
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        string anchorId = self.gameObject.name;

        Debug.Log("Anchor " + anchorId + " tracking: " + located);

        if (arData.allAnchorsDict.ContainsKey(anchorId))
        {
            GameObject anchorObj = GameObject.Find(anchorId);

            if (anchorObj)
            {
                anchorObj.SetActive(located);
            }
        }
    }
Ejemplo n.º 16
0
    /// <summary>
    /// Anchors the game object to world.
    /// </summary>
    /// <returns>The game object to world.</returns>
    /// <param name="gameObj">Game object.</param>
    /// <param name="hit">Trackable hit.</param>
    public string AnchorGameObjectToWorld(GameObject gameObj, MultiARInterop.TrackableHit hit)
    {
        string anchorId = string.Empty;

        if (hit.psObject != null && hit.psObject is TrackableHit)
        {
            // valid anchor - attach the tracked plane
            TrackableHit intHit = (TrackableHit)hit.psObject;
            Anchor       anchor = intHit.Trackable.CreateAnchor(intHit.Pose);
            if (anchor == null)
            {
                return(string.Empty);
            }

            anchorId = anchor.m_NativeHandle.ToString();
            DontDestroyOnLoad(anchor.gameObject);              // don't destroy it accross scenes

            if (gameObj)
            {
                gameObj.transform.SetParent(anchor.transform, true);
                gameObj.transform.localPosition = Vector3.zero;
                gameObj.transform.localRotation = Quaternion.identity;
            }

            MultiARInterop.MultiARData arData = arManager.GetARData();
            arData.allAnchorsDict[anchorId] = new List <GameObject>();

            if (gameObj)
            {
                arData.allAnchorsDict[anchorId].Add(gameObj);
            }
        }
        else
        {
            anchorId = AnchorGameObjectToWorld(gameObj, hit.point, hit.rotation);
        }

        return(anchorId);
    }
    // invoked by FrameUpdated-event
    private void ARFrameUpdated(UnityARCamera camera)
    {
        // current timestamp
        lastFrameTimestamp = GetCurrentTimestamp();

        // current light intensity
        UnityARLightEstimate lightEstimate = camera.lightData.arLightEstimate;

        currentLightIntensity   = lightEstimate.ambientIntensity / 1000f;
        currentColorTemperature = lightEstimate.ambientColorTemperature;

        // point cloud
        if (arManager.pointCloudPrefab != null)
        {
            MultiARInterop.MultiARData arData = arManager.GetARData();

            arData.pointCloudData      = camera.pointCloudData;
            arData.pointCloudLength    = arData.pointCloudData.Length;
            arData.pointCloudTimestamp = lastFrameTimestamp;
            //Debug.Log("Point cloud size: " + arData.pointCloudLength + " @ " + arData.pointCloudTimestamp);
        }
    }
    /// <summary>
    /// Unparents the game object and removes the anchor from the system (if possible).
    /// </summary>
    /// <returns><c>true</c>, if game object anchor was removed, <c>false</c> otherwise.</returns>
    /// <param name="anchorId">Anchor identifier.</param>
    /// <param name="keepObjActive">If set to <c>true</c> keeps the object active afterwards.</param>
    public bool RemoveGameObjectAnchor(string anchorId, bool keepObjActive)
    {
        if (!isInitialized || !arManager)
        {
            return(false);
        }

        MultiARInterop.MultiARData arData = arManager.GetARData();
        if (arData.allAnchorsDict.ContainsKey(anchorId))
        {
            // remove the anchor from the system
            UnityARSessionNativeInterface.GetARSessionNativeInterface().RemoveUserAnchor(anchorId);

            // get the child game objects
            List <GameObject> anchoredObjs = arData.allAnchorsDict[anchorId];
            arData.allAnchorsDict.Remove(anchorId);

            foreach (GameObject anchoredObj in anchoredObjs)
            {
                if (anchoredObj && anchoredObj.transform.parent)
                {
                    GameObject parentObj = anchoredObj.transform.parent.gameObject;
                    anchoredObj.transform.parent = null;

                    if (!keepObjActive)
                    {
                        anchoredObj.SetActive(false);
                    }

                    Destroy(parentObj);
                }
            }

            return(true);
        }

        return(false);
    }
    /// <summary>
    /// Gets reference to the background render texture. Creates or recreates it, if needed.
    /// </summary>
    /// <param name="arData">AR data</param>
    /// <returns>The background render texture, or null</returns>
    protected RenderTexture GetBackgroundTexureRef(MultiARInterop.MultiARData arData)
    {
        if (arData != null)
        {
            if (arData.backgroundTex == null || arData.backScreenW != Screen.width || arData.backScreenH != Screen.height)
            {
                if (arData.backgroundTex != null)
                {
                    arData.backgroundTex.Release();
                    arData.backgroundTex = null;
                }

                arData.backgroundTex = new RenderTexture(Screen.width, Screen.height, 0);
                arData.backScreenW   = Screen.width;
                arData.backScreenH   = Screen.height;
                arData.backTexTime   = 0.0;
            }

            return(arData.backgroundTex);
        }

        return(null);
    }
    // -- // -- // -- // -- // -- // -- // -- // -- // -- // -- //

    void Start()
    {
        if (!isInterfaceEnabled)
        {
            return;
        }

        // determine if display is opaque or transparent
        isDisplayOpaque = HolographicSettings.IsDisplayOpaque;
        Debug.Log("Display: " + (isDisplayOpaque ? "Opaque" : "Transparent"));

        // modify the main camera in the scene
        Camera currentCamera = MultiARInterop.GetMainCamera();

        if (!currentCamera)
        {
            GameObject currentCameraObj = new GameObject("Main Camera");
            currentCameraObj.tag = "MainCamera";

            currentCamera = currentCameraObj.AddComponent <Camera>();
        }

        // reset camera position & rotation
        //currentCamera.transform.position = Vector3.zero;
        currentCamera.transform.rotation = Quaternion.identity;

        // set camera parameters
        currentCamera.clearFlags      = CameraClearFlags.SolidColor;
        currentCamera.backgroundColor = new Color(0f, 0f, 0f, 0f);
        currentCamera.nearClipPlane   = 0.5f;        // HoloLens recommended
        currentCamera.farClipPlane    = 100f;

        if (isDisplayOpaque)
        {
            currentCamera.clearFlags = CameraClearFlags.Skybox;
        }

        // set the fastest quality setting
        QualitySettings.SetQualityLevel((int)qualityLevel);
        Debug.Log("QualityLevel: " + QualitySettings.names[(int)qualityLevel]);

        // reference to the AR main camera
        mainCamera = currentCamera;

        // don't destroy the light between scenes
        DontDestroyOnLoad(currentCamera.gameObject);

//		// add camera parent
//		if(currentCamera.transform.parent == null)
//		{
//			GameObject cameraParent = new GameObject("CameraParent");
//			currentCamera.transform.SetParent(cameraParent.transform);
//		}

        // modify the directional light
        Light currentLight = MultiARInterop.GetDirectionalLight();

        if (!currentLight)
        {
            GameObject currentLightObj = new GameObject("Directional light");

            currentLight      = currentLightObj.AddComponent <Light>();
            currentLight.type = LightType.Directional;
        }

        // reset light position & rotation
        currentLight.transform.position = Vector3.zero;
        currentLight.transform.rotation = Quaternion.Euler(40f, 40f, 0f);

        // set light parameters
        currentLight.color = new Color32(255, 254, 244, 255);

        // add the ar-light component
        //currentLight.gameObject.AddComponent<MultiARDirectionalLight>();

        // reference to the AR directional light
        //directionalLight = currentLight;

        // don't destroy the light between scenes
        DontDestroyOnLoad(currentLight.gameObject);

        // there is no point cloud in WinMR
        MultiARInterop.MultiARData arData = arManager.GetARData();

        // check for point cloud getter
        if (arManager.pointCloudPrefab != null)
        {
            arData.pointCloudData      = new Vector3[0];
            arData.pointCloudLength    = 0;
            arData.pointCloudTimestamp = 0.0;
        }

        // set tracking state
        cameraTrackingState = WorldManager.state;
        WorldManager.OnPositionalLocatorStateChanged += WorldManager_OnPositionalLocatorStateChanged;

//		// set tracking space type
//		Debug.Log("Before: " + XRDevice.GetTrackingSpaceType());
//		if(XRDevice.GetTrackingSpaceType() != TrackingSpaceType.Stationary)
//		{
//			if(!XRDevice.SetTrackingSpaceType(TrackingSpaceType.Stationary))
//			{
//				Debug.LogError("Cannot set stationary space type!");
//			}
//		}

        // create gesture input
        if (!isDisplayOpaque)
        {
            gestureRecognizer = new GestureRecognizer();
            gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.Hold |
                                                      GestureSettings.NavigationX | GestureSettings.NavigationY | GestureSettings.NavigationZ);

            gestureRecognizer.Tapped += GestureRecognizer_Tapped;

//			gestureRecognizer.HoldStarted += GestureRecognizer_HoldStarted;
//			gestureRecognizer.HoldCompleted += GestureRecognizer_HoldCompleted;
//			gestureRecognizer.HoldCanceled += GestureRecognizer_HoldCanceled;

            gestureRecognizer.NavigationStarted   += GestureRecognizer_NavigationStarted;
            gestureRecognizer.NavigationUpdated   += GestureRecognizer_NavigationUpdated;
            gestureRecognizer.NavigationCompleted += GestureRecognizer_NavigationCompleted;
            gestureRecognizer.NavigationCanceled  += GestureRecognizer_NavigationCanceled;

            gestureRecognizer.StartCapturingGestures();
            Debug.Log("Gesture recognizer inited and started.");
        }
        //else
        {
            // init interaction manager
//			InteractionManager.InteractionSourcePressed += InteractionManager_InteractionSourcePressed;
//			InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated;
//			InteractionManager.InteractionSourceReleased += InteractionManager_InteractionSourceReleased;

            InteractionManager.InteractionSourceDetected += InteractionManager_InteractionSourceDetected;
            InteractionManager.InteractionSourceLost     += InteractionManager_InteractionSourceLost;
            InteractionManager.InteractionSourceUpdated  += InteractionManager_InteractionSourceUpdated;

            Debug.Log("Interaction manager inited.");
        }

        // create surface renderer
        if (arManager.useOverlaySurface != MultiARManager.SurfaceRenderEnum.None)
        {
            GameObject objRenderer = new GameObject();
            objRenderer.name           = "SurfaceRenderer";
            objRenderer.layer          = MultiARInterop.GetSurfaceLayer();
            arData.surfaceRendererRoot = objRenderer;

            surfaceRootTransform = objRenderer.transform;
            DontDestroyOnLoad(objRenderer);

            if (!isDisplayOpaque)
            {
                // hololens
                surfaceRenderer = objRenderer.AddComponent <SpatialMappingRenderer>();
                surfaceRenderer.surfaceParent = objRenderer;

                switch (arManager.useOverlaySurface)
                {
                case MultiARManager.SurfaceRenderEnum.None:
                    surfaceRenderer.renderState = SpatialMappingRenderer.RenderState.None;
                    break;

                case MultiARManager.SurfaceRenderEnum.Visualization:
                    surfaceRenderer.renderState = SpatialMappingRenderer.RenderState.Visualization;
                    break;

                case MultiARManager.SurfaceRenderEnum.Occlusion:
                case MultiARManager.SurfaceRenderEnum.OcclusionWithShadows:
                    surfaceRenderer.renderState = SpatialMappingRenderer.RenderState.Occlusion;
                    break;
                }

                if (arManager.useOverlaySurface != MultiARManager.SurfaceRenderEnum.None)
                {
                    surfaceRenderer.visualMaterial    = arManager.surfaceVisualizationMaterial;
                    surfaceRenderer.occlusionMaterial = arManager.useOverlaySurface == MultiARManager.SurfaceRenderEnum.OcclusionWithShadows ?
                                                        arManager.surfaceOcclusionWithShadowsMaterial : arManager.surfaceOcclusionMaterial;
                }
            }
            else
            {
                // use special surface material on opaque displays
                Material visualMaterial = arManager.GetSurfaceMaterial();
                if (arManager.useOverlaySurface == MultiARManager.SurfaceRenderEnum.Visualization && vrSurfaceMaterial)
                {
                    visualMaterial = vrSurfaceMaterial;
                }

                // mr headsets
                CreateBoundaryPlane(objRenderer.transform, visualMaterial, arManager.surfaceCollider, arManager.colliderMaterial);

                boundaryMgr           = objRenderer.AddComponent <HoloToolkit.Unity.Boundary.BoundaryManager>();
                boundaryMgr.FloorQuad = boundaryPlane;
                boundaryMgr.AwakeBoundaryManager();
            }
        }

        // create surface collider
        if (arManager.surfaceCollider)
        {
            GameObject objCollider = new GameObject();
            objCollider.name  = "SurfaceCollider";
            objCollider.layer = MultiARInterop.GetSurfaceLayer();
            DontDestroyOnLoad(objCollider);

            if (!isDisplayOpaque)
            {
                // hololens
                surfaceCollider = objCollider.AddComponent <SpatialMappingCollider>();
                surfaceCollider.surfaceParent = objCollider;

                surfaceCollider.lodType = SpatialMappingBase.LODType.Low;
                surfaceCollider.layer   = MultiARInterop.GetSurfaceLayer();

                if (arManager.colliderMaterial)
                {
                    surfaceCollider.material = arManager.colliderMaterial;
                }
            }
            else
            {
                // mr headsets
                if (boundaryPlane == null)
                {
                    // there was no boundary rendering
                    CreateBoundaryPlane(objCollider.transform, null, true, arManager.colliderMaterial);

                    boundaryMgr           = objCollider.AddComponent <HoloToolkit.Unity.Boundary.BoundaryManager>();
                    boundaryMgr.FloorQuad = boundaryPlane;
                    boundaryMgr.AwakeBoundaryManager();
                }
            }
        }

//		// if camera is too near to the floor, lower the floor 1.5 meter below the camera
//		if(currentCamera && boundaryMgr)
//		{
//			if(currentCamera.transform.position.y < 0.1f)
//			{
//				boundaryMgr.CurrentFloorHeightOffset = currentCamera.transform.position.y - 1.5f;
//				Debug.Log(string.Format("FloorHeightOffset set below the camera at {0:F2}m.", boundaryMgr.CurrentFloorHeightOffset));
//			}
//		}

        // starts co-routine to check rendered surfaces
        StartCoroutine(CheckSurfacesRoutine());

        Debug.Log("TrackingSpaceType: " + XRDevice.GetTrackingSpaceType());
        Debug.Log("Screen size: " + Screen.width + " x " + Screen.height);

        int surfaceLayer = MultiARInterop.GetSurfaceLayer();          // LayerMask.NameToLayer("SpatialSurface");

        Debug.Log("SpatialSurfaceLayer: " + surfaceLayer);

        // interface is initialized
        isInitialized = true;
    }
    void OnDestroy()
    {
        // remove event handlers
        if (isInitialized)
        {
            isInitialized = false;

            WorldManager.OnPositionalLocatorStateChanged -= WorldManager_OnPositionalLocatorStateChanged;

            if (surfaceRootTransform)
            {
                Destroy(surfaceRootTransform.gameObject);
                surfaceRootTransform = null;
                surfaceRenderer      = null;
            }

            if (surfaceCollider)
            {
                Destroy(surfaceCollider.gameObject);
                surfaceCollider = null;
            }

            if (gestureRecognizer != null)
            {
                gestureRecognizer.StopCapturingGestures();

                gestureRecognizer.Tapped -= GestureRecognizer_Tapped;
//				gestureRecognizer.HoldStarted -= GestureRecognizer_HoldStarted;
//				gestureRecognizer.HoldCompleted -= GestureRecognizer_HoldCompleted;
//				gestureRecognizer.HoldCanceled -= GestureRecognizer_HoldCanceled;

                gestureRecognizer.NavigationStarted   -= GestureRecognizer_NavigationStarted;
                gestureRecognizer.NavigationUpdated   -= GestureRecognizer_NavigationUpdated;
                gestureRecognizer.NavigationCompleted -= GestureRecognizer_NavigationCompleted;
                gestureRecognizer.NavigationCanceled  -= GestureRecognizer_NavigationCanceled;
            }

            //if(isDisplayOpaque)
            {
                // stop interaction manager
//				InteractionManager.InteractionSourcePressed -= InteractionManager_InteractionSourcePressed;
//				InteractionManager.InteractionSourceUpdated -= InteractionManager_InteractionSourceUpdated;
//				InteractionManager.InteractionSourceReleased -= InteractionManager_InteractionSourceReleased;

                InteractionManager.InteractionSourceDetected -= InteractionManager_InteractionSourceDetected;
                InteractionManager.InteractionSourceLost     -= InteractionManager_InteractionSourceLost;
                InteractionManager.InteractionSourceUpdated  -= InteractionManager_InteractionSourceUpdated;
            }

            if (arManager)
            {
                // get arData-reference
                MultiARInterop.MultiARData arData = arManager.GetARData();

                // destroy all world anchors
                foreach (string anchorId in arData.allAnchorsDict.Keys)
                {
                    // remove the anchor from the system
                    GameObject anchorObj = GameObject.Find(anchorId);
                    if (anchorObj)
                    {
                        WorldAnchor anchor = anchorObj.GetComponent <WorldAnchor>();

                        if (anchor)
                        {
                            anchor.OnTrackingChanged -= Anchor_OnTrackingChanged;
                            Destroy(anchor);
                        }
                    }

                    Destroy(anchorObj);
                }

                // clear the list
                arData.allAnchorsDict.Clear();
            }
        }
    }
    // -- // -- // -- // -- // -- // -- // -- // -- // -- // -- //

    void Start()
    {
        if (!isInterfaceEnabled)
        {
            return;
        }

        if (!yuvMaterial)
        {
            Debug.LogError("ARKit-interface cannot start: YuvMaterial is not set.");
            return;
        }

        // modify the main camera in the scene
        Camera currentCamera = MultiARInterop.GetMainCamera();

        if (!currentCamera)
        {
            GameObject currentCameraObj = new GameObject("Main Camera");
            currentCameraObj.tag = "MainCamera";

            currentCamera = currentCameraObj.AddComponent <Camera>();
        }

        // reset camera position & rotation
        currentCamera.transform.position = Vector3.zero;
        currentCamera.transform.rotation = Quaternion.identity;

        // set camera parameters
        currentCamera.clearFlags    = CameraClearFlags.Depth;
        currentCamera.nearClipPlane = 0.1f;
        currentCamera.farClipPlane  = 30f;

        // reference to the AR main camera
        mainCamera = currentCamera;

        // add camera parent
        if (currentCamera.transform.parent == null)
        {
            GameObject cameraParent = new GameObject("CameraParent");
            currentCamera.transform.SetParent(cameraParent.transform);
            DontDestroyOnLoad(cameraParent);
        }
        else
        {
            DontDestroyOnLoad(currentCamera.transform.root.gameObject);
        }

        // add the needed camera components
        arVideoRenderer = currentCamera.gameObject.AddComponent <UnityARVideo>();
        arVideoRenderer.m_ClearMaterial = yuvMaterial;
        backgroundMat = yuvMaterial;

        currentCamera.gameObject.AddComponent <UnityARCameraNearFar>();

        // modify the directional light
        Light currentLight = MultiARInterop.GetDirectionalLight();

        if (!currentLight)
        {
            GameObject currentLightObj = new GameObject("Directional light");

            currentLight      = currentLightObj.AddComponent <Light>();
            currentLight.type = LightType.Directional;
        }

        // reset light position & rotation
        currentLight.transform.position = Vector3.zero;
        currentLight.transform.rotation = Quaternion.Euler(40f, 40f, 0f);
        DontDestroyOnLoad(currentLight.gameObject);

        // set light parameters
        //currentLight.lightmapBakeType = LightmapBakeType.Mixed;
        currentLight.color = new Color32(255, 254, 244, 255);

        // add the ar-light component
        currentLight.gameObject.AddComponent <MultiARDirectionalLight>();

        // reference to the AR directional light
        //directionalLight = currentLight;

        // create camera manager
        GameObject camManagerObj = new GameObject("ARCameraManager");

        DontDestroyOnLoad(camManagerObj);

        UnityARCameraManager camManager = camManagerObj.AddComponent <UnityARCameraManager>();

        camManager.m_camera = currentCamera;

        camManager.startAlignment = cameraAlignment;
        camManager.planeDetection = UnityARPlaneDetection.HorizontalAndVertical;

        //Debug.Log("arImageDatabase: " + (arImageDatabase != null ? arImageDatabase.resourceGroupName : "-"));
        if (arImageDatabase != null)
        {
            camManager.detectionImages = arImageDatabase;
            Debug.Log("camManager.detectionImages set to: " + arImageDatabase);
        }

        // allow relocalization after session interruption
        UnityARSessionNativeInterface.ARSessionShouldAttemptRelocalization = true;

        // get ar-data
        MultiARInterop.MultiARData arData = arManager ? arManager.GetARData() : null;

        // check for point cloud getter
        if (arManager && arManager.pointCloudPrefab != null)
        {
            arData.pointCloudData      = new Vector3[0];
            arData.pointCloudLength    = 0;
            arData.pointCloudTimestamp = 0.0;
        }

        // create surface renderer
        if (arManager && arData != null)
        {
            arData.surfaceRendererRoot      = new GameObject();
            arData.surfaceRendererRoot.name = "SurfaceRenderer";
            DontDestroyOnLoad(arData.surfaceRendererRoot);
        }

//		// check for tracked plane display
//		if(arManager.displayTrackedSurfaces && trackedPlanePrefab)
//		{
//			UnityARUtility.InitializePlanePrefab(trackedPlanePrefab);
//		}

        // add needed events
        UnityARSessionNativeInterface.ARFrameUpdatedEvent           += ARFrameUpdated;
        UnityARSessionNativeInterface.ARSessionTrackingChangedEvent += ARSessionTrackingChanged;

        UnityARSessionNativeInterface.ARAnchorAddedEvent   += PlaneAnchorAdded;
        UnityARSessionNativeInterface.ARAnchorUpdatedEvent += PlaneAnchorUpdated;
        UnityARSessionNativeInterface.ARAnchorRemovedEvent += PlaneAnchorRemoved;

        UnityARSessionNativeInterface.ARUserAnchorAddedEvent   += UserAnchorAdded;
        UnityARSessionNativeInterface.ARUserAnchorRemovedEvent += UserAnchorRemoved;

        // create ARCoreSession component, if the cloud functionality is used
        GoogleARCore.ARCoreSession arCoreSession = gameObject.GetComponent <GoogleARCore.ARCoreSession>();
        if (arCoreSession == null)
        {
            TextAsset cloudApiKey = Resources.Load("RuntimeSettings/CloudServicesApiKey") as TextAsset;

            if (cloudApiKey != null)
            {
                arCoreSession = gameObject.AddComponent <GoogleARCore.ARCoreSession>();
            }
        }

        // interface is initialized
        isInitialized = true;
    }
Ejemplo n.º 23
0
 /// <summary>
 /// Gets the background (reality) texture
 /// </summary>
 /// <param name="arData">AR data</param>
 /// <returns>The background texture, or null</returns>
 public virtual Texture GetBackgroundTex(MultiARInterop.MultiARData arData)
 {
     return(arData != null ? arData.backgroundTex : null);
 }
Ejemplo n.º 24
0
    // initializes the AR-Core components
    private void InitArCore()
    {
        //Debug.Log("InitArCore started.");

        // disable the main camera, if any
        Camera currentCamera = MultiARInterop.GetMainCamera();

        if (currentCamera)
        {
            currentCamera.gameObject.SetActive(false);
        }

        // create ARCore-Device in the scene
        arCoreDeviceObj      = Instantiate(arCoreDevicePrefab, Vector3.zero, Quaternion.identity);
        arCoreDeviceObj.name = "ARCore Device";
        DontDestroyOnLoad(arCoreDeviceObj);

        // get background material
        arCodeRenderer = FindObjectOfType <ARCoreBackgroundRenderer>();
        if (arCodeRenderer)
        {
            backgroundMat = arCodeRenderer.BackgroundMaterial;
        }

        // update the session config, if needed
        ARCoreSession arSession = arCoreDeviceObj.GetComponent <ARCoreSession>();

        if (arSession != null && arSession.SessionConfig != null && arImageDatabase != null)
        {
            arSession.SessionConfig.AugmentedImageDatabase = arImageDatabase;
        }

        // reference to the AR main camera
        mainCamera = arCoreDeviceObj.GetComponentInChildren <Camera>();

//		// disable directional light, if any
//		Light currentLight = MultiARInterop.GetDirectionalLight();
//		if(currentLight)
//		{
//			currentLight.gameObject.SetActive(false);
//		}
//
//		// create AR environmental light
//		GameObject envLight = new GameObject("Evironmental Light");
//		//envLight.transform.position = Vector3.zero;
//		//envLight.transform.rotation = Quaternion.identity;
//		envLight.AddComponent<EnvironmentalLight>();
//
//		// reference to the AR directional light
//		//directionalLight = envLight.GetComponent<Light>();

        // modify the directional light
        Light currentLight = MultiARInterop.GetDirectionalLight();

        if (!currentLight)
        {
            GameObject currentLightObj = new GameObject("Directional light");

            currentLight      = currentLightObj.AddComponent <Light>();
            currentLight.type = LightType.Directional;
        }

        // reset light position & rotation
        currentLight.transform.position = Vector3.zero;
        currentLight.transform.rotation = Quaternion.Euler(40f, 40f, 0f);
        DontDestroyOnLoad(currentLight.gameObject);

        // set light parameters
        //currentLight.lightmapBakeType = LightmapBakeType.Mixed;
        currentLight.color = new Color32(255, 254, 244, 255);

        // add the ar-light component
        currentLight.gameObject.AddComponent <MultiARDirectionalLight>();

        // get ar-data
        MultiARInterop.MultiARData arData = arManager ? arManager.GetARData() : null;

        if (arManager && arManager.usePointCloudData)
        {
            arData.pointCloudData      = new Vector3[MultiARInterop.MAX_POINT_COUNT];
            arData.pointCloudLength    = 0;
            arData.pointCloudTimestamp = 0.0;
        }

        // create surface renderer
        if (arManager && arData != null)
        {
            arData.surfaceRendererRoot      = new GameObject();
            arData.surfaceRendererRoot.name = "SurfaceRenderer";
            DontDestroyOnLoad(arData.surfaceRendererRoot);
        }

        // interface is initialized
        isInitialized = true;

        //Debug.Log("InitArCore finished.");
    }
Ejemplo n.º 25
0
    void Update()
    {
        if (!isInitialized)
        {
            return;
        }

        // check for errors
        _QuitOnConnectionErrors();

        // check for input (touch)
        CheckForInputAction();

        // estimate the tracking state
        SessionStatus status = Session.Status;

        if (status.IsError() || status.IsNotInitialized())
        {
            cameraTrackingState = TrackingState.Stopped;
            return;
        }
        else if (status == SessionStatus.Tracking)
        {
            cameraTrackingState = TrackingState.Tracking;
        }
        else
        {
            cameraTrackingState = TrackingState.Paused;
        }

        // get frame timestamp and light intensity
        lastFrameTimestamp = GetCurrentTimestamp();

        if (Frame.LightEstimate.State == LightEstimateState.Valid)
        {
            // Normalize pixel intensity by middle gray in gamma space.
            const float middleGray = 0.466f;
            currentLightIntensity = Frame.LightEstimate.PixelIntensity / middleGray;
        }

        // get point cloud, if needed
        MultiARInterop.MultiARData arData = arManager.GetARData();

        if (arManager.usePointCloudData)
        {
            if (Frame.PointCloud.PointCount > 0 && Frame.PointCloud.IsUpdatedThisFrame)
            {
                // Copy the point cloud points
                for (int i = 0; i < Frame.PointCloud.PointCount; i++)
                {
                    PointCloudPoint point = Frame.PointCloud.GetPointAsStruct(i);
                    arData.pointCloudData[i] = new Vector3(point.Position.x, point.Position.y, point.Position.z);
                }

                arData.pointCloudLength    = Frame.PointCloud.PointCount;
                arData.pointCloudTimestamp = lastFrameTimestamp;
            }
        }

//		// display the tracked planes if needed
//		if(arManager.displayTrackedSurfaces && trackedPlanePrefab)
//		{
//			// get the new planes
//			Frame.GetNewPlanes(ref newTrackedPlanes);
//
//			// Iterate over planes found in this frame and instantiate corresponding GameObjects to visualize them.
//			for (int i = 0; i < newTrackedPlanes.Count; i++)
//			{
//				// Instantiate a plane visualization prefab and set it to track the new plane.
//				GameObject planeObject = Instantiate(trackedPlanePrefab, Vector3.zero, Quaternion.identity);
//				planeObject.GetComponent<GoogleARCore.HelloAR.TrackedPlaneVisualizer>().SetTrackedPlane(newTrackedPlanes[i]);
//
//				// Apply a random color and grid rotation.
//				planeObject.GetComponent<Renderer>().material.SetColor("_GridColor", planeColors[Random.Range(0, planeColors.Length - 1)]);
//				planeObject.GetComponent<Renderer>().material.SetFloat("_UvRotation", Random.Range(0.0f, 360.0f));
//			}
//		}

        // get all tracked planes
        Session.GetTrackables <DetectedPlane>(allTrackedPlanes, TrackableQueryFilter.All);

        // create overlay surfaces as needed
        if (arManager.useOverlaySurface != MultiARManager.SurfaceRenderEnum.None)
        {
            alSurfacesToDelete.Clear();
            alSurfacesToDelete.AddRange(arData.dictOverlaySurfaces.Keys);

            // estimate the material
            Material surfaceMat   = arManager.GetSurfaceMaterial();
            int      surfaceLayer = MultiARInterop.GetSurfaceLayer();

            for (int i = 0; i < allTrackedPlanes.Count; i++)
            {
                string surfId = allTrackedPlanes[i].m_TrackableNativeHandle.ToString();

                if (!arData.dictOverlaySurfaces.ContainsKey(surfId))
                {
                    GameObject overlaySurfaceObj = new GameObject();
                    overlaySurfaceObj.name = "surface-" + surfId;

                    overlaySurfaceObj.layer = surfaceLayer;
                    overlaySurfaceObj.transform.SetParent(arData.surfaceRendererRoot ? arData.surfaceRendererRoot.transform : null);

//					GameObject overlayCubeObj = GameObject.CreatePrimitive(PrimitiveType.Cube);
//					overlayCubeObj.name = "surface-cube-" + surfId;
//					overlayCubeObj.transform.localScale = new Vector3(0.2f, 0.2f, 0.2f);
//					overlayCubeObj.transform.SetParent(overlaySurfaceObj.transform);

                    OverlaySurfaceUpdater overlaySurface = overlaySurfaceObj.AddComponent <OverlaySurfaceUpdater>();
                    overlaySurface.SetSurfaceMaterial(surfaceMat);
                    overlaySurface.SetSurfaceCollider(arManager.surfaceCollider, arManager.colliderMaterial);

                    arData.dictOverlaySurfaces.Add(surfId, overlaySurface);
                }

                // update the surface mesh
                bool bValidSurface = UpdateOverlaySurface(arData.dictOverlaySurfaces[surfId], allTrackedPlanes[i]);

                if (bValidSurface && alSurfacesToDelete.Contains(surfId))
                {
                    alSurfacesToDelete.Remove(surfId);
                }
            }

            // delete not tracked surfaces
            foreach (string surfId in alSurfacesToDelete)
            {
                OverlaySurfaceUpdater overlaySurface = arData.dictOverlaySurfaces[surfId];
                arData.dictOverlaySurfaces.Remove(surfId);

                Destroy(overlaySurface.gameObject);
            }
        }

        // check status of the anchors
        List <string> alAnchorsToRemove = new List <string>();

        foreach (string anchorId in arData.allAnchorsDict.Keys)
        {
            List <GameObject> anchoredObjs = arData.allAnchorsDict[anchorId];

            foreach (GameObject anchoredObj in anchoredObjs)
            {
                Transform parentTrans = anchoredObj.transform.parent;

                if (parentTrans == null)
                {
                    if (!alAnchorsToRemove.Contains(anchorId))
                    {
                        alAnchorsToRemove.Add(anchorId);
                    }
                    anchoredObj.SetActive(false);
                }
                else
                {
                    Anchor anchor = parentTrans.GetComponent <Anchor>();

                    if (anchor == null || anchor.TrackingState == TrackingState.Stopped)
                    {
                        if (!alAnchorsToRemove.Contains(anchorId))
                        {
                            alAnchorsToRemove.Add(anchorId);
                        }

                        anchoredObj.transform.parent = null;
                        anchoredObj.SetActive(false);
                    }
                }
            }
        }

        // remove the stopped anchors from our list
        foreach (string anchorId in alAnchorsToRemove)
        {
            arData.allAnchorsDict.Remove(anchorId);
        }

        // clean up
        alAnchorsToRemove.Clear();

        // look for image anchors, if enabled
        if (arData.imageAnchorsEnabled)
        {
            // Get updated augmented images for this frame.
            Session.GetTrackables <AugmentedImage>(alTrackedAugmentedImages, TrackableQueryFilter.Updated);

            foreach (var image in alTrackedAugmentedImages)
            {
                string sImageName      = image.Name;
                bool   wasImageTracked = dictImageAnchors.ContainsKey(sImageName);

                if (!wasImageTracked && image.TrackingState == TrackingState.Tracking)
                {
                    // Create an anchor to ensure that ARCore keeps tracking this augmented image.
                    Anchor anchor = image.CreateAnchor(image.CenterPose);
                    anchor.gameObject.name = "ImageAnchor-" + sImageName;
                    DontDestroyOnLoad(anchor.gameObject);

                    alImageAnchorNames.Add(sImageName);
                    dictImageAnchors.Add(sImageName, anchor.gameObject);
                }
                else if (wasImageTracked && image.TrackingState == TrackingState.Stopped)
                {
                    // remove the anchor
                    GameObject anchorObj = dictImageAnchors[sImageName];

                    alImageAnchorNames.Remove(sImageName);
                    dictImageAnchors.Remove(sImageName);

                    GameObject.Destroy(anchorObj);
                }
            }
        }
    }
Ejemplo n.º 26
0
    // -- // -- // -- // -- // -- // -- // -- // -- // -- // -- //

    void Start()
    {
        if (!isInterfaceEnabled)
        {
            return;
        }

        if (!metaCameraRigPrefab)
        {
            Debug.LogError("Meta2-interface cannot start: MetaCameraRig-prefab is not set.");
            return;
        }

        // disable the main camera, if any
        Camera currentCamera = MultiARInterop.GetMainCamera();

        if (currentCamera)
        {
            currentCamera.gameObject.SetActive(false);
        }

        // create ARCore-Device in the scene
        GameObject arCoreDeviceObj = Instantiate(metaCameraRigPrefab, Vector3.zero, Quaternion.identity);

        arCoreDeviceObj.name = "ARCore Device";
        DontDestroyOnLoad(arCoreDeviceObj);

        // reference to the AR main camera
        mainCamera = arCoreDeviceObj.GetComponentInChildren <Camera>();

//		// add camera parent
//		if(currentCamera.transform.parent == null)
//		{
//			GameObject cameraParent = new GameObject("CameraParent");
//			currentCamera.transform.SetParent(cameraParent.transform);
//		}

        // modify the directional light
        Light currentLight = MultiARInterop.GetDirectionalLight();

        if (!currentLight)
        {
            GameObject currentLightObj = new GameObject("Directional light");

            currentLight      = currentLightObj.AddComponent <Light>();
            currentLight.type = LightType.Directional;
        }

        // reset light position & rotation
        currentLight.transform.position = Vector3.zero;
        currentLight.transform.rotation = Quaternion.Euler(40f, 40f, 0f);

        // set light parameters
        currentLight.color = new Color32(255, 254, 244, 255);

        // add the ar-light component
        //currentLight.gameObject.AddComponent<MultiARDirectionalLight>();

        // reference to the AR directional light
        //directionalLight = currentLight;

        // don't destroy the light between scenes
        DontDestroyOnLoad(currentLight.gameObject);

        // there is no point cloud in WinMR
        MultiARInterop.MultiARData arData = arManager.GetARData();

        // check for point cloud getter
        if (arManager.pointCloudPrefab != null)
        {
            arData.pointCloudData      = new Vector3[0];
            arData.pointCloudLength    = 0;
            arData.pointCloudTimestamp = 0.0;
        }

        // set tracking state
        cameraTrackingState = WorldManager.state;
        WorldManager.OnPositionalLocatorStateChanged += WorldManager_OnPositionalLocatorStateChanged;

//		// set tracking space type
//		Debug.Log("Before: " + XRDevice.GetTrackingSpaceType());
//		if(XRDevice.GetTrackingSpaceType() != TrackingSpaceType.Stationary)
//		{
//			if(!XRDevice.SetTrackingSpaceType(TrackingSpaceType.Stationary))
//			{
//				Debug.LogError("Cannot set stationary space type!");
//			}
//		}

        // create gesture input
        if (!isDisplayOpaque)
        {
            gestureRecognizer = new GestureRecognizer();
            gestureRecognizer.SetRecognizableGestures(GestureSettings.Tap | GestureSettings.Hold |
                                                      GestureSettings.NavigationX | GestureSettings.NavigationY | GestureSettings.NavigationZ);

            gestureRecognizer.Tapped += GestureRecognizer_Tapped;

//			gestureRecognizer.HoldStarted += GestureRecognizer_HoldStarted;
//			gestureRecognizer.HoldCompleted += GestureRecognizer_HoldCompleted;
//			gestureRecognizer.HoldCanceled += GestureRecognizer_HoldCanceled;

            gestureRecognizer.NavigationStarted   += GestureRecognizer_NavigationStarted;
            gestureRecognizer.NavigationUpdated   += GestureRecognizer_NavigationUpdated;
            gestureRecognizer.NavigationCompleted += GestureRecognizer_NavigationCompleted;
            gestureRecognizer.NavigationCanceled  += GestureRecognizer_NavigationCanceled;

            gestureRecognizer.StartCapturingGestures();
            Debug.Log("Gesture recognizer inited and started.");
        }
        else
        {
            InteractionManager.InteractionSourcePressed  += InteractionManager_InteractionSourcePressed;
            InteractionManager.InteractionSourceUpdated  += InteractionManager_InteractionSourceUpdated;
            InteractionManager.InteractionSourceReleased += InteractionManager_InteractionSourceReleased;
            Debug.Log("Interaction manager inited.");
        }

        // create surface renderer
        if (arManager.useOverlaySurface != MultiARManager.SurfaceRenderEnum.None)
        {
            GameObject objRenderer = new GameObject();
            objRenderer.name           = "SurfaceRenderer";
            objRenderer.layer          = MultiARInterop.GetSurfaceLayer();
            arData.surfaceRendererRoot = objRenderer;

            surfaceRootTransform = objRenderer.transform;
            DontDestroyOnLoad(objRenderer);

            if (!isDisplayOpaque)
            {
                // hololens
                surfaceRenderer = objRenderer.AddComponent <SpatialMappingRenderer>();
                surfaceRenderer.surfaceParent = objRenderer;

                surfaceRenderer.renderState = (SpatialMappingRenderer.RenderState)arManager.useOverlaySurface;

                if (arManager.useOverlaySurface != MultiARManager.SurfaceRenderEnum.None)
                {
                    surfaceRenderer.visualMaterial    = arManager.surfaceVisualizationMaterial;
                    surfaceRenderer.occlusionMaterial = arManager.surfaceOcclusionMaterial;
                }
            }
            else
            {
                // mr headsets
                CreateBoundaryPlane(objRenderer.transform, arManager.GetSurfaceMaterial(),
                                    arManager.surfaceCollider, arManager.colliderMaterial);

                boundaryMgr           = objRenderer.AddComponent <HoloToolkit.Unity.Boundary.BoundaryManager>();
                boundaryMgr.FloorQuad = boundaryPlane;
                boundaryMgr.AwakeBoundaryManager();
            }
        }

        // create surface collider
        if (arManager.surfaceCollider)
        {
            GameObject objCollider = new GameObject();
            objCollider.name  = "SurfaceCollider";
            objCollider.layer = MultiARInterop.GetSurfaceLayer();
            DontDestroyOnLoad(objCollider);

            if (!isDisplayOpaque)
            {
                // hololens
                surfaceCollider = objCollider.AddComponent <SpatialMappingCollider>();
                surfaceCollider.surfaceParent = objCollider;

                surfaceCollider.lodType = SpatialMappingBase.LODType.Low;
                surfaceCollider.layer   = MultiARInterop.GetSurfaceLayer();

                if (arManager.colliderMaterial)
                {
                    surfaceCollider.material = arManager.colliderMaterial;
                }
            }
            else
            {
                // mr headsets
                if (boundaryPlane == null)
                {
                    // there was no boundary rendering
                    CreateBoundaryPlane(objCollider.transform, null, true, arManager.colliderMaterial);

                    boundaryMgr           = objCollider.AddComponent <HoloToolkit.Unity.Boundary.BoundaryManager>();
                    boundaryMgr.FloorQuad = boundaryPlane;
                    boundaryMgr.AwakeBoundaryManager();
                }
            }
        }

//		// if camera is too near to the floor, lower the floor 1.5 meter below the camera
//		if(currentCamera && boundaryMgr)
//		{
//			if(currentCamera.transform.position.y < 0.1f)
//			{
//				boundaryMgr.CurrentFloorHeightOffset = currentCamera.transform.position.y - 1.5f;
//				Debug.Log(string.Format("FloorHeightOffset set below the camera at {0:F2}m.", boundaryMgr.CurrentFloorHeightOffset));
//			}
//		}

        // starts co-routine to check rendered surfaces
        StartCoroutine(CheckSurfacesRoutine());

        Debug.Log("TrackingSpaceType: " + XRDevice.GetTrackingSpaceType());
        Debug.Log("Screen size: " + Screen.width + " x " + Screen.height);

        int surfaceLayer = MultiARInterop.GetSurfaceLayer();          // LayerMask.NameToLayer("SpatialSurface");

        Debug.Log("SpatialSurfaceLayer: " + surfaceLayer);

        // interface is initialized
        isInitialized = true;
    }