// Update is called once per frame void Update() { if (snakeInstance == null || snakeInstance.activeSelf == false) { pointer.SetActive(false); return; } else { pointer.SetActive(true); } TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinBounds; if (Frame.Raycast(Screen.width / 2, Screen.height / 2, raycastFilter, out hit)) { //Frame holds information about ARCore's state including tracking status, //the pose of the camera relative to the world, estimated lighting parameters, //and information on updates to objects (like Planes or Point Clouds) that ARCore is tracking. Vector3 pt = hit.Pose.position; //Set the Y to the Y of the snakeInstance pt.y = snakeInstance.transform.position.y; // Set the y position relative to the plane and attach the pointer to the plane Vector3 pos = pointer.transform.position; pos.y = pt.y; pointer.transform.position = pos; // Now lerp to the position pointer.transform.position = Vector3.Lerp(pointer.transform.position, pt, Time.smoothDeltaTime * speed); } float dist = Vector3.Distance(pointer.transform.position, snakeInstance.transform.position) - 0.05f; if (dist < 0) { dist = 0; } Rigidbody rb = snakeInstance.GetComponent <Rigidbody>(); rb.transform.LookAt(pointer.transform.position); rb.velocity = snakeInstance.transform.localScale.x * snakeInstance.transform.forward * dist / .01f; }
private void _selectPlane(Touch touch) { // RayCast against the location the player touched to search for planes. TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { if (Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.LogError("back rayCast"); return; } // Use hit pose and camera pose to check if hitTest is from the // back of the plane, if it is, no need to create the anchor. if ((hit.Trackable is DetectedPlane) && _currentPlane == null) { _currentPlane = (DetectedPlane)hit.Trackable; _aimAnchor = Session.CreateAnchor(hit.Pose); if (_referencePlan == null) { // Instantiate Plan model at the hit pose. _referencePlan = Instantiate(ReferencePlanPrefab, _aimAnchor.transform.position, _aimAnchor.transform.rotation); // Make Plan model a child of the anchor. _referencePlan.transform.parent = _aimAnchor.transform; } // Remove Point Cloud (blue dots int he Finding Mode if (_pointCloud != null) { PointcloudVisualizer p = _pointCloud.GetComponent <PointcloudVisualizer>(); p.IsAllow = false; } var session = GameObject.Find("ARCore Device") .GetComponent <ARCoreSession>(); session.SessionConfig.PlaneFindingMode = DetectedPlaneFindingMode.Disabled; // session.OnEnable(); } } }
public bool Raycast(IntPtr frameHandle, Vector3 origin, Vector3 direction, float maxDistance, TrackableHitFlags filter, List <TrackableHit> outHitList) { outHitList.Clear(); IntPtr hitResultListHandle = IntPtr.Zero; ExternApi.ArHitResultList_create(m_NativeSession.SessionHandle, ref hitResultListHandle); // Invert z to match ARCore coordinate system. origin.z = -origin.z; direction.z = -direction.z; ExternApi.ArFrame_hitTestRay(m_NativeSession.SessionHandle, frameHandle, ref origin, ref direction, hitResultListHandle); FilterTrackableHits(hitResultListHandle, maxDistance, filter, outHitList); ExternApi.ArHitResultList_destroy(hitResultListHandle); return(outHitList.Count != 0); }
public void Spawn(LeanFinger finger) { if (AndyPlanePrefab != null && finger != null) { TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinBounds | TrackableHitFlags.PlaneWithinBounds; if (Frame.Raycast(finger.ScreenPosition.x, finger.ScreenPosition.y, raycastFilter, out hit)) { var ARObject = Instantiate(AndyPlanePrefab, hit.Pose.position, hit.Pose.rotation); var anchor = hit.Trackable.CreateAnchor(hit.Pose); ARObject.transform.LookAt(FirstPersonCamera.transform); ARObject.transform.rotation = Quaternion.Euler(0.0f, ARObject.transform.rotation.eulerAngles.y, ARObject.transform.rotation.z); ARObject.transform.parent = anchor.transform; } } }
public bool Raycast( IntPtr frameHandle, float x, float y, TrackableHitFlags filter, List <TrackableHit> outHitList) { outHitList.Clear(); IntPtr hitResultListHandle = IntPtr.Zero; ExternApi.ArHitResultList_create( m_NativeSession.SessionHandle, ref hitResultListHandle); ExternApi.ArFrame_hitTest( m_NativeSession.SessionHandle, frameHandle, x, y, hitResultListHandle); FilterTrackableHits(hitResultListHandle, Mathf.Infinity, filter, outHitList); ExternApi.ArHitResultList_destroy(hitResultListHandle); return(outHitList.Count != 0); }
public static bool RaycastAll( Vector3 origin, Vector3 direction, List <TrackableHit> hitResults, float maxDistance = Mathf.Infinity, TrackableHitFlags filter = TrackableHitFlags.Default) { hitResults.Clear(); var nativeSession = LifecycleManager.Instance.NativeSession; if (nativeSession == null) { return(false); } return(nativeSession.HitTestApi.Raycast( nativeSession.FrameHandle, origin, direction, maxDistance, filter, hitResults)); }
void ProcessTouches() { Touch touch; if (Input.touchCount != 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinBounds | TrackableHitFlags.PlaneWithinPolygon; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { SetSelectedPlane(hit.Trackable as DetectedPlane); } }
private void Update() { if (Input.touchCount == 0) { return; // 터치가 이루어지지 않았다면 더 이상 진행하지 말고 return해라 } // 첫 번째 터치 정보 추출 Touch touch = Input.GetTouch(0); Vector3 mousePosition = Input.mousePosition; // 현재 터치한 위치를 얻는다 Ray ray = ARCamera.ScreenPointToRay(mousePosition); // ARCamera로부터 발사한 광선 정보 RaycastHit rHit; // 광선이 부딪히는 GameObject 정보 Debug.Log("-------------- " + mousePosition.ToString()); // 터치가 발생하였고 And 광선과 부딪힌 GameObject가 존재한다면 if (touch.phase == TouchPhase.Began && Physics.Raycast(ray, out rHit)) { // 부딪힌 GameObject의 Tag가 Speaker라면 더 이상 진행하지 말고 return 해라 if (rHit.collider.tag == "Speaker") { return; } } //ARCore에서 제공하는 RaycastHit와 유사한 구조체 TrackableHit hit; //검출 대상을 평면 또는 Feature Point로 한정 TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; //터치한 지점으로 레이 발사하고 And 터치한 위치에서 광선을 발사했을 때 평면이나 특징점(Feature Point)과 충돌했다면 if (touch.phase == TouchPhase.Began && Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { Debug.Log("-------------- Touch"); //객체를 고정할 앵커를 생성 var anchor = hit.Trackable.CreateAnchor(hit.Pose); //placeObject에 담긴 Prefab 객체를 생성 GameObject obj = (GameObject)Instantiate(placeObject, hit.Pose.position, Quaternion.identity, anchor.transform); // 생성한 객체가 사용자 쪽을 바라보도록 회전값 계산 var rot = Quaternion.LookRotation(ARCamera.transform.position - hit.Pose.position); // 사용자 쪽 회전값 적용 obj.transform.rotation = Quaternion.Euler(ARCamera.transform.position.x, rot.eulerAngles.y, ARCamera.transform.position.z); } }
// Update is called once per frame void Update() { if (playerInstance == null || playerInstance.activeSelf == false) { pointer.SetActive(false); return; } else { pointer.SetActive(true); } TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinBounds; if (Frame.Raycast(Screen.width / 2, Screen.height / 2, raycastFilter, out hit)) { Vector3 pt = hit.Pose.position; //Set the Y to the Y of the snakeInstance pt.y = playerInstance.transform.position.y; // Set the y position relative to the plane and attach the pointer to the plane Vector3 pos = pointer.transform.position; pos.y = pt.y; pointer.transform.position = pos; // Now lerp to the position pointer.transform.position = Vector3.Lerp(pointer.transform.position, pt, Time.smoothDeltaTime * speed); } // Move towards the pointer, slow down if very close. float dist = Vector3.Distance(pointer.transform.position, playerInstance.transform.position) - 0.1f; if (dist < 0) { dist = 0; } Rigidbody rb = playerInstance.GetComponent <Rigidbody>(); rb.transform.LookAt(pointer.transform.position); rb.velocity = playerInstance.transform.localScale.x * playerInstance.transform.forward * dist / .01f; SpawnPoint = playerInstance.transform.GetChild(1); timer = Time.time; }
void Update() { AppLifecycle(); Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { if ((hit.Trackable is DetectedPlane) && Vector3.Dot(MainCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.Log("Hit at back of the current DetectedPlane"); } else { GameObject prefab; if (hit.Trackable is FeaturePoint) { prefab = PointPrefab; } else { prefab = PlanePrefab; } if (!spawned) { var Map = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation); Map.transform.Rotate(0, ModelRotation, 0, Space.Self); var anchor = hit.Trackable.CreateAnchor(hit.Pose); Map.transform.parent = anchor.transform; spawned = true; } } } }
protected override void ProcessTouch(Touch touch) { TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon; if (ARCoreWorldOriginHelper.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { m_LastPlacedAnchor = hit.Trackable.CreateAnchor(hit.Pose); } if (_CanPlaceFlags()) { ((ClientMapBuilder)mapBuilder).ProcessTouches(); } }
// Update is called once per frame void Update() { Touch myTouch;//touch type of input to store the input of touch of user if (Input.touchCount < 1 || (myTouch = Input.GetTouch(0)).phase != TouchPhase.Began) //executed if touch is found else return to go find touch { return; } TrackableHit myHit;//store all the info of hit on screen TrackableHitFlags RayCastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; //a filter for tracking the farame that are beean selected by the google model if (Frame.Raycast(myTouch.position.x, myTouch.position.y, RayCastFilter, out myHit)) //start the raycast of the touch respective to the x and y axis of the plane, only if hit is there { // read frame and frame.raycast GameObject prefab;//creting a object of a prefab depending upon the hit of respective point or plane if (myHit.Trackable is FeaturePoint) //if it is a point also called as a feature point { prefab = AndyPointPrefab; } else //else it is a plane for a prefab { prefab = AndyPlanePrefab; } var andyObject = Instantiate(prefab, myHit.Pose.position, myHit.Pose.rotation); //to generate the game objet that has been called on the plane or points of the AR andyObject.transform.Rotate(0, 180.0f, 0, Space.Self); //to stop the rotate of the object with respect to the frame of the camera var anchor = myHit.Trackable.CreateAnchor(myHit.Pose); // create an anchor to hold the object on its own place in the respect of the plane of the virtual points andyObject.transform.parent = anchor.transform; //creating the ancor a parent of the andyobject to keep the andyobject in it positon } }
// Update is called once per frame void Update() { Touch touch = Input.GetTouch(0); if (Input.touchCount > 0 && touch.phase == TouchPhase.Began) { TrackableHit hit; TrackableHitFlags flags = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; //ARCore레이캐스트 if (Frame.Raycast(touch.position.x, touch.position.y, flags, out hit)) { var anchor = hit.Trackable.CreateAnchor(hit.Pose); Instantiate(andy, hit.Pose.position, hit.Pose.rotation, anchor.transform); } } }
//Raycast from the touch of the screen to the real world, searching for trackables public TrackableHit UpdateWorldRayCast(Touch touch) { TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { if (hit.Trackable is FeaturePoint || hit.Trackable is DetectedPlane) { if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) { //Debug.Log("Touched position: " + hit.Pose.position); } } } return(hit); }
// Update is called once per frame void Update() { if (Session.Status != SessionStatus.Tracking) { return; } Session.GetTrackables <DetectedPlane>(m_newTrackedPlanes, TrackableQueryFilter.New); for (int i = 0; i < m_newTrackedPlanes.Count; ++i) { GameObject grid = Instantiate(GridPrefab, Vector3.zero, Quaternion.identity, transform); grid.GetComponent <GridVisualizer>().Initialize(m_newTrackedPlanes[i]); } // Check if user touches the screen Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } // Check if user touched a detected plane TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { Debug.Log("Detected Plane touched"); // place object on top of detected plane Portal.SetActive(true); Anchor anchor = hit.Trackable.CreateAnchor(hit.Pose); Portal.transform.position = hit.Pose.position; Portal.transform.rotation = hit.Pose.rotation; // have placed objected face camera Vector3 cameraPosition = ARCamera.transform.position; cameraPosition.y = hit.Pose.position.y; Portal.transform.LookAt(cameraPosition, Portal.transform.up); Portal.transform.parent = anchor.transform; } }
/// <summary> /// The Unity Update() method. /// </summary> public void Update() { _UpdateApplicationLifecycle(); // If the player has not touched the screen, we are done with this update. Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } // Should not handle input if the player is pointing on UI. if (EventSystem.current.IsPointerOverGameObject(touch.fingerId)) { return; } // Raycast against the location the player touched to search for planes. TrackableHit hit; //TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |TrackableHitFlags.FeaturePointWithSurfaceNormal; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { GameObject.Find("instructions").SetActive(false); outfit.SetActive(true); if (flag) { foutfit.SetActive(true); try { GameObject.Find("Manager").GetComponent <DressSelection>().currentSelectedItem = foutfit; foutfit.GetComponent <Lean.Touch.LeanTranslate>().enabled = true; foutfit.GetComponent <Lean.Touch.LeanScale>().enabled = true; } catch (Exception e) { } flag = false; } outfit.transform.position = hit.Pose.position; outfit.transform.Translate(0, 1.83f, 0); outfit.transform.rotation = Quaternion.Euler(0, 0, 0); outfit.transform.rotation = Quaternion.identity; } }
private static void RetracePath(Node startNode, Node endNode, int countCrossingNodes) { Node currentNode = endNode; TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; while (currentNode != startNode) { // Применяем гравитацию Node gravityNode = currentNode; if (Frame.Raycast(currentNode.worldPosition, -Vector3.up, out hit, Mathf.Infinity, raycastFilter)) { gravityNode = grid.NodeFromWorldPosition(new Vector3(currentNode.worldPosition.x, hit.Pose.position.y + 0.1f /* + seeker.localScale.y / 2*/, currentNode.worldPosition.z)); } /////////////////////// path.Add(gravityNode); currentNode = currentNode.parent; } path.Reverse(); //if (path.Count > countCrossingNodes * 3 + 1) //{ // for (int i = 0; i < path.Count; i++) // { // if (i < path.Count - 1 + countCrossingNodes) // { // if (path[i].worldPosition.y > path[i + 1].worldPosition.y) // { // path.RemoveRange(i + 1, countCrossingNodes); // } // } // if (i > countCrossingNodes) // { // if (path[i].worldPosition.y < path[i + 1].worldPosition.y) // { // path.RemoveRange(i + 1 - countCrossingNodes, countCrossingNodes); // } // } // } //} }
/// <summary> /// The Unity Update() method. /// </summary> public void Update() { _UpdateApplicationLifecycle(); // Hide snackbar when currently tracking at least one plane. Session.GetTrackables <DetectedPlane>(m_AllPlanes); // If the player has not touched the screen, we are done with this update. Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } // Raycast against the location the player touched to search for planes. TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { // Use hit pose and camera pose to check if hittest is from the // back of the plane, if it is, no need to create the anchor. if ((hit.Trackable is DetectedPlane) && Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.Log("Hit at back of the current DetectedPlane"); } else { var old = CurrentAncher; //新しいアンカー生成 CurrentAncher = hit.Trackable.CreateAnchor(hit.Pose); _onAncherChanged.OnNext(Unit.Default); //アンカー更新通知 if (old != null) { Destroy(old.gameObject); } } } }
// Update is called once per frame void Update() { // The session status must be Tracking in order to access the Frame. if (Session.Status != SessionStatus.Tracking) { int lostTrackingSleepTimeout = 15; Screen.sleepTimeout = lostTrackingSleepTimeout; return; } Screen.sleepTimeout = SleepTimeout.NeverSleep; if (clicked == true) { List <TrackableHit> hits = new List <TrackableHit>(); //TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinBounds | TrackableHitFlags.PlaneWithinPolygon; Vector3 fwd = firstPersonCamera.transform.TransformDirection(Vector3.forward); if (Frame.RaycastAll(firstPersonCamera.transform.position, fwd, hits, 50f, raycastFilter)) { Pose hitPose = hits[0].Pose; var cameraForward = Camera.current.transform.forward; var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized; hitPose.rotation = Quaternion.LookRotation(cameraBearing); if (inst == false) { just = Instantiate(marker, hitPose.position, hitPose.rotation); just.SetActive(true); inst = true; } else { just.transform.position = hitPose.position; } } } if (checkPlane == true) { ProcessTouches(); } else { SSTools.ShowMessage("create a plane first!", SSTools.Position.bottom, SSTools.Time.twoSecond); } }
public static bool TryFindDetectedPlane(Touch touch, Vector2 touchPosition, out Pose?hitPose) { if (IsUItouched(touch)) { hitPose = null; return(false); } TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touchPosition.x, touchPosition.y, raycastFilter, out hit)) { if ((hit.Trackable is DetectedPlane) && Vector3.Dot(Camera.main.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.Log("Hit at back of the current DetectedPlane"); } else { if (hit.Trackable is DetectedPlane) { DetectedPlane detectedPlane = hit.Trackable as DetectedPlane; if (detectedPlane.PlaneType == DetectedPlaneType.Vertical) { hitPose = null; return(true); } else { hitPose = new Pose(hit.Pose.position, hit.Pose.rotation); return(true); } } else { hitPose = null; return(false); } } } hitPose = null; return(false); }
private void UpdatePlacementPose() { //var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f)); /// Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } placementPoseIsValid = true; TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.PlaneWithinBounds; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { if ((hit.Trackable is DetectedPlane) && Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.Log("射线击中了DetectedPlane的背面!"); } else { placementPose = hit.Pose; var FoxObject = Instantiate(routePoint, hit.Pose.position, hit.Pose.rotation); FoxObject.transform.Rotate(0, mModelRotation, 0, Space.Self); var anchor = hit.Trackable.CreateAnchor(hit.Pose); FoxObject.transform.parent = anchor.transform; wayPoints.Add(FoxObject); Debug.Log(wayPoints.Count); } } /// //arOrigin.GetComponent<ARRaycastManager>().Raycast(screenCenter, hits, UnityEngine.XR.ARSubsystems.TrackableType.Planes); //placementPoseIsValid = hits.Count > 0; /*if (placementPoseIsValid) * { * placementPose = hits[0].pose; * var cameraForward = Camera.current.transform.forward; * var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized; * placementPose.rotation = Quaternion.LookRotation(cameraBearing); * }*/ }
public void _SpawnARObject() { Touch touch; touch = Input.GetTouch(0); Debug.Log("touch count is " + Input.touchCount); TrackableHit hit; // Raycast against the location the player touched to search for planes. TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (touch.phase == TouchPhase.Began) { Debug.Log("Touch Began"); if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { if (CurrentNumberOfGameObjects < numberOfGameObjectsAllowed) { Debug.Log("Screen Touched"); Destroy(ARObject); // Use hit pose and camera pose to check if hittest is from the // back of the plane, if it is, no need to create the anchor. if ((hit.Trackable is DetectedPlane) && Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.Log("Hit at back of the current DetectedPlane"); } else { ARObject = Instantiate(ARAndroidPrefab, hit.Pose.position, hit.Pose.rotation); // Instantiate Andy model at the hit pose. ARObject.transform.Rotate(-90, 0, 0, Space.Self); // Compensate for the hitPose rotation facing away from the raycast (i.e. camera). var anchor = hit.Trackable.CreateAnchor(hit.Pose); ARObject.transform.parent = anchor.transform; CurrentNumberOfGameObjects = CurrentNumberOfGameObjects + 1; // Hide Plane once ARObject is Instantiated foreach (GameObject Temp in DetectedPlaneGenerator.instance.PLANES) //RK { Temp.SetActive(false); } } } } } }
/*private void Control3DTouch() * { * * Touch touch; * // 如果没有触屏或有触屏但触屏不等于刚按下;且没有任意一种鼠标点击 * if ((Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) && !(Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2) || Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))) * { * return; * } * * * if (Input.GetMouseButton(0) || (Input.touchCount > 0 && (touch = Input.GetTouch(0)).phase == TouchPhase.Began)) * { * //Debug.Log(Perfeb.isModelExist); * * if (Input.touchCount > 0 && !Input.GetMouseButton(0)) * { * // 如果是触屏 * touch = Input.GetTouch(0); * positionX = touch.position.x; * positionY = touch.position.y; * } * else if (Input.touchCount < 1 && Input.GetMouseButton(0)) * { * * //如果是鼠标 * positionX = Input.mousePosition.x; * positionY = Input.mousePosition.y; * } * * Ray ray = Camera.main.ScreenPointToRay(new Vector3(positionX, positionY)); * RaycastHit hitObject; * TrackableHit Thit = new TrackableHit(); * TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.PlaneWithinBounds; * if (Physics.Raycast(ray, out hitObject) && !isObjectSelect) * { * moveObject = hitObject.collider.gameObject; * moveObjectName = hitObject.transform.name; * isObjectSelect = true; * Debug.Log(isObjectSelect); * Debug.Log(name); * Debug.Log(moveObjectName); * * } * if (name == moveObjectName) * { * * Frame.Raycast(positionX, positionY, raycastFilter, out Thit); * * temp = Thit.Pose.position; * Debug.Log(temp); * * transform.position = temp; * } * * } * * if (Input.GetMouseButtonUp(0)) * { * //移动后,当鼠标放开,重置取到的objectName。 * //否则,当鼠标放开再点击时,将会无论是否点到目标物体都移动上一个物体(因为moveObjectName没变所以if永远为真) * * moveObjectName = "default"; * isObjectSelect = false; * //Debug.Log(isObjectSelect); * } * }*/ private void Control3DMouse() { if (!(Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2) || Input.GetMouseButtonUp(0) || Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(2))) { return; } // 如果没有触屏或有触屏但触屏不等于刚按下;且没有任意一种鼠标点击 if (Input.GetMouseButton(0)) { //Debug.Log(Perfeb.isModelExist); positionX = Input.mousePosition.x; positionY = Input.mousePosition.y; Ray ray = Camera.main.ScreenPointToRay(new Vector3(positionX, positionY)); RaycastHit hitObject; TrackableHit Thit = new TrackableHit(); TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.PlaneWithinBounds; if (Physics.Raycast(ray, out hitObject) && !isObjectSelect) { moveObject = hitObject.collider.gameObject; moveObjectName = hitObject.transform.name; isObjectSelect = true; //Debug.Log(isObjectSelect); //Debug.Log(name); //Debug.Log(moveObjectName); } if (name == moveObjectName) { Frame.Raycast(positionX, positionY, raycastFilter, out Thit); temp = Thit.Pose.position; //Debug.Log(temp); transform.position = temp; } } if (Input.GetMouseButtonUp(0)) { //移动后,当鼠标放开,重置取到的objectName。 //否则,当鼠标放开再点击时,将会无论是否点到目标物体都移动上一个物体(因为moveObjectName没变所以if永远为真) moveObjectName = "default"; isObjectSelect = false; //Debug.Log(isObjectSelect); } }
private void DropCheck() { Touch touch = Input.GetTouch(0); // Raycast against the location the player touched to search for planes. TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { // Use hit pose and camera pose to check if hittest is from the // back of the plane, if it is, no need to create the anchor. if ((hit.Trackable is DetectedPlane) && Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.Log("Hit at back of the current DetectedPlane"); } else { var anchor = hit.Trackable.CreateAnchor(hit.Pose); RootObject.transform.parent = anchor.transform; RootObject.transform.position = anchor.transform.position; RootObject.transform.rotation = anchor.transform.rotation; RootObject.transform.Rotate(0, k_ModelRotation, 0, Space.Self); if (m_arObject != null) { Destroy(m_arObject); m_arObject = null; } // Instantiate prefab at the hit pose. m_arObject = Instantiate(ArPrefab); m_arObject.transform.parent = RootObject.transform; m_arObject.transform.localPosition = Vector3.zero; m_arObject.transform.localEulerAngles = Vector3.zero; m_arObject.transform.localScale = Vector3.one; } } }
void SpawnWall() { Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { hitPos = hit.Pose.position; wallObject = Instantiate(wallPrefab, hit.Pose.position + new Vector3(0f, height, 0f), hit.Pose.rotation); // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical // world evolves. var anchor = hit.Trackable.CreateAnchor(hit.Pose); wallObject.transform.parent = anchor.transform; NetworkServer.Spawn(wallObject); if (!audioSource.isPlaying) { audioSource.PlayOneShot(spawnSound); } Debug.Log("wall"); allGrids = wallObject.transform.Find(name: "Grid").gameObject; Debug.Log(allGrids.name); for (int i = 0; i < 20; i++) { grids[i] = allGrids.transform.Find(name: i + "").gameObject; Debug.Log(grids[i].name); } Debug.Log("wallGrid got!!"); ifWallSpawn = true; } }
/// <summary> /// Function called when the manipulation is ended. /// </summary> /// <param name="gesture">The current gesture.</param> protected override void OnEndManipulation(TapGesture gesture) { if (gesture.WasCancelled) { return; } // If gesture is targeting an existing object we are done. if (gesture.TargetObject != null) { return; } // Raycast against the location the player touched to search for planes. TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon; if (Frame.Raycast( gesture.StartPosition.x, gesture.StartPosition.y, raycastFilter, out hit)) { // Use hit pose and camera pose to check if hittest is from the // back of the plane, if it is, no need to create the anchor. if ((hit.Trackable is DetectedPlane) && Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.Log("Hit at back of the current DetectedPlane"); } else { // Instantiate game object at the hit pose. DetectedPlane detectedPlane = hit.Trackable as DetectedPlane; Debug.Log(detectedPlane.PlaneType); if (detectedPlane.PlaneType == DetectedPlaneType.HorizontalUpwardFacing && currentNumberOfObjects < numberOfObjectsAllowed) { modelprefab = Instantiate(GameObjectHorizontalPlanePrefab, hit.Pose.position, hit.Pose.rotation); modelprefab.transform.position = new Vector3(0.09f, -4.43f, 2.42f); GenerateModels(modelprefab, hit); currentNumberOfObjects++; } } } }
public bool GetWorldPointFromTouchPosition(Vector2 touchPosition, out Vector3 worldPosition) { TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinBounds | TrackableHitFlags.PlaneWithinPolygon; if (Frame.Raycast(touchPosition.x, touchPosition.y, raycastFilter, out hit)) { // Create an anchor to allow ARCore to track the hitpoint as understanding of the physical // world evolves. var anchor = hit.Trackable.CreateAnchor(hit.Pose); worldPosition = anchor.transform.position; return(true); } worldPosition = Vector3.zero; return(false); }
//COROUTINES IEnumerator RoundStarting() { while (currentstate == GAME_STATE.STARTING) { StartingUI.SetActive(false); //CombatUI.SetActive(false); SearchingForPlaneUI.SetActive(true); Touch touch; while (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { yield return(null); } // Raycast against the location the player touched to search for planes. TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { var mettleStageInstance = Instantiate(MettleStagePrefab, hit.Pose.position, hit.Pose.rotation); var anchor = hit.Trackable.CreateAnchor(hit.Pose); if ((hit.Flags & TrackableHitFlags.PlaneWithinPolygon) != TrackableHitFlags.None) { Vector3 cameraPositionSameY = FirstPersonCamera.transform.position; cameraPositionSameY.y = hit.Pose.position.y; mettleStageInstance.transform.LookAt(cameraPositionSameY, mettleStageInstance.transform.up); } mettleStageInstance.transform.parent = anchor.transform; yield return(new WaitForSeconds(3)); yield return(SpawnMettles()); CurrentState = GAME_STATE.PLAYING; break; } } }
// Update is called once per frame void Update() { if (Input.touchCount == 0) { return; } Touch touch = Input.GetTouch(0); TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (touch.phase == TouchPhase.Began && Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { var anchor = hit.Trackable.CreateAnchor(hit.Pose); if (prevAnchor == null) { prevAnchor = anchor; } float dist = Vector3.Distance(prevAnchor.transform.position, anchor.transform.position); measureLable.text = dist.ToString("#0.00m"); prevAnchor = anchor; GameObject obj = Instantiate(placeObject, hit.Pose.position, Quaternion.identity, anchor.transform); var rot = Quaternion.LookRotation(ARCamera.transform.position - hit.Pose.position); obj.transform.rotation = Quaternion.Euler(ARCamera.transform.position.x, rot.eulerAngles.y, ARCamera.transform.position.z); } }
/// <summary> /// Updates touch event. Triggers free space identification when touching. /// </summary> protected void UpdateTouch() { // If the player has not touched the screen, we are done with this update. Touch touch; if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) { return; } // Raycasts against the location the player touched to search for planes. TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) { // Uses hit pose and camera pose to check if hittest is from the // back of the plane, if it is, no need to create the anchor. if ((hit.Trackable is DetectedPlane) && Vector3.Dot(Camera.main.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { DebugPanel.Instance.Print("Hit at back of the current DetectedPlane."); } else { // Creates an anchor to allow ARCore to track the hitpoint. m_TouchAnchor = hit.Trackable.CreateAnchor(hit.Pose); DebugPanel.Instance.Print("Created anchor, now rendering free spaces."); if (m_IsRendering) { Debug.Log("Now rendering, please wait."); return; } m_IsRendering = true; var renderThread = new Thread(RenderFreeSpace); renderThread.Start(); } } }