// We use Update() here, but be aware that unless ARController has been configured to // execute first (Unity Editor->Edit->Project Settings->Script Execution Order) then // state produced by this update may lag by one frame. void Update() { float[] matrixRawArray = new float[16]; //ARController.Log(LogTag + "ARMarker.Update()"); if (UID == NO_ID || !PluginFunctions.inited) { visible = false; return; } // Query visibility if we are running in the Player. if (Application.isPlaying) { visible = PluginFunctions.arwQueryMarkerTransformation(UID, matrixRawArray); //ARController.Log(LogTag + "ARMarker.Update() UID=" + UID + ", visible=" + visible); if (visible) { matrixRawArray[12] *= 0.001f; // Scale the position from ARToolKit units (mm) into Unity units (m). matrixRawArray[13] *= 0.001f; matrixRawArray[14] *= 0.001f; Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray); //ARController.Log("arwQueryMarkerTransformation(" + UID + ") got matrix: [" + Environment.NewLine + matrixRaw.ToString("F3").Trim() + "]"); // ARToolKit uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of +z. // Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of -z. transformationMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw); } } }
public bool SetupCamera(float nearClipPlane, float farClipPlane, Matrix4x4 projectionMatrix, ref bool opticalOut) { Camera c = this.gameObject.GetComponent <Camera>(); // A perspective projection matrix from the tracker c.orthographic = false; // Shouldn't really need to set these, because they are part of the custom // projection matrix, but it seems that in the editor, the preview camera view // isn't using the custom projection matrix. c.nearClipPlane = nearClipPlane; c.farClipPlane = farClipPlane; if (Optical) { float fovy; float aspect; float[] m = new float[16]; float[] p = new float[16]; opticalSetupOK = PluginFunctions.arwLoadOpticalParams(null, OpticalParamsFileContents, OpticalParamsFileContents.Length, out fovy, out aspect, m, p); if (!opticalSetupOK) { ARController.Log(LogTag + "Error loading optical parameters."); return(false); } m[12] *= 0.001f; m[13] *= 0.001f; m[14] *= 0.001f; ARController.Log(LogTag + "Optical parameters: fovy=" + fovy + ", aspect=" + aspect + ", camera position (m)={" + m[12].ToString("F3") + ", " + m[13].ToString("F3") + ", " + m[14].ToString("F3") + "}"); c.projectionMatrix = ARUtilityFunctions.MatrixFromFloatArray(p); opticalViewMatrix = ARUtilityFunctions.MatrixFromFloatArray(m); if (OpticalEyeLateralOffsetRight != 0.0f) { opticalViewMatrix = Matrix4x4.TRS(new Vector3(-OpticalEyeLateralOffsetRight, 0.0f, 0.0f), Quaternion.identity, Vector3.one) * opticalViewMatrix; } // Convert to left-hand matrix. opticalViewMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(opticalViewMatrix); opticalOut = true; } else { c.projectionMatrix = projectionMatrix; } // Don't clear anything or else we interfere with other foreground cameras c.clearFlags = CameraClearFlags.Nothing; // Renders after the clear and background cameras c.depth = 2; c.transform.position = new Vector3(0.0f, 0.0f, 0.0f); c.transform.rotation = new Quaternion(0.0f, 0.0f, 0.0f, 1.0f); c.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); return(true); }
// 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; }
public ARPattern(IPluginFunctions pluginFunctions, int trackableID, int patternID) { float[] matrixRawArray = new float[16]; float widthRaw = 0.0f; float heightRaw = 0.0f; // Get the pattern local transformation and size. if (!pluginFunctions.arwGetTrackablePatternConfig(trackableID, patternID, matrixRawArray, out widthRaw, out heightRaw, out imageSizeX, out imageSizeY)) { throw new ArgumentException("Invalid argument", "markerID,patternID"); } width = widthRaw * 0.001f; height = heightRaw * 0.001f; matrixRawArray[12] *= 0.001f; // Scale the position from artoolkitX units (mm) into Unity units (m). matrixRawArray[13] *= 0.001f; matrixRawArray[14] *= 0.001f; Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray); //ARController.Log("arwGetMarkerPatternConfig(" + markerID + ", " + patternID + ", ...) got matrix: [" + Environment.NewLine + matrixRaw.ToString("F3").Trim() + "]"); // artoolkitX uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of +z. // Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of -z. matrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw); // Handle pattern image. if (imageSizeX > 0 && imageSizeY > 0) { // Allocate a new texture for the pattern image texture = new Texture2D(imageSizeX, imageSizeY, TextureFormat.RGBA32, false); texture.filterMode = FilterMode.Point; texture.wrapMode = TextureWrapMode.Clamp; texture.anisoLevel = 0; // Get the pattern image data and load it into the texture Color[] colors = new Color[imageSizeX * imageSizeY]; if (pluginFunctions.arwGetTrackablePatternImage(trackableID, patternID, colors)) { texture.SetPixels(colors); texture.Apply(); } } }
public ARPattern(int markerID, int patternID) { float[] matrixRawArray = new float[16]; float widthRaw = 0.0f; float heightRaw = 0.0f; // Get the pattern local transformation and size. if (!PluginFunctions.arwGetTrackableAppearanceConfig(markerID, patternID, matrixRawArray, out widthRaw, out heightRaw, out imageSizeX, out imageSizeY)) { throw new System.ArgumentException("Invalid argument", "markerID,patternID"); } width = widthRaw * ARTrackable.UNITY_TO_ARTOOLKIT; height = heightRaw * ARTrackable.UNITY_TO_ARTOOLKIT; // Scale the position from ARToolKit units (mm) into Unity units (m). matrixRawArray[12] *= ARTrackable.UNITY_TO_ARTOOLKIT; matrixRawArray[13] *= ARTrackable.UNITY_TO_ARTOOLKIT; matrixRawArray[14] *= ARTrackable.UNITY_TO_ARTOOLKIT; Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray); // ARToolKit uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of +z. // Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of -z. matrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw); // Handle pattern image. if (imageSizeX > 0 && imageSizeY > 0) { // Allocate a new texture for the pattern image texture = new Texture2D(imageSizeX, imageSizeY, TextureFormat.RGBA32, false); texture.filterMode = FilterMode.Point; texture.wrapMode = TextureWrapMode.Clamp; texture.anisoLevel = 0; // Get the pattern image data and load it into the texture Color32[] colors32 = new Color32[imageSizeX * imageSizeY]; if (PluginFunctions.arwGetTrackableAppearanceImage(markerID, patternID, colors32)) { texture.SetPixels32(colors32); texture.Apply(); } } }
// We use Update() here, but be aware that unless ARController has been configured to // execute first (Unity Editor->Edit->Project Settings->Script Execution Order) then // state produced by this update may lag by one frame. void Update() { // Only query visibility if we are running in the Player. if (!Application.isPlaying) { return; } lock (loadLock) { //ARController.Log(LogTag + "ARTrackable.Update()"); if (UID == NO_ID) { visible = false; return; } if (pluginFunctions == null || !pluginFunctions.IsInited()) { visible = false; return; } float[] matrixRawArray = new float[16]; visible = pluginFunctions.arwQueryTrackableVisibilityAndTransformation(UID, matrixRawArray); //ARController.Log(LogTag + "ARTrackable.Update() UID=" + UID + ", visible=" + visible); if (visible) { matrixRawArray[12] *= 0.001f; // Scale the position from artoolkitX units (mm) into Unity units (m). matrixRawArray[13] *= 0.001f; matrixRawArray[14] *= 0.001f; Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray); //.Log("arwQueryTrackableTransformation(" + UID + ") got matrix: [" + Environment.NewLine + matrixRaw.ToString("F3").Trim() + "]"); // artoolkitX uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of +z. // Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of -z. transformationMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw); } } }
// We use Update() here, but be aware that unless ARController has been configured to // execute first (Unity Editor->Edit->Project Settings->Script Execution Order) then // state produced by this update may lag by one frame. void Update() { float[] matrixRawArray = new float[16]; ARController.Log(LogTag + "ARMarker.Update()"); if (UID == NO_ID || !PluginFunctions.inited) { visible = false; return; } // Query visibility if we are running in the Player. if (Application.isPlaying) { visible = PluginFunctions.arwQueryMarkerTransformation(UID, matrixRawArray); ARController.Log(LogTag + "ARMarker.Update() UID=" + UID + ", visible=" + visible); if (visible) { matrixRawArray[12] *= 0.001f; // Scale the position from ARToolKit units (mm) into Unity units (m). matrixRawArray[13] *= 0.001f; matrixRawArray[14] *= 0.001f; Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray); //ARController.Log("arwQueryMarkerTransformation(" + UID + ") got matrix: [" + Environment.NewLine + matrixRaw.ToString("F3").Trim() + "]"); // ARToolKit uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of +z. // Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of -z. transformationMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw); // Output current position: Added by Kazu on Apr 2 2016 Vector3 position = ARUtilityFunctions.PositionFromMatrix(transformationMatrix); print("Position of Barcode ID #" + BarcodeID + ": (" + position.x * 1000 + ", " + position.y * 1000 + ", " + position.z * 1000 + ")"); // print ("position.x [mm]: "+ position.x * 1000); // print ("position.y [mm]: "+ position.y * 1000); // print ("position.z [mm]: "+ position.z * 1000); // If you need quaternion, you can use the followings. // Quaternion orientation = ARUtilityFunctions.QuaternionFromMatrix(transformationMatrix); } } }
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); }
// Update is called once per frame void Update() { foreach (var item in possiblePieces) { // Debug.Log("x: " + (Origin.transform.position.x - item.Item1.transform.position.x) + //" y: " + (Origin.transform.position.y - item.Item1.transform.position.y) + //" z: " + (Origin.transform.position.z - item.Item1.transform.position.z)); if (item.Item1.activeInHierarchy) { ARMarker myMarker = item.Item1.transform.parent.GetComponent <ARTrackedObject>().GetMarker(); Matrix4x4 pose = myMarker.TransformationMatrix; Vector3 position = ARUtilityFunctions.PositionFromMatrix(pose); //Quaternion orientation = ARUtilityFunctions.QuaternionFromMatrix(pose); //float x = position.x - distanceToOrigin[5].x; //float y = position.y - distanceToOrigin[5].y; //float z = position.z - distanceToOrigin[5].z; //Debug.Log("x: " + x + //" y: " + y + //" z: " + z); var minDistance = float.MaxValue; int minIndex = -1; for (int i = 0; i < NUMBER_OF_SQUARES; i++) { var distance = Vector3.Distance(position, piecePositionsFromCamera[i]); if (distance < minDistance) { minDistance = distance; minIndex = i; } } if (minDistance < MaxSlackDistance) { if (positions[minIndex].pieceColor != item.Item2) { if (minDistance < positions[minIndex].distance) { UpdatePosition(item.Item2, minDistance, minIndex); } } else { positions[minIndex].dateTime = DateTime.UtcNow; } } } } for (int i = 0; i < positions.Count; i++) { if (positions[i].pieceColor != PieceColor.None) { if ((DateTime.UtcNow - positions[i].dateTime).TotalMilliseconds > TimeToLoseAPieceMS) { UpdatePosition(PieceColor.None, float.MaxValue, i); } } } //for (int i = 0; i < NUMBER_OF_SQUARES; i++) //{ // if (pieces[i].Item2.MyColor == PieceColor.None) // { // bool hintExists = false; // foreach (var item in neighbours[i]) // { // if (pieces[item].Item2.MyColor != PieceColor.None) // { // hintExists = true; // hintImages[i].enabled = true; // } // } // if (!hintExists) // { // hintImages[i].enabled = false; // } // } else if(hintImages[i].enabled == true) // { // hintImages[i].enabled = false; // } //} }
// Load the underlying ARToolKit marker structure(s) and set the UID. public void Load() { lock (loadLock) { if (UID != NO_ID) { return; } if (!PluginFunctions.inited) { // If arwInitialiseAR() has not yet been called, we can't load the native trackable yet. // ARController.InitialiseAR() will call this again when arwInitialiseAR() has been called. return; } // Work out the configuration string to pass to ARToolKit. string assetDirectory = Application.streamingAssetsPath; string configuration = string.Empty; string path = string.Empty; switch (Type) { case TrackableType.TwoD: if (string.IsNullOrEmpty(TwoDImageName)) { ARController.Log(string.Format(LOAD_FAILURE, "2D image trackable due to no TwoDImageName")); return; } path = Path.Combine(TWOD_FORMAT, TwoDImageName); if (!ARUtilityFunctions.GetFileFromStreamingAssets(path, out assetDirectory)) { ARController.Log(string.Format(LOAD_FAILURE, TwoDImageName)); return; } if (!string.IsNullOrEmpty(assetDirectory)) { configuration = string.Format(TWOD_CONFIG, assetDirectory, TwoDImageHeight * ARTOOLKIT_TO_UNITY); } break; case TrackableType.Square: // Multiply width by 1000 to convert from metres to ARToolKit's millimetres. configuration = string.Format(SINGLE_BUFFER_CONFIG, PatternWidth * ARTOOLKIT_TO_UNITY, PatternContents); break; case TrackableType.SquareBarcode: // Multiply width by 1000 to convert from metres to ARToolKit's millimetres. configuration = string.Format(SINGLE_BARCODE_CONFIG, BarcodeID, PatternWidth * ARTOOLKIT_TO_UNITY); break; case TrackableType.Multimarker: if (string.IsNullOrEmpty(MultiConfigFile)) { ARController.Log(string.Format(LOAD_FAILURE, "multimarker due to no MultiConfigFile")); return; } path = Path.Combine(MULTI_FORMAT, MultiConfigFile + MULTI_EXT); ARUtilityFunctions.GetFileFromStreamingAssets(path, out assetDirectory); if (!string.IsNullOrEmpty(assetDirectory)) { configuration = string.Format(MULTI_CONFIG, assetDirectory); } break; default: // Unknown marker type? ARController.Log(string.Format(LOAD_FAILURE, "due to unknown marker")); return; } // If a valid config. could be assembled, get ARToolKit to process it, and assign the resulting ARMarker UID. if (string.IsNullOrEmpty(configuration)) { ARController.Log(LOG_TAG + "trackable configuration is null or empty."); return; } uid = PluginFunctions.arwAddMarker(configuration); if (UID == NO_ID) { ARController.Log(LOG_TAG + "Error loading trackable."); return; } // Trackable loaded. Do any additional configuration. if (Type == TrackableType.Square || Type == TrackableType.SquareBarcode) { UseContPoseEstimation = currentUseContPoseEstimation; } Filtered = currentFiltered; FilterSampleRate = currentFilterSampleRate; FilterCutoffFreq = currentFilterCutoffFreq; // Retrieve any required information from the configured ARTrackable. if (Type == TrackableType.TwoD) { int dummyImageSizeX, dummyImageSizeY; float dummyTwoDImageHeight; PluginFunctions.arwGetTrackableAppearanceConfig(UID, 0, null, out TwoDImageWidth, out dummyTwoDImageHeight, out dummyImageSizeX, out dummyImageSizeY); TwoDImageWidth *= UNITY_TO_ARTOOLKIT; } else { // Create array of patterns. A single marker will have array length 1. int numPatterns = PluginFunctions.arwGetTrackableAppearanceCount(UID); if (numPatterns > 0) { patterns = new ARPattern[numPatterns]; for (int i = 0; i < numPatterns; ++i) { patterns[i] = new ARPattern(UID, i); } } } } }
// 1 - Query for visibility. // 2 - Determine if visibility state is new. // 3 - If visible, calculate marker pose. // 4 - If visible, set marker pose. // 5 - If visibility state is new, notify event receivers via "OnMarkerFound" or "OnMarkerLost". // 6 - If visibility state is new, set appropriate active state for marker children. // 7 - If visible, notify event receivers that the marker's pose has been updated via "OnMarkerTracked". protected virtual void LateUpdate() { if (!Application.isPlaying) { return; } float[] matrixRawArray = new float[16]; lock (loadLock) { if (UID == NO_ID || !PluginFunctions.inited) { visible = false; return; } Vector3 storedScale = transform.localScale; transform.localScale = Vector3.one; // 1 - Query for visibility. bool nowVisible = PluginFunctions.arwQueryMarkerTransformation(UID, matrixRawArray); // 2 - Determine if visibility state is new. bool notify = (nowVisible != visible); visible = nowVisible; // 3 - If visible, calculate marker pose. if (visible) { // Scale the position from ARToolKit units (mm) into Unity units (m). matrixRawArray[12] *= UNITY_TO_ARTOOLKIT; matrixRawArray[13] *= UNITY_TO_ARTOOLKIT; matrixRawArray[14] *= UNITY_TO_ARTOOLKIT; Matrix4x4 matrixRaw = ARUtilityFunctions.MatrixFromFloatArray(matrixRawArray); // ARToolKit uses right-hand coordinate system where the marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of +z. // Need to convert to Unity's left-hand coordinate system where marker lies in x-y plane with right in direction of +x, // up in direction of +y, and forward (towards viewer) in direction of -z. transformationMatrix = ARUtilityFunctions.LHMatrixFromRHMatrix(matrixRaw); // 4 - If visible, set marker pose. Matrix4x4 pose = ARStaticCamera.Instance.transform.localToWorldMatrix * transformationMatrix; transform.position = ARUtilityFunctions.PositionFromMatrix(pose); transform.rotation = ARUtilityFunctions.RotationFromMatrix(pose); transform.localScale = storedScale; } // 5 - If visibility state is new, notify event receivers via "OnMarkerFound" or "OnMarkerLost". if (notify) { if (null != eventReceivers && eventReceivers.Count > 0) { if (visible) { eventReceivers.ForEach(x => x.OnMarkerFound(this)); } else { eventReceivers.ForEach(x => x.OnMarkerLost(this)); } } // 6 - If visibility state is new, set appropriate active state for marker children. for (int i = 0; i < transform.childCount; ++i) { transform.GetChild(i).gameObject.SetActive(visible); } } if (visible) { // 7 - If visible, notify event receivers that the marker's pose has been updated via "OnMarkerTracked". if (null != eventReceivers && eventReceivers.Count > 0) { eventReceivers.ForEach(x => x.OnMarkerTracked(this)); } } } }
// 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 }
public void CalibratePiecePositions() { //The piece with the greater x and lower y is going to be the one on the bottom left corner List <Tuple <GameObject, Vector3> > piecesPositions = new List <Tuple <GameObject, Vector3> >(); foreach (var item in possiblePieces) { if (item.Item1.activeInHierarchy) { ARMarker myMarker = item.Item1.transform.parent.GetComponent <ARTrackedObject>().GetMarker(); Matrix4x4 pose = myMarker.TransformationMatrix; Vector3 position = ARUtilityFunctions.PositionFromMatrix(pose); piecesPositions.Add(new Tuple <GameObject, Vector3>(item.Item1, position)); } } if (piecesPositions.Count == 6) { //Sort the positions by x piecesPositions.Sort((x, y) => y.Item2.x.CompareTo(x.Item2.x)); Vector3 bottomLeft, bottomRight, topLeft, topRight, middleLeft, middleRight; if (piecesPositions[0].Item2.y < piecesPositions[1].Item2.y) { bottomLeft = piecesPositions[0].Item2; bottomRight = piecesPositions[1].Item2; } else { bottomLeft = piecesPositions[1].Item2; bottomRight = piecesPositions[0].Item2; } if (piecesPositions[4].Item2.y < piecesPositions[5].Item2.y) { topLeft = piecesPositions[4].Item2; topRight = piecesPositions[5].Item2; } else { topLeft = piecesPositions[5].Item2; topRight = piecesPositions[4].Item2; } if (piecesPositions[2].Item2.y < piecesPositions[3].Item2.y) { middleLeft = piecesPositions[2].Item2; middleRight = piecesPositions[3].Item2; } else { middleLeft = piecesPositions[3].Item2; middleRight = piecesPositions[2].Item2; } piecePositionsFromCamera.Clear(); piecePositionsFromCamera.Add(bottomLeft); piecePositionsFromCamera.Add(new Vector3(middleLeft.x, bottomLeft.y, (bottomLeft.z + middleLeft.z) / 2)); piecePositionsFromCamera.Add(new Vector3(middleRight.x, topLeft.y, (topLeft.z + middleRight.z) / 2)); piecePositionsFromCamera.Add(topLeft); piecePositionsFromCamera.Add(new Vector3(bottomLeft.x, middleLeft.y, (middleLeft.z + bottomLeft.z) / 2)); piecePositionsFromCamera.Add(middleLeft); piecePositionsFromCamera.Add(new Vector3(middleRight.x, middleLeft.y, (middleLeft.z + middleRight.z) / 2)); piecePositionsFromCamera.Add(new Vector3(topLeft.x, middleLeft.y, (middleLeft.z + topLeft.z) / 2)); piecePositionsFromCamera.Add(new Vector3(bottomRight.x, middleRight.y, (middleRight.z + bottomRight.z) / 2)); piecePositionsFromCamera.Add(new Vector3(middleLeft.x, middleRight.y, (middleRight.z + middleLeft.z) / 2)); piecePositionsFromCamera.Add(middleRight); piecePositionsFromCamera.Add(new Vector3(topRight.x, middleRight.y, (middleRight.z + topRight.z) / 2)); piecePositionsFromCamera.Add(bottomRight); piecePositionsFromCamera.Add(new Vector3(middleLeft.x, bottomRight.y, (bottomRight.z + middleLeft.z) / 2)); piecePositionsFromCamera.Add(new Vector3(middleRight.x, topRight.y, (topRight.z + middleRight.z) / 2)); piecePositionsFromCamera.Add(topRight); SaveCalibration(); } else { Debug.Log("Did not find 6 pieces to calibrate!"); } }
// 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"); } }
void OnMarkerTracked(ARMarker marker) { Vector3 positionTarget = ARUtilityFunctions.PositionFromMatrix(marker.TransformationMatrix); Vector3 normalPosition = new Vector3(positionTarget.x / (0.5f * positionTarget.z), positionTarget.y / (0.5f * positionTarget.z), markersFX[0].transform.localPosition.z); int lowCutoffvalue = (int)(((normalPosition.x + 0.9) / 1.8) * 4900) + 100; int highCutoffvalue = (int)(((normalPosition.x + 0.9) / 1.8) * 1495) + 5; AudioSource theAudio = null; if (marker.Tag == markersTags[0]) { theAudio = audio1; markersFX[0].SetActive(true); markersFX[0].transform.localPosition = normalPosition; } else if (marker.Tag == markersTags[1]) { audio1.GetComponent <AudioEchoFilter> ().delay = 30; audio1.GetComponent <AudioEchoFilter> ().decayRatio = 0.7f; //theAudio = audio2; //markersFX[1].SetActive (true); //markersFX[1].transform.localPosition = normalPosition; } else if (marker.Tag == markersTags[2]) { audio1.GetComponent <SEF_lowpass> ().cutoffFrequency = lowCutoffvalue; audio2.GetComponent <SEF_lowpass> ().cutoffFrequency = lowCutoffvalue; } else if (marker.Tag == markersTags[3]) { audio1.GetComponent <SEF_highpass> ().cutoffFrequency = highCutoffvalue; audio2.GetComponent <SEF_highpass> ().cutoffFrequency = highCutoffvalue; } if (theAudio != null) { float pitch = 0; if (normalPosition.x > -0.3f && normalPosition.x < 0.3f) { pitch = 1f; if (audio1.GetComponent <AudioEchoFilter> ().enabled) { pitch = 0.9f; } theAudio.pitch = pitch; } else if (normalPosition.x > -0.9f && normalPosition.x < -0.3f) { pitch = 0.7f; theAudio.pitch = pitch; } else if (normalPosition.x > 0.3f && normalPosition.x < 0.9f) { pitch = 1.4f; theAudio.pitch = pitch; } if (normalPosition.y > -0.2f && normalPosition.y < 0.2f) { int a = 0; } else if (normalPosition.y > -0.6f && normalPosition.y < -0.2f) { int a = 0; } else if (normalPosition.y > 0.2f && normalPosition.y < 0.6f) { int a = 0; } } }