現在ARCoreを用いてARコンテンツを作る練習をしています。
サンプルのHalloARを用いています。このサンプルは平面に制限なくオブジェクトが置けるのですが、
設置できるオブジェクトを1~3つぐらいの少数にしたいです。
とりあえずオブジェクトの設置の部分であろうプログラムです。
ヒントでもいいので教えていただけるとありがたいです。
Unity
1namespace GoogleARCore.Examples.HelloAR 2{ 3 using System.Collections.Generic; 4 using GoogleARCore; 5 using GoogleARCore.Examples.Common; 6 using UnityEngine; 7 using UnityEngine.EventSystems; 8 9#if UNITY_EDITOR 10 // Set up touch input propagation while using Instant Preview in the editor. 11 using Input = InstantPreviewInput; 12#endif 13 14 /// <summary> 15 /// Controls the HelloAR example. 16 /// </summary> 17 public class HelloARController : MonoBehaviour 18 { 19 /// <summary> 20 /// The first-person camera being used to render the passthrough camera image (i.e. AR 21 /// background). 22 /// </summary> 23 public Camera FirstPersonCamera; 24 25 /// <summary> 26 /// A prefab to place when a raycast from a user touch hits a vertical plane. 27 /// </summary> 28 public GameObject GameObjectVerticalPlanePrefab; 29 30 /// <summary> 31 /// A prefab to place when a raycast from a user touch hits a horizontal plane. 32 /// </summary> 33 public GameObject GameObjectHorizontalPlanePrefab; 34 35 /// <summary> 36 /// A prefab to place when a raycast from a user touch hits a feature point. 37 /// </summary> 38 public GameObject GameObjectPointPrefab; 39 40 /// <summary> 41 /// The rotation in degrees need to apply to prefab when it is placed. 42 /// </summary> 43 private const float k_PrefabRotation = 180.0f; 44 45 /// <summary> 46 /// True if the app is in the process of quitting due to an ARCore connection error, 47 /// otherwise false. 48 /// </summary> 49 private bool m_IsQuitting = false; 50 51 52 /// <summary> 53 /// The Unity Awake() method. 54 /// </summary> 55 public void Awake() 56 { 57 // Enable ARCore to target 60fps camera capture frame rate on supported devices. 58 // Note, Application.targetFrameRate is ignored when QualitySettings.vSyncCount != 0. 59 Application.targetFrameRate = 60; 60 } 61 62 /// <summary> 63 /// The Unity Update() method. 64 /// </summary> 65 public void Update() 66 { 67 _UpdateApplicationLifecycle(); 68 69 Touch touch; 70 71 if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began) 72 { 73 return; 74 } 75 76 if (EventSystem.current.IsPointerOverGameObject(touch.fingerId)) 77 { 78 return; 79 } 80 81 82 // Raycast against the location the player touched to search for planes. 83 TrackableHit hit; 84 TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | 85 TrackableHitFlags.FeaturePointWithSurfaceNormal; 86 87 if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit)) 88 { 89 // Use hit pose and camera pose to check if hittest is from the 90 // back of the plane, if it is, no need to create the anchor. 91 if ((hit.Trackable is DetectedPlane) && 92 Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position, 93 hit.Pose.rotation * Vector3.up) < 0) 94 { 95 Debug.Log("Hit at back of the current DetectedPlane"); 96 } 97 else 98 { 99 // Choose the prefab based on the Trackable that got hit. 100 GameObject prefab; 101 if (hit.Trackable is FeaturePoint) 102 { 103 prefab = GameObjectPointPrefab; 104 } 105 else if (hit.Trackable is DetectedPlane) 106 { 107 DetectedPlane detectedPlane = hit.Trackable as DetectedPlane; 108 if (detectedPlane.PlaneType == DetectedPlaneType.Vertical) 109 { 110 prefab = GameObjectVerticalPlanePrefab; 111 } 112 else 113 { 114 prefab = GameObjectHorizontalPlanePrefab; 115 } 116 } 117 else 118 { 119 prefab = GameObjectHorizontalPlanePrefab; 120 } 121 122 // Instantiate prefab at the hit pose. 123 var gameObject = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation); 124 125 // Compensate for the hitPose rotation facing away from the raycast (i.e. 126 // camera). 127 gameObject.transform.Rotate(0, k_PrefabRotation, 0, Space.Self); 128 129 // Create an anchor to allow ARCore to track the hitpoint as understanding of 130 // the physical world evolves. 131 var anchor = hit.Trackable.CreateAnchor(hit.Pose); 132 133 // Make game object a child of the anchor. 134 gameObject.transform.parent = anchor.transform; 135 } 136 } 137 } 138 139 /// <summary> 140 /// Check and update the application lifecycle. 141 /// </summary> 142 private void _UpdateApplicationLifecycle() 143 { 144 // Exit the app when the 'back' button is pressed. 145 if (Input.GetKey(KeyCode.Escape)) 146 { 147 Application.Quit(); 148 } 149 150 // Only allow the screen to sleep when not tracking. 151 if (Session.Status != SessionStatus.Tracking) 152 { 153 Screen.sleepTimeout = SleepTimeout.SystemSetting; 154 } 155 else 156 { 157 Screen.sleepTimeout = SleepTimeout.NeverSleep; 158 } 159 160 if (m_IsQuitting) 161 { 162 return; 163 } 164 165 // Quit if ARCore was unable to connect and give Unity some time for the toast to 166 // appear. 167 if (Session.Status == SessionStatus.ErrorPermissionNotGranted) 168 { 169 _ShowAndroidToastMessage("Camera permission is needed to run this application."); 170 m_IsQuitting = true; 171 Invoke("_DoQuit", 0.5f); 172 } 173 else if (Session.Status.IsError()) 174 { 175 _ShowAndroidToastMessage( 176 "ARCore encountered a problem connecting. Please start the app again."); 177 m_IsQuitting = true; 178 Invoke("_DoQuit", 0.5f); 179 } 180 } 181 182 /// <summary> 183 /// Actually quit the application. 184 /// </summary> 185 private void _DoQuit() 186 { 187 Application.Quit(); 188 } 189 190 /// <summary> 191 /// Show an Android toast message. 192 /// </summary> 193 /// <param name="message">Message string to show in the toast.</param> 194 private void _ShowAndroidToastMessage(string message) 195 { 196 AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 197 AndroidJavaObject unityActivity = 198 unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); 199 200 if (unityActivity != null) 201 { 202 AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast"); 203 unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() => 204 { 205 AndroidJavaObject toastObject = 206 toastClass.CallStatic<AndroidJavaObject>( 207 "makeText", unityActivity, message, 0); 208 toastObject.Call("show"); 209 })); 210 } 211 } 212 } 213}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。