This repository was archived by the owner on Mar 21, 2025. It is now read-only.
forked from sharpdx/SharpDX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cs
More file actions
309 lines (274 loc) · 12.2 KB
/
Model.cs
File metadata and controls
309 lines (274 loc) · 12.2 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using SharpDX.Toolkit.Content;
namespace SharpDX.Toolkit.Graphics
{
public delegate Texture ModelMaterialTextureLoaderDelegate(string name);
[ContentReader(typeof(ModelContentReader))]
public class Model : Component
{
private static Matrix[] sharedDrawBones;
public MaterialCollection Materials;
public ModelBoneCollection Bones;
public ModelMeshCollection Meshes;
public ModelAnimationCollection Animations;
public PropertyCollection Properties;
/// <summary>
/// Copies a transform of each bone in a model relative to all parent bones of the bone into a given array.
/// </summary>
/// <param name="destinationBoneTransformsPtr">The array to receive bone transforms. Length of the allocated array must be at least == sizeof(Matrix) * Bones.Count</param>
/// <exception cref="System.ArgumentNullException">destinationBoneTransforms</exception>
public unsafe void CopyAbsoluteBoneTransformsTo(IntPtr destinationBoneTransformsPtr)
{
if (destinationBoneTransformsPtr == IntPtr.Zero)
{
throw new ArgumentNullException("destinationBoneTransformsPtr");
}
var destinationBoneTransforms = (Matrix*)destinationBoneTransformsPtr;
int count = Bones.Count;
for (int i = 0; i < count; i++)
{
ModelBone bone = Bones[i];
if (bone.Parent == null)
{
destinationBoneTransforms[i] = bone.Transform;
}
else
{
destinationBoneTransforms[i] = bone.Transform * destinationBoneTransforms[bone.Parent.Index];
}
}
}
/// <summary>
/// Copies a transform of each bone in a model relative to all parent bones of the bone into a given array.
/// </summary>
/// <param name="destinationBoneTransforms">The array to receive bone transforms.</param>
/// <exception cref="System.ArgumentNullException">destinationBoneTransforms</exception>
/// <exception cref="System.ArgumentOutOfRangeException">destinationBoneTransforms</exception>
public void CopyAbsoluteBoneTransformsTo(Matrix[] destinationBoneTransforms)
{
if (destinationBoneTransforms == null)
{
throw new ArgumentNullException("destinationBoneTransforms");
}
if (destinationBoneTransforms.Length < Bones.Count)
{
throw new ArgumentOutOfRangeException("destinationBoneTransforms");
}
int count = Bones.Count;
for (int i = 0; i < count; i++)
{
ModelBone bone = Bones[i];
if (bone.Parent == null)
{
destinationBoneTransforms[i] = bone.Transform;
}
else
{
destinationBoneTransforms[i] = bone.Transform * destinationBoneTransforms[bone.Parent.Index];
}
}
}
/// <summary>
/// Copies an array of transforms into each bone in the model.
/// </summary>
/// <param name="sourceBoneTransforms">An array containing new bone transforms.</param>
/// <exception cref="System.ArgumentNullException">sourceBoneTransforms</exception>
/// <exception cref="System.ArgumentOutOfRangeException">sourceBoneTransforms</exception>
public void CopyBoneTransformsFrom(Matrix[] sourceBoneTransforms)
{
if (sourceBoneTransforms == null)
{
throw new ArgumentNullException("sourceBoneTransforms");
}
if (sourceBoneTransforms.Length < Bones.Count)
{
throw new ArgumentOutOfRangeException("sourceBoneTransforms");
}
int count = Bones.Count;
for (int i = 0; i < count; i++)
{
Bones[i].Transform = sourceBoneTransforms[i];
}
}
/// <summary>
/// Copies each bone transform relative only to the parent bone of the model to a given array.
/// </summary>
/// <param name="destinationBoneTransforms">The array to receive bone transforms.</param>
/// <exception cref="System.ArgumentNullException">destinationBoneTransforms</exception>
/// <exception cref="System.ArgumentOutOfRangeException">destinationBoneTransforms</exception>
public void CopyBoneTransformsTo(Matrix[] destinationBoneTransforms)
{
if (destinationBoneTransforms == null)
{
throw new ArgumentNullException("destinationBoneTransforms");
}
if (destinationBoneTransforms.Length < Bones.Count)
{
throw new ArgumentOutOfRangeException("destinationBoneTransforms");
}
int count = Bones.Count;
for (int i = 0; i < count; i++)
{
destinationBoneTransforms[i] = Bones[i].Transform;
}
}
/// <summary>
/// Render a model after applying the matrix transformations.
/// </summary>
/// <param name="context">The <see cref="GraphicsDevice"/> context.</param>
/// <param name="world">A world transformation matrix.</param>
/// <param name="view">A view transformation matrix.</param>
/// <param name="projection">A projection transformation matrix.</param>
/// <param name="effectOverride">An effect instance that will override all effects attached to this model. Null by default (no override)</param>
/// <exception cref="System.InvalidOperationException">Mesh has no effect and effectOverride is null</exception>
public unsafe void Draw(GraphicsDevice context, Matrix world, Matrix view, Matrix projection, Effect effectOverride = null)
{
int count = Meshes.Count;
int boneCount = Bones.Count;
if (sharedDrawBones == null || sharedDrawBones.Length < boneCount)
{
sharedDrawBones = new Matrix[boneCount];
}
CopyAbsoluteBoneTransformsTo(sharedDrawBones);
var defaultParametersContext = default(EffectDefaultParametersContext);
for (int i = 0; i < count; i++)
{
var mesh = Meshes[i];
int index = mesh.ParentBone.Index;
int effectCount = mesh.Effects.Count;
if (effectOverride != null)
{
if (effectOverride is SkinnedEffect)
{
effectOverride.DefaultParameters.Apply(ref defaultParametersContext, ref world, ref view, ref projection);
}
else
{
Matrix worldTranformed;
Matrix.Multiply(ref sharedDrawBones[index], ref world, out worldTranformed);
effectOverride.DefaultParameters.Apply(ref defaultParametersContext, ref worldTranformed, ref view, ref projection);
}
}
else
{
for (int j = 0; j < effectCount; j++)
{
var effect = mesh.Effects[j];
if (effect == null)
{
throw new InvalidOperationException("Mesh has no effect and effectOverride is null");
}
Matrix worldTranformed;
Matrix.Multiply(ref sharedDrawBones[index], ref world, out worldTranformed);
var matrices = effect as IEffectMatrices;
if (matrices == null)
{
effect.DefaultParameters.Apply(ref defaultParametersContext, ref world, ref view, ref projection);
}
else
{
matrices.World = effect is SkinnedEffect ? world : worldTranformed;
matrices.View = view;
matrices.Projection = projection;
}
}
}
mesh.Draw(context, sharedDrawBones, effectOverride);
}
}
/// <summary>
/// Calculates the bounds of this model.
/// </summary>
/// <returns>BoundingSphere.</returns>
public unsafe BoundingSphere CalculateBounds()
{
return CalculateBounds(Matrix.Identity);
}
/// <summary>
/// Calculates the bounds of this model in world space.
/// </summary>
/// <param name="world">The world.</param>
/// <returns>BoundingSphere.</returns>
public unsafe BoundingSphere CalculateBounds(Matrix world)
{
int count = Meshes.Count;
int boneCount = Bones.Count;
if (sharedDrawBones == null || sharedDrawBones.Length < boneCount)
{
sharedDrawBones = new Matrix[boneCount];
}
CopyAbsoluteBoneTransformsTo(sharedDrawBones);
var defaultSphere = new BoundingSphere(Vector3.Zero, 0.0f);
for (int i = 0; i < count; i++)
{
var mesh = Meshes[i];
int index = mesh.ParentBone.Index;
Matrix result;
Matrix.Multiply(ref sharedDrawBones[index], ref world, out result);
var meshSphere = mesh.BoundingSphere;
Vector3.TransformCoordinate(ref meshSphere.Center, ref result, out meshSphere.Center);
BoundingSphere.Merge(ref defaultSphere, ref meshSphere, out defaultSphere);
}
return defaultSphere;
}
/// <summary>
/// Iterate on each <see cref="ModelMeshPart"/>.
/// </summary>
/// <param name="meshPartFunction">The mesh part function.</param>
public void ForEach(Action<ModelMeshPart> meshPartFunction)
{
int meshCount = Meshes.Count;
for (int i = 0; i < meshCount; i++)
{
Meshes[i].ForEach(meshPartFunction);
}
}
/// <summary>
/// Gets the root bone for this model.
/// </summary>
/// <value>The root.</value>
public ModelBone Root
{
get
{
return Bones.Count > 0 ? Bones[0] : null;
}
}
public virtual Model Clone()
{
var model = (Model)MemberwiseClone();
throw new NotImplementedException();
}
public static Model Load(GraphicsDevice graphicsDevice, Stream stream, ModelMaterialTextureLoaderDelegate textureLoader)
{
using (var serializer = new ModelReader(graphicsDevice, stream, textureLoader))
{
return serializer.ReadModel();
}
}
public override string ToString()
{
return string.Format("{0} {1}", this.GetType().Name, Name);
}
}
}