void Update() { // report location position if (locationEnabled && Input.location.status == LocationServiceStatus.Running) { lastLoc = Input.location.lastData; compHeading = Input.compass.enabled ? Input.compass.trueHeading : 0f; if (locationInfoText) { string sMessage = "LocStatus: " + Input.location.status.ToString() + ", Enabled: " + Input.location.isEnabledByUser; sMessage += "\nLat: " + lastLoc.latitude + ", Lon: " + lastLoc.longitude + ", Alt: " + lastLoc.altitude; sMessage += "\nHeading: " + FormatHeading(compHeading) + ", Start: " + FormatHeading(startHeading); locationInfoText.text = sMessage; } } else { string sMessage = "LocStatus: " + Input.location.status.ToString() + ", Enabled: " + Input.location.isEnabledByUser; locationInfoText.text = sMessage; } // report gyro rotation string sGyroMessage = string.Empty; if (gyroEnabled) { gyroAttitude = gyro.attitude; gyroRotation = gyroParentRot * (gyroAttitude * initialGyroRot); sGyroMessage = "GyroEnabled: " + gyro.enabled + "\nAtt: " + FormatQuat(gyroAttitude) + ", Rot: " + FormatQuat(gyroRotation); } // get the main camera Camera mainCamera = arManager ? arManager.GetMainCamera() : null; // report ar-camera pose and point-cloud size if (mainCamera) { camPosition = mainCamera.transform.position; camRotation = mainCamera.transform.rotation; sGyroMessage += string.Format("\nCamPos: {0}, CamRot: {1}\n", camPosition, FormatQuat(camRotation)); } if (gyroInfoText) { gyroInfoText.text = sGyroMessage; } // set start heading, when one is available //if (!startHeadingSet && mainCamera && gyroEnabled && gyroAttitude != Quaternion.identity) if (!startHeadingSet && mainCamera && locationEnabled && compHeading != 0f) { Debug.Log("Set heading with gyroRot: " + gyroRotation.eulerAngles + ", and gyroAtt: " + gyroAttitude.eulerAngles + ", compHead: " + compHeading); //startHeading = (gyroRotation.eulerAngles.y + 90f); startHeading = compHeading; // if (startHeading >= 360f) // startHeading -= 360f; startHeadingSet = true; } // check for click if (arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true)) { MultiARInterop.InputAction action = arManager.GetInputAction(); float navMagnitude = action == MultiARInterop.InputAction.Grip ? arManager.GetInputNavCoordinates().magnitude : 0f; if (action == MultiARInterop.InputAction.Grip && navMagnitude >= 0.1f && !routineRunning) { routineRunning = true; StartCoroutine(LoadButtonClicked()); } } }
void Update() { // check for click if (objectPrefab && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true)) { MultiARInterop.InputAction action = arManager.GetInputAction(); if (action == MultiARInterop.InputAction.Click && (Time.time - lastInstanceTime) >= 1.5f) { // dont allow too often instance creation lastInstanceTime = Time.time; // remove current object, if any if (objectInstance) { DestroyObjectInstance(); } // raycast world MultiARInterop.TrackableHit hit; if (arManager.RaycastToWorld(true, out hit)) { // instantiate the object and anchor it to the world position objectInstance = Instantiate(objectPrefab, hit.point, !verticalModel ? hit.rotation : Quaternion.identity); arManager.AnchorGameObjectToWorld(objectInstance, hit); // get object renderer & initial scale objectRenderer = GetObjectRenderer(objectInstance); objectScale = objectInstance.transform.localScale; // look at the camera if (modelLookingAtCamera) { Camera arCamera = arManager.GetMainCamera(); MultiARInterop.TurnObjectToCamera(objectInstance, arCamera, hit.point, hit.normal); } if (infoText) { infoText.text = string.Format("{0} placed at {1}", objectPrefab.name, hit.point); } } } else if (objectInstance && action == MultiARInterop.InputAction.Grip) { // get nav coordinates Vector3 navCoords = arManager.GetInputNavCoordinates(); lastInstanceTime = Time.time; // estimate the scale change and target scale Vector3 scaleChange = navCoords.x >= 0 ? (objectScale * navCoords.x) : ((objectScale / 2f) * navCoords.x); targetScale = objectScale + scaleChange; objectInstance.transform.localScale = Vector3.Lerp(objectInstance.transform.localScale, targetScale, smoothFactor * Time.deltaTime); if (infoText) { float fScaleChange = 1f + (navCoords.x >= 0 ? navCoords.x : (0.5f * navCoords.x)); infoText.text = string.Format("Scale change: {0:F2}", fScaleChange); } // outline object bounds if (objectRenderer && boundsPrefab) { Bounds objectBounds = objectRenderer.bounds; // instantiate bounds-cube, if needed if (boundsInstance == null) { boundsInstance = GameObject.Instantiate(boundsPrefab); boundsInstance.transform.SetParent(objectInstance.transform); } // set the bounds-cube tras=nsform boundsInstance.transform.position = objectBounds.center; boundsInstance.transform.rotation = objectInstance.transform.rotation; Vector3 objScale = objectInstance.transform.localScale; Vector3 boundsScale = new Vector3(objectBounds.size.x / objScale.x, objectBounds.size.y / objScale.y, objectBounds.size.z / objScale.z); boundsInstance.transform.localScale = boundsScale; } } else if (action == MultiARInterop.InputAction.Release) { // instantiate bounds-cube, if needed if (boundsInstance != null) { Destroy(boundsInstance); boundsInstance = null; } if (infoText) { infoText.text = "Tap to place the object, then drag right or left, to scale it up or down."; } } } }