public FindPlane ( Camera cam, Vector2 pos, Vector3 &planeCenter, Plane &plane ) : bool | ||
cam | Camera | The Unity camera. |
pos | Vector2 | The point in screen space to perform detection on. |
planeCenter | Vector3 | Filled in with the center of the plane in Unity world space. |
plane | Plane | Filled in with a model of the plane in Unity world space. |
return | bool |
private IEnumerator WaitForDepthAndDetectPlanes() { StartCoroutine(WaitForDepth()); Camera cam = Camera.main; Vector3 planeCenter; Plane findedPlane; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { Vector2 point = new Vector2((i + Random.value) * Screen.width / 2, (j + Random.value) * Screen.height / 2); if (m_pointCloud.FindPlane(cam, point, out planeCenter, out findedPlane)) { if (Vector3.Distance(planeCenter, m_tangoARPoseController.m_tangoPosition) < 2.0f) { yield return(null); continue; } Vector3 forward; if (Vector3.Angle(findedPlane.normal, cam.transform.forward) < 175) { forward = Vector3.Cross(Vector3.Cross(findedPlane.normal, cam.transform.forward).normalized, findedPlane.normal).normalized; } else { forward = Vector3.Cross(findedPlane.normal, cam.transform.right); } float angle = Vector3.Angle(findedPlane.normal, m_yAxis); if (angle < 80.0f) { if (Random.value < 0.5) { m_ZomBunnyHoles.Add(Instantiate(m_ZomBunnyHole, planeCenter, Quaternion.LookRotation(forward, findedPlane.normal))); m_ZomBunnyHoles[m_ZomBunnyHoles.Count - 1].GetComponent <CompleteProject.HoleHealth>().allHoles = m_ZomBunnyHoles; } else { m_ZomBearHoles.Add(Instantiate(m_ZomBearHole, planeCenter, Quaternion.LookRotation(forward, findedPlane.normal))); m_ZomBearHoles[m_ZomBearHoles.Count - 1].GetComponent <CompleteProject.HoleHealth>().allHoles = m_ZomBearHoles; } } else if (angle < 110.0f) { m_HellephantHoles.Add(Instantiate(m_HellephantHole, planeCenter, Quaternion.LookRotation(forward, findedPlane.normal))); m_HellephantHoles[m_HellephantHoles.Count - 1].GetComponent <CompleteProject.HoleHealth>().allHoles = m_HellephantHoles; } } yield return(null); } } }
// deprecated function i made to read depth //void ReadDepth(Vector2 touchPosition) //{ // Camera cam = Camera.main; // Vector3 colorCameraPoint; // m_pointCloud.EstimateDepthOnScreen(cam, touchPosition, out colorCameraPoint); // text = Convert.ToString(colorCameraPoint.z); // debug_text = touchPosition.ToString(); //} void PlaceTriad(Vector2 touchPosition) { // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("cannot find plane."); text = "cannot find plane."; return; } // remove previous axis if it's already been set if (axis_set) { Destroy(triad_instance); axis_set = false; } // if normal faces towards us, reorient triad if (Vector3.Angle(plane.normal, Vector3.up) > 60.0f && Vector3.Angle(plane.normal, Vector3.up) < 120.0f) { text = "placing reoriented"; Vector3 up = Vector3.up; Vector3 forward = plane.normal; //Vector3 right = Vector3.Cross(up, forward).normalized; triad_instance = Instantiate(triad, planeCenter, Quaternion.LookRotation(forward, up)) as GameObject; triad_instance.transform.localScale = new Vector3(5.0f, 5.0f, 5.0f); } // else if normal faces positive gravitiy, place normally else { text = "normal placement"; Vector3 up = plane.normal; Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; Vector3 forward = Vector3.Cross(right, plane.normal).normalized; triad_instance = Instantiate(triad, planeCenter, Quaternion.LookRotation(-forward, up)) as GameObject; triad_instance.transform.localScale = new Vector3(5.0f, 5.0f, 5.0f); } axis_set = true; // *** spawning new triad across network *** var board_triad = PhotonNetwork.Instantiate("triad", new Vector3(0, 0, 18.60f), Quaternion.identity, 0); board_triad.GetComponent <PhotonView>().RPC("SpawnTriad", PhotonTargets.All); // *** still not working *** new_origin_pos = planeCenter; }
/// <summary> /// Wait for the next depth update, then find the plane at the touch position. /// </summary> /// <returns>Coroutine IEnumerator.</returns> /// <param name="touchPosition">Touch position to find a plane at.</param> private IEnumerator _WaitForDepthAndFindPlane(Vector2 touchPosition) { m_findPlaneWaitingForDepth = true; // Turn on the camera and wait for a single depth update. m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM); while (m_findPlaneWaitingForDepth) { yield return(null); } m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.DISABLED); // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { yield break; } Vector3 camForward = Camera.main.transform.forward; camForward.y = 0; camForward.Normalize(); m_instantiatedGame = (GameObject)Instantiate(m_prefabMarker, planeCenter, Quaternion.LookRotation(camForward, Vector3.up)); m_placingTable = false; m_selectedMarker = null; }
void PlaceKitten(Vector2 touchPosition) { // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("cannot find plane."); return; } // Place kitten on the surface, and make it always face the camera. if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f) { Vector3 up = plane.normal; Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; Vector3 forward = Vector3.Cross(right, plane.normal).normalized; Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up)); } else { Debug.Log("surface is too steep for kitten to stand on."); } }
public IEnumerator Touch(Vector3 touchPosition, Action <Vector3, Quaternion, int> action) { _findPlaneWaitingForDepth = true; _tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM); while (_findPlaneWaitingForDepth) { // action (Vector3.zero, Quaternion.identity, false); yield return(null); } _tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.DISABLED); Vector3 planeCenter; Plane plane; if (_pointCloud.FindPlane(Camera.main, touchPosition, out planeCenter, out plane)) { if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f) { var forward = CameraForwardOnPlane(plane); var lookToCamera = Quaternion.LookRotation(-forward, plane.normal); action(planeCenter, lookToCamera, 1); } else { action(Vector3.zero, Quaternion.identity, 0); } } else { action(Vector3.zero, Quaternion.identity, 2); } }
/// <summary> /// Creates a Surface on a real-world plane using either a DragSurfaceMesh or TapSurfaceMesh. /// </summary> /// <returns><c>true</c>, if Surface was successfully created, <c>false</c> otherwise.</returns> private bool CreateSurface() { Vector3 planeCenter; Plane plane; if (!tangoPointCloud.FindPlane(Camera.main, Camera.main.WorldToScreenPoint(Vector3.Lerp(_firstCorner, _oppositeCorner, 0.5f)), out planeCenter, out plane)) { ScreenLog.Write("Please try again."); return(false); } var surface = Instantiate(surfaceTemplate) as Surface; surface.SetTransform(plane, planeCenter); SurfaceMesh surfaceMesh; var mode = MainMenuController.GetEdgeDetectionMode(); if (mode == "DRAG") { surfaceMesh = new DragSurfaceMesh(surface, _firstCorner, _oppositeCorner); } else { surfaceMesh = new TapSurfaceMesh(surface, FindVerticesOnPlane(plane)); } if (surfaceMesh == null) { //Do I need to destroy the surfaceMesh object, too? ScreenLog.Write("Please try again."); surface.Undo(); return(false); } surface.SetMeshAndSelect(surfaceMesh.mesh); return(true); }
void PlacePictureFrame(Vector2 touchPosition) { // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("cannot find plane."); return; } // Place picture frame on the surface, and make it always face the camera. if (Vector3.Angle(plane.normal, Vector3.up) > 60.0f && Vector3.Angle(plane.normal, Vector3.up) < 140.0f) { Vector3 forward = plane.normal; // Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; // Vector3 forward = Vector3.Cross(right, plane.normal).normalized; Instantiate(m_pictureFrame, planeCenter, Quaternion.LookRotation(forward, Vector3.up)); } else { Debug.Log("surface is not steep enough for picture frame to be placed on."); } }
void PlaceStage(Vector2 touchPosition) { // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; #if UNITY_EDITOR isExists = true; FindObjectOfType <EditStage>().instantiateStage(Instantiate(spawnObject, Vector3.zero, Quaternion.identity)); #else if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("cannot find plane."); return; } // Place kitten on the surface, and make it always face the camera. if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f) { Vector3 up = plane.normal; Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; Vector3 forward = Vector3.Cross(right, plane.normal).normalized; //PhotonNetwork.Instantiate("Kitty", planeCenter, Quaternion.LookRotation(forward, up), 0); //PhotonManager.playerExists = true; isExists = true; FindObjectOfType <EditStage>().instantiateStage(Instantiate(spawnObject, planeCenter, Quaternion.identity)); } else { Debug.Log("surface is too steep for Stage to be placed."); } #endif }
/// <summary> /// Updates the active placed location. /// </summary> private void _UpdatePlacedLocation() { if (Input.touchCount != 1) { return; } Touch t = Input.GetTouch(0); if (t.phase != TouchPhase.Began) { return; } Camera cam = m_arScreen.m_renderCamera; // Find the plane for the selected point. Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, t.position, TAP_PIXEL_TOLERANCE, MIN_PLANE_FIT_PERCENTAGE, out planeCenter, out plane)) { return; } if (m_placedLocation) { m_placedLocation.SendMessage("Hide"); } m_placedLocation = (GameObject)Instantiate(m_prefabLocation, planeCenter, Quaternion.FromToRotation(Vector3.up, plane.normal)); }
/// <summary> /// Wait for the next depth update, then find the plane at the touch position. /// </summary> /// <returns>Coroutine IEnumerator.</returns> /// <param name="touchPosition">Touch position to find a plane at.</param> private IEnumerator _WaitForDepthAndFindPlane(Vector2 touchPosition) { m_findPlaneWaitingForDepth = true; Debug.Log("touch at " + touchPosition); // Turn on the camera and wait for a single depth update. m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM); while (m_findPlaneWaitingForDepth) { yield return(null); } Debug.Log("Got depth"); m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.DISABLED); // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("Cannot find plane"); yield break; } // Ensure the location is always facing the camera. This is like a LookRotation, but for the Y axis. Vector3 up = plane.normal; Vector3 forward; if (Vector3.Angle(plane.normal, cam.transform.forward) < 175) { Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized; forward = Vector3.Cross(right, up).normalized; } else { // Normal is nearly parallel to camera look direction, the cross product would have too much // floating point error in it. forward = Vector3.Cross(up, cam.transform.right); } m_adding = true; if (m_placedObject != null) { Destroy(m_placedObject); } m_placedObject = (GameObject)Instantiate(m_prefabMarker, planeCenter, Quaternion.LookRotation(forward, up)); m_placedObject.gameObject.SetActive(true); IGetSculptController[] comps = m_placedObject.GetComponentsInChildren <IGetSculptController>(); if (comps != null && comps.Length > 0) { foreach (IGetSculptController c in comps) { c.RegisterSculptController(this); } } m_selectedMarker = null; }
private void SetMarker() { Vector3 centerPos = Vector3.zero; Plane centerPlane = new Plane(); Vector2 centerScreenPos = new Vector2(Screen.width / 2f, Screen.height / 2f); if (TangoPC.FindPlane(ARCamera, centerScreenPos, out centerPos, out centerPlane)) { Marker.transform.position = centerPos; Marker.transform.up = centerPlane.normal; } }
void PlaceObject(Vector2 touchPosition) { // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("cannot find plane."); return; } // Place object on the surface, and make it always face the camera. if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f && m_instObject == null) { if (!weHaveGround) { weHaveGround = true; m_planeGO.transform.position = planeCenter; m_instPlaneGO = Instantiate(m_planeGO, planeCenter, Quaternion.identity); m_instPlaneGO.name = "MyPlane"; } Vector3 up = plane.normal; Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; Vector3 forward = Vector3.Cross(right, plane.normal).normalized; Vector3 dropPos = new Vector3(planeCenter.x, planeCenter.y + 2, planeCenter.z); m_instObject = Instantiate(m_selectedObject, dropPos, Quaternion.LookRotation(forward, up)); if (m_selectedObject.name == "Sphere" || m_selectedObject.name == "Cube") { list.Add(m_instObject); m_instObject = null; } } else { Debug.Log("surface is too steep for object to stand on."); } }
/// <summary> /// Wait for the next depth update, then find the plane at the touch position. /// </summary> /// <returns>Coroutine IEnumerator.</returns> /// <param name="touchPosition">Touch position to find a plane at.</param> private IEnumerator _WaitForDepthAndFindPlane(Vector2 touchPosition) { m_findPlaneWaitingForDepth = true; // Turn on the camera and wait for a single depth update. m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM); while (m_findPlaneWaitingForDepth) { yield return(null); } m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.DISABLED); // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { yield break; } // Ensure the location is always facing the camera. This is like a LookRotation, but for the Y axis. Vector3 up = plane.normal; Vector3 forward; if (Vector3.Angle(plane.normal, cam.transform.forward) < 175) { Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized; forward = Vector3.Cross(right, up).normalized; } else { // Normal is nearly parallel to camera look direction, the cross product would have too much // floating point error in it. forward = Vector3.Cross(up, cam.transform.right); } #region Cfd _markerPositions.Add(planeCenter); if (_markerPositions.Count >= 4) { CalculateTransform(); } #endregion Instantiate(m_prefabMarker, planeCenter, Quaternion.LookRotation(forward, up)); m_selectedMarker = null; }
bool GetScreenRaycastHit(float x, float y, out Vector3 hitPosition) { bool ok; if (usingTango && (Phase == CalibrationPhase.Point1Scan || Phase == CalibrationPhase.Point2Scan)) { var screenPos = new Vector2(x, y); Plane plane; ok = m_pointCloud.FindPlane(Camera.main, screenPos, out hitPosition, out plane); } else { var ray = Camera.main.ScreenPointToRay(new Vector3(x, y, 0)); RaycastHit hit; ok = Physics.Raycast(ray, out hit); hitPosition = hit.point; } return(ok); }
void placeObject(Vector2 touchPosition) { // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("cannot find plane."); return; } // Place Object on the surface, and make it always face the camera. Vector3 up = plane.normal; Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; Vector3 forward = Vector3.Cross(right, plane.normal).normalized; Instantiate(m_object, planeCenter, Quaternion.LookRotation(forward, up)); }
void PlaceKitten(Vector2 touchPosition) { // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("cannot find plane."); return; } GameObject realKittyAugmented = null; // Place kitten on the surface, and make it always face the camera. if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f) { //horizontal and sloped plane behaviour Vector3 up = plane.normal; Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; Vector3 forward = Vector3.Cross(right, plane.normal).normalized; if (realKittyAugmented == null) { realKittyAugmented = Instantiate(realKitty, planeCenter, Quaternion.LookRotation(forward, up)); } } else { //Debug.Log("surface is too steep for kitten to stand on."); //Verticle plane behaviour Vector3 up = plane.normal; vertualPlaneNormal = plane.normal; Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; Vector3 forward = Vector3.Cross(right, plane.normal).normalized; virtualPlane = Instantiate(m_kitten, planeCenter, Quaternion.LookRotation(forward, up)); //instantiatedObject.transform.Rotate(Input.gyro.gravity);// = (Input.gyro.gravity); planeReady = true; } }
void PlaceItem(Vector2 touchPosition) { // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { Debug.Log("cannot find plane."); return; } // Place kitten on the surface, and make it always face the camera. if (Vector3.Angle(plane.normal, Vector3.up) < 30.0f) { Vector3 up = plane.normal; Vector3 right = Vector3.Cross(plane.normal, cam.transform.forward).normalized; Vector3 forward = Vector3.Cross(right, plane.normal).normalized; GameObject temp = menuScript.currentItem; if (temp == null) { Debug.Log("Item not found."); } else { GameObject tempGameObject = Instantiate(menuScript.currentItem, planeCenter, Quaternion.LookRotation(forward, up)); itemSpawnPos = planeCenter; menuScript.currentItem = tempGameObject; //menuScript.currentItem.GetComponent<SetAllScripts>().ToggleOutline(true); } } else { Debug.Log("surface is too steep for Object to stand on."); } }
/// <summary> /// Wait for the next depth update, then find the plane at the touch position. /// </summary> /// <returns>Coroutine IEnumerator.</returns> /// <param name="touchPosition">Touch position to find a plane at.</param> /// <param name="doorCorridorPosition">Door position where to put the door marker.</param> /// <param name="wall">Touch position to find a plane at.</param> /// <param name="doorCorridorBool">True if we are putting a door marker.</param> private IEnumerator _WaitForDepthAndFindPlane(Vector2 touchPosition, Vector3 doorCorridorPosition = new Vector3(), Wall wall = null, bool doorCorridorBool = false) { if (!doorCorridorBool) { if (m_currentMarkType == 1 || m_currentMarkType == 2) { AndroidHelper.ShowAndroidToastMessage("Door or corridors must be on walls"); yield break; } } m_findPlaneWaitingForDepth = true; // Turn on the camera and wait for a single depth update. m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM); while (m_findPlaneWaitingForDepth) { yield return(null); } m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.DISABLED); // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; // If I'm selecting the floor, set the floor plane if (m_currentMarkType == 3) { if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { yield break; } m_pointCloud.FindFloor(); floor = new Floor(plane, planeCenter); Debug.Log("Height of the floor " + planeCenter.y); AndroidHelper.ShowAndroidToastMessage("Floor found!"); yield break; } else if (!m_pointCloud.m_floorFound) { AndroidHelper.ShowAndroidToastMessage("Please, select first the floor"); yield break; } // Find me the point I touched on screen on world coordinates if I'm not putting a door... if (!doorCorridorBool) { int i = m_pointCloud.FindClosestPoint(cam, touchPosition, 10); if (i < 0) { yield break; } planeCenter = m_pointCloud.m_points [i]; // Put the marker on the floor height planeCenter.y = floor.floorCenter.y; } //... else my door position is already set as parameter. else { planeCenter = doorCorridorPosition; } // Ensure the location is always facing the camera. This is like a LookRotation, but for the Y axis. Vector3 up = floor.floorPlane.normal; Vector3 forward; if (Vector3.Angle(floor.floorPlane.normal, cam.transform.forward) < 175) { Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized; forward = Vector3.Cross(right, up).normalized; } else { // Normal is nearly parallel to camera look direction, the cross product would have too much // floating point error in it. forward = Vector3.Cross(up, cam.transform.right); } // Instantiate marker object. newMarkObject = Instantiate(m_markPrefabs [m_currentMarkType], planeCenter, Quaternion.LookRotation(forward, up)) as GameObject; newMarkObject.tag = "CornersAndWalls"; ARMarker markerScript = newMarkObject.GetComponent <ARMarker> (); markerScript.m_type = m_currentMarkType; markerScript.m_timestamp = (float)m_poseController.LastPoseTimestamp; Matrix4x4 uwTDevice = Matrix4x4.TRS(m_poseController.transform.position, m_poseController.transform.rotation, Vector3.one); Matrix4x4 uwTMarker = Matrix4x4.TRS(newMarkObject.transform.position, newMarkObject.transform.rotation, Vector3.one); markerScript.m_deviceTMarker = Matrix4x4.Inverse(uwTDevice) * uwTMarker; m_markerList.Add(newMarkObject); prevTouchPos = null; selectedLine = null; m_selectedMarker = null; switch (m_currentMarkType) { // Corner case. case 0: corners.Add(new Corner(newMarkObject)); break; // Door case. case 1: TouchScreenKeyboard kb = TouchScreenKeyboard.Open("", TouchScreenKeyboardType.Default, false, false, false, false, "Write the name for this destination marker"); while (!kb.done && !kb.wasCanceled) { yield return(null); } newMarkObject.name = kb.text; doors.Add(new Corner(newMarkObject, wall)); break; default: Debug.Log("No valid point inserted."); break; } }
/// <summary> /// Wait for the next depth update, then find the plane at the touch position. /// </summary> /// <returns>Coroutine IEnumerator.</returns> /// <param name="touchPosition">Touch position to find a plane at.</param> private IEnumerator _WaitForDepthAndFindPlane(Vector2 touchPosition) { m_findPlaneWaitingForDepth = true; // Turn on the camera and wait for a single depth update. m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.MAXIMUM); while (m_findPlaneWaitingForDepth) { yield return(null); } m_tangoApplication.SetDepthCameraRate(TangoEnums.TangoDepthCameraRate.DISABLED); // Find the plane. Camera cam = Camera.main; Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, touchPosition, out planeCenter, out plane)) { yield break; } // Ensure the location is always facing the camera. This is like a LookRotation, but for the Y axis. Vector3 up = plane.normal; Vector3 forward; if (Vector3.Angle(plane.normal, cam.transform.forward) < 175) { Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized; forward = Vector3.Cross(right, up).normalized; } else { // Normal is nearly parallel to camera look direction, the cross product would have too much // floating point error in it. forward = Vector3.Cross(up, cam.transform.right); } // Instantiate produce object. newProduceObject = Instantiate(m_producePrefabs[m_currentProduceType], planeCenter, Quaternion.LookRotation(forward, up)) as GameObject; ARProduce produceScript = newProduceObject.GetComponent <ARProduce>(); produceScript.m_type = m_currentProduceType; produceScript.m_timestamp = (float)m_poseController.LastPoseTimestamp; Matrix4x4 uwTDevice = Matrix4x4.TRS(m_poseController.transform.position, m_poseController.transform.rotation, Vector3.one); Matrix4x4 uwTProduce = Matrix4x4.TRS(newProduceObject.transform.position, newProduceObject.transform.rotation, Vector3.one); produceScript.m_deviceTProduce = Matrix4x4.Inverse(uwTDevice) * uwTProduce; m_produceList.Add(newProduceObject); m_selectedProduce = null; }
/// <summary> /// Update location marker state. /// </summary> private void _UpdateLocationMarker() { if (Input.touchCount == 1) { // Single tap -- place new location or select existing location. Touch t = Input.GetTouch(0); Vector2 guiPosition = new Vector2(t.position.x, Screen.height - t.position.y); Camera cam = m_arScreen.m_renderCamera; RaycastHit hitInfo; if (t.phase != TouchPhase.Began) { return; } if (m_selectedRect.Contains(guiPosition) || m_hideAllRect.Contains(guiPosition)) { // do nothing, the button will handle it } else if (Physics.Raycast(cam.ScreenPointToRay(t.position), out hitInfo)) { // Found a marker, select it (so long as it isn't disappearing)! GameObject tapped = hitInfo.collider.gameObject; if (tapped.GetComponent <ARLocationCharacter>()) { tapped.GetComponent <ARLocationCharacter>().beDamaged(55); } else if (tapped.GetComponent <ARLocationSpawnPoint>()) { tapped.GetComponent <ARLocationSpawnPoint>().beDamaged(55); } else { Vector3 planeCenter; // Ensure the location is always facing the camera. This is like a LookRotation, but for the Y axis. Vector3 up = new Vector3(0, 1, 0); Vector3 forward; if (Vector3.Angle(up, cam.transform.forward) < 175) { Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized; forward = Vector3.Cross(right, up).normalized; } else { // Normal is nearly parallel to camera look direction, the cross product would have too much // floating point error in it. forward = Vector3.Cross(up, cam.transform.right); } if (mode == MODE.ZOMBIE) { GameObject zombie = Instantiate(m_prefabLocation, hitInfo.point + new Vector3(0, 0.1f, 0), Quaternion.LookRotation(forward * -1, up)) as GameObject; //zombie.GetComponent<Rigidbody>().velocity = (forward * -1 /2); } else if (mode == MODE.COFFIN) { Instantiate(coffin, hitInfo.point, Quaternion.LookRotation(forward * -1, up)); } else if (mode == MODE.SHOOT) { GameObject ball = Instantiate(bullet, Camera.main.transform.position - (Camera.main.transform.up * bullet.transform.localScale.y), Quaternion.LookRotation(forward * -1, up)) as GameObject; ball.GetComponent <Rigidbody>().velocity = (Camera.main.transform.forward * 5) + (Camera.main.transform.up * 5 / 2); } m_selectedMarker = null; } } else { // Place a new point at that location, clear selection Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, t.position, TAP_PIXEL_TOLERANCE, MIN_PLANE_FIT_PERCENTAGE, out planeCenter, out plane)) { return; } // Ensure the location is always facing the camera. This is like a LookRotation, but for the Y axis. Vector3 up = plane.normal; Vector3 forward; if (Vector3.Angle(plane.normal, cam.transform.forward) < 175) { Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized; forward = Vector3.Cross(right, up).normalized; } else { // Normal is nearly parallel to camera look direction, the cross product would have too much // floating point error in it. forward = Vector3.Cross(up, cam.transform.right); } if (mode == MODE.ZOMBIE) { Instantiate(m_prefabLocation, planeCenter, Quaternion.LookRotation(forward * -1, up)); } else if (mode == MODE.COFFIN) { Instantiate(coffin, planeCenter, Quaternion.LookRotation(forward * -1, up)); } else if (mode == MODE.SHOOT) { GameObject ball = Instantiate(bullet, Camera.main.transform.position - (Camera.main.transform.up * bullet.transform.localScale.y), Quaternion.LookRotation(forward * -1, up)) as GameObject; ball.GetComponent <Rigidbody>().velocity = (Camera.main.transform.forward * 5) + (Camera.main.transform.up * 5 / 2); } m_selectedMarker = null; } } if (Input.touchCount == 2) { // Two taps -- toggle debug text Touch t0 = Input.GetTouch(0); Touch t1 = Input.GetTouch(1); if (t0.phase != TouchPhase.Began && t1.phase != TouchPhase.Began) { return; } m_showDebug = !m_showDebug; return; } if (Input.touchCount != 1) { return; } }
/// <summary> /// Update location marker state. /// </summary> private void _UpdateLocationMarker() { if (Input.touchCount == 1) { // Single tap -- place new location or select existing location. Touch t = Input.GetTouch(0); // first finger Vector2 guiPosition = new Vector2(t.position.x, Screen.height - t.position.y); Camera cam = m_arScreen.m_renderCamera; RaycastHit hitInfo; if (t.phase != TouchPhase.Began) { return; } if (m_selectedRect.Contains(guiPosition) || m_hideAllRect.Contains(guiPosition)) { // do nothing, the button will handle it } else if (Physics.Raycast(cam.ScreenPointToRay(t.position), out hitInfo)) { // Found a marker, select it (so long as it isn't disappearing)! GameObject tapped = hitInfo.collider.gameObject; if (!tapped.GetComponent <Animation>().isPlaying) { m_selectedMarker = tapped.GetComponent <ARLocationMarker>(); } } else { // Place a new point at that location, clear selection Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, t.position, TAP_PIXEL_TOLERANCE, MIN_PLANE_FIT_PERCENTAGE, out planeCenter, out plane)) { return; } // Ensure the location is always facing the camera. This is like a LookRotation, but for the Y axis. Vector3 up = plane.normal; Vector3 forward; if (Vector3.Angle(plane.normal, cam.transform.forward) < 175) { Vector3 right = Vector3.Cross(up, cam.transform.forward).normalized; forward = Vector3.Cross(right, up).normalized; } else { // Normal is nearly parallel to camera look direction, the cross product would have too much // floating point error in it. forward = Vector3.Cross(up, cam.transform.right); } if (!portalMade) { Instructs.gameObject.SetActive(false); portalMade = true; portal.transform.position = planeCenter; portal.SetActive(true); } m_selectedMarker = null; } } if (Input.touchCount == 2) { // Two taps -- toggle debug text Touch t0 = Input.GetTouch(0); Touch t1 = Input.GetTouch(1); if (t0.phase != TouchPhase.Began && t1.phase != TouchPhase.Began) { return; } m_showDebug = !m_showDebug; return; } if (Input.touchCount != 1) { return; } }
/// <summary> /// Update location marker state. /// </summary> private void _UpdateLocationMarker() { if (Input.touchCount == 1) { // Single tap -- place new location or select existing location. Touch t = Input.GetTouch(0); Vector2 guiPosition = new Vector2(t.position.x, Screen.height - t.position.y); Camera cam = m_arScreen.m_renderCamera; RaycastHit hitInfo; if (t.phase != TouchPhase.Began) { return; } if (m_selectedRect.Contains(guiPosition) || m_hideAllRect.Contains(guiPosition)) { // do nothing, the button will handle it } else if (Physics.Raycast(cam.ScreenPointToRay(t.position), out hitInfo)) { // Found a marker, select it (so long as it isn't disappearing)! GameObject tapped = hitInfo.collider.gameObject; if (!tapped.GetComponent <Animation>().isPlaying) { m_selectedMarker = tapped.GetComponent <ARLocationMarker>(); } } else { // Place a new point at that location, clear selection Vector3 planeCenter; Plane plane; if (!m_pointCloud.FindPlane(cam, t.position, TAP_PIXEL_TOLERANCE, MIN_PLANE_FIT_PERCENTAGE, out planeCenter, out plane)) { return; } Instantiate(m_prefabLocation, planeCenter, Quaternion.FromToRotation(Vector3.up, plane.normal)); m_selectedMarker = null; } } if (Input.touchCount == 2) { // Two taps -- toggle debug text Touch t0 = Input.GetTouch(0); Touch t1 = Input.GetTouch(1); if (t0.phase != TouchPhase.Began && t1.phase != TouchPhase.Began) { return; } m_showDebug = !m_showDebug; return; } if (Input.touchCount != 1) { return; } }