|
| 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 | +} |
0 commit comments