所以我找到了这些方法/函数来为我的缩小模型创建一个BoundingBox,显然我必须将BoundingBox缩放到相同的比例。我不太熟悉顶点的使用,但我知道如何使用矩阵进行扩展。在这种情况下,我如何缩放BoundingBox?我已经在网上搜索了一段时间,我还没有找到解决方案。
public Vector3[] GetVertexElement(ModelMeshPart meshPart, VertexElementUsage usage)
{
VertexDeclaration vd = meshPart.VertexBuffer.VertexDeclaration;
VertexElement[] elements = vd.GetVertexElements();
Func<VertexElement, bool> elementPredicate = ve => ve.VertexElementUsage == usage && ve.VertexElementFormat == VertexElementFormat.Vector3;
if (!elements.Any(elementPredicate))
return null;
VertexElement element = elements.First(elementPredicate);
Vector3[] vertexData = new Vector3[meshPart.NumVertices];
meshPart.VertexBuffer.GetData((meshPart.VertexOffset * vd.VertexStride) + element.Offset, vertexData, 0, vertexData.Length, vd.VertexStride);
return vertexData;
}
public BoundingBox? GetBoundingBox(ModelMeshPart meshPart, Matrix transform)
{
if (meshPart.VertexBuffer == null)
return null;
Vector3[] positions = GetVertexElement(meshPart, VertexElementUsage.Position);
if (positions == null)
return null;
Vector3[] transformedPositions = new Vector3[positions.Length];
Vector3.Transform(positions, ref transform, transformedPositions);
return BoundingBox.CreateFromPoints(transformedPositions);
}
public BoundingBox CreateBoundingBox(Model model)
{
Matrix[] boneTransforms = new Matrix[model.Bones.Count];
model.CopyAbsoluteBoneTransformsTo(boneTransforms);
BoundingBox result = new BoundingBox();
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
BoundingBox? meshPartBoundingBox = GetBoundingBox(meshPart, boneTransforms[mesh.ParentBone.Index]);
if (meshPartBoundingBox != null)
result = BoundingBox.CreateMerged(result, meshPartBoundingBox.Value);
}
}
return result;
}
答案 0 :(得分:0)
我不确定,但我认为必须通过创建一个新的最大值和最小值来自己做。 也不确定它是否有效,但我认为这样做。
private Vector3 Scale(float scale, BoundingBox b)
{
//Get delta values
float dx = Math.Abs(b.Max.X - b.Min.X);
float dy = Math.Abs(b.Max.Y - b.Min.Y);
float dz = Math.Abs(b.Max.Z - b.Min.Z);
//get new delta values
float newdx = dx * scale;
float newdy = dy * scale;
float newdz = dz * scale;
//new max vector
//oldvalue - removed delta, of course divided by 2(half for max and half for min).
Vecotr3 newMax = new Vecotr3(b.Max.X - ((dx-newdx)/2),
b.Max.Y - ((dy-newdy)/2),
b.Max.Z - ((dz-newdz)/2));
//new min vector
//oldvalue + removed delta, of course divided by 2(half for max and half for min).
Vecotr3 newMin = new Vecotr3(b.Min.X + ((dx-newdx)/2),
b.Min.Y + ((dy-newdy)/2),
b.Min.Z + ((dz-newdz)/2));
return new BoundingBox(newMin, mewMax);
{