回答編集履歴
1
軸平行くり抜きの例を追記
test
CHANGED
@@ -5,3 +5,397 @@
|
|
5
5
|
ご質問者さんのおっしゃる「メッシュを組みなおして重なっていない部分のみのオブジェクトを生成すれば」というアプローチもおそらく可能でしょう(私は実装した経験はないですが...)。
|
6
6
|
|
7
7
|
たとえばちょっと検索してみたところ[Simple and Robust Boolean Operations for Triangulated Surfaces](https://arxiv.org/abs/1308.4434)([PDF](https://arxiv.org/pdf/1308.4434.pdf))なんて論文が出てきました。こういった方法ならメッシュが本当にくり抜かれた形になるので、MeshColliderを使えばくり抜かれた形通りに衝突判定を行うなんてことも可能かもしれません。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
**追記**
|
12
|
+
|
13
|
+
「[unity上で三次元座標の二点を選択して、直方体のブロックを生成したい](https://teratail.com/questions/210981)」を拝見しますに、くり抜く側もくり抜かれる側も軸平行な直方体だという制限を設けてもいいのでしょうかね?
|
14
|
+
|
15
|
+
でしたら問題がだいぶ簡略化でき、最初の回答で例示しました論文のような複雑なことをしなくても済むかもしれません。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
今さらながらその条件で実験してみました。軸平行にくり抜いていくなら、くり抜かれた結果は直方体の集まりとして表現できるはずだと思い、くり抜く側の上・下・左・右・前・後の6面でくり抜かれる側を切断して小さな直方体に分割するという方針で行きました。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
直方体を最大隅・最小隅の2点で表現する構造体を作り...
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
```C#
|
28
|
+
|
29
|
+
using System;
|
30
|
+
|
31
|
+
using System.Collections.Generic;
|
32
|
+
|
33
|
+
using UnityEngine;
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
public struct Box
|
38
|
+
|
39
|
+
{
|
40
|
+
|
41
|
+
public readonly Vector3 Max;
|
42
|
+
|
43
|
+
public readonly Vector3 Min;
|
44
|
+
|
45
|
+
public readonly Vector3 Center;
|
46
|
+
|
47
|
+
public readonly Vector3 Size;
|
48
|
+
|
49
|
+
public readonly bool HasVolume;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
// 対角隅2点を指定してBoxを作る
|
54
|
+
|
55
|
+
public Box(Vector3 cornerA, Vector3 cornerB)
|
56
|
+
|
57
|
+
{
|
58
|
+
|
59
|
+
this.Max = Vector3.Max(cornerA, cornerB);
|
60
|
+
|
61
|
+
this.Min = Vector3.Min(cornerA, cornerB);
|
62
|
+
|
63
|
+
this.Center = (this.Max + this.Min) * 0.5f;
|
64
|
+
|
65
|
+
this.Size = this.Max - this.Min;
|
66
|
+
|
67
|
+
this.HasVolume = (this.Size.x * this.Size.y * this.Size.z) > 0.0f;
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
// Cubeを伸縮させたオブジェクトをもとにBoxを作る
|
74
|
+
|
75
|
+
public Box(Transform cube)
|
76
|
+
|
77
|
+
{
|
78
|
+
|
79
|
+
if (cube == null)
|
80
|
+
|
81
|
+
{
|
82
|
+
|
83
|
+
this = new Box();
|
84
|
+
|
85
|
+
return;
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
var position = cube.position;
|
92
|
+
|
93
|
+
var halfExtents = cube.localScale * 0.5f;
|
94
|
+
|
95
|
+
this = new Box(position + halfExtents, position - halfExtents);
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
// BoxをもとにCubeを伸縮させたオブジェクトを作る
|
102
|
+
|
103
|
+
public Transform ToCube(Transform parent = null, Material sharedMaterial = null)
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
if (!this.HasVolume)
|
108
|
+
|
109
|
+
{
|
110
|
+
|
111
|
+
return null;
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
118
|
+
|
119
|
+
var cubeTransform = cube.transform;
|
120
|
+
|
121
|
+
cubeTransform.position = this.Center;
|
122
|
+
|
123
|
+
cubeTransform.localScale = this.Size;
|
124
|
+
|
125
|
+
cubeTransform.SetParent(parent);
|
126
|
+
|
127
|
+
if (sharedMaterial != null)
|
128
|
+
|
129
|
+
{
|
130
|
+
|
131
|
+
cubeTransform.GetComponent<Renderer>().sharedMaterial = sharedMaterial;
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
return cubeTransform;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
// Boxを特定の軸で切断し2つのBoxに分ける
|
144
|
+
|
145
|
+
// 軸番号はXが0、Yが1、Zが2
|
146
|
+
|
147
|
+
public (Box upper, Box lower) Cut(float at, int axis)
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
if (!this.HasVolume)
|
152
|
+
|
153
|
+
{
|
154
|
+
|
155
|
+
return (new Box(), new Box());
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
var sourceMax = this.Max[axis];
|
162
|
+
|
163
|
+
var sourceMin = this.Min[axis];
|
164
|
+
|
165
|
+
if (at <= sourceMin)
|
166
|
+
|
167
|
+
{
|
168
|
+
|
169
|
+
return (this, new Box());
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
if (at >= sourceMax)
|
176
|
+
|
177
|
+
{
|
178
|
+
|
179
|
+
return (new Box(), this);
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
var lowerMin = this.Min;
|
186
|
+
|
187
|
+
var lowerMax = this.Max;
|
188
|
+
|
189
|
+
lowerMax[axis] = at;
|
190
|
+
|
191
|
+
var upperMin = this.Min;
|
192
|
+
|
193
|
+
var upperMax = this.Max;
|
194
|
+
|
195
|
+
upperMin[axis] = at;
|
196
|
+
|
197
|
+
return (new Box(upperMin, upperMax), new Box(lowerMin, lowerMax));
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
// Boxを別のBoxでくり抜き、できあがった断片をnewBoxesに詰める
|
204
|
+
|
205
|
+
public void Carve(Box carver, List<Box> newBoxes)
|
206
|
+
|
207
|
+
{
|
208
|
+
|
209
|
+
if (newBoxes == null)
|
210
|
+
|
211
|
+
{
|
212
|
+
|
213
|
+
throw new InvalidOperationException($"{nameof(newBoxes)} must not be null.");
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
if (!this.HasVolume)
|
220
|
+
|
221
|
+
{
|
222
|
+
|
223
|
+
throw new InvalidOperationException($"carvee must have volume.");
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
newBoxes.Clear();
|
230
|
+
|
231
|
+
var residue = this;
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
// X、Y、Z軸についてそれぞれ...
|
236
|
+
|
237
|
+
for (var i = 0; i < 3; i++)
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
// 最小側の面で切断
|
242
|
+
|
243
|
+
var (upper, lower) = residue.Cut(carver.Min[i], i);
|
244
|
+
|
245
|
+
if (lower.HasVolume)
|
246
|
+
|
247
|
+
{
|
248
|
+
|
249
|
+
newBoxes.Add(lower);
|
250
|
+
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
if (!upper.HasVolume)
|
256
|
+
|
257
|
+
{
|
258
|
+
|
259
|
+
break;
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
residue = upper;
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
// 最大側の面で切断
|
270
|
+
|
271
|
+
(upper, lower) = residue.Cut(carver.Max[i], i);
|
272
|
+
|
273
|
+
if (upper.HasVolume)
|
274
|
+
|
275
|
+
{
|
276
|
+
|
277
|
+
newBoxes.Add(upper);
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
if (!lower.HasVolume)
|
284
|
+
|
285
|
+
{
|
286
|
+
|
287
|
+
break;
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
residue = lower;
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
}
|
300
|
+
|
301
|
+
```
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
くり抜かれる側には下記のようなスクリプトをアタッチして...
|
306
|
+
|
307
|
+
```C#
|
308
|
+
|
309
|
+
using System.Collections.Generic;
|
310
|
+
|
311
|
+
using System.Linq;
|
312
|
+
|
313
|
+
using UnityEngine;
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
public class Carvee : MonoBehaviour
|
318
|
+
|
319
|
+
{
|
320
|
+
|
321
|
+
public void Carve(Transform carverTransform)
|
322
|
+
|
323
|
+
{
|
324
|
+
|
325
|
+
if (carverTransform == null)
|
326
|
+
|
327
|
+
{
|
328
|
+
|
329
|
+
return;
|
330
|
+
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
var carver = new Box(carverTransform);
|
336
|
+
|
337
|
+
if (!carver.HasVolume)
|
338
|
+
|
339
|
+
{
|
340
|
+
|
341
|
+
return;
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
// くり抜かれる側自体は空オブジェクトになっており、子オブジェクトとしてCubeオブジェクトを持っている
|
348
|
+
|
349
|
+
// Carveを実行すると子オブジェクトをそれぞれくり抜いて、断片を再び自身の子とする
|
350
|
+
|
351
|
+
var newBoxes = new List<Box>();
|
352
|
+
|
353
|
+
var carveeTransforms = this.transform.Cast<Transform>().ToArray();
|
354
|
+
|
355
|
+
foreach (var carveeTransform in carveeTransforms)
|
356
|
+
|
357
|
+
{
|
358
|
+
|
359
|
+
var carvee = new Box(carveeTransform);
|
360
|
+
|
361
|
+
if (!carvee.HasVolume)
|
362
|
+
|
363
|
+
{
|
364
|
+
|
365
|
+
continue;
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
carvee.Carve(carver, newBoxes);
|
372
|
+
|
373
|
+
var carveeMaterial = carveeTransform.GetComponent<Renderer>().sharedMaterial;
|
374
|
+
|
375
|
+
foreach (var newBox in newBoxes)
|
376
|
+
|
377
|
+
{
|
378
|
+
|
379
|
+
newBox.ToCube(this.transform, carveeMaterial);
|
380
|
+
|
381
|
+
}
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
Destroy(carveeTransform.gameObject);
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
}
|
392
|
+
|
393
|
+
```
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
その他オブジェクト操作用のスクリプト(コードは省略)を付けて実行したところ、下図のような感じになりました。
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+

|