private void PlaneChanged(ARPlanesChangedEventArgs args) { if (args.added != null && placedObject_flask == null) { ARPlane arPlane = args.added[0]; var rotation_flask = Quaternion.LookRotation(arPlane.transform.position); rotation_flask *= Quaternion.Euler(0, 260, 10); placedObject_flask = Instantiate(placedPrefab_flask, arPlane.transform.position, rotation_flask); placedObject_flask.transform.localScale -= new Vector3(0.5F, 0.5F, 0.5F); //var text_rescued = Instantiate(TextPrefab); //var position = arPlane.transform.position; //text_rescued.gameObject.transform.position = new Vector3(position.x, position.y + 0.1f, position.z); ////Instantiates the Object //GameObject rescuedTextBox = (GameObject)Instantiate(rescuedText, arPlane.transform.position, rotation_rescued); ////Grabs the TextMesh component from the game object //TextMesh theText = rescuedTextBox.transform.GetComponent<TextMesh>(); ////Sets the text. //theText.text = "The Text"; //var rotation_rescuer = Quaternion.LookRotation(arPlane.transform.position); //rotation_rescuer *= Quaternion.Euler(90, -90, 90); var rotation_bottle = Quaternion.LookRotation(placedObject_flask.transform.position); rotation_bottle *= Quaternion.Euler(0, 180, 0); //placedObject_bottle = Instantiate(placedPrefab_bottle, new Vector3((arPlane.transform.position.x)-0.2F,(arPlane.transform.position.y) + 0.42F, (arPlane.transform.position.z) + 0.45F), rotation_rescuer); placedObject_bottle = Instantiate(placedPrefab_bottle, new Vector3((placedObject_flask.transform.position.x) - 0.25F, (placedObject_rescued.transform.position.y) - 0.07F, (placedObject_rescued.transform.position.z) + 0.1F), rotation_rescuer); placedObject_bottle.transform.localScale -= new Vector3(0.5F, 0.5F, 0.5F); } }
private void Update() { if (!TryGetTouchPosition(out Vector2 touchPosition) || IsObjectPlaced || _placedObject == null) { return; } if (arRaycastManager.Raycast(touchPosition, hits, TrackableType.PlaneWithinPolygon)) { var hitPose = hits[0].pose; GameObject instantiatedObject = Instantiate(_placedObject, hitPose.position, hitPose.rotation); instantiatedObject.SetActive(true); instantiatedObject.AddComponent <ContentScaler>(); IsObjectPlaced = true; ARPlane hitPlane = arPlaneManager.GetPlane(hits[0].trackableId); if (hitPlane != null) { Debug.Log("Plane is found"); ARAnchor anchor = arAnchorManager.AttachAnchor(hitPlane, hitPose); Debug.Log("Anchor is attached to plane : " + anchor.name); anchor.transform.SetParent(instantiatedObject.transform); } StopPlaneDetection(); } }
private void PlaneChanged(ARPlanesChangedEventArgs args) { if (args.added != null && placedObject1 == null) { ARPlane arPlane = args.added[0]; Vector3 cameraPosition = arCamera.transform.position; Vector3 planePosition = arPlane.transform.position; //place on plane pos placedObject1 = Instantiate(objectArray[Random.Range(0, objectArray.Length)], planePosition, Quaternion.Euler(0, Random.Range(0, 360), 0)); // 2 ~ 3 meters away, random x (-2~2) Vector3 objectPosition1 = new Vector3(planePosition.x + Random.Range(-2f, 2f), planePosition.y, cameraPosition.z + Random.Range(2f, 3f)); placedObject2 = Instantiate(objectArray[Random.Range(0, objectArray.Length)], objectPosition1, Quaternion.Euler(0, Random.Range(0, 360), 0)); // 4 ~ 5 meters //Vector3 objectPosition2 = new Vector3(planePosition.x + Random.Range(-2f, 2f), planePosition.y, cameraPosition.z + Random.Range(4f, 5f)); //placedObject3 = Instantiate(objectArray[Random.Range(0, objectArray.Length)], objectPosition2, Quaternion.Euler(0, Random.Range(0, 360), 0)); // 6 ~ 7 meters //Vector3 objectPosition3 = new Vector3(planePosition.x + Random.Range(-2f, 2f), planePosition.y, cameraPosition.z + Random.Range(6f, 7f)); //placedObject4 = Instantiate(objectArray[Random.Range(0, objectArray.Length)], objectPosition3, Quaternion.Euler(0, Random.Range(0, 360), 0)); // 8 ~ 9 meter //Vector3 objectPosition4 = new Vector3(planePosition.x + Random.Range(-2f, 2f), planePosition.y, cameraPosition.z + Random.Range(8f, 9f)); //placedObject5 = Instantiate(objectArray[Random.Range(0, objectArray.Length)], objectPosition4, Quaternion.Euler(0, Random.Range(0, 360), 0)); } }
// Update is called once per frame void Update() { if (pm.trackables.count > 0 && !firstPlane) { foreach (var plane in pm.trackables) { if (!firstPlane) { myPlane = plane;//Get the first plane Generated firstPlane = true; } } planeDetectGUI.text = "Plane Detected!";//Updates GUI planeDetectGUI.color = Color.green; myCube = Instantiate(cubePrefab, new Vector3(0f, cubePrefab.transform.localScale.y, 0f), Quaternion.identity); aud.Play(); sessionOrigin.MakeContentAppearAt(myCube.transform, myPlane.center, Quaternion.identity);//Places cube at centre of the found plane myPlane.boundaryChanged += UpdateCube;//Repositions the cube whenever the plane is resized } }
private void UpdatePlacementPose() { if (Camera.current != null) { var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f)); var hits = new List <ARRaycastHit>(); arRaycast.Raycast(screenCenter, hits, TrackableType.PlaneWithinBounds); placementPoseIsValid = hits.Count > 0; if (placementPoseIsValid) { var trackId = hits[0].trackableId; placementPlane = arPlanes.GetPlane(trackId); placementPose.position = placementPlane.center; //Set rotation of indicator to follow phones forward. var cameraForward = Camera.current.transform.forward; var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized; placementPose.rotation = Quaternion.LookRotation(cameraBearing); //Maybe rotated according to the planes normal? } } }
private void PlaneChanged(ARPlanesChangedEventArgs args) { if (args.updated != null && isPlacedObj == false) //if (args.added != null && placedObject==null) { foreach (var plane in args.updated) { ARPlane arPlane = plane; txtIsPlaced.text = arPlane.size.ToString(); string str = String.Format("***************** arPlane.size.x : {0}, arPlane.size.y : {1}", arPlane.size.x, arPlane.size.y); Debug.Log(str); for (int i = 0; i < 5; i++) { Vector3 pos = arPlane.center; pos.x += UnityEngine.Random.Range(-1.5f, 1.5f); //pos.y += UnityEngine.Random.Range(-0.03f, 0.03f); pos.y = arPlane.transform.position.y; pos.z += UnityEngine.Random.Range(-1.5f, 1.5f); placedObject[i] = Instantiate(placedPrefab, pos, Quaternion.identity); } Debug.Log("***************** 오브젝트 생성됨"); isPlacedObj = true; //StartCoroutine(coTimedelay(arPlane)); break; } } }
private void Update() { ARRaycastHit hitInfo; ARPlane trackedPlane; bool wasHit = _arHitController.CheckHitOnPlane(out hitInfo, out trackedPlane); if (!_downLastFrame && wasHit) { _downLastFrame = true; print("found hit on plane: " + hitInfo.pose.position); if (_gameState == GameState.Searching) { _gameState = GameState.PlaneFound; SetAnchorToPlane(trackedPlane); _networkManager.SendAnchor(trackedPlane); _arPlane = trackedPlane; } else if (_gameState == GameState.PlaneFound) { GameObject spawnedObject = SpawnObject(hitInfo.pose); // since object is parented to an anchor, we send local info since on recieving end it should also be parented to an anchor _networkManager.SendModelSpawn(spawnedObject.transform.localPosition, spawnedObject.transform.localRotation); } } _downLastFrame = wasHit; }
private void Update() { if (m_RaycastManager.Raycast(m_Center, s_Hits, TrackableType.PlaneWithinPolygon)) { Pose hitPose = s_Hits[0].pose; if (spawnedFocus == null) { spawnedFocus = Instantiate(m_FocusPrefab, hitPose.position, hitPose.rotation); //if (onPlacedFocus != null) //{ // onPlacedFocus(); //} } spawnedFocus.transform.localPosition = hitPose.position; ARPlane plane = m_planeManager.GetPlane(s_Hits[0].trackableId); if (plane.alignment == PlaneAlignment.Vertical) { Vector3 eulerAngles = new Vector3(90f, plane.transform.localEulerAngles.y, 0f); spawnedFocus.transform.GetChild(0).rotation = Quaternion.Euler(eulerAngles); } else { spawnedFocus.transform.GetChild(0).rotation = Quaternion.Euler(Vector3.zero); } } }
private void UpdatePlacementPose() { var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f)); arRaycastManager.Raycast(screenCenter, hits, UnityEngine.XR.ARSubsystems.TrackableType.Planes); ARPlane playerPlane = null; if (currentPlayerTurn == PlayerTurn.Blue) { playerPlane = gameManager.bluePlane; } else if (currentPlayerTurn == PlayerTurn.Red) { playerPlane = gameManager.redPlane; } placementPoseIsValid = (hits.Count > 0 && planeManager.GetPlane(hits[0].trackableId) == playerPlane); 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); } }
void Update() { if (PlayerTracker.instance.CurrentPlanetState != PlanetState.FOUND) { return; } ARPlane largestPlane = null; foreach (ARPlane plane in manager.trackables) { float size = 0; // check for tracking quality if (plane.trackingState == UnityEngine.XR.ARSubsystems.TrackingState.Tracking) { float planeExtends = plane.extents.x * plane.extents.y; if (planeExtends > size) { size = planeExtends; largestPlane = plane; } } } if (largestPlane) { PlayerTracker.instance.CurrentPlanetState = PlanetState.PLACEABLE; placeableLocation = largestPlane.center; } }
private void Awake() { wall = GetComponentInChildren <Wall>(); plane = GetComponent <ARPlane>(); plane.boundaryChanged += OnBoundaryChanged; }
/// <summary> /// もぐらオブジェクト生成処理 /// </summary> /// <param name="placedPrefab">もぐらPrefab</param> /// <returns>もぐらオブジェクト</returns> private GameObject GenerateMoleObj(GameObject placedPrefab) { // もぐらをプレーンの上に生成 ARPlane alPlane = playArPlaneList[Random.Range(0, playArPlaneList.Count)]; Transform planeTransform = alPlane.gameObject.transform; // ランダム位置に生成 float width = alPlane.gameObject.GetComponent <Renderer>().bounds.size.x; float height = alPlane.gameObject.GetComponent <Renderer>().bounds.size.z; float randomWidth = Random.Range(-width / 2, width / 2); float randomHeight = Random.Range(-height / 2, height / 2); if (planeTransform.rotation.eulerAngles.x >= 45) { // オブジェクトの角度が45度以上(垂直) // 位置 x: 平面 + ランダム, y: 平面 + ランダム, z: 平面 // 回転 x: 平面 * -1, y: 平面 + 180, z:平面 return(Instantiate(placedPrefab, new Vector3(planeTransform.position.x + randomWidth, planeTransform.position.y + randomHeight, planeTransform.position.z), Quaternion.Euler(planeTransform.rotation.eulerAngles.x * -1, planeTransform.rotation.eulerAngles.y + 180.0f, planeTransform.rotation.eulerAngles.z))); } else { // オブジェクトの角度が0度(水平) // 位置 x: 平面 + ランダム, y: 平面, z: 平面 + ランダム // 回転 x: 平面 * -1, y: 平面 + 180, z:平面 return(Instantiate(placedPrefab, new Vector3(planeTransform.position.x + randomWidth, planeTransform.position.y, planeTransform.position.z + randomHeight), Quaternion.Euler(planeTransform.rotation.eulerAngles.x * -1, planeTransform.rotation.eulerAngles.y + 180.0f, planeTransform.rotation.eulerAngles.z))); } }
public void SelectPlaneButton() { dbox.PrintLine("Button pressed"); #if UNITY_EDITOR PrepStuff(sceneQuad); #else if (gm.arPlaneList.Count == 0) { dbox.PrintLine("Need at least one detected plane"); } else { dbox.PrintLine("Setup Started"); ARPlane maxPlane = null; float maxArea = -1; foreach (ARPlane plane in gm.arPlaneList) { if (plane.size.x * plane.size.y > maxArea) { maxArea = plane.size.x * plane.size.y; maxPlane = plane; } } GameObject quad = Instantiate(quadPrefab, maxPlane.center, Quaternion.identity); quad.transform.localScale = new Vector3(maxPlane.size.x, quad.transform.localScale.y, maxPlane.size.y); quad.transform.rotation = Quaternion.Euler(90, 0, 0); PrepStuff(quad); } #endif }
public void PlaceObjectOnPlane(Pose pose, ARPlane plane) { var objectToPlaceGameObject = objectToPlace; if (placeMultiple) { objectToPlaceGameObject = Instantiate(objectToPlace); } objectToPlaceGameObject.SetActive(true); objectToPlaceGameObject.transform.position = pose.position + new Vector3(0, verticalOffset, 0); objectToPlaceGameObject.transform.rotation = pose.rotation; if (plane.alignment.Equals(PlaneAlignment.HorizontalUp | PlaneAlignment.HorizontalDown)) { objectToPlaceGameObject.transform.LookAt(new Vector3( mainCamera.transform.position.x, objectToPlaceGameObject.transform.position.y, mainCamera.transform.position.z )); } GameObjectPlaced?.Invoke(objectToPlaceGameObject); }
void Start() { plane = FloorPlacementController.savePlane; randomTarget = new Vector3(plane.center.x + Random.Range(-1f, 1f), transform.position.y, plane.center.z + Random.Range(-1f, 1f)); normalizeDirection = (randomTarget - transform.position).normalized; transform.LookAt(randomTarget); }
private void ProcessPlane(ARPlane plane) { // Debug.Log("[AR+GPS][GroundHeight#ProcessPlane]: Processing plane " + plane.trackableId.subId1 + ", " + plane.trackableId.subId2); if (plane.alignment != PlaneAlignment.HorizontalDown && plane.alignment != PlaneAlignment.HorizontalUp) { // Debug.LogWarning("[AR+GPS][GroundHeight#ProcessPlane]: Wrong plane alignment!"); return; } if (!IsValidHeightForGround(plane.center.y)) { // Debug.LogWarning("[AR+GPS][GroundHeight#ProcessPlane]: Invalid plane height!"); return; } var distance = MathUtils.HorizontalDistance(transform.position, plane.center); if (!(state.CurrentPlaneDistance < 0) && (distance >= state.CurrentPlaneDistance)) { // Debug.LogWarning("[AR+GPS][GroundHeight#ProcessPlane]: Plane too far!"); return; } // Debug.Log("[AR+GPS][GroundHeight#ProcessPlane]: New plane Y: " + plane.center.y); state.CurrentPlaneDistance = distance; state.CurrentGroundY = plane.center.y; state.CurrentPlaneCenter = plane.center; state.NeedsUpdate = true; }
void Start() { _plane = GetComponent <ARPlane>(); _renderer = GetComponent <Renderer>(); _lineRenderer = GetComponent <LineRenderer>(); UpdateRenderer(); }
public void PlaceFurniture(ARRaycastHit raycastHit) { ARPlane plane = planeManager.GetPlane(raycastHit.trackableId); if (plane.alignment != UnityEngine.XR.ARSubsystems.PlaneAlignment.HorizontalUp) { Debug.Log("Incompatible plane allignment " + plane.alignment); return; } // does the selected prefab fit? bool fits = CheckIfSurfaceIsBigEnough(activeFurniture.FurnitureMeshFilter.mesh, plane); if (fits) { if (activeFurniture.IsColliding()) { Debug.Log("Can't place due to collision"); } else { activeFurniture.Place(); CreateNewActiveFurniture(); Debug.Log("Placed prefab"); } } else { Debug.Log("prefab doesn't fit to surface"); } }
private bool CheckIfSurfaceIsBigEnough(Mesh mesh, ARPlane plane) { float meshSize = mesh.bounds.size.x * mesh.bounds.size.z; float planeSize = plane.size.x * plane.size.y; return(meshSize < planeSize); }
private void ProcessPlane(ARPlane plane) { if (plane.alignment != PlaneAlignment.HorizontalDown && plane.alignment != PlaneAlignment.HorizontalUp) { return; } if (!IsValidHeightForGround(plane.center.y)) { return; } var distance = MathUtils.HorizontalDistance(transform.position, plane.center); if (!(state.CurrentPlaneDistance < 0) && (distance >= state.CurrentPlaneDistance)) { return; } state.CurrentPlaneDistance = distance; state.CurrentGroundY = plane.center.y; state.CurrentPlaneCenter = plane.center; state.NeedsUpdate = true; }
IEnumerator MouseHandler() { inMouseHandler = true; Ray ray = arCamera.ScreenPointToRay(Input.mousePosition); RaycastHit rayHit; if (Physics.Raycast(ray, out rayHit)) { ARPlane arPlane = rayHit.collider.gameObject.GetComponent <ARPlane>(); if (arPlane != null) { bool isHorizontal = (arPlane.boundedPlane.Alignment & PlaneAlignment.Horizontal) != 0; if (isHorizontal) { tappedPosition = arPlane.boundedPlane.Pose.position; tappedRotation = arPlane.boundedPlane.Pose.rotation; tappedMinDimension = Mathf.Min(arPlane.boundedPlane.Width, arPlane.boundedPlane.Height); doneChoosingPlane = true; } } } inMouseHandler = false; yield return(null); }
void Update() { if (selectPlane.enabled && aRPlaneManager.trackables.count > 0) { selectPlane.text = "Select A Plane"; } if (!TryGetTouchPosition(out Vector2 touchPosition)) { return; } if (!surfaceFound && Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began) { if (aRRaycastManager.Raycast(touchPosition, hits, TrackableType.PlaneWithinPolygon)) { savePlane = aRPlaneManager.GetPlane(hits[0].trackableId); hitPose = hits[0].pose; foreach (var plane in aRPlaneManager.trackables) { if (hits[0].trackableId != plane.trackableId) { plane.gameObject.SetActive(false); } } aRPlaneManager.enabled = false; selectPlane.enabled = false; startButton.SetActive(true); surfaceFound = true; } } }
public void Place(int index) { Transform cam = Camera.main?.transform; m_ARSpace = FindObjectOfType <ARSpace>(); if (cam != null && m_objects[index] != null && m_ARSpace != null) { RaycastHit hit; Vector3 direction = cam.forward; Vector3 origin = cam.position; if (Physics.Raycast(origin, direction, out hit, Mathf.Infinity)) { if (hit.collider != null) { ARPlane arPlane = hit.collider.GetComponentInParent <ARPlane>(); if (arPlane) { Vector3 pos = hit.point; GameObject go = Instantiate(m_objects[index], m_ARSpace.transform); go.transform.localRotation = Quaternion.identity; go.transform.position = pos; } } } } }
void FixedUpdate() { if (Input.touchCount > 0 && flag == 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Ended) { if (Input.touchCount == 1) { if (m_RaycastManager.Raycast(touch.position, s_Hits, TrackableType.PlaneWithinPolygon)) { chosenPlane = m_PlaneManager.GetPlane(s_Hits[0].trackableId); Vector3 newCenter = new Vector3(chosenPlane.center.x, chosenPlane.center.y, chosenPlane.center.z); spawnedObject = Instantiate(placedPrefab, newCenter, Quaternion.identity); for (int j = 1; j < numberOfTargets; i += 0.07f, j++, k -= 0.07f) { spawnedObject = Instantiate(placedPrefab, newCenter + new Vector3(i, 0, k), Quaternion.identity); } foreach (var plane in m_PlaneManager.trackables) { plane.gameObject.SetActive(false); } startGame(); flag = 1; } } } } }
private void PlaceModel(ARPlane plane) { Debug.Log("start palce model"); ModelParent.position = plane.transform.position; //强制离人1M Vector3 cameraPos = Camera.main.transform.position; Vector3 modelParentPos = ModelParent.position; cameraPos.y = modelParentPos.y; float dis = Vector3.Distance(cameraPos, modelParentPos); ModelParent.position += (1.3f - dis) * (modelParentPos - cameraPos).normalized; Vector3 forward = Camera.main.transform.forward; forward.y = 0; ModelParent.forward = -forward.normalized; mModelParentLasetForward = ModelParent.forward; ModelClone = Instantiate <GameObject>(ModelPrefab); ModelClone.transform.SetParent(ModelParent); ModelClone.transform.localPosition = Vector3.zero; ModelClone.transform.localRotation = Quaternion.identity; ModelClone.transform.localScale = Vector3.one * 0.375f; ModelParent.transform.localScale = Vector3.zero; StartCoroutine(BornAni()); mIsClone = true; }
void OnEnable() { m_Plane = GetComponent <ARPlane>(); m_Animator = GetComponent <Animator>(); m_Plane.boundaryChanged += PlaneOnBoundaryChanged; }
}//FindObj가 호출되면 패널 비활성화 실행하기 /* public void FindObj() * { * Debug.Log("111111111"); * IntroPanel.SetActive(false); * }*/ private void PlaneChanged(ARPlanesChangedEventArgs args) // 객체가 같은지 비교하는 구조체를 매개변수로 담은 메서드 { /* args.added != null => 인식된 Plane이 존재하느냐? / isPlaceObj == false => 5개가 생성된 적이 없을 때 */ if (args.updated != null && isPlacedObj == false) { foreach (var plane in args.updated) // 처음 인식된 Plane { ARPlane arPlane = plane; // ARPlane arPlane = args.added[0]; // 처음 인식된 Plane string str = String.Format("***************** arPlane.size.x : {0}, arPlane.size.y : {1}", arPlane.size.x, arPlane.size.y); Debug.Log(str); // 적당한 크기(1.0, 1.0 이하) if (arPlane.size.x < 2.0f && arPlane.size.x > 0.0f || arPlane.size.y < 2.0f && arPlane.size.y > 0.0f) { // 5개를 생성해라 for (int i = 0; i < 5; i++) { Vector3 pos = arPlane.center; // 바닥의 중심 위치에 보물 생성하라 pos.x += UnityEngine.Random.Range(-2.0f, 2.0f); /*중심위치에서 좌우 양옆 1m내에*/ pos.y = arPlane.transform.position.y; pos.z += UnityEngine.Random.Range(-2.0f, 2.0f); /*중심위치에서 전or후 1m내에*/ placedObject[i] = Instantiate(placedPrefab, pos, Quaternion.identity); } Debug.Log("***************** 오브젝트 생성됨"); isPlacedObj = true; StartCoroutine(coTimedelay(arPlane)); break; } } } }
public void PlaceObjectOnPlane(Pose pose, ARPlane plane) { var objectToPlaceGameObject = objectToPlace; if (placeMultiple) { objectToPlaceGameObject = Instantiate(objectToPlace); } objectToPlaceGameObject.SetActive(true); objectToPlaceGameObject.transform.position = pose.position + new Vector3(0, verticalOffset, 0); objectToPlaceGameObject.transform.rotation = pose.rotation; if (plane.alignment.Equals(PlaneAlignment.None) || plane.alignment.Equals(PlaneAlignment.NotAxisAligned)) { return; } var cameraPosition = mainCamera.transform.position; objectToPlaceGameObject.transform.LookAt(new Vector3( cameraPosition.x, objectToPlaceGameObject.transform.position.y, cameraPosition.z )); GameObjectPlaced?.Invoke(objectToPlaceGameObject); }
private void PlaneUpdate(ARPlanesChangedEventArgs args) { if (args.added != null && placedPrefab == null) { ARPlane arPlane = args.added[0]; placedPrefab = Instantiate(spawnedObject, arPlane.transform.position, Quaternion.identity); } }
private void Awake() { #if !(UNITY_EDITOR || UNITY_STANDALONE) _plane = GetComponent <ARPlane>(); _planeMaterial = GetComponent <MeshRenderer>().material; _planeMeshVisualizer = GetComponent <ARPlaneMeshVisualizer>(); #endif }