質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
ARCore

ARCoreは、グーグル社が提供しているAndroid向けのAR(拡張現実)フレームワークです。スマホ内蔵のカメラとIMUセンサー(慣性計測装置)を使って、モーショントラッキングや水平面の検出、光源の推測を行い、ARを実現します。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

Q&A

解決済

1回答

831閲覧

ARCoreのサンプルを用いて、オブジェクトを決まった数設置したい

Asagiri

総合スコア9

ARCore

ARCoreは、グーグル社が提供しているAndroid向けのAR(拡張現実)フレームワークです。スマホ内蔵のカメラとIMUセンサー(慣性計測装置)を使って、モーショントラッキングや水平面の検出、光源の推測を行い、ARを実現します。

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

0グッド

0クリップ

投稿2020/05/28 18:06

現在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}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下の処理でGameObjectを生成しているので、生成する度にカウントチェックすることで対処するのはいかがでしょうか。

GameObject生成処理抜粋

C#

1// 修正前 2 3// Instantiate prefab at the hit pose. 4var gameObject = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation); 5 6// Compensate for the hitPose rotation facing away from the raycast (i.e. 7// camera). 8gameObject.transform.Rotate(0, k_PrefabRotation, 0, Space.Self); 9 10// Create an anchor to allow ARCore to track the hitpoint as understanding of 11// the physical world evolves. 12var anchor = hit.Trackable.CreateAnchor(hit.Pose); 13 14// Make game object a child of the anchor. 15gameObject.transform.parent = anchor.transform;

C#

1// 修正後 2// 1、インスタンス変数としてカウントアップ用変数を、クラスの先頭に宣言 3private int objCount = 0; 4 5 6// 3、カウント数チェック 7if (objCount > 3) { 8 return 9} 10 11// Instantiate prefab at the hit pose. 12var gameObject = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation); 13 14// Compensate for the hitPose rotation facing away from the raycast (i.e. 15// camera). 16gameObject.transform.Rotate(0, k_PrefabRotation, 0, Space.Self); 17 18// Create an anchor to allow ARCore to track the hitpoint as understanding of 19// the physical world evolves. 20var anchor = hit.Trackable.CreateAnchor(hit.Pose); 21 22// Make game object a child of the anchor. 23gameObject.transform.parent = anchor.transform; 24 25// 2、カウントアップ用変数をインクリメント 26objCount++;

投稿2020/05/29 00:05

tsuki01

総合スコア1751

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問