// 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."); }
// -- // -- // -- // -- // -- // -- // -- // -- // -- // -- // 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 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; }
// -- // -- // -- // -- // -- // -- // -- // -- // -- // -- // 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; }