// Updates arVisible, arPosition, arRotation based on linked marker state. private void UpdateTracking() { // Note the current time timeLastUpdate = Time.realtimeSinceStartup; // First, ensure we have a base marker. If none, then no markers are currently in view. ARMarker marker = GetMarker(); if (marker == null) { markerInsight = false; if (arVisible) { // Marker was visible but now is hidden. timeTrackingLost = timeLastUpdate; arVisible = false; } } else { markerInsight = true; if (marker.Visible) { Matrix4x4 pose; if (Optical && opticalSetupOK) { pose = (opticalViewMatrix * marker.TransformationMatrix).inverse; } else { pose = marker.TransformationMatrix.inverse; } arPosition = ARUtilityFunctions.PositionFromMatrix(pose); // Camera orientation: In ARToolKit, zero rotation of the camera corresponds to looking vertically down on a marker // lying flat on the ground. In Unity however, if we still treat markers as being flat on the ground, we clash with Unity's // camera "rotation", because an unrotated Unity camera is looking horizontally. // So we choose to treat an unrotated marker as standing vertically, and apply a transform to the scene to // to get it to lie flat on the ground. arRotation = ARUtilityFunctions.QuaternionFromMatrix(pose); if (!arVisible) { // Marker was hidden but now is visible. arVisible = true; } } else { if (arVisible) { // Marker was visible but now is hidden. timeTrackingLost = timeLastUpdate; arVisible = false; } } } }
public override void Start() { base.Start(); Matrix4x4 targetInWorldFrame = targetObject.transform.localToWorldMatrix; Matrix4x4 targetInCameraFrame = this.gameObject.GetComponent <Camera>().transform.parent.worldToLocalMatrix *targetInWorldFrame; vrTargetPosition = ARUtilityFunctions.PositionFromMatrix(targetInCameraFrame); vrTargetRotation = ARUtilityFunctions.QuaternionFromMatrix(targetInCameraFrame); vrObserverAzimuth = vrObserverElevation = 0.0f; // VR mode starts pointing in direction specified by the axes of the target. vrObserverOffset = Vector3.zero; }
List <Vector4> getMeanPos() { List <Vector4> p = new List <Vector4>(); int ct = 0; Vector3 tpos = new Vector3(0.0f, 0.0f, 0.0f); Vector4 trot = new Vector4(0.0f, 0.0f, 0.0f, 0.0f); int i = 0; for (i = 0; i < markers.Length; i++) { if (!markers[i].Visible) { continue; } ct++; Matrix4x4 pose = markers[i].TransformationMatrix; Vector3 position = ARUtilityFunctions.PositionFromMatrix(pose); Quaternion orientation = ARUtilityFunctions.QuaternionFromMatrix(pose); Vector4 ori = new Vector4(orientation [0], orientation [1], orientation [2], orientation[3]); tpos += position; trot += ori; } if (ct == 0) { return(p); } Debug.Log(ct); tpos /= ct; trot /= ct; p.Add(new Vector4(tpos[0], tpos[1], tpos[2], 0.0f)); p.Add(trot); return(p); }
// Use LateUpdate to be sure the ARMarker has updated before we try and use the transformation. void LateUpdate() { // Local scale is always 1 for now transform.localScale = Vector3.one; // Update tracking if we are running in the Player. if (Application.isPlaying) { // Sanity check, make sure we have an AROrigin in parent hierachy. AROrigin origin = GetOrigin(); if (origin == null) { ARController.Log(LogTag + "No Origin"); //visible = visibleOrRemain = false; } else { // Sanity check, make sure we have an ARMarker assigned. ARMarker marker = GetMarker(); if (marker == null) { ARController.Log(LogTag + "No ARMarker"); //visible = visibleOrRemain = false; } else { // Note the current time float timeNow = Time.realtimeSinceStartup; ARMarker baseMarker = origin.GetBaseMarker(); if (baseMarker != null && marker.Visible) { if (!visible) { // Marker was hidden but now is visible. ARController.Log(LogTag + "Marker was hidden but now is visible."); visible = visibleOrRemain = true; if (eventReceiver != null) { eventReceiver.BroadcastMessage("OnMarkerFound", marker, SendMessageOptions.DontRequireReceiver); } for (int i = 0; i < this.transform.childCount; i++) { this.transform.GetChild(i).gameObject.SetActive(true); } } else { // ARController.Log (LogTag + "Marker stayed visible"); } Matrix4x4 pose; if (marker == baseMarker) { // If this marker is the base, no need to take base inverse etc. pose = origin.transform.localToWorldMatrix; } else { pose = (origin.transform.localToWorldMatrix * baseMarker.TransformationMatrix.inverse * marker.TransformationMatrix); } transform.position = ARUtilityFunctions.PositionFromMatrix(pose); transform.rotation = ARUtilityFunctions.QuaternionFromMatrix(pose); if (eventReceiver != null) { eventReceiver.BroadcastMessage("OnMarkerTracked", marker, SendMessageOptions.DontRequireReceiver); } } else { if (visible) { // Marker was visible but now is hidden. ARController.Log(LogTag + "Marker was visible but now is hidden. (after " + secondsToRemainVisible + "s)"); visible = false; timeTrackingLost = timeNow; } else { // ARControllertroller.Log (LogTag + "Marker stayed hidden."); } if (visibleOrRemain && (timeNow - timeTrackingLost >= secondsToRemainVisible)) { visibleOrRemain = false; if (eventReceiver != null) { eventReceiver.BroadcastMessage("OnMarkerLost", marker, SendMessageOptions.DontRequireReceiver); } for (int i = 0; i < this.transform.childCount; i++) { this.transform.GetChild(i).gameObject.SetActive(false); } } } } // marker } // origin } // Application.isPlaying else { ARController.Log(LogTag + "Applicaiton Not Playing"); } }
// Use LateUpdate to be sure the ARTrackable has updated before we try and use the transformation. void LateUpdate() { // Local scale is always 1 for now transform.localScale = Vector3.one; // Update tracking if we are running in the Player. if (Application.isPlaying) { // Sanity check, make sure we have an AROrigin in parent hierachy. AROrigin origin = GetOrigin(); if (origin == null) { //visible = visibleOrRemain = false; } else { // Sanity check, make sure we have an ARTrackable assigned. ARTrackable trackable = GetTrackable(); if (trackable == null) { //visible = visibleOrRemain = false; } else { // Note the current time float timeNow = Time.realtimeSinceStartup; ARTrackable baseTrackable = origin.GetBaseTrackable(); if (baseTrackable != null && trackable.Visible) { if (!visible) { // Trackable was hidden but now is visible. visible = visibleOrRemain = true; if (eventReceiver != null) { eventReceiver.BroadcastMessage("OnTrackableFound", trackable, SendMessageOptions.DontRequireReceiver); } for (int i = 0; i < this.transform.childCount; i++) { this.transform.GetChild(i).gameObject.SetActive(true); } } Matrix4x4 pose; if (trackable == baseTrackable) { // If this marker is the base, no need to take base inverse etc. pose = origin.transform.localToWorldMatrix; } else { pose = (origin.transform.localToWorldMatrix * baseTrackable.TransformationMatrix.inverse * trackable.TransformationMatrix); } transform.position = ARUtilityFunctions.PositionFromMatrix(pose); transform.rotation = ARUtilityFunctions.QuaternionFromMatrix(pose); if (eventReceiver != null) { eventReceiver.BroadcastMessage("OnTrackableTracked", trackable, SendMessageOptions.DontRequireReceiver); } } else { if (visible) { // Trackable was visible but now is hidden. visible = false; timeTrackingLost = timeNow; } if (visibleOrRemain && (timeNow - timeTrackingLost >= secondsToRemainVisible)) { visibleOrRemain = false; if (eventReceiver != null) { eventReceiver.BroadcastMessage("OnTrackableLost", trackable, SendMessageOptions.DontRequireReceiver); } for (int i = 0; i < this.transform.childCount; i++) { this.transform.GetChild(i).gameObject.SetActive(false); } } } } // marker } // origin } // Application.isPlaying }
// Display the rotaion and position part to scene void showText(Matrix4x4 curPose) // Newly added { Rotation.text = "Rotation: " + ARUtilityFunctions.QuaternionFromMatrix(curPose).ToString(); Position.text = "Position: " + ARUtilityFunctions.PositionFromMatrix(curPose).ToString(); Transformation.text = curPose.ToString(); }
// Updates arVisible, arPosition, arRotation based on linked marker state. private void UpdateTracking(Boolean write) { // Note the current time timeLastUpdate = Time.realtimeSinceStartup; // First, ensure we have a base marker. If none, then no markers are currently in view. ARMarker marker = GetMarker(); if (marker == null) { if (arVisible) { // Marker was visible but now is hidden. timeTrackingLost = timeLastUpdate; arVisible = false; } } else { if (marker.Visible) { Matrix4x4 pose; //Matrix4x4 myPose; if (Optical && opticalSetupOK) { pose = (opticalViewMatrix * marker.TransformationMatrix).inverse; // myPose is the transformation from marker to camera myPose = (opticalViewMatrix * marker.TransformationMatrix); } else { pose = marker.TransformationMatrix.inverse; // myPose is the transformation from marker to camera myPose = marker.TransformationMatrix; } // choose to treat an unrotated marker as standing vertically, and apply a transform to the scene to // to get it to lie flat on the ground. arPosition = ARUtilityFunctions.PositionFromMatrix(pose); arRotation = ARUtilityFunctions.QuaternionFromMatrix(pose); // Show the pose to Text showText(myPose); // Write the pose to txt if (write) { writeText(myPose); } // read pivot calibration from txt file string path = "C:/Users/zhaoz/Desktop/JohnsHopkins/Spring2017/CISII/F200/Pivot Calibration/pointTipNeedle.txt"; Vector4 tipMarker; tipMarker = readPivotCalibration(path); // transform the tip position from wrt marker to wrt camera tipCamera = myPose * tipMarker; // convert Vector4 tipMarker to 3D renderEndMarker.x = tipMarker.x; renderEndMarker.y = tipMarker.y; renderEndMarker.z = tipMarker.z; tipPositionCamera.text = "Tip Position: " + tipCamera.ToString(); if (!arVisible) { // Marker was hidden but still show the augmentation as it was there arVisible = true; } } else { if (arVisible) { // Marker was hidden but still show the augmentation as it was there timeTrackingLost = timeLastUpdate; arVisible = false; } } } }
// Use LateUpdate to be sure the ARMarker has updated before we try and use the transformation. void LateUpdate() { // Local scale is always 1 for now transform.localScale = Vector3.one; // Update tracking if we are running in the Player. if (Application.isPlaying) { // Sanity check, make sure we have an AROrigin in parent hierachy. AROrigin origin = GetOrigin(); if (origin == null) { //visible = visibleOrRemain = false; return; } // Sanity check, make sure we have an ARMarker assigned. ARMarker marker = GetMarker(); if (marker == null) { //visible = visibleOrRemain = false; return; } // Note the current time timeNowSeconds = Time.realtimeSinceStartup; ARMarker baseMarker = origin.GetBaseMarker(); if (baseMarker != null && marker.Visible) { if (!visible) { // Marker was hidden but now is visible. visible = visibleOrRemain = true; if (eventReceiver != null) { eventReceiver.BroadcastMessage("OnMarkerFound", marker, SendMessageOptions.DontRequireReceiver); } for (int i = 0; i < this.transform.childCount; i++) { this.transform.GetChild(i).gameObject.SetActive(true); } } Matrix4x4 pose; if (marker == baseMarker) { // If this marker is the base, no need to take base inverse etc. pose = origin.transform.localToWorldMatrix; } else { pose = (origin.transform.localToWorldMatrix * baseMarker.TransformationMatrix.inverse * marker.TransformationMatrix); } transform.position = ARUtilityFunctions.PositionFromMatrix(pose); transform.rotation = ARUtilityFunctions.QuaternionFromMatrix(pose); if (eventReceiver != null) { eventReceiver.BroadcastMessage("OnMarkerTracked", marker, SendMessageOptions.DontRequireReceiver); } OnMarkerMadeVisible(marker); } else { OnMarkerLost(marker); } } // Application.isPlaying }