Removed TOTU 103
This commit is contained in:
		
							
								
								
									
										13
									
								
								Assets/Rendering/Scripts/Furniture/Furniture.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								Assets/Rendering/Scripts/Furniture/Furniture.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| using JetBrains.Annotations; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using UnityEngine.XR.ARFoundation; | ||||
| using UnityEngine.XR.ARSubsystems; | ||||
|  | ||||
| public class Furniture : MonoBehaviour | ||||
| { | ||||
|     public void PlaceFurniture([CanBeNull] ARTrackable aRTrackable) | ||||
|     { | ||||
|         transform.SetParent(aRTrackable != null ? aRTrackable.transform : null); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/Furniture.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/Furniture.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 17e788a765b8a8540a245deee922b393 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										19
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureButton.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureButton.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | ||||
| using System.Collections; | ||||
| using UnityEngine; | ||||
| using UnityEngine.UI; | ||||
|  | ||||
| namespace Assets.Scripts.Furniture | ||||
| { | ||||
|     public class FurnitureButton : MonoBehaviour | ||||
|     { | ||||
|         public GameObject furniturePrefab; | ||||
|         private FurnitureManager _furnitureManager; | ||||
|  | ||||
|         private void Start() | ||||
|         { | ||||
|             _furnitureManager = FindFirstObjectByType<FurnitureManager>(); | ||||
|  | ||||
|             GetComponent<Button>().onClick.AddListener(() => _furnitureManager.SetSelectedFurniture(furniturePrefab)); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureButton.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureButton.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 8c4072d6f1c55d34bb1bcdd573b0db96 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										90
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureDragger.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureDragger.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,90 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using UnityEngine.XR.ARFoundation; | ||||
| using UnityEngine.XR.ARSubsystems; | ||||
|  | ||||
| namespace Assets.Scripts.Furniture | ||||
| { | ||||
|     public class FurnitureDragger : MonoBehaviour | ||||
|     { | ||||
|         private Camera _mainCamera; | ||||
|         private ARRaycastManager _arRaycastManager; | ||||
|  | ||||
|         private bool _isDragging; | ||||
|         private bool _isRotating; | ||||
|         private Vector2 _initialTouchPosition; | ||||
|         private float _initialRotation; | ||||
|  | ||||
|         private void Start() | ||||
|         { | ||||
|             _mainCamera = Camera.main; | ||||
|             _arRaycastManager = FindFirstObjectByType<ARRaycastManager>(); | ||||
|         } | ||||
|  | ||||
|         private void Update() | ||||
|         { | ||||
|             if (Input.touchCount == 1) | ||||
|             { | ||||
|                 HandleDrag(); | ||||
|             } | ||||
|             else if (Input.touchCount == 2) | ||||
|             { | ||||
|                 HandleRotate(); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private void HandleRotate() | ||||
|         { | ||||
|             Touch touch = Input.GetTouch(0); | ||||
|  | ||||
|             if (touch.phase == TouchPhase.Began) | ||||
|             { | ||||
|                 Ray ray = _mainCamera.ScreenPointToRay(touch.position); | ||||
|                 if (Physics.Raycast(ray, out RaycastHit hit) && hit.transform == transform) | ||||
|                 { | ||||
|                     _isDragging = true; | ||||
|                 } | ||||
|             } | ||||
|             else if (touch.phase == TouchPhase.Moved && _isDragging) | ||||
|             { | ||||
|                 List<ARRaycastHit> hits = new(); | ||||
|                 if (_arRaycastManager.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon)) | ||||
|                 { | ||||
|                     Pose hitPose = hits[0].pose; | ||||
|                     transform.position = hitPose.position; | ||||
|                 } | ||||
|             } | ||||
|             else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { } | ||||
|             { | ||||
|                 _isDragging = false; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         private void HandleDrag() | ||||
|         { | ||||
|             Touch touch1 = Input.GetTouch(0); | ||||
|             Touch touch2 = Input.GetTouch(1); | ||||
|  | ||||
|             if (touch1.phase == TouchPhase.Began || touch2.phase == TouchPhase.Began) | ||||
|             { | ||||
|                 // Registrar el ángulo inicial para la rotación | ||||
|                 Vector2 initialVector = touch2.position - touch1.position; | ||||
|                 _initialRotation = Mathf.Atan2(initialVector.y, initialVector.x) * Mathf.Rad2Deg; | ||||
|             } | ||||
|             else if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved) | ||||
|             { | ||||
|                 // Calcular el ángulo actual | ||||
|                 Vector2 currentVector = touch2.position - touch1.position; | ||||
|                 float currentRotation = Mathf.Atan2(currentVector.y, currentVector.x) * Mathf.Rad2Deg; | ||||
|  | ||||
|                 // Calcular la diferencia de ángulo y aplicar la rotación | ||||
|                 float deltaRotation = currentRotation - _initialRotation; | ||||
|                 transform.Rotate(Vector3.up, deltaRotation, Space.World); | ||||
|  | ||||
|                 // Actualizar el ángulo inicial | ||||
|                 _initialRotation = currentRotation; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureDragger.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureDragger.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: f762d4bce59928241a92c04ffe468dfe | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										94
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureManager.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,94 @@ | ||||
| using Assets.Scripts.Furniture; | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using UnityEngine.XR.ARFoundation; | ||||
| using UnityEngine.XR.ARSubsystems; | ||||
|  | ||||
| public class FurnitureManager : MonoBehaviour | ||||
| { | ||||
|     [SerializeField] private ARRaycastManager arRaycastManager; // Para detectar superficies | ||||
|     [SerializeField] private List<GameObject> furniturePrefabs; // Lista de prefabs de muebles | ||||
|     [SerializeField] private GameObject furniturePreviewPrefab; // Prefab para previsualizaci<63>n | ||||
|  | ||||
|     private GameObject _currentPreview; | ||||
|     private GameObject _currentFurniturePrefab; | ||||
|     private Vector3 _detectedPosition = Vector3.zero; | ||||
|     private Quaternion _detectedRotation = Quaternion.identity; | ||||
|     private bool _canAddFurniture = false; | ||||
|  | ||||
|     private void Start() | ||||
|     { | ||||
|         // Establece el primer mueble como predeterminado | ||||
|         if (furniturePrefabs.Count > 0) | ||||
|         { | ||||
|             SetSelectedFurniture(furniturePrefabs[0]); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void Update() | ||||
|     { | ||||
|         DetectPlaneAndUpdatePreview(); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     /// M<>todo para detectar planos y actualizar la posici<63>n de previsualizaci<63>n. | ||||
|     /// </summary> | ||||
|     private void DetectPlaneAndUpdatePreview() | ||||
|     { | ||||
|         if (_currentPreview == null) return; | ||||
|  | ||||
|         var hits = new List<ARRaycastHit>(); | ||||
|         var middleScreen = new Vector2(Screen.width / 2, Screen.height / 2); | ||||
|  | ||||
|         if (arRaycastManager.Raycast(middleScreen, hits, TrackableType.PlaneWithinPolygon)) | ||||
|         { | ||||
|             _detectedPosition = hits[0].pose.position; | ||||
|             _detectedRotation = hits[0].pose.rotation; | ||||
|  | ||||
|             _currentPreview.transform.SetPositionAndRotation(_detectedPosition, _detectedRotation); | ||||
|             _currentPreview.SetActive(true); | ||||
|  | ||||
|             _canAddFurniture = true; | ||||
|         } | ||||
|         else | ||||
|         { | ||||
|             _currentPreview.SetActive(false); | ||||
|             _canAddFurniture = false; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     /// M<>todo para colocar un mueble en la posici<63>n detectada. | ||||
|     /// </summary> | ||||
|     public void PlaceFurniture() | ||||
|     { | ||||
|         if (!_canAddFurniture || _currentFurniturePrefab == null) return; | ||||
|  | ||||
|         var newFurniture = Instantiate(_currentFurniturePrefab); | ||||
|         newFurniture.transform.SetPositionAndRotation(_detectedPosition, _detectedRotation); | ||||
|  | ||||
|         // A<>adir funcionalidad de manipulaci<63>n (rotar, mover, etc.) | ||||
|         newFurniture.AddComponent<FurnitureDragger>(); | ||||
|  | ||||
|         Debug.Log("Mueble colocado en la posici<63>n detectada."); | ||||
|     } | ||||
|  | ||||
|     /// <summary> | ||||
|     /// Cambiar el prefab del mueble seleccionado. | ||||
|     /// </summary> | ||||
|     /// <param name="furniturePrefab">Prefab del mueble seleccionado.</param> | ||||
|     public void SetSelectedFurniture(GameObject furniturePrefab) | ||||
|     { | ||||
|         _currentFurniturePrefab = furniturePrefab; | ||||
|  | ||||
|         // Actualizar el preview | ||||
|         if (_currentPreview != null) | ||||
|         { | ||||
|             Destroy(_currentPreview); | ||||
|         } | ||||
|  | ||||
|         _currentPreview = Instantiate(furniturePreviewPrefab ?? furniturePrefab); | ||||
|         _currentPreview.SetActive(false); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureManager.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureManager.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 4754f676fdfa9f0418689553f7af213f | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										63
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureSpawner.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureSpawner.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | ||||
| using System.Collections.Generic; | ||||
| using UnityEngine; | ||||
| using UnityEngine.XR.ARFoundation; | ||||
| using UnityEngine.XR.ARSubsystems; | ||||
|  | ||||
| public class FurnitureSpawner : MonoBehaviour | ||||
| { | ||||
|     [SerializeField] private ARRaycastManager arRaycastManager; | ||||
|     [SerializeField] private GameObject furniturePrefab; | ||||
|  | ||||
|     private bool _canAddFurniture; | ||||
|     private GameObject _furniturePreview; | ||||
|     private Vector3 _detectedPosition = new(); | ||||
|     private Quaternion _detectedRotation = Quaternion.identity; | ||||
|     private ARTrackable _currentTrackable = null; | ||||
|  | ||||
|     private void Start() | ||||
|     { | ||||
|         InputHandler.OnTap += SpawnFurniture; | ||||
|         _furniturePreview = Instantiate(furniturePrefab); | ||||
|         SetCanAddFurniture(true); | ||||
|     } | ||||
|  | ||||
|     private void SpawnFurniture() | ||||
|     { | ||||
|         if (!_canAddFurniture) return; | ||||
|  | ||||
|         var furniture = Instantiate(furniturePrefab); | ||||
|  | ||||
|         furniture.GetComponent<Furniture>().PlaceFurniture(_currentTrackable); | ||||
|         furniture.transform.SetPositionAndRotation(_detectedPosition, _detectedRotation); | ||||
|     } | ||||
|  | ||||
|     private void Update() | ||||
|     { | ||||
|         // Detect a position and rotation over the detected surface | ||||
|         GetRaycastHitTransform(); | ||||
|     } | ||||
|  | ||||
|     private void GetRaycastHitTransform() | ||||
|     { | ||||
|         var hits = new List<ARRaycastHit>(); | ||||
|         var middleScreen = new Vector2(Screen.width / 2, Screen.height / 2); | ||||
|         if (arRaycastManager.Raycast(middleScreen, hits, TrackableType.PlaneWithinPolygon)) | ||||
|         { | ||||
|             _detectedPosition = hits[0].pose.position; | ||||
|             _detectedRotation = hits[0].pose.rotation; | ||||
|             _furniturePreview.transform.SetPositionAndRotation(_detectedPosition, _detectedRotation); | ||||
|             _currentTrackable = hits[0].trackable; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private void OnDestroy() | ||||
|     { | ||||
|         InputHandler.OnTap -= SpawnFurniture; | ||||
|     } | ||||
|  | ||||
|     private void SetCanAddFurniture(bool canAddFurniture) | ||||
|     { | ||||
|         _canAddFurniture = canAddFurniture; | ||||
|         _furniturePreview.SetActive(canAddFurniture); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureSpawner.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Rendering/Scripts/Furniture/FurnitureSpawner.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: bc7cde5cab4e5c6409f75a3825da5fa5 | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
		Reference in New Issue
	
	Block a user
	 Ignacio Gómez Puga
					Ignacio Gómez Puga