void Update() { // If world center is target then tracking pose should be set to main camera's transform if (worldCenterMode == WorldCenterMode.TARGET) { TrackingState trackingState = TrackerManager.GetInstance().GetTrackingState(); TrackingResult trackingResult = trackingState.GetTrackingResult(); if (trackingResult.GetCount() > 0) { Trackable trackable = trackingResult.GetTrackable(0); Matrix4x4 targetPose = trackable.GetTargetPose().inverse; if (targetPose == Matrix4x4.zero) { return; } Quaternion rotation = Quaternion.Euler(90, 0, 0); Matrix4x4 m = Matrix4x4.TRS(new Vector3(0, 0, 0), rotation, new Vector3(1, 1, 1)); targetPose = m * targetPose; Camera.main.transform.position = MatrixUtils.PositionFromMatrix(targetPose); Camera.main.transform.rotation = MatrixUtils.QuaternionFromMatrix(targetPose); Camera.main.transform.localScale = MatrixUtils.ScaleFromMatrix(targetPose); } } }
public void FindImageOfCloudRecognition() { if (autoState) { return; } new Thread(() => { TrackingState trackingState = TrackerManager.GetInstance().GetTrackingState(); TrackingResult trackingResult = trackingState.GetTrackingResult(); TrackedImage trackedImage = trackingState.GetImage(); int trackingCount = trackingResult.GetCount(); if (trackingCount == 0) { if (cloudState == CloudState.CLOUDSTATE_TRACKING || cloudState == CloudState.CLOUDSTATE_STOP) { cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; } } else { cloudState = CloudState.CLOUDSTATE_TRACKING; } if (cloudState != CloudState.CLOUDSTATE_FEATURE_COLLECT_READY) { return; } GetCloudRecognition(trackedImage, (bool cloudResult, string featureBase64) => { if (cloudResult) { this.featureBase64 = featureBase64; this.cloudState = CloudState.CLOUDSTATE_CONNECT; } else { this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; } if (this.restart) { this.loopState = true; this.restart = false; } }); }).Start(); }
internal void StartCloud() { loopState = true; this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; while (this.loopState) { Thread.Sleep(100); TrackingState trackingState = TrackerManager.GetInstance().UpdateTrackingState(); TrackingResult trackingResult = trackingState.GetTrackingResult(); TrackedImage trackedImage = trackingState.GetImage(); int trackingCount = trackingResult.GetCount(); if (trackingCount == 0) { if (cloudState == CloudState.CLOUDSTATE_TRACKING) { cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; } } else { cloudState = CloudState.CLOUDSTATE_TRACKING; } if (trackingCount == 0 && (cloudState == CloudState.CLOUDSTATE_TRACKING || cloudState == CloudState.CLOUDSTATE_FEATURE_COLLECT_READY)) { if (!TrackerManager.GetInstance().IsTrackerDataLoadCompleted() || cloudState == CloudState.CLOUDSTATE_STOP) { continue; } isGetFeatureState = true; bool isCameraMove = CameraDevice.GetInstance().CheckCameraMove(trackedImage); isGetFeatureState = false; if (isCameraMove) { GetCloudRecognition(trackedImage, (bool cloudResult, string featureBase64) => { if (cloudResult) { this.featureBase64 = featureBase64; this.cloudState = CloudState.CLOUDSTATE_CONNECT; } else { this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; } if (this.restart) { this.loopState = true; this.restart = false; } cloudSemaphore.Release(); }); cloudSemaphore.WaitOne(); } } if (this.cloudState == CloudState.CLOUDSTATE_STOP) { this.loopState = false; } } }
private void Update() { if (cloudState == CloudState.CLOUDSTATE_CONNECT) { cloudState = CloudState.CLOUDSTATE_CONNECTING; CloudRecognitionAPIController.Instance.Recognize(this.secretId, this.secretKey, featureBase64, (recognitionResult) => { if (this.restart == true && recognitionResult == null) { this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; return; } CloudRecognitionData cloudRecognitionData = null; try { cloudRecognitionData = JsonReader.Deserialize <CloudRecognitionData>(recognitionResult); } catch (Exception ex) { Debug.Log("error"); this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; return; } if (recognitionResult != null && cloudRecognitionData.ImgId != "" && cloudRecognitionData.ImgGSUrl != "") { Debug.Log(recognitionResult); string fileName = Path.GetFileName(cloudRecognitionData.ImgGSUrl); CloudRecognitionAPIController.Instance.DownloadCloudDataAndSave(cloudRecognitionData.ImgGSUrl, fileName, (localPath) => { if (this.restart == true) { this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; return; } if (localPath == null) { this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; return; } TrackingState trackingState = TrackerManager.GetInstance().GetTrackingState(); TrackingResult trackingResult = trackingState.GetTrackingResult(); if (trackingResult.GetCount() > 0) { cloudState = CloudState.CLOUDSTATE_TRACKING; } else { string mapDirectory = Path.GetDirectoryName(localPath); string mapFilePath = mapDirectory + "/" + Path.GetFileNameWithoutExtension(localPath) + ".2dmap"; string imageFilePath = localPath; string customToBase64 = ""; if (cloudRecognitionData.Custom != null) { byte[] customToByteArray = Encoding.UTF8.GetBytes(cloudRecognitionData.Custom); customToBase64 = Convert.ToBase64String(customToByteArray); } string command = ""; if (File.Exists(mapFilePath)) { command = "{\"cloud\":\"add_image\",\"cloud_2dmap_path\":\"" + mapFilePath + "\",\"image_width\":" + cloudRecognitionData.RealWidth + ",\"cloud_name\":\"" + cloudRecognitionData.Name + "\",\"cloud_meta\":\"" + customToBase64 + "\"}"; } else { command = "{\"cloud\":\"add_image\",\"cloud_image_path\":\"" + imageFilePath + "\",\"output_path\":\"" + mapFilePath + "\",\"image_width\":" + cloudRecognitionData.RealWidth + ",\"cloud_name\":\"" + cloudRecognitionData.Name + "\",\"cloud_meta\":\"" + customToBase64 + "\"}"; } if (this.restart == true || command == "") { this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; return; } TrackerManager.GetInstance().StartTracker(TrackerManager.TRACKER_TYPE_IMAGE); CloudRecognizerCache.GetInstance().ADD(fileName, command); CloudRecognizerCache.GetInstance().LOAD(); cloudState = CloudState.CLOUDSTATE_TRACKING; } }); } else { this.cloudState = CloudState.CLOUDSTATE_FEATURE_COLLECT_READY; } }); } }