MeshTransform.Rigid.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using XNAV3 = Microsoft.Xna.Framework.Vector3;
  5. using XNAV4 = Microsoft.Xna.Framework.Vector4;
  6. using XNAMAT = Microsoft.Xna.Framework.Matrix;
  7. using VERTEXINFLUENCES = Microsoft.Xna.Framework.Graphics.PackedVector.BoneInfluences;
  8. namespace MonoScene.Graphics
  9. {
  10. sealed class _MeshRigidTransform : _MeshMorphTransform, IMeshTransform
  11. {
  12. #region constructor
  13. public _MeshRigidTransform(Content.RigidDrawableContent owner)
  14. {
  15. _Owner = owner;
  16. Update(XNAMAT.Identity);
  17. // Update(default, false);
  18. }
  19. #endregion
  20. #region data
  21. private readonly Content.RigidDrawableContent _Owner;
  22. private XNAMAT _WorldMatrix;
  23. private Boolean _Visible;
  24. private Boolean _FlipFaces;
  25. #endregion
  26. #region properties
  27. public Boolean Visible => _Visible;
  28. public Boolean FlipFaces => _FlipFaces;
  29. public XNAMAT WorldMatrix => _WorldMatrix;
  30. #endregion
  31. #region API
  32. public void Update(IArmatureTransform armature)
  33. {
  34. // TODO: Update morph targets
  35. Update(armature.GetModelMatrix(_Owner._NodeIndex));
  36. }
  37. public bool TryGetModelMatrix(out XNAMAT modelMatrix)
  38. {
  39. modelMatrix = _WorldMatrix;
  40. return true;
  41. }
  42. public XNAMAT[] TryGetSkinMatrices()
  43. {
  44. return null;
  45. }
  46. public void Update(XNAMAT worldMatrix)
  47. {
  48. _WorldMatrix = worldMatrix;
  49. // http://m-hikari.com/ija/ija-password-2009/ija-password5-8-2009/hajrizajIJA5-8-2009.pdf
  50. float determinant3x3 =
  51. +(worldMatrix.M13 * worldMatrix.M21 * worldMatrix.M32)
  52. + (worldMatrix.M11 * worldMatrix.M22 * worldMatrix.M33)
  53. + (worldMatrix.M12 * worldMatrix.M23 * worldMatrix.M31)
  54. - (worldMatrix.M12 * worldMatrix.M21 * worldMatrix.M33)
  55. - (worldMatrix.M13 * worldMatrix.M22 * worldMatrix.M31)
  56. - (worldMatrix.M11 * worldMatrix.M23 * worldMatrix.M32);
  57. _Visible = Math.Abs(determinant3x3) > float.Epsilon;
  58. _FlipFaces = determinant3x3 < 0;
  59. }
  60. public XNAV3 TransformPosition(XNAV3 position, IReadOnlyList<XNAV3> morphTargets, in VERTEXINFLUENCES skinWeights)
  61. {
  62. position = MorphVectors(position, morphTargets);
  63. return XNAV3.Transform(position, _WorldMatrix);
  64. }
  65. public XNAV3 TransformNormal(XNAV3 normal, IReadOnlyList<XNAV3> morphTargets, in VERTEXINFLUENCES skinWeights)
  66. {
  67. normal = MorphVectors(normal, morphTargets);
  68. return XNAV3.Normalize(XNAV3.TransformNormal(normal, _WorldMatrix));
  69. }
  70. public XNAV4 TransformTangent(XNAV4 tangent, IReadOnlyList<XNAV3> morphTargets, in VERTEXINFLUENCES skinWeights)
  71. {
  72. var t = MorphVectors(new XNAV3(tangent.X, tangent.Y, tangent.Z), morphTargets);
  73. t = XNAV3.Normalize(XNAV3.TransformNormal(t, _WorldMatrix));
  74. return new XNAV4(t, tangent.W);
  75. }
  76. #endregion
  77. }
  78. }