Skip to content

Commit 62475a3

Browse files
azenoxen2
authored andcommitted
fix: Regression in mesh bounds calculation introduced through #2858
Reverts the method to what it was before #2858. This fix is not necessary on main branch where it was already applied by 8e791ad. However that fix is not applicable on 4.2 branch because it needs .net10.
1 parent 7cfd7f2 commit 62475a3

File tree

1 file changed

+32
-36
lines changed

1 file changed

+32
-36
lines changed

sources/engine/Stride.Rendering/Extensions/BoundingExtensions.cs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,69 @@
66
using Stride.Core.Mathematics;
77
using Stride.Graphics;
88
using Stride.Graphics.Data;
9-
using Stride.Graphics.Semantics;
109

1110
namespace Stride.Extensions
1211
{
1312
public static class BoundingExtensions
1413
{
15-
public static BoundingBox ComputeBounds(this VertexBufferBinding vertexBufferBinding, ref Matrix matrix, out BoundingSphere boundingSphere)
14+
public static unsafe BoundingBox ComputeBounds(this VertexBufferBinding vertexBufferBinding, ref Matrix matrix, out BoundingSphere boundingSphere)
1615
{
17-
var helper = new VertexBufferHelper(vertexBufferBinding, vertexBufferBinding.Buffer.GetSerializationData().Content, out _);
16+
var positionOffset = vertexBufferBinding.Declaration
17+
.EnumerateWithOffsets()
18+
.First(x => x.VertexElement.SemanticAsText == "POSITION")
19+
.Offset;
1820

19-
var computeBoundsStruct = new ComputeBoundsStruct
20-
{
21-
Box = BoundingBox.Empty,
22-
Sphere = new BoundingSphere(),
23-
Matrix = matrix
24-
};
25-
helper.Read<PositionSemantic, Vector3, ComputeBoundsStruct>(default, computeBoundsStruct);
26-
27-
boundingSphere = computeBoundsStruct.Sphere;
28-
return computeBoundsStruct.Box;
29-
}
30-
31-
struct ComputeBoundsStruct : VertexBufferHelper.IReader<Vector3>
32-
{
33-
public required BoundingBox Box;
34-
public required BoundingSphere Sphere;
35-
public required Matrix Matrix;
21+
var boundingBox = BoundingBox.Empty;
22+
boundingSphere = new BoundingSphere();
3623

37-
public unsafe void Read<TConverter, TSource>(byte* startPointer, int elementCount, int stride, Span<Vector3> destination) where TConverter : IConverter<TSource, Vector3> where TSource : unmanaged
24+
var vertexStride = vertexBufferBinding.Declaration.VertexStride;
25+
fixed (byte* bufferStart = &vertexBufferBinding.Buffer.GetSerializationData().Content[vertexBufferBinding.Offset])
3826
{
3927
// Calculates bounding box and bounding sphere center
40-
for (byte* sourcePtr = startPointer, end = startPointer + elementCount * stride; sourcePtr < end; sourcePtr += stride)
28+
byte* buffer = bufferStart + positionOffset;
29+
for (int i = 0; i < vertexBufferBinding.Count; ++i)
4130
{
42-
TConverter.Convert(*(TSource*)sourcePtr, out var position);
31+
var position = (Vector3*)buffer;
4332
Vector3 transformedPosition;
4433

45-
Vector3.TransformCoordinate(ref position, ref Matrix, out transformedPosition);
34+
Vector3.TransformCoordinate(ref *position, ref matrix, out transformedPosition);
4635

4736
// Prepass calculate the center of the sphere
48-
Vector3.Add(ref transformedPosition, ref Sphere.Center, out Sphere.Center);
49-
50-
BoundingBox.Merge(ref Box, ref transformedPosition, out Box);
37+
Vector3.Add(ref transformedPosition, ref boundingSphere.Center, out boundingSphere.Center);
38+
39+
BoundingBox.Merge(ref boundingBox, ref transformedPosition, out boundingBox);
40+
41+
buffer += vertexStride;
5142
}
5243

5344
//This is the center of our sphere.
54-
Sphere.Center /= elementCount;
45+
boundingSphere.Center /= (float)vertexBufferBinding.Count;
5546

5647
// Calculates bounding sphere center
57-
for (byte* sourcePtr = startPointer, end = startPointer + elementCount * stride; sourcePtr < end; sourcePtr += stride)
48+
buffer = bufferStart + positionOffset;
49+
for (int i = 0; i < vertexBufferBinding.Count; ++i)
5850
{
59-
TConverter.Convert(*(TSource*)sourcePtr, out var position);
51+
var position = (Vector3*)buffer;
6052
Vector3 transformedPosition;
6153

62-
Vector3.TransformCoordinate(ref position, ref Matrix, out transformedPosition);
54+
Vector3.TransformCoordinate(ref *position, ref matrix, out transformedPosition);
6355

64-
//We are doing a relative distance comparison to find the maximum distance
56+
//We are doing a relative distance comparasin to find the maximum distance
6557
//from the center of our sphere.
6658
float distance;
67-
Vector3.DistanceSquared(ref Sphere.Center, ref transformedPosition, out distance);
59+
Vector3.DistanceSquared(ref boundingSphere.Center, ref transformedPosition, out distance);
6860

69-
if (distance > Sphere.Radius)
70-
Sphere.Radius = distance;
61+
if (distance > boundingSphere.Radius)
62+
boundingSphere.Radius = distance;
63+
64+
buffer += vertexStride;
7165
}
7266

7367
//Find the real distance from the DistanceSquared.
74-
Sphere.Radius = MathF.Sqrt(Sphere.Radius);
68+
boundingSphere.Radius = MathF.Sqrt(boundingSphere.Radius);
7569
}
70+
71+
return boundingBox;
7672
}
7773
}
7874
}

0 commit comments

Comments
 (0)