-
-
Notifications
You must be signed in to change notification settings - Fork 909
/
ModelComponent.cs
316 lines (283 loc) · 11.8 KB
/
ModelComponent.cs
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
310
311
312
313
314
315
316
// Copyright (c) Stride contributors (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.Generic;
using System.ComponentModel;
using Stride.Core;
using Stride.Core.Annotations;
using Stride.Core.Collections;
using Stride.Core.Mathematics;
using Stride.Engine.Design;
using Stride.Engine.Processors;
using Stride.Rendering;
using Stride.Updater;
namespace Stride.Engine
{
/// <summary>
/// Add a <see cref="Model"/> to an <see cref="Entity"/>, that will be used during rendering.
/// </summary>
[DataContract("ModelComponent")]
[Display("Model", Expand = ExpandRule.Once)]
// TODO GRAPHICS REFACTOR
[DefaultEntityComponentProcessor(typeof(ModelTransformProcessor))]
[DefaultEntityComponentRenderer(typeof(ModelRenderProcessor))]
[ComponentOrder(11000)]
[ComponentCategory("Model")]
public sealed class ModelComponent : ActivableEntityComponent, IModelInstance
{
private readonly List<MeshInfo> meshInfos = new List<MeshInfo>();
private Model model;
private SkeletonUpdater skeleton;
private bool modelViewHierarchyDirty = true;
/// <summary>
/// Per-entity state of each individual mesh of a model.
/// </summary>
public class MeshInfo
{
/// <summary>
/// The current blend matrices of a skinned meshes, transforming from mesh space to world space, for each bone.
/// </summary>
public Matrix[] BlendMatrices;
/// <summary>
/// The meshes current bounding box in world space.
/// </summary>
public BoundingBox BoundingBox;
/// <summary>
/// The meshes current sphere box in world space.
/// </summary>
public BoundingSphere BoundingSphere;
}
/// <summary>
/// Initializes a new instance of the <see cref="ModelComponent"/> class.
/// </summary>
public ModelComponent() : this(null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ModelComponent"/> class.
/// </summary>
/// <param name="model">The model.</param>
public ModelComponent(Model model)
{
Model = model;
IsShadowCaster = true;
}
/// <summary>
/// Gets or sets the model.
/// </summary>
/// <value>
/// The model.
/// </value>
/// <userdoc>The reference to the model asset to attach to this entity</userdoc>
[DataMemberCustomSerializer]
[DataMember(10)]
public Model Model
{
get
{
return model;
}
set
{
if (model != value)
modelViewHierarchyDirty = true;
model = value;
}
}
/// <summary>
/// Gets the materials; non-null ones will override materials from <see cref="Stride.Rendering.Model.Materials"/> (same slots should be used).
/// </summary>
/// <value>
/// The materials overriding <see cref="Stride.Rendering.Model.Materials"/> ones.
/// </value>
/// <userdoc>The list of materials to use with the model. This list overrides the default materials of the model.</userdoc>
[DataMember(40)]
[Category]
[MemberCollection(ReadOnly = true)]
public IndexingDictionary<Material> Materials { get; } = new IndexingDictionary<Material>();
[DataMemberIgnore, DataMemberUpdatable]
[DataMember]
public SkeletonUpdater Skeleton
{
get
{
CheckSkeleton();
return skeleton;
}
}
/// <summary>
/// Gets the current per-entity state for each mesh in the associated model.
/// </summary>
[DataMemberIgnore]
public IReadOnlyList<MeshInfo> MeshInfos => meshInfos;
private void CheckSkeleton()
{
if (modelViewHierarchyDirty)
{
ModelUpdated();
modelViewHierarchyDirty = false;
}
}
/// <summary>
/// Gets or sets a boolean indicating if this model component is casting shadows.
/// </summary>
/// <value>A boolean indicating if this model component is casting shadows.</value>
/// <userdoc>Generate a shadow (when shadow maps are enabled)</userdoc>
[DataMember(30)]
[DefaultValue(true)]
[Display("Cast shadows")]
public bool IsShadowCaster { get; set; }
/// <summary>
/// The render group for this component.
/// </summary>
[DataMember(20)]
[DefaultValue(RenderGroup.Group0)]
[Display("Render group")]
public RenderGroup RenderGroup { get; set; }
/// <summary>
/// Gets the bounding box in world space.
/// </summary>
/// <value>The bounding box.</value>
[DataMemberIgnore]
public BoundingBox BoundingBox;
/// <summary>
/// Gets the bounding sphere in world space.
/// </summary>
/// <value>The bounding sphere.</value>
[DataMemberIgnore]
public BoundingSphere BoundingSphere;
/// <summary>
/// Gets the material at the specified index. If the material is not overriden by this component, it will try to get it from <see cref="Stride.Rendering.Model.Materials"/>
/// </summary>
/// <param name="index">The index of the material</param>
/// <returns>The material at the specified index or null if not found</returns>
public Material GetMaterial(int index)
{
if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), @"index cannot be < 0");
Material material;
if (Materials.TryGetValue(index, out material))
{
return material;
}
// TODO: if Model is null, shouldn't we always return null?
if (Model != null && index < Model.Materials.Count)
{
material = Model.Materials[index].Material;
}
return material;
}
/// <summary>
/// Gets the number of materials (computed from <see cref="Stride.Rendering.Model.Materials"/>)
/// </summary>
/// <returns></returns>
public int GetMaterialCount()
{
if (Model != null)
{
return Model.Materials.Count;
}
return 0;
}
private void ModelUpdated()
{
if (model != null)
{
// Create mesh-per-entity state
meshInfos.Clear();
foreach (var mesh in model.Meshes)
{
var meshData = new MeshInfo();
meshInfos.Add(meshData);
if (mesh.Skinning != null)
meshData.BlendMatrices = new Matrix[mesh.Skinning.Bones.Length];
}
if (skeleton != null)
{
// Reuse previous ModelViewHierarchy
skeleton.Initialize(model.Skeleton);
}
else
{
skeleton = new SkeletonUpdater(model.Skeleton);
}
}
}
/// <summary>
/// Updates the skeleton, skinning and bounding box with the associated transform component.
/// </summary>
/// <param name="transformComponent">The transform component.</param>
internal void Update(TransformComponent transformComponent)
{
if (!Enabled || model == null)
return;
ref Matrix worldMatrix = ref transformComponent.WorldMatrix;
// Check if scaling is negative
var up = Vector3.Cross(worldMatrix.Right, worldMatrix.Forward);
bool isScalingNegative = Vector3.Dot(worldMatrix.Up, up) < 0.0f;
// Make sure skeleton is up to date
CheckSkeleton();
if (skeleton != null)
{
// Update model view hierarchy node matrices
skeleton.NodeTransformations[0].LocalMatrix = worldMatrix;
skeleton.NodeTransformations[0].IsScalingNegative = isScalingNegative;
skeleton.UpdateMatrices();
}
// Update the bounding sphere / bounding box in world space
BoundingSphere = BoundingSphere.Empty;
BoundingBox = BoundingBox.Empty;
bool modelHasBoundingBox = false;
for (int meshIndex = 0; meshIndex < Model.Meshes.Count; meshIndex++)
{
var mesh = Model.Meshes[meshIndex];
var meshInfo = meshInfos[meshIndex];
meshInfo.BoundingSphere = BoundingSphere.Empty;
meshInfo.BoundingBox = BoundingBox.Empty;
if (mesh.Skinning != null && skeleton != null)
{
bool meshHasBoundingBox = false;
var bones = mesh.Skinning.Bones;
// For skinned meshes, bounding box is union of the bounding boxes of the unskinned mesh, transformed by each affecting bone.
for (int boneIndex = 0; boneIndex < bones.Length; boneIndex++)
{
var nodeIndex = bones[boneIndex].NodeIndex;
Matrix.Multiply(ref bones[boneIndex].LinkToMeshMatrix, ref skeleton.NodeTransformations[nodeIndex].WorldMatrix, out meshInfo.BlendMatrices[boneIndex]);
BoundingBox skinnedBoundingBox;
BoundingBox.Transform(ref mesh.BoundingBox, ref meshInfo.BlendMatrices[boneIndex], out skinnedBoundingBox);
BoundingSphere skinnedBoundingSphere;
BoundingSphere.Transform(ref mesh.BoundingSphere, ref meshInfo.BlendMatrices[boneIndex], out skinnedBoundingSphere);
if (meshHasBoundingBox)
{
BoundingBox.Merge(ref meshInfo.BoundingBox, ref skinnedBoundingBox, out meshInfo.BoundingBox);
BoundingSphere.Merge(ref meshInfo.BoundingSphere, ref skinnedBoundingSphere, out meshInfo.BoundingSphere);
}
else
{
meshHasBoundingBox = true;
meshInfo.BoundingSphere = skinnedBoundingSphere;
meshInfo.BoundingBox = skinnedBoundingBox;
}
}
}
else
{
// If there is a skeleton, use the corresponding node's transform. Otherwise, fall back to the model transform.
var transform = skeleton != null ? skeleton.NodeTransformations[mesh.NodeIndex].WorldMatrix : worldMatrix;
BoundingBox.Transform(ref mesh.BoundingBox, ref transform, out meshInfo.BoundingBox);
BoundingSphere.Transform(ref mesh.BoundingSphere, ref transform, out meshInfo.BoundingSphere);
}
if (modelHasBoundingBox)
{
BoundingBox.Merge(ref BoundingBox, ref meshInfo.BoundingBox, out BoundingBox);
BoundingSphere.Merge(ref BoundingSphere, ref meshInfo.BoundingSphere, out BoundingSphere);
}
else
{
BoundingBox = meshInfo.BoundingBox;
BoundingSphere = meshInfo.BoundingSphere;
modelHasBoundingBox = true;
}
}
}
}
}