回答編集履歴
1
複数オブジェクトの場合を追記
answer
CHANGED
@@ -35,4 +35,102 @@
|
|
35
35
|
}
|
36
36
|
}
|
37
37
|
}
|
38
|
+
```
|
39
|
+
|
40
|
+
###[追記]
|
41
|
+
もうすでにご自身で複数キューブの場合に書き換え済みかもしれませんが、ご参考までに例を追記します。
|
42
|
+
コメントで申し上げた、「最初に1回だけ頂点モデル座標を求めて、以後はワールド座標変換だけ行う」方式にしてみました。
|
43
|
+
```Javascript
|
44
|
+
#pragma strict
|
45
|
+
|
46
|
+
import System.Linq;
|
47
|
+
|
48
|
+
class CubeVertices extends MonoBehaviour {
|
49
|
+
|
50
|
+
var Units : GameObject[];
|
51
|
+
|
52
|
+
private static var CubeTopVertices : Vector3[];
|
53
|
+
private static var CubeTopVerticesLength : int;
|
54
|
+
|
55
|
+
private var Coordinatex : float[];
|
56
|
+
private var Coordinatez : float[];
|
57
|
+
private var UnitsLength : int;
|
58
|
+
|
59
|
+
function Start() {
|
60
|
+
UnitsLength = Units.length;
|
61
|
+
|
62
|
+
if (UnitsLength == 0) {
|
63
|
+
return;
|
64
|
+
}
|
65
|
+
|
66
|
+
InitCubeTopVertices(Units[0]); // オブジェクトを代表して、Units[0]からメッシュを取得しCubeTopVertices・CubeTopVerticesLengthを求める
|
67
|
+
Coordinatex = new float[UnitsLength * CubeTopVerticesLength];
|
68
|
+
Coordinatez = new float[UnitsLength * CubeTopVerticesLength];
|
69
|
+
|
70
|
+
// 実験のため、各オブジェクトをランダムに移動・回転させる
|
71
|
+
for (var i : int = 0; i < UnitsLength; i++) {
|
72
|
+
var unitRigidbody = Units[i].GetComponent.<Rigidbody>();
|
73
|
+
|
74
|
+
unitRigidbody.AddForce(Random.value * 2 - 1, 0, Random.value * 2 - 1, ForceMode.VelocityChange);
|
75
|
+
unitRigidbody.AddTorque(0, Random.value * 2 - 1, 0, ForceMode.VelocityChange);
|
76
|
+
}
|
77
|
+
}
|
78
|
+
|
79
|
+
function Update() {
|
80
|
+
// スペースキー押下で一時停止、頂点ワールド座標を調べる
|
81
|
+
if (Input.GetKeyDown(KeyCode.Space)) {
|
82
|
+
if (Time.timeScale > 0) {
|
83
|
+
Time.timeScale = 0;
|
84
|
+
UpdateCoordinates();
|
85
|
+
PrintCoordinates();
|
86
|
+
} else {
|
87
|
+
Time.timeScale = 1;
|
88
|
+
}
|
89
|
+
}
|
90
|
+
}
|
91
|
+
|
92
|
+
private function UpdateCoordinates() {
|
93
|
+
// 各オブジェクトのtransformでキューブの頂点モデル座標をワールド座標に変換し、Coordinatex・Coordinatezを更新する
|
94
|
+
for (var i : int = 0; i < UnitsLength; i++) {
|
95
|
+
var unitTransform : Transform = Units[i].transform;
|
96
|
+
var offset : int = i * CubeTopVerticesLength;
|
97
|
+
|
98
|
+
for (var j : int = 0; j < CubeTopVerticesLength; j++) {
|
99
|
+
var worldPosition : Vector3 = unitTransform.TransformPoint(CubeTopVertices[j]);
|
100
|
+
|
101
|
+
Coordinatex[offset + j] = worldPosition.x;
|
102
|
+
Coordinatez[offset + j] = worldPosition.z;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
private function PrintCoordinates() {
|
108
|
+
// Coordinatex・Coordinatezの内容をコンソールに表示してみる
|
109
|
+
Debug.Log("Coordinates:");
|
110
|
+
for (var i : int = 0; i < UnitsLength; i++) {
|
111
|
+
var offset : int = i * CubeTopVerticesLength;
|
112
|
+
|
113
|
+
Debug.LogFormat(Units[i],
|
114
|
+
"Unit {0} ({1}) ... ({2:F4}, {3:F4}), ({4:F4}, {5:F4}), ({6:F4}, {7:F4}), ({8:F4}, {9:F4})",
|
115
|
+
i, Units[i].name,
|
116
|
+
Coordinatex[offset + 0], Coordinatez[offset + 0],
|
117
|
+
Coordinatex[offset + 1], Coordinatez[offset + 1],
|
118
|
+
Coordinatex[offset + 2], Coordinatez[offset + 2],
|
119
|
+
Coordinatex[offset + 3], Coordinatez[offset + 3]);
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
private static function InitCubeTopVertices(meshSource : GameObject) {
|
124
|
+
// 上面の頂点抽出までをあらかじめやっておく
|
125
|
+
if (CubeTopVertices == null) {
|
126
|
+
CubeTopVertices = meshSource.GetComponent.<MeshFilter>().sharedMesh.vertices
|
127
|
+
.Distinct.<Vector3>()
|
128
|
+
.Where.<Vector3>(function(p : Vector3) : boolean {
|
129
|
+
return p.y > 0.0f;
|
130
|
+
})
|
131
|
+
.ToArray.<Vector3>();
|
132
|
+
CubeTopVerticesLength = CubeTopVertices.length;
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
38
136
|
```
|