void Update() { if (!TryGetTouchPosition(out Vector2 touchPosition)) { return; } // if(placedGameObject != null) // return; if (numOfPlaced == _arCloudAnchorManager.NUM_OF_ANCHOR) { return; } if (_arRaycastManager.Raycast(touchPosition, hits, UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon)) { var hitPose = hits[0].pose; placedGameObjectTmp = Instantiate(placedPrefab, hitPose.position, hitPose.rotation); placedGameObjectList[_arCloudAnchorManager.numOfQueued] = placedGameObjectTmp; var anchor = _arAnchorManager.AddAnchor(new Pose(hitPose.position, hitPose.rotation)); placedGameObjectList[_arCloudAnchorManager.numOfQueued].transform.parent = anchor.transform; // _arCloudAnchorManager.hi(); // this won't host the anchor just add a reference to be later host it _arCloudAnchorManager.QueueAnchor(anchor); numOfPlaced++; } }
protected override SpongyAnchor CreateAnchor(AnchorId id, Transform parent, Pose initialPose) { DebugLogExtra($"Creating anchor {id.FormatStr()}"); initialPose = AnchorFromSpongy.Multiply(initialPose); SpongyAnchorARF newAnchor = null; #if WLT_ADD_ANCHOR_COMPONENT GameObject go = new GameObject(id.FormatStr()); go.transform.SetParent(parent); go.transform.SetGlobalPose(initialPose); ARAnchor arAnchor = go.AddComponent <ARAnchor>(); newAnchor = WrapARAnchor(id, arAnchor.trackableId, go); #else // WLT_ADD_ANCHOR_COMPONENT var arAnchor = arAnchorManager.AddAnchor(initialPose); if (arAnchor == null) { Debug.LogError($"ARAnchorManager failed to create ARAnchor {id}"); return(null); } arAnchor.gameObject.name = id.FormatStr(); newAnchor = WrapARAnchor(id, arAnchor.trackableId, arAnchor.gameObject); #endif // WLT_ADD_ANCHOR_COMPONENT Debug.Assert(newAnchor != null); return(newAnchor); }
// Update is called once per frame void Update() { if (Input.touchCount == 0) { return; } Touch touch = Input.GetTouch(0); if (touch.phase != TouchPhase.Began) { return; } if (arRaycastManager.Raycast(touch.position, hits, TrackableType.FeaturePoint)) { Pose hitPose = hits[0].pose; ARAnchor arAnchor = arAnchorManager.AddAnchor(hitPose); if (arAnchor == null) { debugLog.gameObject.SetActive(true); string errorEntry = "There was an error creating an anchor\n"; Debug.Log(errorEntry); debugLog.text += errorEntry; } } }
void Hosting() { // 탭 없을 경우 리턴 if (Input.touchCount < 1) { return; } // 첫 번째 터치가 아닐 경우 리턴 Touch touch = Input.GetTouch(0); if (touch.phase != TouchPhase.Began) { return; } // 로컬 앵커가 존재하는지 여부를 확인 if (localAnchor == null) { // Raycast 발사 if (raycastManager.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon)) { // 로컬 앵커를 생성 localAnchor = anchorManager.AddAnchor(hits[0].pose); // 로컬 앵커 위치에 Mummy 증강시키고 변수에 저장 anchorGameObject = Instantiate(anchorPrefab, localAnchor.transform); } } }
void Update() { if (Input.touchCount == 0) { return; } Touch touch = Input.GetTouch(0); if (touch.phase != TouchPhase.Began) { return; } if (arRaycastManager.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon)) { Pose hitPose = hits[0].pose; ARAnchor referencePoint = arReferencePointManager.AddAnchor(hitPose); if (referencePoint == null) { debugLog.gameObject.SetActive(true); string errorEntry = "There was an error creating a reference point\n"; Debug.Log(errorEntry); debugLog.text += errorEntry; } else { referencePoints.Add(referencePoint); referencePointCount.text = $"Reference Point Count: {referencePoints.Count}"; } } }
// Update is called once per frame void Update() { if (m_AppMode == AppMode.TouchToHostCloudReferencePoint) { OutputText.text = m_AppMode.ToString(); if (Input.touchCount >= 1 && Input.GetTouch(0).phase == TouchPhase.Began && !EventSystem.current.IsPointerOverGameObject( Input.GetTouch(0).fingerId)) { List <ARRaycastHit> hitResults = new List <ARRaycastHit>(); RaycastManager.Raycast(Input.GetTouch(0).position, hitResults); if (hitResults.Count > 0) { Pose pose = hitResults[0].pose; // dokundurulan yerde referans noktası oluşturuyor. ARAnchor referencePoint = AnchorManager.AddAnchor(hitResults[0].pose); // bulut referans noktasını oluşturuyor. m_CloudReferencePoint = AnchorManager.HostCloudAnchor(referencePoint); if (m_CloudReferencePoint == null) { OutputText.text = "Create Failed!"; return; } // Wait for the reference point to be ready. m_AppMode = AppMode.WaitingForHostedReferencePoint; } } } else if (m_AppMode == AppMode.WaitingForHostedReferencePoint) { OutputText.text = m_AppMode.ToString(); CloudAnchorState cloudReferenceState = m_CloudReferencePoint.cloudAnchorState; OutputText.text += " - " + cloudReferenceState.ToString(); if (cloudReferenceState == CloudAnchorState.Success) { GameObject cloudAnchor = Instantiate( HostedPointPrefab, Vector3.zero, Quaternion.identity); cloudAnchor.transform.SetParent( m_CloudReferencePoint.transform, false); panel.gameObject.SetActive(true); m_CloudReferenceId = m_CloudReferencePoint.cloudAnchorId; cloudAnchorId.text = m_CloudReferenceId; m_CloudReferencePoint = null; m_AppMode = AppMode.TouchToHostCloudReferencePoint; } } }
public void AddAnchor(Pose hitPose) { _anchor = _anchorManager.AddAnchor(hitPose); if (_anchor == null) { Logger.Log("Error creating anchor"); } }
public void CreateAnchor() { ARAnchor anchorToAdd = aRAnchorManager.AddAnchor(lastPosePlaced); spawnedObject.transform.parent = anchorToAdd.transform; spawnedObject = null; aRAnchors.Add(anchorToAdd); HideConfirmButton(); }
public void ExitSelection() { _animator.SetBool("selected", false); PlacementIndicator.SetActive(false); if (_anchor != null) { _anchor.destroyOnRemoval = false; _anchorManager.RemoveAnchor(_anchor); _anchorManager.anchorPrefab = null; _anchor = _anchorManager.AddAnchor(new Pose(transform.position, transform.rotation)); } }
/// <summary> /// Immediately creates local anchor after detected marker intersects detected plane beneath it. /// Cloud anchor is created afterwards, but it takes some time. When it is finished, scene will be attached to it. /// Called if user clicks on the calibration cube displayed over detected marker. /// </summary> /// <param name="tf"></param> public void CreateAnchor(Transform tf) { #if (UNITY_ANDROID || UNITY_IOS) && AR_ON ARPlane plane = null; UnityEngine.Pose hitPose = new UnityEngine.Pose(); // try to raycast straight down to intersect closest plane List <ARRaycastHit> raycastHits = new List <ARRaycastHit>(); if (ARRaycastManager.Raycast(new Ray(tf.position, Vector3.down), raycastHits, TrackableType.PlaneWithinPolygon)) { hitPose = raycastHits[0].pose; TrackableId hitPlaneId = raycastHits[0].trackableId; plane = ARPlaneManager.GetPlane(hitPlaneId); } // remove all old local anchors, if there are some (in case we are recalibrating) RemoveLocalWorldAnchor(); RemoveCloudWorldAnchor(); // set temporary world anchor //WorldAnchorLocal = ARAnchorManager.AttachAnchor(plane, // new Pose(hitPose.position, Quaternion.FromToRotation(tf.up, plane.normal) * tf.rotation)); //WorldAnchorLocal = ARAnchorManager.AddAnchor(new UnityEngine.Pose(hitPose != new UnityEngine.Pose() ? hitPose.position : tf.position, // plane != null ? Quaternion.FromToRotation(tf.up, plane.normal) * tf.rotation : tf.rotation)); WorldAnchorLocal = ARAnchorManager.AddAnchor(new UnityEngine.Pose(tf.position, plane != null ? Quaternion.FromToRotation(tf.up, plane.normal) * tf.rotation : tf.rotation)); // immediately attach scene to local anchor (after cloud anchor is created, scene will be attached to it) AttachScene(WorldAnchorLocal.gameObject); // Create cloud anchor if (Settings.Instance.UseCloudAnchors) { WorldAnchorCloud = ARAnchorManager.HostCloudAnchor(WorldAnchorLocal); StartCoroutine(HostCloudAnchor()); } else { Calibrated = true; UsingCloudAnchors = false; OnARCalibrated?.Invoke(this, new CalibrationEventArgs(true, WorldAnchorLocal.gameObject)); Notifications.Instance.ShowNotification("Calibration successful", ""); worldAnchorVis = null; ActivateCalibrationElements((bool)MainSettingsMenu.Instance.CalibrationElements.GetValue()); } GameManager.Instance.SceneSetActive(true); ActivateTrackableMarkers(false); #endif }
void instantiateCloudAnchorAt(Pose pose) { // Let localAnchor = new anchor placed at "pose" ARAnchor localAnchor = anchorManager.AddAnchor(pose); // Log position of new local anchor if (verboseLog) { Debug.Log("Placed local anchor at: " + pose.position.ToString()); } // Instantiate a new cloud anchor based off of localAnchor and push to unregisteredCloudAnchors unregisteredCloudAnchors.Add(anchorManager.HostCloudAnchor(localAnchor)); }
private void AddImageAnchor(ARTrackedImage trackedImage) { if (trackedImageAnchors.Count >= numberOfTrackingImageAnchor) { // トラッキングされるイメージアンカー数の制限 Debug.LogWarning("ARTrackedImageAnchorManager :: Limit of number recognized."); return; } if (trackedImageAnchors.ContainsKey(trackedImage.referenceImage.name)) { // 同一マーカーの複数認識を拒否 Debug.LogWarning("ARTrackedImageAnchorManager :: Multiple identical markers have been recognized."); return; } // 画面外判定 if (IsOutOfView(trackedImage.transform.position)) { return; } Debug.LogFormat("ARTrackedImageAnchorManager :: ImageAnchor added [{0}].", trackedImage.referenceImage.name); // マーカー画像検出位置にアンカーを設定 ARAnchor anchor = anchorManager.AddAnchor(new Pose(trackedImage.transform.position, trackedImage.transform.rotation)); anchor.destroyOnRemoval = true; float scale = trackedImage.size.x / trackedImage.referenceImage.size.x; anchor.gameObject.transform.localScale = Vector3.one * scale; // アンカーの子供にオブジェクトを生成 ARImageAnchorOriginData org = imageAnchorOrigins.Find(o => o.imageName == trackedImage.referenceImage.name); if (org != null && org.imageAnchorPrefab != null) { GameObject go = Instantiate(org.imageAnchorPrefab) as GameObject; go.name += string.Format(" [{0}]", org.imageName); go.transform.SetParent(anchor.gameObject.transform); go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; } trackedImageAnchors.Add(trackedImage.referenceImage.name, anchor); // マーカー検出イベント this.OnFoundImageAnchor?.Invoke(trackedImage.referenceImage.name); }
protected override SpongyAnchor CreateAnchor(AnchorId id, Transform parent, Pose initialPose) { #if WLT_EXTRA_LOGGING Debug.Log($"Creating anchor {id.FormatStr()}"); #endif // WLT_EXTRA_LOGGING initialPose = AnchorFromSpongy.Multiply(initialPose); var arAnchor = arAnchorManager.AddAnchor(initialPose); if (arAnchor == null) { Debug.Log($"ARAnchorManager failed to create ARAnchor {id}"); return(null); } arAnchor.gameObject.name = id.FormatStr(); SpongyAnchorARF newAnchor = arAnchor.gameObject.AddComponent <SpongyAnchorARF>(); return(newAnchor); }
private void AttachScene(GameObject worldAnchor, bool initLocalAnchor = false) { if (initLocalAnchor) { WorldAnchorLocal = ARAnchorManager.AddAnchor(new Pose(Camera.main.transform.position, Camera.main.transform.rotation)); GameManager.Instance.Scene.transform.parent = WorldAnchorLocal.gameObject.transform; } else if (worldAnchor != null) { GameManager.Instance.Scene.transform.parent = worldAnchor.transform; } GameManager.Instance.Scene.transform.localPosition = Vector3.zero; GameManager.Instance.Scene.transform.localScale = new Vector3(1f, 1f, 1f); GameManager.Instance.Scene.transform.localEulerAngles = Vector3.zero; }
private bool PlaceGeospatialAnchor(GeospatialAnchorHistory history) { Quaternion quaternion = Quaternion.AngleAxis(180f - (float)history.Heading, Vector3.up); var anchor = AnchorManager.AddAnchor( history.Latitude, history.Longitude, history.Altitude, quaternion); if (anchor != null) { GameObject anchorGO = Instantiate(GeospatialPrefab, anchor.transform); _anchorObjects.Add(anchorGO); return(true); } return(false); }
public void onPlaceOrigin() { if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began) { List <ARRaycastHit> hitResults = new List <ARRaycastHit>(); raycastManager.Raycast(Input.touches[0].position, hitResults); if (hitResults.Count > 0) { CmdChangeGameState(GameState.States.WaitingForOriginRegistration); ARAnchor localAnchor = anchorManager.AddAnchor(hitResults[0].pose); origin = anchorManager.HostCloudAnchor(localAnchor); } } }
void DrawOnTouch() { if (!CanDraw) { return; } int tapCount = Input.touchCount > 1 && lineSettings.allowMultiTouch ? Input.touchCount : 1; for (int i = 0; i < tapCount; i++) { Touch touch = Input.GetTouch(i); Vector3 touchPosition = arCamera.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, lineSettings.distanceFromCamera)); ARDebugManager.Instance.LogInfo($"{touch.fingerId}"); if (touch.phase == TouchPhase.Began) { OnDraw?.Invoke(); ARAnchor anchor = anchorManager.AddAnchor(new Pose(touchPosition, Quaternion.identity)); if (anchor == null) { Debug.LogError("Error creating reference point"); } else { anchors.Add(anchor); ARDebugManager.Instance.LogInfo($"Anchor created & total of {anchors.Count} anchor(s)"); } ARLine line = new ARLine(lineSettings); Lines.Add(touch.fingerId, line); line.AddNewLineRenderer(transform, anchor, touchPosition); } else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { Lines[touch.fingerId].AddPoint(touchPosition); } else if (touch.phase == TouchPhase.Ended) { Lines.Remove(touch.fingerId); } } }
public void AddAnchor(Pose pose) { #if AR_FOUNDATION_4_1_1_OR_LATER Debug.Log($"Instantiating new GameObject containing an ARAnchor"); Instantiate(m_arAnchorManager.anchorPrefab, pose.position, pose.rotation); #else ARAnchor newAnchor = m_arAnchorManager.AddAnchor(pose); if (newAnchor == null) { Debug.Log($"Anchor creation failed"); } else { Debug.Log($"Anchor created: {newAnchor.trackableId}"); m_anchors.Add(newAnchor); } #endif }
void Update() { if (Input.touchCount == 0) { return; } Touch touch = Input.GetTouch(0); if (touch.phase != TouchPhase.Began) { return; } Vector3 newPos = arCamera.ScreenToWorldPoint(new Vector3(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y, 0.3f)); arReferencePointManager.AddAnchor(new Pose(newPos, Quaternion.identity)); }
IEnumerator Start() { m_Anchors = new List <ARAnchor>(); yield return(new WaitForSeconds(1f)); for (int i = 0; i < m_Count; ++i) { var position = Random.insideUnitSphere * m_Radius + transform.position; var rotation = Quaternion.AngleAxis(Random.Range(0, 360), Random.onUnitSphere); var anchor = m_AnchorManager.AddAnchor(new Pose(position, rotation)); if (anchor != null) { m_Anchors.Add(anchor); } yield return(new WaitForSeconds(.5f)); } var previousPosition = transform.localPosition; while (enabled) { if (transform.hasChanged) { var delta = transform.position - previousPosition; previousPosition = transform.position; foreach (var anchor in m_Anchors) { var pose = new Pose(anchor.transform.position + delta, anchor.transform.rotation); AnchorApi.Update(anchor.trackableId, pose, TrackingState.Tracking); yield return(new WaitForSeconds(.5f)); } transform.hasChanged = false; } yield return(null); } }
void SpawnFoodInstance() { var boundary = detectedPlane.boundary; var planeSpaceVertex = boundary[Random.Range(0, boundary.Length)]; var worldSpaceVertex = detectedPlane.transform.TransformPoint(new Vector3(planeSpaceVertex.x, 0, planeSpaceVertex.y)); float dist = Random.Range(0.05f, 1f); Vector3 position = Vector3.Lerp(worldSpaceVertex, detectedPlane.center, dist); anchor = null; anchor = aRReferencePointManager.AddAnchor(new Pose(position, Quaternion.identity)); foodInstance = foodModels[Random.Range(0, foodModels.Length)]; foodInstance.transform.position = position; foodInstance.SetActive(true); foodInstance.transform.parent = null; foodInstance.transform.SetParent(anchor.transform); }
void Update() { //タップの検出 int tapCount = Input.touchCount; for (int i = 0; i < tapCount; i++) { Touch touch = Input.GetTouch(i); //カメラ手前30cmの位置(x,y,z) Vector3 touchPosition = arCamera.ScreenToWorldPoint(new Vector3(Input.GetTouch(i).position.x, Input.GetTouch(i).position.y, distanceFromCamera)); //タップスタート if (touch.phase == TouchPhase.Began) { //実世界の位置と方向を持ったオブジェクト //Quaternion.identityとは、無回転状態のこと ARAnchor anchor = anchorManager.AddAnchor(new Pose(touchPosition, Quaternion.identity)); if (anchor == null) { Debug.LogError("Error creating reference point"); } else { anchors.Add(anchor); } AddNweLineRenderer(anchor, touchPosition); this.director.GetComponent <GameDirector>().Tapping(); } // タップ中 else if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary) { AddPoint(touchPosition); } else if (touch.phase == TouchPhase.Ended) { // タッチ終了 this.director.GetComponent <GameDirector>().NoTapping(); } } }
public void OnButtonCLick() { if (aRRaycastManager.Raycast(new Vector2(Screen.width / 2, Screen.height / 2), hits, TrackableType.PlaneWithinPolygon)) { // testen ob Object vorhanden, schauen ob Raycast auf Object zeigt Pose hitPose = hits[0].pose; ARAnchor referencePoint = referencePointManager.AddAnchor(hitPose); referencePoint.name = "Cube"; Debug.Log("AR"); if (referencePoint == null) { Debug.Log("There was an error creating the referencPoint"); } else { referencePoints.Add(referencePoint); } } }
public void AddAnchor(Vector2 touchPosition) { if (m_RaycastManager.Raycast(touchPosition, s_Hits, TrackableType.FeaturePoint)) { // Raycast hits are sorted by distance, so the first one // will be the closest hit. Pose hitPose = s_Hits[0].pose; if (m_anchor != null) { //update position of previous anchor m_anchor.transform.position = hitPose.position; lastHitPoseRotation = hitPose.rotation; m_anchor.transform.rotation = lastHitPoseRotation * deltaRotation; } else { m_anchor = m_anchorManager.AddAnchor(hitPose); } } }
void Update() { if (anchor != null || Input.touchCount == 0) { return; } var touch = Input.GetTouch(0); if (touch.phase != TouchPhase.Ended) { return; } if (ARRaycast.Raycast(touch.position, s_Hits, TrackableType.FeaturePoint)) { // Raycast hits are sorted by distance, so the first one // will be the closest hit. var hitPose = s_Hits[0].pose; anchor = anchorManager.AddAnchor(hitPose); } }
private void X_SpawnIFC(Pose spawnPose) { // IFC aktivieren GameManager.Instance.PlacedIFC.SetActive(true); // Platzieren #if UNITY_EDITOR || UNITY_STANDALONE var newObj = new GameObject("Parent"); newObj.transform.SetPositionAndRotation(spawnPose.position, spawnPose.rotation); GameManager.Instance.PlacedIFC.transform.SetParent(newObj.transform, false); #else // Anchor erstellen und damit platzieren var anchor = _anchorManager.AddAnchor(spawnPose); GameManager.Instance.PlacedIFC.transform.SetParent(anchor.transform, false); #endif // Modus wechseln GameManager.Instance.SwitchMenu(GameManager.MenuMode.Placement); // Detection deaktivieren _planeManager.subsystem?.Stop(); _raycastManager.subsystem?.Stop(); _imageManager.subsystem?.Stop(); // Script deaktivieren enabled = false; }
void Update() { if (!TryGetTouchPosition(out Vector2 touchPosition)) { return; } if (placedGameObject != null) { return; } if (arRaycastManager.Raycast(touchPosition, hits, UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinPolygon)) { var hitPose = hits[0].pose; placedGameObject = Instantiate(placedPrefab, hitPose.position, hitPose.rotation); var anchor = arAnchorManager.AddAnchor(new Pose(hitPose.position, hitPose.rotation)); placedGameObject.transform.parent = anchor.transform; // this won't host the anchor just add a reference to be later host it ARCloudAnchorManager.Instance.QueueAnchor(anchor); } }
/// <summary> /// The Unity Update() method. /// Modified for project mARble /// </summary> public void Update() { _UpdateApplicationLifecycle(); if (m_CurrentActiveScreen != ActiveScreen.ARScreen) { return; } // If we are neither in hosting nor resolving mode then the update is complete. if (m_CurrentMode != ApplicationMode.Hosting && m_CurrentMode != ApplicationMode.Resolving) { return; } // Give AR session some time to prepare for resolving and update the UI message // once the preparation time passed. if (m_CurrentMode == ApplicationMode.Resolving && !m_PassedResolvingPreparedTime) { m_TimeSinceStart += Time.deltaTime; if (m_TimeSinceStart > k_ResolvingPrepareTime) { m_PassedResolvingPreparedTime = true; NetworkUIController.ShowDebugMessage( "Waiting for Cloud Anchor to be hosted..."); } } // If the origin anchor has not been placed yet, then update in resolving mode is // complete. if (m_CurrentMode == ApplicationMode.Resolving && !IsOriginPlaced) { return; } // If the player has not touched the screen then the update is complete. Touch touch; if (Input.touchCount < 1) { return; } touch = Input.GetTouch(0); // Ignore the touch if it's pointing on UI objects. if (EventSystem.current.IsPointerOverGameObject(touch.fingerId) || IsPointerOverUIObject()) { return; } List <ARRaycastHit> hitResults = new List <ARRaycastHit>(); RaycastManager.Raycast(touch.position, hitResults); if (touch.phase == TouchPhase.Began) { // First check if the player is selecting an existing item Ray ray = ARCamera.ScreenPointToRay(touch.position); RaycastHit hitObject; // Sets item touched as Selected Object if (Physics.Raycast(ray, out hitObject)) { lastSelectedObject = hitObject.transform.GetComponent <PlacementObject>(); if (lastSelectedObject != null) { placedObjects = FindObjectsOfType <PlacementObject>(); foreach (PlacementObject placementObject in placedObjects) { placementObject.Selected = placementObject == lastSelectedObject; } } } } // Deselect object if the player is no longer touching if (touch.phase == TouchPhase.Ended && lastSelectedObject != null) { lastSelectedObject.Selected = false; } // If there was an anchor placed, then instantiate the corresponding object. if (hitResults.Count > 0) { // The first touch on the Hosting mode will instantiate the origin anchor. Any // subsequent touch will instantiate a star, both in Hosting and Resolving modes. if (_CanPlaceStars()) { Pose hitPose = hitResults[0].pose; // yOffset to prevent object clipping through AR plane // If no existing object is selected, instantiate a new object if (lastSelectedObject == null && ModeManager.Instance.GetCurrMode() == GameMode.PLACEMENT_MODE) { // Vector3 yOffsetPrefab = new Vector3(0f, currSelectedPrefab.GetComponent<Renderer>().bounds.size.y/2.001f, 0f); // hitPose.position += yOffsetPrefab; _InstantiateStar(_ToWorldOriginPose(hitPose)); } else { // Move the selected object if (lastSelectedObject.Selected) { //V ector3 yOffsetSelectedObject = new Vector3(0f, lastSelectedObject.GetComponent<Renderer>().bounds.size.y/2.001f, 0f); if (lastSelectedObject.tag == "Marble") { float snapDist = 0.075f; //0.05f; MoveMarble(hitPose, snapDist); } else { lastSelectedObject.transform.position = hitPose.position; // we don't use the + yOffsetSelectedObject anymore } } } } else if (!IsOriginPlaced && m_CurrentMode == ApplicationMode.Hosting) { ARAnchor anchor = AnchorManager.AddAnchor(hitResults[0].pose); WorldOrigin = anchor.transform; _InstantiateAnchor(anchor); OnAnchorInstantiated(true); } } }
/// <summary> /// The Unity Update() method. /// </summary> public void Update() { _UpdateApplicationLifecycle(); if (m_CurrentActiveScreen != ActiveScreen.ARScreen) { return; } // If we are neither in hosting nor resolving mode then the update is complete. if (m_CurrentMode != ApplicationMode.Hosting && m_CurrentMode != ApplicationMode.Resolving) { return; } // Give AR session some time to prepare for resolving and update the UI message // once the preparation time passed. if (m_CurrentMode == ApplicationMode.Resolving && !m_PassedResolvingPreparedTime) { m_TimeSinceStart += Time.deltaTime; if (m_TimeSinceStart > k_ResolvingPrepareTime) { m_PassedResolvingPreparedTime = true; NetworkUIController.ShowDebugMessage( "Waiting for Cloud Anchor to be hosted..."); } } // If the origin anchor has not been placed yet, then update in resolving mode is // complete. if (m_CurrentMode == ApplicationMode.Resolving && !IsOriginPlaced) { return; } // If the player has not touched the screen then the update is complete. Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } // Ignore the touch if it's pointing on UI objects. if (EventSystem.current.IsPointerOverGameObject(touch.fingerId)) { return; } List <ARRaycastHit> hitResults = new List <ARRaycastHit>(); RaycastManager.Raycast(Input.GetTouch(0).position, hitResults); // If there was an anchor placed, then instantiate the corresponding object. if (hitResults.Count > 0) { // The first touch on the Hosting mode will instantiate the origin anchor. Any // subsequent touch will instantiate a star, both in Hosting and Resolving modes. if (_CanPlaceStars()) { _InstantiateStar(_ToWorldOriginPose(hitResults[0].pose)); } else if (!IsOriginPlaced && m_CurrentMode == ApplicationMode.Hosting) { ARAnchor anchor = AnchorManager.AddAnchor(hitResults[0].pose); WorldOrigin = anchor.transform; _InstantiateAnchor(anchor); OnAnchorInstantiated(true); } } }
/// <summary> /// Spawn a portal at the given position. If a portal already exists, destroy the old one and create a new one. /// If the new portal is close to the existing portal, set new values for the existing portal. /// </summary> /// <param name="position">Position of a new AR Anchor that holds the position of the portal.</param> /// <param name="rotation">Rotation of the portal.</param> /// <param name="width">Width of the portal window.</param> /// <param name="height">Height of the portal window.</param> /// <returns>No return value.</returns> public void SpawnObject(Vector3 position, Quaternion rotation, float width, float height) { // Create an anchor that can be tracked from now on ARAnchor newAnchor = anchorManager.AddAnchor(new Pose(position, rotation)); if (activeAnchor != null) { anchorManager.RemoveAnchor(activeAnchor); } activeAnchor = newAnchor; if (activePortal == null) { GameObject obj = Instantiate(objectToSpawn); obj.transform.parent = activeAnchor.transform; obj.transform.localPosition = Vector3.zero; obj.transform.rotation = rotation; activePortal = obj; // Set the augmentationCenter active and place behind the door if (!inside) { Vector3 offset = rotation * new Vector3(0, 0, 1); augmentationCenter.transform.position = position + offset; augmentationCenter.transform.rotation = rotation; } else { // Make sure the door is always facing into the augmented world obj.transform.Rotate(Vector3.up, 180.0f); } if (!created) { augmentationManager.Active = true; created = true; } // Only scale up the portal window, so child objects are not stretched GameObject portalWindow = activePortal.transform.Find("PortalWindow").gameObject; Vector3 scale = new Vector3(width, height, 1); StartCoroutine(ScaleOverTime(portalWindow, spawnTime, scale)); } else { float distance = Vector3.Distance(activePortal.transform.position, position); if (distance <= replacementDistance) { ReplacePortal(position, rotation, width, height); } else { Destroy(activePortal); activePortal = null; // Immidiate reset for recursive function call // Rerun the function to create a new portal SpawnObject(position, rotation, width, height); } } }