回答編集履歴

1

複数オブジェクトの場合を追記

2018/01/05 21:50

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -73,3 +73,199 @@
73
73
  }
74
74
 
75
75
  ```
76
+
77
+
78
+
79
+ ###[追記]
80
+
81
+ もうすでにご自身で複数キューブの場合に書き換え済みかもしれませんが、ご参考までに例を追記します。
82
+
83
+ コメントで申し上げた、「最初に1回だけ頂点モデル座標を求めて、以後はワールド座標変換だけ行う」方式にしてみました。
84
+
85
+ ```Javascript
86
+
87
+ #pragma strict
88
+
89
+
90
+
91
+ import System.Linq;
92
+
93
+
94
+
95
+ class CubeVertices extends MonoBehaviour {
96
+
97
+
98
+
99
+ var Units : GameObject[];
100
+
101
+
102
+
103
+ private static var CubeTopVertices : Vector3[];
104
+
105
+ private static var CubeTopVerticesLength : int;
106
+
107
+
108
+
109
+ private var Coordinatex : float[];
110
+
111
+ private var Coordinatez : float[];
112
+
113
+ private var UnitsLength : int;
114
+
115
+
116
+
117
+ function Start() {
118
+
119
+ UnitsLength = Units.length;
120
+
121
+
122
+
123
+ if (UnitsLength == 0) {
124
+
125
+ return;
126
+
127
+ }
128
+
129
+
130
+
131
+ InitCubeTopVertices(Units[0]); // オブジェクトを代表して、Units[0]からメッシュを取得しCubeTopVertices・CubeTopVerticesLengthを求める
132
+
133
+ Coordinatex = new float[UnitsLength * CubeTopVerticesLength];
134
+
135
+ Coordinatez = new float[UnitsLength * CubeTopVerticesLength];
136
+
137
+
138
+
139
+ // 実験のため、各オブジェクトをランダムに移動・回転させる
140
+
141
+ for (var i : int = 0; i < UnitsLength; i++) {
142
+
143
+ var unitRigidbody = Units[i].GetComponent.<Rigidbody>();
144
+
145
+
146
+
147
+ unitRigidbody.AddForce(Random.value * 2 - 1, 0, Random.value * 2 - 1, ForceMode.VelocityChange);
148
+
149
+ unitRigidbody.AddTorque(0, Random.value * 2 - 1, 0, ForceMode.VelocityChange);
150
+
151
+ }
152
+
153
+ }
154
+
155
+
156
+
157
+ function Update() {
158
+
159
+ // スペースキー押下で一時停止、頂点ワールド座標を調べる
160
+
161
+ if (Input.GetKeyDown(KeyCode.Space)) {
162
+
163
+ if (Time.timeScale > 0) {
164
+
165
+ Time.timeScale = 0;
166
+
167
+ UpdateCoordinates();
168
+
169
+ PrintCoordinates();
170
+
171
+ } else {
172
+
173
+ Time.timeScale = 1;
174
+
175
+ }
176
+
177
+ }
178
+
179
+ }
180
+
181
+
182
+
183
+ private function UpdateCoordinates() {
184
+
185
+ // 各オブジェクトのtransformでキューブの頂点モデル座標をワールド座標に変換し、Coordinatex・Coordinatezを更新する
186
+
187
+ for (var i : int = 0; i < UnitsLength; i++) {
188
+
189
+ var unitTransform : Transform = Units[i].transform;
190
+
191
+ var offset : int = i * CubeTopVerticesLength;
192
+
193
+
194
+
195
+ for (var j : int = 0; j < CubeTopVerticesLength; j++) {
196
+
197
+ var worldPosition : Vector3 = unitTransform.TransformPoint(CubeTopVertices[j]);
198
+
199
+
200
+
201
+ Coordinatex[offset + j] = worldPosition.x;
202
+
203
+ Coordinatez[offset + j] = worldPosition.z;
204
+
205
+ }
206
+
207
+ }
208
+
209
+ }
210
+
211
+
212
+
213
+ private function PrintCoordinates() {
214
+
215
+ // Coordinatex・Coordinatezの内容をコンソールに表示してみる
216
+
217
+ Debug.Log("Coordinates:");
218
+
219
+ for (var i : int = 0; i < UnitsLength; i++) {
220
+
221
+ var offset : int = i * CubeTopVerticesLength;
222
+
223
+
224
+
225
+ Debug.LogFormat(Units[i],
226
+
227
+ "Unit {0} ({1}) ... ({2:F4}, {3:F4}), ({4:F4}, {5:F4}), ({6:F4}, {7:F4}), ({8:F4}, {9:F4})",
228
+
229
+ i, Units[i].name,
230
+
231
+ Coordinatex[offset + 0], Coordinatez[offset + 0],
232
+
233
+ Coordinatex[offset + 1], Coordinatez[offset + 1],
234
+
235
+ Coordinatex[offset + 2], Coordinatez[offset + 2],
236
+
237
+ Coordinatex[offset + 3], Coordinatez[offset + 3]);
238
+
239
+ }
240
+
241
+ }
242
+
243
+
244
+
245
+ private static function InitCubeTopVertices(meshSource : GameObject) {
246
+
247
+ // 上面の頂点抽出までをあらかじめやっておく
248
+
249
+ if (CubeTopVertices == null) {
250
+
251
+ CubeTopVertices = meshSource.GetComponent.<MeshFilter>().sharedMesh.vertices
252
+
253
+ .Distinct.<Vector3>()
254
+
255
+ .Where.<Vector3>(function(p : Vector3) : boolean {
256
+
257
+ return p.y > 0.0f;
258
+
259
+ })
260
+
261
+ .ToArray.<Vector3>();
262
+
263
+ CubeTopVerticesLength = CubeTopVertices.length;
264
+
265
+ }
266
+
267
+ }
268
+
269
+ }
270
+
271
+ ```