forked from stride3d/stride
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticMeshColliderShape.cs
More file actions
270 lines (232 loc) · 10.4 KB
/
StaticMeshColliderShape.cs
File metadata and controls
270 lines (232 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Mathematics;
using Stride.Core.Serialization.Contents;
using Stride.Extensions;
using Stride.Graphics;
using Stride.Graphics.GeometricPrimitives;
using Stride.Graphics.Semantics;
using Stride.Rendering;
namespace Stride.Physics
{
public class StaticMeshColliderShape : ColliderShape
{
/// <summary> Can be null when this was created without Model </summary>
[CanBeNull]
public readonly Model Model;
private readonly SharedMeshData sharedData;
private static readonly Dictionary<string, SharedMeshData> MeshSharingCache = new();
/// <summary>
/// Create a static collider from an asset model, any changes the model receives won't be reflected on the collider
/// </summary>
public StaticMeshColliderShape(Model model, IServiceRegistry services) : this(BuildAndShareMeshes(model, services)) => this.Model = model;
/// <summary>
/// Create a static collider from the data provided, data will only be read, changes to it
/// won't be reflected on the collider.
/// </summary>
public StaticMeshColliderShape(ICollection<Vector3> vertices, ICollection<int> indices)
: this(new SharedMeshData
{
BulletMesh = new BulletSharp.TriangleIndexVertexArray(indices, new StrideToBulletWrapper(vertices))
})
{
}
StaticMeshColliderShape(SharedMeshData sharedDataParam)
{
sharedData = sharedDataParam;
Type = ColliderShapeTypes.StaticMesh;
Is2D = false;
InternalShape = new BulletSharp.BvhTriangleMeshShape(sharedDataParam.BulletMesh, true);
DebugPrimitiveMatrix = Matrix.Scaling(Vector3.One * DebugScaling);
}
public override void Dispose()
{
base.Dispose();
if (sharedData.Key == null)
{
// Not actually shared, dispose and move on
sharedData.Dispose();
return;
}
lock (MeshSharingCache)
{
sharedData.RefCount--;
if (sharedData.RefCount == 0)
{
MeshSharingCache.Remove(sharedData.Key);
sharedData.Dispose();
}
}
}
public unsafe void GetMeshDataCopy(out Vector3[] verts, out int[] indices)
{
var iMesh = sharedData.BulletMesh.IndexedMeshArray[0];
{
int lengthInBytes = iMesh.NumVertices * iMesh.VertexStride;
verts = new Span<Vector3>( (void*)iMesh.VertexBase, lengthInBytes / sizeof(Vector3) ).ToArray();
}
{
int lengthInBytes = iMesh.NumTriangles * iMesh.TriangleIndexStride;
indices = new Span<int>( (void*)iMesh.TriangleIndexBase, lengthInBytes / sizeof(int) ).ToArray();
}
}
public override MeshDraw CreateDebugPrimitive(GraphicsDevice device)
{
GetMeshDataCopy(out var verts, out var indices);
var vPos = new VertexPositionNormalTexture[verts.Length];
for (int i = 0; i < vPos.Length; i++)
vPos[i].Position = verts[i];
var meshData = new GeometricMeshData<VertexPositionNormalTexture>(vPos, indices, false);
return new GeometricPrimitive(device, meshData).ToMeshDraw();
}
static SharedMeshData BuildAndShareMeshes(Model model, IServiceRegistry services)
{
var sharedContent = services.GetService<ContentManager>();
string modelUrl = null;
if (sharedContent != null && sharedContent.TryGetAssetUrl(model, out modelUrl))
{
lock (MeshSharingCache)
{
if (MeshSharingCache.TryGetValue(modelUrl, out var sharedData))
{
sharedData.RefCount++;
return sharedData;
}
}
}
Matrix[] nodeTransforms = null;
if (model.Skeleton != null)
{
var nodesLength = model.Skeleton.Nodes.Length;
nodeTransforms = new Matrix[nodesLength];
nodeTransforms[0] = Matrix.Identity;
for (var i = 0; i < nodesLength; i++)
{
var node = model.Skeleton.Nodes[i];
Matrix.Transformation(ref node.Transform.Scale, ref node.Transform.Rotation, ref node.Transform.Position, out var localMatrix);
Matrix worldMatrix;
if (node.ParentIndex != -1)
{
if (node.ParentIndex >= i)
throw new InvalidOperationException("Skeleton nodes are not sorted");
var nodeTransform = nodeTransforms[node.ParentIndex];
Matrix.Multiply(ref localMatrix, ref nodeTransform, out worldMatrix);
}
else
{
worldMatrix = localMatrix;
}
if (i != 0)
{
nodeTransforms[i] = worldMatrix;
}
}
}
int totalVerts = 0, totalIndices = 0;
foreach (var meshData in model.Meshes)
{
totalVerts += meshData.Draw.VertexBuffers[0].Count;
totalIndices += meshData.Draw.IndexBuffer.Count;
}
var combinedVerts = new Vector3[totalVerts];
var combinedIndices = new int[totalIndices];
var verticesLeft = combinedVerts.AsSpan();
var indicesLeft = combinedIndices.AsSpan();
int indexOffset = 0;
foreach (var meshData in model.Meshes)
{
meshData.Draw.VertexBuffers[0].AsReadable(services, out var vertexHelper, out var vertexCount);
meshData.Draw.IndexBuffer.AsReadable(services, out var indexHelper, out var indexCount);
var vertSlice = verticesLeft[..vertexCount];
vertexHelper.Copy<PositionSemantic, Vector3>(vertSlice);
if (nodeTransforms != null)
{
for (int i = 0; i < vertSlice.Length; i++)
{
Matrix posMatrix = Matrix.Translation(vertSlice[i]);
Matrix.Multiply(ref posMatrix, ref nodeTransforms[meshData.NodeIndex], out var finalMatrix);
vertSlice[i] = finalMatrix.TranslationVector;
}
}
var indicesForSlice = indicesLeft[..indexCount];
indexHelper.CopyTo(indicesForSlice);
for (int i = 0; i < indicesForSlice.Length; i++)
indicesForSlice[i] += indexOffset;
indexOffset += vertexCount;
verticesLeft = verticesLeft[vertexCount..];
indicesLeft = indicesLeft[indexCount..];
}
if (string.IsNullOrWhiteSpace(modelUrl))
{
return new SharedMeshData
{
BulletMesh = new BulletSharp.TriangleIndexVertexArray(combinedIndices, new StrideToBulletWrapper(combinedVerts))
};
}
lock (MeshSharingCache)
{
if (MeshSharingCache.TryGetValue(modelUrl, out var sharedData))
{
// Another thread was building this concurrently and it finished before us,
// nothing to cleanup so we can just use theirs and move on.
sharedData.RefCount++;
return sharedData;
}
sharedData = new SharedMeshData
{
BulletMesh = new BulletSharp.TriangleIndexVertexArray(combinedIndices, new StrideToBulletWrapper(combinedVerts)),
Key = modelUrl,
RefCount = 1,
};
MeshSharingCache.Add(modelUrl, sharedData);
return sharedData;
}
}
private record SharedMeshData : IDisposable
{
public BulletSharp.TriangleIndexVertexArray BulletMesh;
public int RefCount;
public string Key;
public void Dispose()
{
BulletMesh.IndexedMeshArray.Clear();
BulletMesh.Dispose();
}
}
private class StrideToBulletWrapper : ICollection<BulletSharp.Math.Vector3>
{
private readonly ICollection<Vector3> internalColl;
public StrideToBulletWrapper(ICollection<Vector3> collectionToConvert)
{
internalColl = collectionToConvert;
}
public int Count => internalColl.Count;
public bool IsReadOnly => true;
public bool Contains(BulletSharp.Math.Vector3 item) => internalColl.Contains(item);
public void CopyTo(BulletSharp.Math.Vector3[] array, int arrayIndex)
{
foreach (var value in internalColl)
{
if (arrayIndex >= array.Length)
return;
array[arrayIndex++] = value;
}
}
public void Add(BulletSharp.Math.Vector3 item) => throw new InvalidOperationException("Collection is read only");
public bool Remove(BulletSharp.Math.Vector3 item) => throw new InvalidOperationException("Collection is read only");
public void Clear() => throw new InvalidOperationException("Collection is read only");
public IEnumerator<BulletSharp.Math.Vector3> GetEnumerator()
{
foreach (var value in internalColl)
yield return value;
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}
}