//---- Controller Interop --- private void ControllerRegisterAndStart() { #if UNITY_EDITOR simPoseController = new BESimulatorPoseController(); simPoseController.trackerUpdateCallback = BridgeEngineUnity.TrackerUpdateCallback; simPoseController.controllerMotionEventCallback = BridgeEngineUnity.ControllerMotionEventCallback; simPoseController.controllerButtonEventCallback = BridgeEngineUnity.ControllerButtonEventCallback; simPoseController.controllerTouchEventCallback = BridgeEngineUnity.ControllerTouchEventCallback; simPoseController.StartWithMainCameraTransform(mainCamera.transform); #endif // Register for controller changes #if UNITY_EDITOR // When in Unity Simulator, don't use the shared controller so it releases after play. BridgeEngineUnityInterop.beControllerInit(false); #else // When on Device, use the shared controller because BE got it first and we need to use the same. BridgeEngineUnityInterop.beControllerInit(true); #endif BridgeEngineUnityInterop.be_registerControllerEventDidConnectCallback(BridgeEngineUnity.ControllerDidConnectCallback); BridgeEngineUnityInterop.be_registerControllerEventDidDisconnectCallback(BridgeEngineUnity.ControllerDidDisconnectCallback); BridgeEngineUnityInterop.be_registerControllerMotionEventCallback(BridgeEngineUnity.ControllerMotionEventCallback); BridgeEngineUnityInterop.be_registerControllerButtonEventCallback(BridgeEngineUnity.ControllerButtonEventCallback); BridgeEngineUnityInterop.be_registerControllerTouchEventCallback(BridgeEngineUnity.ControllerTouchEventCallback); }
IEnumerator HideOverlayAfterSplash() { while (!SplashScreen.isFinished) { SplashScreen.Draw(); yield return(null); } BridgeEngineUnityInterop.be_hideOverlay(); }
/** * Receive the 6DOF tracker updates from BridgeEngine. */ void TrackerUpdate(ref BETrackerUpdateInterop interop) { lastTrackerUpdate = interop; if (mainCamera != null) { #if !UNITY_EDITOR mainCamera.projectionMatrix = lastTrackerUpdate.cameraProj.ToMatrix(); // uses the intrinsics from Bridge Engine to get a correct projection matrix #endif mainCamera.transform.position = lastTrackerUpdate.pos.ToVector3(); mainCamera.transform.rotation = lastTrackerUpdate.rot.ToQuaternion(); mainCamera.transform.localScale = lastTrackerUpdate.scale.ToVector3(); } if (isStereoModeActive && stereoCamera != null) { stereoCamera.UpdateStereoSetup(lastTrackerUpdate.stereoSetup); } // Update the BEController's cameraTransform, from our latest mainCamera tracking. Matrix4x4 unityCameraWorldMatrix = mainCamera.transform.localToWorldMatrix; beMatrix4 beCameraWorldMatrix = new beMatrix4(); beCameraWorldMatrix.Set(unityCameraWorldMatrix); BridgeEngineUnityInterop.beControllerUpdateCamera(beCameraWorldMatrix); // Update the listeners if pose accuracy changes. bool poseAccuracyChanged = lastTrackerUpdate.trackerPoseAccuracy != interop.trackerPoseAccuracy; if (poseAccuracyChanged && onPoseAccuracyChanged != null) { onPoseAccuracyChanged.Invoke(interop.trackerPoseAccuracy); } if (onPoseChanged != null) { onPoseChanged.Invoke(); } // Refresh the camera texture last, so we have an opportunity to update the color samplers. CameraTextureUpdate(ref lastTrackerUpdate.cameraTextureInfo); }
/** * Detect if there's a duplicate instance of BridgeEngineUnity main script loaded, * as this could seriousely muck-up interop to BE. * Set up the camera near and far clipping planes, and register the interop callbacks. */ IEnumerator Start() { #if !UNITY_EDITOR // Bridge Engine is guaranteed to be ready on start. // Immediately load the mesh, only if we have a BEScene to load them into. if (BEScene.IsInScene()) { BridgeEngineUnityInterop.be_loadMeshes(ScanMeshCallback); } else { Debug.Log("No @BridgeEngineScene present, skipping loading world meshes"); } #endif if (isStereoModeActive) { stereoCamera = mainCamera.gameObject.AddComponent <BEStereoCamera>(); } else { monoCamera = mainCamera.gameObject.AddComponent <BEMonoCamera>(); } Application.targetFrameRate = 60; lastTrackerUpdate.trackerPoseAccuracy = BETrackerPoseAccuracy.Uninitialized; // Configure the Tracking UIs BridgeEngineUnity instance. var lostTrackingBillboard = gameObject.GetComponentInChildren <BELostTrackingBillboard>(includeInactive: true); if (lostTrackingBillboard) { lostTrackingBillboard.beUnity = this; } // Register for callbacks. RegisterTrackerCallback(); ControllerRegisterAndStart(); return(HideOverlayAfterSplash()); }
void Awake() { _scaleBiasMatrixColor_.SetRow(0, new Vector4(/*c1*/ 0.5f, 0.0f, 0.0f, 0.5f)); _scaleBiasMatrixColor_.SetRow(1, new Vector4(/*c2*/ 0.0f, 0.5f, 0.0f, 0.5f)); _scaleBiasMatrixColor_.SetRow(2, new Vector4(/*c3*/ 0.0f, 0.0f, 0.5f, 0.5f)); _scaleBiasMatrixColor_.SetRow(3, new Vector4(/*c4*/ 0.0f, 0.0f, 0.0f, 1.0f)); UnityEngine.XR.XRSettings.enabled = false; // Force VR mode off, because BridgeEngine will drive it with our own camera rig. Screen.sleepTimeout = SleepTimeout.NeverSleep; // Never sleep for MR/VR. We use controller input. #if UNITY_EDITOR isStereoModeActive = stereoInEditor; #else isStereoModeActive = BridgeEngineUnityInterop.be_isStereoMode(); #endif // Relocate the LostTrackingCanvas onto the MainCamera. mainCamera = GetMainCamera(); Assert.IsNotNull(mainCamera, "BridgeEngineUnity is missing main camera!"); var lostTrackingBillboard = gameObject.GetComponentInChildren <BELostTrackingBillboard>(includeInactive: true); Assert.IsNotNull(lostTrackingBillboard, "BridgeEngineUnity is missing a child LostTrackingBillboard for announcing tracking loss"); if (mainCamera && lostTrackingBillboard) { // We leave the lostTrackingCanvas disabled by default, so it doesn't interfere with the user's work. lostTrackingBillboard.gameObject.SetActive(true); // Inherit the camera's local Z for placing the canvas plane, but no closer than 0.3 so it's a good distance. float visibleNearZ = mainCamera.nearClipPlane + 0.01f; var canvasLocalPosition = lostTrackingBillboard.transform.transform.localPosition; canvasLocalPosition.z = Math.Max(visibleNearZ, 0.30f); lostTrackingBillboard.transform.transform.localPosition = canvasLocalPosition; lostTrackingBillboard.transform.SetParent(mainCamera.transform, false); // Reparent, keeping local transform. } }
/** * Called when mesh i / n is scanned, so we can re-create it in Unity */ void ScannedMeshTransfer(int meshIndex, int meshCount, int verticesCount, IntPtr positions, IntPtr normals, IntPtr colors, IntPtr uvs, int indiciesCount, IntPtr indicies16) { string desc = string.Format("Scanned Object {0}/{1}", meshIndex + 1, meshCount); // Debug.LogFormat("ScannedMeshTransfer. {0} / {1}. verticesCount = {2}. indiciesCount = {3}", meshIndex, meshCount, verticesCount, indiciesCount); if (meshCount <= 0) { return; } if (meshIndex >= meshCount) { return; } // Make sure we have a valid BEScene object to attach the meshes to. var beSceneObject = BEScene.FindBEScene(); // We expect a valid scene, or something's wrong. if (beSceneObject == null) { Debug.LogFormat("BridgeEngineScene not found, couldn't load mesh \"{0}\"", desc); return; } var meshInTransfer = new Mesh(); // strings allocate string meshDesc = "[Mesh]" + desc; meshInTransfer.name = meshDesc; meshInTransfer.subMeshCount = 1; meshInTransfer.vertices = BridgeEngineUnityInterop.GetNativeArray <Vector3>(positions, verticesCount); int[] indxArray = BridgeEngineUnityInterop.GetNativeIndxArray(indicies16, indiciesCount); bool useForRendering = true; if (useForRendering) { meshInTransfer.normals = BridgeEngineUnityInterop.GetNativeArray <Vector3>(normals, verticesCount); meshInTransfer.colors = BridgeEngineUnityInterop.GetNativeArray <Color>(colors, verticesCount); meshInTransfer.uv = BridgeEngineUnityInterop.GetNativeArray <Vector2>(uvs, verticesCount); if (meshInTransfer.normals == null) { meshInTransfer.RecalculateNormals(); } } meshInTransfer.SetIndices(indxArray, MeshTopology.Triangles, 0, true); var meshObject = new GameObject(desc); meshObject.transform.SetParent(beSceneObject.transform, false); // Don't do any magic with the BEScene transform. if (useForRendering) { meshObject.AddComponent <MeshFilter>().sharedMesh = meshInTransfer; meshObject.AddComponent <MeshRenderer>(); } // Make sure the mesh has physics MeshCollider collider = meshObject.GetComponent <MeshCollider>() ?? meshObject.AddComponent <MeshCollider>(); collider.sharedMesh = meshInTransfer; // transfer complete if (meshIndex == meshCount - 1) { beSceneObject.ApplyCorrectMaterialAndLayerSetting(); Debug.Log("ScannedMeshTransfer. Complete, applying materials"); } }
public bool isBridgeControllerConnected() { return(BridgeEngineUnityInterop.beControllerIsBridgeControllerConnected()); }
void ControllerShutdown() { BridgeEngineUnityInterop.beControllerShutdown(); }
//---- BridgeEngine Tracker Interop --- private void RegisterTrackerCallback() { BridgeEngineUnityInterop.be_registerTrackerEventCallback(BridgeEngineUnity.TrackerUpdateCallback); }