|
6 | 6 | using Stride.Core.Mathematics; |
7 | 7 | using Stride.Graphics; |
8 | 8 | using Stride.Graphics.Data; |
9 | | -using Stride.Graphics.Semantics; |
10 | 9 |
|
11 | 10 | namespace Stride.Extensions |
12 | 11 | { |
13 | 12 | public static class BoundingExtensions |
14 | 13 | { |
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) |
16 | 15 | { |
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; |
18 | 20 |
|
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(); |
36 | 23 |
|
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]) |
38 | 26 | { |
39 | 27 | // 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) |
41 | 30 | { |
42 | | - TConverter.Convert(*(TSource*)sourcePtr, out var position); |
| 31 | + var position = (Vector3*)buffer; |
43 | 32 | Vector3 transformedPosition; |
44 | 33 |
|
45 | | - Vector3.TransformCoordinate(ref position, ref Matrix, out transformedPosition); |
| 34 | + Vector3.TransformCoordinate(ref *position, ref matrix, out transformedPosition); |
46 | 35 |
|
47 | 36 | // 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; |
51 | 42 | } |
52 | 43 |
|
53 | 44 | //This is the center of our sphere. |
54 | | - Sphere.Center /= elementCount; |
| 45 | + boundingSphere.Center /= (float)vertexBufferBinding.Count; |
55 | 46 |
|
56 | 47 | // 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) |
58 | 50 | { |
59 | | - TConverter.Convert(*(TSource*)sourcePtr, out var position); |
| 51 | + var position = (Vector3*)buffer; |
60 | 52 | Vector3 transformedPosition; |
61 | 53 |
|
62 | | - Vector3.TransformCoordinate(ref position, ref Matrix, out transformedPosition); |
| 54 | + Vector3.TransformCoordinate(ref *position, ref matrix, out transformedPosition); |
63 | 55 |
|
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 |
65 | 57 | //from the center of our sphere. |
66 | 58 | float distance; |
67 | | - Vector3.DistanceSquared(ref Sphere.Center, ref transformedPosition, out distance); |
| 59 | + Vector3.DistanceSquared(ref boundingSphere.Center, ref transformedPosition, out distance); |
68 | 60 |
|
69 | | - if (distance > Sphere.Radius) |
70 | | - Sphere.Radius = distance; |
| 61 | + if (distance > boundingSphere.Radius) |
| 62 | + boundingSphere.Radius = distance; |
| 63 | + |
| 64 | + buffer += vertexStride; |
71 | 65 | } |
72 | 66 |
|
73 | 67 | //Find the real distance from the DistanceSquared. |
74 | | - Sphere.Radius = MathF.Sqrt(Sphere.Radius); |
| 68 | + boundingSphere.Radius = MathF.Sqrt(boundingSphere.Radius); |
75 | 69 | } |
| 70 | + |
| 71 | + return boundingBox; |
76 | 72 | } |
77 | 73 | } |
78 | 74 | } |
0 commit comments