-
-
Notifications
You must be signed in to change notification settings - Fork 907
/
InstancingUserArray.cs
103 lines (89 loc) · 3.51 KB
/
InstancingUserArray.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
// Copyright (c) Stride contributors (https://stride3d.net) and Tebjan Halm
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using Stride.Core;
using Stride.Core.Mathematics;
using Stride.Engine.Processors;
namespace Stride.Engine
{
[DataContract("InstancingUserArray")]
[Display("UserArray")]
public class InstancingUserArray : IInstancing
{
[DataMember(10)]
[Display("Model Transformation Usage")]
public virtual ModelTransformUsage ModelTransformUsage { get; set; }
/// <summary>
/// The instance count
/// </summary>
[DataMemberIgnore]
public virtual int InstanceCount { get; private set; }
/// <summary>
/// The bounding box of the world matrices, updated automatically by the <see cref="InstancingProcessor"/>.
/// </summary>
[DataMemberIgnore]
public virtual BoundingBox BoundingBox { get; private set; } = BoundingBox.Empty;
/// <summary>
/// The instance transformation matrices.
/// </summary>
[DataMemberIgnore]
public Matrix[] WorldMatrices = Array.Empty<Matrix>();
/// <summary>
/// The inverse instance transformation matrices, updated automatically by the <see cref="InstancingProcessor"/>.
/// </summary>
[DataMemberIgnore]
public Matrix[] WorldInverseMatrices = Array.Empty<Matrix>();
/// <summary>
/// A flag indicating whether the inverse matrices and bounding box should be calculated this frame.
/// </summary>
bool matricesUpdated;
/// <summary>
/// Updates the world matrices.
/// </summary>
/// <param name="matrices">The matrices.</param>
/// <param name="instanceCount">The instance count. When set to -1 the length if the matrices array is used</param>
public void UpdateWorldMatrices(Matrix[] matrices, int instanceCount = -1)
{
WorldMatrices = matrices;
if (WorldMatrices != null)
{
InstanceCount = instanceCount < 0 ? WorldMatrices.Length : Math.Min(WorldMatrices.Length, instanceCount);
}
else
{
InstanceCount = 0;
}
matricesUpdated = true;
}
public virtual void Update()
{
if (matricesUpdated)
{
if (WorldMatrices != null)
{
// Local copy of virtual instance count property
var instanceCount = InstanceCount;
// Make sure inverse matrices array is big enough
if (WorldInverseMatrices.Length < instanceCount)
{
WorldInverseMatrices = new Matrix[instanceCount];
}
// Invert matrices and update bounding box
var bb = BoundingBox.Empty;
for (int i = 0; i < instanceCount; i++)
{
Matrix.Invert(ref WorldMatrices[i], out WorldInverseMatrices[i]);
var pos = WorldMatrices[i].TranslationVector;
BoundingBox.Merge(ref bb, ref pos, out bb);
}
BoundingBox = bb;
}
else
{
BoundingBox = BoundingBox.Empty;
}
}
matricesUpdated = false;
}
}
}