ModelMeshごとのBoundingSphereはプロパティとして用意されているが、ModelMeshPartには無いようなので作ってみた。
動かしてみたところ、コードに問題があるのか綺麗に作成されなかったりする。
改善案は今のところ思いつかない。
精密に作るなら手動で作るべきか。
public static BoundingSphere[] CreateBoundingSpheres(ModelMeshPartCollection parts, VertexBuffer vb, IndexBuffer ib)
{
BoundingSphere[] spheres = new BoundingSphere[parts.Count];
// 頂点データを取得する。頂点フォーマットは適切なものを使用する。
VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[vb.SizeInBytes /
VertexPositionNormalTexture.SizeInBytes];
vb.GetData(vertices);
// 頂点インデックスを取得する。
ushort[] indices = new ushort[ib.SizeInBytes / sizeof(ushort)];
ib.GetData(indices);
// BoundingSphereを作成する。
for (int i = 0; i < spheres.Length; i++)
{
ModelMeshPart part = parts[i];
Vector3[] points = new Vector3[part.PrimitiveCount * 3];
// 頂点の位置を取得する。
for (int j = 0; j < part.PrimitiveCount * 3; j++)
{
points[j] = vertices[indices[j + part.StartIndex] + part.BaseVertex].Position;
}
spheres[i] = BoundingSphere.CreateFromPoints(points);
}
return spheres;
}
{
BoundingSphere[] spheres = new BoundingSphere[parts.Count];
// 頂点データを取得する。頂点フォーマットは適切なものを使用する。
VertexPositionNormalTexture[] vertices = new VertexPositionNormalTexture[vb.SizeInBytes /
VertexPositionNormalTexture.SizeInBytes];
vb.GetData(vertices);
// 頂点インデックスを取得する。
ushort[] indices = new ushort[ib.SizeInBytes / sizeof(ushort)];
ib.GetData(indices);
// BoundingSphereを作成する。
for (int i = 0; i < spheres.Length; i++)
{
ModelMeshPart part = parts[i];
Vector3[] points = new Vector3[part.PrimitiveCount * 3];
// 頂点の位置を取得する。
for (int j = 0; j < part.PrimitiveCount * 3; j++)
{
points[j] = vertices[indices[j + part.StartIndex] + part.BaseVertex].Position;
}
spheres[i] = BoundingSphere.CreateFromPoints(points);
}
return spheres;
}
動かしてみたところ、コードに問題があるのか綺麗に作成されなかったりする。
改善案は今のところ思いつかない。
精密に作るなら手動で作るべきか。

