Skip to content

Commit c6d3aea

Browse files
committed
WriteSettings constructor is now public.
reversed c# 8 feature. split runtime files.
1 parent 617e678 commit c6d3aea

File tree

7 files changed

+438
-416
lines changed

7 files changed

+438
-416
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Numerics;
4+
using System.Text;
5+
6+
namespace SharpGLTF.Runtime
7+
{
8+
/// <summary>
9+
/// Defines a reference to a drawable mesh
10+
/// </summary>
11+
abstract class DrawableReference
12+
{
13+
#region lifecycle
14+
15+
protected DrawableReference(Schema2.Node node)
16+
{
17+
_LogicalMeshIndex = node.Mesh.LogicalIndex;
18+
}
19+
20+
#endregion
21+
22+
#region data
23+
24+
private readonly int _LogicalMeshIndex;
25+
26+
#endregion
27+
28+
#region properties
29+
30+
/// <summary>
31+
/// Gets the index of a <see cref="Schema2.Mesh"/> in <see cref="Schema2.ModelRoot.LogicalMeshes"/>
32+
/// </summary>
33+
public int LogicalMeshIndex => _LogicalMeshIndex;
34+
35+
#endregion
36+
37+
#region API
38+
39+
public abstract Transforms.IGeometryTransform CreateGeometryTransform();
40+
41+
public abstract void UpdateGeometryTransform(Transforms.IGeometryTransform geoxform, IReadOnlyList<NodeInstance> instances);
42+
43+
#endregion
44+
}
45+
46+
/// <summary>
47+
/// Defines a reference to a drawable rigid mesh
48+
/// </summary>
49+
sealed class RigidDrawableReference : DrawableReference
50+
{
51+
#region lifecycle
52+
53+
internal RigidDrawableReference(Schema2.Node node, Func<Schema2.Node, int> indexFunc)
54+
: base(node)
55+
{
56+
_NodeIndex = indexFunc(node);
57+
}
58+
59+
#endregion
60+
61+
#region data
62+
63+
private readonly int _NodeIndex;
64+
65+
#endregion
66+
67+
#region API
68+
69+
public override Transforms.IGeometryTransform CreateGeometryTransform() { return new Transforms.RigidTransform(); }
70+
71+
public override void UpdateGeometryTransform(Transforms.IGeometryTransform rigidTransform, IReadOnlyList<NodeInstance> instances)
72+
{
73+
var node = instances[_NodeIndex];
74+
75+
var statxform = (Transforms.RigidTransform)rigidTransform;
76+
statxform.Update(node.WorldMatrix);
77+
statxform.Update(node.MorphWeights, false);
78+
}
79+
80+
#endregion
81+
}
82+
83+
/// <summary>
84+
/// Defines a reference to a drawable skinned mesh
85+
/// </summary>
86+
sealed class SkinnedDrawableReference : DrawableReference
87+
{
88+
#region lifecycle
89+
90+
internal SkinnedDrawableReference(Schema2.Node node, Func<Schema2.Node, int> indexFunc)
91+
: base(node)
92+
{
93+
var skin = node.Skin;
94+
95+
_MorphNodeIndex = indexFunc(node);
96+
97+
_JointsNodeIndices = new int[skin.JointsCount];
98+
_BindMatrices = new Matrix4x4[skin.JointsCount];
99+
100+
for (int i = 0; i < _JointsNodeIndices.Length; ++i)
101+
{
102+
var (j, ibm) = skin.GetJoint(i);
103+
104+
_JointsNodeIndices[i] = indexFunc(j);
105+
_BindMatrices[i] = ibm;
106+
}
107+
}
108+
109+
#endregion
110+
111+
#region data
112+
113+
private readonly int _MorphNodeIndex;
114+
private readonly int[] _JointsNodeIndices;
115+
private readonly Matrix4x4[] _BindMatrices;
116+
117+
#endregion
118+
119+
#region API
120+
121+
public override Transforms.IGeometryTransform CreateGeometryTransform() { return new Transforms.SkinnedTransform(); }
122+
123+
public override void UpdateGeometryTransform(Transforms.IGeometryTransform skinnedTransform, IReadOnlyList<NodeInstance> instances)
124+
{
125+
var skinxform = (Transforms.SkinnedTransform)skinnedTransform;
126+
skinxform.Update(_JointsNodeIndices.Length, idx => _BindMatrices[idx], idx => instances[_JointsNodeIndices[idx]].WorldMatrix);
127+
skinxform.Update(instances[_MorphNodeIndex].MorphWeights, false);
128+
}
129+
130+
#endregion
131+
}
132+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
using SharpGLTF.Transforms;
6+
7+
using XFORM = System.Numerics.Matrix4x4;
8+
9+
namespace SharpGLTF.Runtime
10+
{
11+
/// <summary>
12+
/// Defines a node of a scene graph in <see cref="SceneInstance"/>
13+
/// </summary>
14+
[System.Diagnostics.DebuggerDisplay("{Name}")]
15+
public sealed class NodeInstance
16+
{
17+
#region lifecycle
18+
19+
internal NodeInstance(NodeTemplate template, NodeInstance parent)
20+
{
21+
_Template = template;
22+
_Parent = parent;
23+
}
24+
25+
#endregion
26+
27+
#region data
28+
29+
private readonly NodeTemplate _Template;
30+
private readonly NodeInstance _Parent;
31+
32+
private XFORM _LocalMatrix;
33+
private XFORM? _WorldMatrix;
34+
35+
private SparseWeight8 _MorphWeights;
36+
37+
#endregion
38+
39+
#region properties
40+
41+
public String Name => _Template.Name;
42+
43+
public NodeInstance VisualParent => _Parent;
44+
45+
public SparseWeight8 MorphWeights
46+
{
47+
get => _MorphWeights;
48+
set => _MorphWeights = value;
49+
}
50+
51+
public XFORM LocalMatrix
52+
{
53+
get => _LocalMatrix;
54+
set
55+
{
56+
_LocalMatrix = value;
57+
_WorldMatrix = null;
58+
}
59+
}
60+
61+
public XFORM WorldMatrix
62+
{
63+
get => _GetWorldMatrix();
64+
set => _SetWorldMatrix(value);
65+
}
66+
67+
/// <summary>
68+
/// Gets a value indicating whether any of the transforms down the scene tree has been modified.
69+
/// </summary>
70+
private bool TransformChainIsDirty
71+
{
72+
get
73+
{
74+
if (!_WorldMatrix.HasValue) return true;
75+
76+
return _Parent == null ? false : _Parent.TransformChainIsDirty;
77+
}
78+
}
79+
80+
#endregion
81+
82+
#region API
83+
84+
private XFORM _GetWorldMatrix()
85+
{
86+
if (!TransformChainIsDirty) return _WorldMatrix.Value;
87+
88+
_WorldMatrix = _Parent == null ? _LocalMatrix : XFORM.Multiply(_LocalMatrix, _Parent.WorldMatrix);
89+
90+
return _WorldMatrix.Value;
91+
}
92+
93+
private void _SetWorldMatrix(XFORM xform)
94+
{
95+
if (_Parent == null) { LocalMatrix = xform; return; }
96+
97+
XFORM.Invert(_Parent._GetWorldMatrix(), out XFORM ipwm);
98+
99+
LocalMatrix = XFORM.Multiply(xform, ipwm);
100+
}
101+
102+
public void SetPoseTransform() { SetAnimationFrame(-1, 0); }
103+
104+
public void SetAnimationFrame(int trackLogicalIndex, float time)
105+
{
106+
this.MorphWeights = _Template.GetMorphWeights(trackLogicalIndex, time);
107+
this.LocalMatrix = _Template.GetLocalMatrix(trackLogicalIndex, time);
108+
}
109+
110+
public void SetAnimationFrame(ReadOnlySpan<int> track, ReadOnlySpan<float> time, ReadOnlySpan<float> weight)
111+
{
112+
this.MorphWeights = _Template.GetMorphWeights(track, time, weight);
113+
this.LocalMatrix = _Template.GetLocalMatrix(track, time, weight);
114+
}
115+
116+
#endregion
117+
}
118+
}

0 commit comments

Comments
 (0)