public override void Update() { TouchEntity entity = TouchHandler.Pop(); if (0 == entity.touchPositionList.Count || TouchPhase.Began != entity.touchPhase) { return; } Vector3 touchPosition = entity.touchPositionList[0]; Vector3 viewportPoint = Camera.main.ScreenToViewportPoint(touchPosition); ARPoint point = new ARPoint(); point.x = viewportPoint.x; point.y = viewportPoint.y; UnityARSessionNativeInterface nativeInterface = UnityARSessionNativeInterface.GetARSessionNativeInterface(); List <ARHitTestResult> hitResultList = nativeInterface.HitTest(point, ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent); if (0 == hitResultList.Count) { return; } else { Notifier notifier = Notifier.GetInstance(); notifier.Notify(NotifyMessage.OnRaycastHit); } return; }
void OnGUI() { var screenW = Screen.width; var screenH = Screen.height; if (GUI.Button(new Rect((screenW - 200) / 2, screenH - 250, 200, 200), magicTexture)) { magicBall = GameObject.Instantiate(magicBallObj); magicBall.transform.position = Camera.main.transform.position; magicBall.transform.rotation = Camera.main.transform.rotation; StartCoroutine(WaitAndRunWithSecond(2)); } if (GUI.Button(new Rect(screenW - 190, screenH - 200, 170, 170), findTexture)) { DestroyObject(spriteBall); if (getRandomValue()) { // 从屏幕中心发送射线 Vector3 shootPos = Camera.main.ScreenToViewportPoint(new Vector3(screenW / 2, screenH / 2)); ARPoint shootPoint = new ARPoint { x = shootPos.x, y = shootPos.y, }; // 开始命中测试,类型为利用现有的平面的范围 List <ARHitTestResult> results = m_session.HitTest(shootPoint, ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent); if (results.Count > 0) { tipStr = "已找到精灵球!"; // 创建一个对象,并根据结果初始化对象的位姿 spriteBall = GameObject.Instantiate(spriteBallObj); spriteBall.transform.position = UnityARMatrixOps.GetPosition(results [0].worldTransform); spriteBall.transform.rotation = UnityARMatrixOps.GetRotation(results [0].worldTransform); } } else { tipStr = "未找到精灵球,请再尝试!"; } } // 显示分数 GUIStyle scoreStyle = new GUIStyle(); scoreStyle.normal.background = fontTexture; scoreStyle.alignment = TextAnchor.MiddleCenter; scoreStyle.fontSize = 120; GUI.Label(new Rect(20, screenH - 200, 170, 170), scoreNum.ToString(), scoreStyle); // 显示提示信息 GUIStyle tipStyle = new GUIStyle(); tipStyle.normal.background = fontTexture; tipStyle.alignment = TextAnchor.MiddleCenter; tipStyle.fontSize = 30; GUI.Label(new Rect(50, 50, screenW - 100, 100), tipStr, tipStyle); }
bool HitTestWithResultType(ARPoint point, ARHitTestResultType resultTypes) { UnityARSessionNativeInterface session = UnityARSessionNativeInterface.GetARSessionNativeInterface(); List <ARHitTestResult> hitResults = session.HitTest(point, resultTypes); if (hitResults.Count > 0) { foreach (var hitResult in hitResults) { Debug.Log("Got hit!"); m_HitTransform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform); if (_component == ARKitOriginComponent.Origin) { _worldPositionAROrigin = UnityARMatrixOps.GetPosition(hitResult.worldTransform); } if (_component == ARKitOriginComponent.Right) { _worldPositionARRight = UnityARMatrixOps.GetPosition(hitResult.worldTransform); } if (_component == ARKitOriginComponent.Forward) { _worldPositionARForward = UnityARMatrixOps.GetPosition(hitResult.worldTransform); } m_HitTransform.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform); Debug.Log(string.Format("x:{0:0.######} y:{1:0.######} z:{2:0.######}", m_HitTransform.position.x, m_HitTransform.position.y, m_HitTransform.position.z)); return(true); } } return(false); }
private void PlaceActorAt(float x, float y) { m_hitPoint.x = x / (float)m_opencvProcessing.m_lastBufferMarshalledWidth; m_hitPoint.y = y / (float)m_opencvProcessing.m_lastBufferMarshalledHeight; List <ARHitTestResult> hitResults = m_arSession.HitTest(m_hitPoint, m_resultTypeToUse); if (hitResults.Count > 0) { ARHitTestResult hitResult = Utils.GetFirstValidHit(hitResults); if (hitResult.isValid) { var nextPosition = UnityARMatrixOps.GetPosition(hitResult.worldTransform); if ((nextPosition - m_actor.position).magnitude > 0.1f) { m_actor.position = nextPosition + m_offset; } } else { Debug.Log("Invalid hit point"); } } else { Debug.Log("No hit points"); } }
void createGameObject() { Vector3 screenPos = Camera.main.ScreenToViewportPoint(Input.GetTouch(0).position); ARPoint point = new ARPoint { x = screenPos.x, y = screenPos.y }; ARHitTestResultType[] resultTypes = { ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, // if you want to use infinite planes use this: //ARHitTestResultType.ARHitTestResultTypeExistingPlane, ARHitTestResultType.ARHitTestResultTypeHorizontalPlane, ARHitTestResultType.ARHitTestResultTypeFeaturePoint }; foreach (ARHitTestResultType resultType in resultTypes) { List <ARHitTestResult> hitResults = m_session.HitTest(point, resultType); if (hitResults.Count > 0) { GameObject currentObj = GameObject.Instantiate(obj); currentObj.transform.position = UnityARMatrixOps.GetPosition(hitResults [0].worldTransform); currentObj.transform.rotation = UnityARMatrixOps.GetRotation(hitResults [0].worldTransform); currentObj.transform.LookAt(new Vector3(Camera.main.transform.position.x, currentObj.transform.position.y, Camera.main.transform.position.z)); } } }
public static bool HitTestFiltered(ARPoint point, ARHitTestResultType resultTypes) { UnityARSessionNativeInterface session = UnityARSessionNativeInterface.GetARSessionNativeInterface(); List <ARHitTestResult> hitResults = session.HitTest(point, resultTypes); if (hitResults.Count > 0) { foreach (var hitResult in hitResults) { return(true); } } return(false); }
public void PlaceOnCamera(Camera camera) { Vector3 viewPortPos = Vector3.zero; switch (_component) { case ARKitOriginComponent.Origin: viewPortPos = Camera.main.ScreenToViewportPoint(Vector3.forward); break; case ARKitOriginComponent.Right: viewPortPos = Camera.main.ScreenToViewportPoint(new Vector3(0.5f, 0.0f, 1.0f)); break; case ARKitOriginComponent.Forward: viewPortPos = Camera.main.ScreenToViewportPoint(new Vector3(0.0f, 0.5f, 1.0f)); break; } Vector3 screenPosition = Camera.main.ScreenToViewportPoint(viewPortPos); ARPoint point = new ARPoint { x = screenPosition.x, y = screenPosition.y }; // prioritize reults types ARHitTestResultType[] resultTypes = { ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, ARHitTestResultType.ARHitTestResultTypeHorizontalPlane, ARHitTestResultType.ARHitTestResultTypeFeaturePoint }; foreach (ARHitTestResultType resultType in resultTypes) { UnityARSessionNativeInterface session = UnityARSessionNativeInterface.GetARSessionNativeInterface(); List <ARHitTestResult> hitResults = session.HitTest(point, resultType); if (hitResults.Count > 0) { foreach (var hitResult in hitResults) { m_HitTransform.position = UnityARMatrixOps.GetPosition(hitResult.worldTransform); m_HitTransform.rotation = UnityARMatrixOps.GetRotation(hitResult.worldTransform); } } } }
void Spawn() { //屏幕坐标转视口坐标 //手指点击位置 //Vector3 screenPos = Camera.main.ScreenToViewportPoint(Input.GetTouch(0).position); //屏幕中心位置 Vector3 screenPos = Camera.main.ScreenToViewportPoint(new Vector2(Screen.width / 2, Screen.height / 2)); ARPoint point = new ARPoint { x = screenPos.x, y = screenPos.y }; // 处理捕捉类型,点还是面,控制虚拟物体可以在哪里创建 ARHitTestResultType[] resultTypes = { //ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingGeometry, //考虑尺寸和形状(没三角面),必须在Plane内创建。Plane带形状的?? //ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent, //尊重平面有限尺寸,只能创建在Plane中,同一高度会自动合并 // if you want to use infinite planes use this: ARHitTestResultType.ARHitTestResultTypeExistingPlane, //不考虑屏幕大小,必须先扫出至少一个Plane,可以创建在Plane外面,同一高度临近平面自动合并 //ARHitTestResultType.ARHitTestResultTypeEstimatedHorizontalPlane, //搜索过程中?? //ARHitTestResultType.ARHitTestResultTypeEstimatedVerticalPlane, //搜索过程中?? ARHitTestResultType.ARHitTestResultTypeFeaturePoint //Plane会出现,没有意义,可以在任意空中创建 }; foreach (var type in resultTypes) { List <ARHitTestResult> hitResults = m_session.HitTest(point, type); Debug.Log(type.ToString() + ": " + hitResults.Count); if (hitResults.Count > 0) { if (avatar == null) { avatar = Instantiate(prefab); } avatar.transform.position = UnityARMatrixOps.GetPosition(hitResults[0].worldTransform); avatar.transform.rotation = UnityARMatrixOps.GetRotation(hitResults[0].worldTransform); avatar.GetComponent <TextMesh>().text = result; } } }
private void HitTest(Vector3 point, Dictionary <string, List <ARHitTestResult> > results) { List <ARHitTestResult> hitResults = arSession.HitTest( new ARPoint { x = point.x, y = point.y }, ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent); foreach (var hitResult in hitResults) { string anchorIdentifier = hitResult.anchorIdentifier; List <ARHitTestResult> list; if (!results.TryGetValue(anchorIdentifier, out list)) { list = new List <ARHitTestResult> (); results.Add(anchorIdentifier, list); } list.Add(hitResult); } }
void StartHitTest() { // 生成ARPoint类型的点击坐标 Vector3 tapPos = Camera.main.ScreenToViewportPoint(Input.GetTouch(0).position); ARPoint tapPoint = new ARPoint { x = tapPos.x, y = tapPos.y, }; // 开始命中测试,类型为利用现有的平面的范围 List <ARHitTestResult> results = m_session.HitTest(tapPoint, ARHitTestResultType.ARHitTestResultTypeExistingPlaneUsingExtent); if (results.Count > 0) { // 创建一个对象,并根据结果初始化对象的位姿 GameObject newObj = GameObject.Instantiate(obj); newObj.transform.position = UnityARMatrixOps.GetPosition(results [0].worldTransform); newObj.transform.rotation = UnityARMatrixOps.GetRotation(results [0].worldTransform); newObj.transform.LookAt(new Vector3(Camera.main.transform.position.x, newObj.transform.position.y, Camera.main.transform.position.z)); } }