// sets the world position of the current model private bool SetModelWorldPos(Vector3 vNewPos, Quaternion qNewRot) { if (modelTransform) { // activate model if needed if (!modelTransform.gameObject.activeSelf) { modelTransform.gameObject.SetActive(true); } // set position and look at the camera modelTransform.position = vNewPos; modelTransform.rotation = qNewRot; if (modelLookingAtCamera) { Camera arCamera = arManager.GetMainCamera(); MultiARInterop.TurnObjectToCamera(modelTransform.gameObject, arCamera, modelTransform.position, modelTransform.up); } return(true); } return(false); }
void Update() { // // don't consider taps over the UI // if(UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()) // return; // check for click if (objectPrefab && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true)) { MultiARInterop.InputAction action = arManager.GetInputAction(); if (action == MultiARInterop.InputAction.Click) { // raycast world //Vector2 screenPos = Input.GetTouch(0).position; MultiARInterop.TrackableHit hit; if (arManager.RaycastToWorld(true, out hit)) { // instantiate the object and anchor it to the world position GameObject spawnObj = Instantiate(objectPrefab, hit.point, !verticalModel ? hit.rotation : Quaternion.identity); arManager.AnchorGameObjectToWorld(spawnObj, hit); // look at the camera if (modelLookingAtCamera) { Camera arCamera = arManager.GetMainCamera(); MultiARInterop.TurnObjectToCamera(spawnObj, arCamera, hit.point, hit.normal); } } } } }
void Update() { // check for local & active player if (!isLocalPlayer) { return; } if (!gameObject.activeSelf) { return; } if (!arMainCamera && arManager && arManager.IsInitialized()) { arMainCamera = arManager.GetMainCamera(); } if (arMainCamera) { transform.position = arMainCamera.transform.position; transform.rotation = arMainCamera.transform.rotation; } // fire when clicked (world anchor must be present) if (arClient && arClient.WorldAnchorObj != null && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true)) { MultiARInterop.InputAction action = arManager.GetInputAction(); if (action == MultiARInterop.InputAction.Click) { CmdFire(); } } }
void Start() { // get reference to ar-manager MultiARManager arManager = MultiARManager.Instance; if (arManager) { // destroys all anchors and anchored objects arManager.DestroyAllAnchors(); // refresh scene references (graphic raycasters, etc.) arManager.RefreshSceneReferences(); // destroy second main camera, if any Camera arCamObject = arManager.GetMainCamera(); GameObject[] mainCamObjects = GameObject.FindGameObjectsWithTag("MainCamera"); for (int i = mainCamObjects.Length - 1; i >= 0; i--) { GameObject mainCamObj = mainCamObjects[i]; if (arCamObject != null && arCamObject.gameObject != mainCamObj) { Destroy(mainCamObj); } } } }
// Update is called once per frame void Update() { //// test only //if(arManager && arManager.IsInitialized()) //{ // Texture texBack = arManager.GetBackgroundTex(); //} // check for click if (ballPrefab && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true)) { MultiARInterop.InputAction action = arManager.GetInputAction(); if (action == MultiARInterop.InputAction.Click) { // gets the main camera Camera arCamera = arManager.GetMainCamera(); if (!arCamera) { return; } // raycast scene objects (including overlay surfaces) MultiARInterop.TrackableHit hit; Vector3 targetPoint = Vector3.zero; if (arManager.RaycastToScene(true, out hit)) { targetPoint = hit.point; } else { targetPoint = arCamera.transform.forward * 3f; // emulate target in the line of sight } // instantiate the cannonball. schedule it for destroy in 3 seconds GameObject cannonBall = Instantiate(ballPrefab, arCamera.transform.position, arCamera.transform.rotation); cannonBall.name = ballPrefab.name + "-" + objectCounter; objectCounter++; if (destroyInSeconds > 0f) { Destroy(cannonBall, destroyInSeconds); } // set random ball material SetBallMaterial(cannonBall); // fire the cannonball targetPoint.y -= aimAboveDistance; FireCannonball(cannonBall, arCamera.transform.position, targetPoint); } } }
void Update() { if (!cameraTransform) { Camera camera = marManager ? marManager.GetMainCamera() : null; cameraTransform = camera ? camera.transform : null; } if (debugText) { string sMessage = string.Empty; Vector3 pos = Vector3.zero; Vector3 ori = Vector3.zero; if (cameraTransform) { pos = cameraTransform.position; ori = cameraTransform.rotation.eulerAngles; sMessage += string.Format("Camera - Pos: ({0:F2}, {1:F2}, {2:F2}), Rot: ({3:F0}, {4:F0}, {5:F0})\n", pos.x, pos.y, pos.z, ori.x, ori.y, ori.z); } pos = transform.position; ori = transform.rotation.eulerAngles; sMessage += string.Format("TrPose - Pos: ({0:F2}, {1:F2}, {2:F2}), Rot: ({3:F0}, {4:F0}, {5:F0})\n", pos.x, pos.y, pos.z, ori.x, ori.y, ori.z); if (Input.location.status == LocationServiceStatus.Running && Input.location.lastData.timestamp != lastLocTimestamp) { LocationInfo locLast = Input.location.lastData; lastLocTimestamp = locLast.timestamp; string sLocMessage = string.Format("Lat: {0:F8}, Long: {1:F8}, Alt: {2:F8} @ {3:F3}\n", locLast.latitude, locLast.longitude, locLast.altitude, locLast.timestamp); sMessage += sLocMessage; if (locationText) { locationText.text = sLocMessage; } } int pcLength = marManager ? marManager.GetPointCloudLength() : 0; sMessage += string.Format("PointCloud: {0} points", pcLength); debugText.text = sMessage; } }
// sets the world position of the current model private bool SetModelWorldPos(Vector3 vNewPos, Quaternion qNewRot) { if (currentModel) { // set position and look at the camera currentModel.position = vNewPos; currentModel.rotation = qNewRot; if (modelLookingAtCamera) { Camera arCamera = arManager.GetMainCamera(); MultiARInterop.TurnObjectToCamera(currentModel.gameObject, arCamera, currentModel.position, currentModel.up); } return(true); } return(false); }
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."; } } } }
// checks for image anchor, to create or destroy the coffee cups private void CheckForImageAnchor() { MultiARManager marManager = MultiARManager.Instance; // Check that motion tracking is tracking. StringBuilder sbMessage = new StringBuilder(); if (!marManager || marManager.GetCameraTrackingState() != MultiARInterop.CameraTrackingState.NormalTracking) { sbMessage.AppendLine("Camera - Not tracking."); sbMessage.AppendLine("detectedImage: " + (imageAnchorName != string.Empty ? imageAnchorName : "-")); ShowDebugText(sbMessage.ToString()); return; } // Get updated augmented images for this frame. List <string> alImageAnchors = marManager.GetTrackedImageAnchorNames(); sbMessage.AppendLine(alImageAnchors.Count.ToString() + " anchor images found: "); // for (int i = 0; i < alImageAnchors.Count; i++) // { // sbMessage.Append(i).Append (" - ").Append (alImageAnchors[i]).AppendLine(); // } sbMessage.AppendLine("detectedImage: " + (imageAnchorName != string.Empty ? imageAnchorName : "-")); sbMessage.AppendLine("imageAnchor: " + (imageAnchor != null ? imageAnchor.name : "none")); ShowDebugText(sbMessage.ToString()); if (infoText) { infoText.text = "Found image anchor: " + (imageAnchorName != string.Empty ? imageAnchorName : "-"); if (cupIndexSelected >= 0) { infoText.text = string.Format("Selected: {0}", coffeeCupTitles[cupIndexSelected]); } } // check for found image anchor string foundImageAnchor = marManager.GetFirstTrackedImageAnchorName(); if (imageAnchorName == string.Empty) { imageAnchorName = foundImageAnchor; if (imageAnchorName != string.Empty) { imageAnchor = marManager.GetTrackedImageAnchorByName(imageAnchorName); Camera mainCamera = marManager.GetMainCamera(); if (imageAnchor != null && mainCamera != null) { // create coffee cups-parent (used to smooth anchor jittering) cupAnchor = new GameObject("CupAnchor"); // GameObject.CreatePrimitive(PrimitiveType.Cube); cupAnchor.transform.position = imageAnchor.transform.position; Vector3 vAnchorDir = imageAnchor.transform.position - mainCamera.transform.position; cupAnchor.transform.rotation = Quaternion.LookRotation(vAnchorDir, Vector3.up); // imageAnchor.transform.rotation; // // create anchor-pointer object (cube) // GameObject anchorPoiterObj = GameObject.CreatePrimitive(PrimitiveType.Cube); // anchorPoiterObj.transform.parent = imageAnchor.transform; // // anchorPoiterObj.transform.localPosition = Vector3.zero; // anchorPoiterObj.transform.localRotation = Quaternion.identity; // anchorPoiterObj.transform.localScale = new Vector3(0.05f, 0.1f, 0.15f); // create cup models StartCoroutine(CreateAndPlaceCups()); } } } else if (imageAnchorName != foundImageAnchor) { // destroy the coffee cups DestroyCups(); } }
// saves the current screen shot public bool SaveScreenShot(string saveFilePath) { if (saveFilePath == string.Empty) { return(false); } MultiARManager marManager = MultiARManager.Instance; Texture2D texScreenshot = MultiARInterop.MakeScreenShot(marManager != null ? marManager.GetMainCamera() : null); if (texScreenshot) { byte[] btScreenShot = texScreenshot.EncodeToJPG(); GameObject.Destroy(texScreenshot); File.WriteAllBytes(saveFilePath, btScreenShot); return(true); } return(false); }
// Update is called once per frame void Update() { // check for tap if (portalPrefab && arManager && arManager.IsInitialized() && arManager.IsInputAvailable(true)) { MultiARInterop.InputAction action = arManager.GetInputAction(); if (action == MultiARInterop.InputAction.Click) { // raycast to world MultiARInterop.TrackableHit hit; if (arManager.RaycastToWorld(true, out hit)) { // create the portal object, if needed if (!portalObj) { portalObj = Instantiate(portalPrefab); } // set its position and rotation portalObj.transform.position = hit.point; portalObj.transform.rotation = !verticalPortal ? hit.rotation : Quaternion.identity; // look at the camera if (portalLookingAtCamera) { Camera arCamera = arManager.GetMainCamera(); MultiARInterop.TurnObjectToCamera(portalObj, arCamera, hit.point, hit.normal); } // remove object anchor, if it was anchored before string anchorId = arManager.GetObjectAnchorId(portalObj); if (anchorId != string.Empty) { arManager.RemoveGameObjectAnchor(anchorId, true); } // anchor it to the new world position arManager.AnchorGameObjectToWorld(portalObj, hit); // apply the vertical offset if (verticalOffset != 0f) { Vector3 objPos = portalObj.transform.position; //objPos.y += verticalOffset; objPos += portalObj.transform.up * verticalOffset; portalObj.transform.position = objPos; } // play portal-open animation if (playAnimation != string.Empty) { // get reference to the portal animator if (!animator) { animator = portalObj.GetComponent <Animator>(); } if (animator) { animator.Play(playAnimation, 0, 0f); } } // create camera rigidbody (no gravity) & box-collider, if needed if (cameraBoxCollider != Vector3.zero) { Camera arCamera = arManager.GetMainCamera(); Rigidbody camRigidbody = arCamera.gameObject.GetComponent <Rigidbody>(); if (camRigidbody == null) { camRigidbody = arCamera.gameObject.AddComponent <Rigidbody>(); camRigidbody.useGravity = false; } BoxCollider camBoxCollider = arCamera.gameObject.GetComponent <BoxCollider>(); if (camBoxCollider == null) { camBoxCollider = arCamera.gameObject.AddComponent <BoxCollider>(); camBoxCollider.size = cameraBoxCollider; camBoxCollider.isTrigger = true; } } } } } }