質問編集履歴
2
内容を少し変えました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,7 +2,45 @@
|
|
2
2
|
|
3
3
|
二つのマーカーを用意してそれぞれのマーカーの上にオブジェクトを乗せることはできました。
|
4
4
|
|
5
|
-
|
5
|
+
以下のソースコードでマーカー1と2を決めて中心点を求めオブジェクトを表示させようとしたのですが、処理が行われないという状況です。ご教示していただけると幸いです。
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
```ここに言語を入力
|
10
|
+
|
11
|
+
string name = trackedImage.referenceImage.name;
|
12
|
+
|
13
|
+
if (name == "MARKER1")
|
14
|
+
|
15
|
+
{
|
16
|
+
|
17
|
+
marker1Pos = trackedImage.transform.position;
|
18
|
+
|
19
|
+
}
|
20
|
+
|
21
|
+
else if (name == "MARKER2")
|
22
|
+
|
23
|
+
{
|
24
|
+
|
25
|
+
marker2Pos = trackedImage.transform.position;
|
26
|
+
|
27
|
+
}
|
28
|
+
|
29
|
+
Vector3 center = Vector3.Lerp(marker1Pos, marker2Pos, 0.5f);
|
30
|
+
|
31
|
+
trackedImage.transform.position = center;
|
32
|
+
|
33
|
+
```
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
6
44
|
|
7
45
|
|
8
46
|
|
@@ -14,65 +52,69 @@
|
|
14
52
|
|
15
53
|
using UnityEngine;
|
16
54
|
|
55
|
+
using UnityEngine.XR;
|
56
|
+
|
17
57
|
using UnityEngine.XR.ARFoundation;
|
18
58
|
|
59
|
+
|
60
|
+
|
19
|
-
u
|
61
|
+
[RequireComponent(typeof(ARTrackedImageManager))]
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
62
|
|
25
63
|
public class AR : MonoBehaviour
|
26
64
|
|
27
65
|
{
|
28
66
|
|
29
|
-
|
30
|
-
|
31
|
-
/// マーカー用オブジェクトのプレハブ
|
32
|
-
|
33
|
-
[SerializeField]
|
67
|
+
[SerializeField]
|
34
|
-
|
68
|
+
|
35
|
-
private GameObject P
|
69
|
+
private GameObject[] placeablePrefabs;
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
private Dictionary<string, GameObject> spawnedPrefabs = new Dictionary<string, GameObject>();
|
74
|
+
|
75
|
+
private ARTrackedImageManager trackedImageManager;
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
Vector3 marker1Pos;
|
80
|
+
|
81
|
+
Vector3 marker2Pos;
|
36
82
|
|
37
83
|
|
38
84
|
|
39
|
-
|
85
|
+
|
40
|
-
|
41
|
-
|
86
|
+
|
42
|
-
|
43
|
-
Vector3 marker1Pos;
|
44
|
-
|
45
|
-
Vector3 marker2Pos;
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
/// マーカー用オブジェクトのプレハブと文字列を紐づけた辞書
|
50
|
-
|
51
|
-
private readonly Dictionary<string, GameObject> _markerNameAndPrefabDictionary = new Dictionary<string, GameObject>();
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
private void
|
87
|
+
private void Awake()
|
56
|
-
|
88
|
+
|
57
|
-
{
|
89
|
+
{
|
58
|
-
|
90
|
+
|
59
|
-
|
91
|
+
trackedImageManager = FindObjectOfType<ARTrackedImageManager>();
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
92
|
+
|
64
|
-
|
93
|
+
|
94
|
+
|
65
|
-
for (
|
95
|
+
foreach (GameObject prefab in placeablePrefabs)
|
66
|
-
|
96
|
+
|
67
|
-
{
|
97
|
+
{
|
98
|
+
|
68
|
-
|
99
|
+
GameObject newPrefab = Instantiate(prefab, Vector3.zero, Quaternion.identity);
|
100
|
+
|
69
|
-
|
101
|
+
newPrefab.name = prefab.name;
|
70
|
-
|
71
|
-
|
102
|
+
|
72
|
-
|
73
|
-
|
103
|
+
newPrefab.SetActive(false);
|
104
|
+
|
74
|
-
|
105
|
+
spawnedPrefabs.Add(prefab.name, newPrefab);
|
106
|
+
|
75
|
-
}
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
private void OnEnable()
|
114
|
+
|
115
|
+
{
|
116
|
+
|
117
|
+
trackedImageManager.trackedImagesChanged += ImageChanged;
|
76
118
|
|
77
119
|
}
|
78
120
|
|
@@ -82,27 +124,53 @@
|
|
82
124
|
|
83
125
|
{
|
84
126
|
|
85
|
-
|
127
|
+
trackedImageManager.trackedImagesChanged -= ImageChanged;
|
86
|
-
|
128
|
+
|
87
|
-
}
|
129
|
+
}
|
88
|
-
|
89
|
-
|
90
|
-
|
130
|
+
|
131
|
+
|
132
|
+
|
91
|
-
private void
|
133
|
+
private void ImageChanged(ARTrackedImagesChangedEventArgs eventArgs)
|
92
|
-
|
134
|
+
|
93
|
-
{
|
135
|
+
{
|
136
|
+
|
94
|
-
|
137
|
+
foreach (ARTrackedImage trackedImage in eventArgs.added)
|
138
|
+
|
139
|
+
{
|
140
|
+
|
141
|
+
UpdateImage(trackedImage);
|
142
|
+
|
143
|
+
}
|
144
|
+
|
145
|
+
foreach (ARTrackedImage trackedImage in eventArgs.updated)
|
146
|
+
|
147
|
+
{
|
148
|
+
|
149
|
+
UpdateImage(trackedImage);
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
foreach (ARTrackedImage trackedImage in eventArgs.removed)
|
154
|
+
|
155
|
+
{
|
156
|
+
|
95
|
-
|
157
|
+
spawnedPrefabs[trackedImage.name].SetActive(false);
|
158
|
+
|
96
|
-
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
private void UpdateImage(ARTrackedImage trackedImage)
|
166
|
+
|
167
|
+
{
|
168
|
+
|
97
|
-
|
169
|
+
string name = trackedImage.referenceImage.name;
|
98
|
-
|
99
|
-
|
170
|
+
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
171
|
+
|
172
|
+
|
105
|
-
if (
|
173
|
+
if (name == "MARKER1")
|
106
174
|
|
107
175
|
{
|
108
176
|
|
@@ -112,7 +180,7 @@
|
|
112
180
|
|
113
181
|
|
114
182
|
|
115
|
-
else if (
|
183
|
+
else if (name == "MARKER2")
|
116
184
|
|
117
185
|
{
|
118
186
|
|
@@ -120,57 +188,39 @@
|
|
120
188
|
|
121
189
|
}
|
122
190
|
|
123
|
-
|
124
|
-
|
125
|
-
|
191
|
+
|
126
|
-
|
127
|
-
|
128
|
-
|
192
|
+
|
129
|
-
Vector3 center= Vector3.Lerp(marker1Pos,marker2Pos,0.5f);
|
193
|
+
Vector3 center = Vector3.Lerp(marker1Pos, marker2Pos, 0.5f);
|
194
|
+
|
195
|
+
|
196
|
+
|
130
|
-
|
197
|
+
trackedImage.transform.position = center;
|
198
|
+
|
199
|
+
|
200
|
+
|
131
|
-
|
201
|
+
//ここでKeyNotFoundException: The given key was not present in the dictionary.
|
202
|
+
|
203
|
+
//「指定されたキーが辞書に存在しませんでした」と表示される
|
204
|
+
|
205
|
+
GameObject prefab = spawnedPrefabs[name];
|
132
206
|
|
133
207
|
prefab.transform.position = center;
|
134
208
|
|
135
|
-
|
136
|
-
|
137
|
-
|
209
|
+
prefab.SetActive(true);
|
138
|
-
|
139
|
-
|
210
|
+
|
140
|
-
|
141
|
-
|
211
|
+
|
142
|
-
|
212
|
+
|
143
|
-
a
|
213
|
+
foreach (GameObject go in spawnedPrefabs.Values)
|
214
|
+
|
144
|
-
|
215
|
+
{
|
145
|
-
|
146
|
-
|
216
|
+
|
147
|
-
|
217
|
+
if (go.name != name)
|
218
|
+
|
148
|
-
|
219
|
+
{
|
220
|
+
|
149
|
-
|
221
|
+
go.SetActive(false);
|
150
|
-
|
222
|
+
|
151
|
-
}
|
223
|
+
}
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
|
156
|
-
|
157
|
-
{
|
158
|
-
|
159
|
-
foreach (var trackedImage in eventArgs.added)
|
160
|
-
|
161
|
-
{
|
162
|
-
|
163
|
-
ActivateARObject(trackedImage);
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
foreach (var trackedImage in eventArgs.updated)
|
170
|
-
|
171
|
-
{
|
172
|
-
|
173
|
-
ActivateARObject(trackedImage);
|
174
224
|
|
175
225
|
}
|
176
226
|
|
1
内容を少し変えました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
ARFoudationを用いて、マーカー間の中心にオブジェクトを置きたいと考えています。
|
2
2
|
|
3
|
-
二つのマーカーを用意してそれぞれのマーカーの上にオブジェクトを乗せることはできました。
|
3
|
+
二つのマーカーを用意してそれぞれのマーカーの上にオブジェクトを乗せることはできました。
|
4
4
|
|
5
|
-
|
5
|
+
private void ActivateARObject(ARTrackedImage trackedImage)の下でマーカー1と2を決めて中心点を求めオブジェクトを表示させようとしたのですが、処理が行われないという状況です。ご教示していただけると幸いです。
|
6
6
|
|
7
7
|
|
8
8
|
|
9
9
|
|
10
10
|
|
11
11
|
```C#
|
12
|
-
|
13
|
-
|
14
12
|
|
15
13
|
using System.Collections.Generic;
|
16
14
|
|
@@ -28,33 +26,27 @@
|
|
28
26
|
|
29
27
|
{
|
30
28
|
|
31
|
-
|
29
|
+
|
32
30
|
|
33
31
|
/// マーカー用オブジェクトのプレハブ
|
34
32
|
|
35
|
-
/// </summary>
|
36
|
-
|
37
33
|
[SerializeField] private GameObject[] _arPrefabs;
|
38
34
|
|
39
|
-
GameObject o
|
35
|
+
private GameObject Point;
|
40
36
|
|
41
|
-
|
37
|
+
|
42
|
-
|
43
|
-
/// <summary>
|
44
38
|
|
45
39
|
/// ARTrackedImageManager
|
46
40
|
|
47
|
-
/// </summary>
|
48
|
-
|
49
41
|
[SerializeField] private ARTrackedImageManager _imageManager;
|
50
42
|
|
43
|
+
Vector3 marker1Pos;
|
51
44
|
|
45
|
+
Vector3 marker2Pos;
|
52
46
|
|
53
|
-
|
47
|
+
|
54
48
|
|
55
49
|
/// マーカー用オブジェクトのプレハブと文字列を紐づけた辞書
|
56
|
-
|
57
|
-
/// </summary>
|
58
50
|
|
59
51
|
private readonly Dictionary<string, GameObject> _markerNameAndPrefabDictionary = new Dictionary<string, GameObject>();
|
60
52
|
|
@@ -96,31 +88,57 @@
|
|
96
88
|
|
97
89
|
|
98
90
|
|
99
|
-
|
100
|
-
|
101
91
|
private void ActivateARObject(ARTrackedImage trackedImage)
|
102
92
|
|
103
|
-
|
104
|
-
|
105
93
|
{
|
106
|
-
|
107
|
-
|
108
94
|
|
109
95
|
//認識した画像マーカーの名前を使って辞書から任意のオブジェクトを引っ張り出す
|
110
96
|
|
111
97
|
var arObject = _markerNameAndPrefabDictionary[trackedImage.referenceImage.name];
|
112
98
|
|
113
|
-
|
114
|
-
|
115
99
|
var imageMarkerTransform = trackedImage.transform;
|
116
100
|
|
117
101
|
|
118
102
|
|
103
|
+
|
104
|
+
|
105
|
+
if (trackedImage.referenceImage.name == "MARKER1")
|
106
|
+
|
107
|
+
{
|
108
|
+
|
109
|
+
marker1Pos = trackedImage.transform.position;
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
else if (trackedImage.referenceImage.name == "MARKER2")
|
116
|
+
|
117
|
+
{
|
118
|
+
|
119
|
+
marker2Pos = trackedImage.transform.position;
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
GameObject prefab = _markerNameAndPrefabDictionary[name];
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
Vector3 center= Vector3.Lerp(marker1Pos,marker2Pos,0.5f);
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
prefab.transform.position = center;
|
134
|
+
|
135
|
+
|
136
|
+
|
119
|
-
|
137
|
+
//位置合わせ
|
120
138
|
|
121
139
|
var markerFrontRotation = imageMarkerTransform.rotation * Quaternion.Euler(90f, 0f, 0f);
|
122
140
|
|
123
|
-
arObject.transform.SetPositionAndRotation(
|
141
|
+
arObject.transform.SetPositionAndRotation(center, markerFrontRotation);
|
124
142
|
|
125
143
|
arObject.transform.SetParent(imageMarkerTransform);
|
126
144
|
|