TransformExportTest.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // ***********************************************************************
  2. // Copyright (c) 2017 Unity Technologies. All rights reserved.
  3. //
  4. // Licensed under the ##LICENSENAME##.
  5. // See LICENSE.md file in the project root for full license information.
  6. // ***********************************************************************
  7. using NUnit.Framework;
  8. using System.Collections;
  9. using Autodesk.Fbx;
  10. namespace Autodesk.Fbx.UseCaseTests
  11. {
  12. public class TransformExportTest : RoundTripTestBase
  13. {
  14. [SetUp]
  15. public override void Init ()
  16. {
  17. fileNamePrefix = "_safe_to_delete__transform_export_test_";
  18. base.Init ();
  19. }
  20. // Export GameObject as standard marker
  21. protected FbxNull ExportNull (FbxScene fbxScene)
  22. {
  23. // create the marker structure.
  24. FbxNull fbxNull = FbxNull.Create (fbxScene, "Null");
  25. fbxNull.Look.Set (FbxNull.ELook.eCross);
  26. fbxNull.Size.Set (1.0f);
  27. return fbxNull;
  28. }
  29. protected override FbxScene CreateScene (FbxManager manager)
  30. {
  31. FbxScene scene = FbxScene.Create (manager, "myScene");
  32. // Create the following node hierarchy with transforms:
  33. // Root
  34. // (t: 0,10,4)
  35. // (r: 0,0,0)
  36. // (s: 1,1,1)
  37. // / \
  38. // child0 child1
  39. // (t: 1,1,1) (t: 0,0,0)
  40. // (r: 0,0,90) (r: 180,5,0)
  41. // (s: 2,2,2) (s: 3,2,1)
  42. // |
  43. // child2
  44. // (t: 5,6,20)
  45. // (r: 0,10,0)
  46. // (s: 1,0.5,1)
  47. FbxNode root = FbxNode.Create (scene, "Root");
  48. root.SetNodeAttribute (ExportNull (scene));
  49. root.SetShadingMode (FbxNode.EShadingMode.eWireFrame);
  50. // Set the transform values
  51. root.LclTranslation.Set(new FbxDouble3(0,10,4));
  52. root.LclRotation.Set(new FbxDouble3(0,0,0));
  53. root.LclScaling.Set(new FbxDouble3(1,1,1));
  54. // Set the pre/post rotation, pivots and offsets
  55. // NOTE: For some reason when using PreRotation.Set() instead of SetPreRotation(),
  56. // the PreRotation does not get imported properly. Same is true for the other properties.
  57. // Also only works if EPivot set is SourcePivot.
  58. // TODO: figure out why the other ways don't work.
  59. root.SetPreRotation(FbxNode.EPivotSet.eSourcePivot, new FbxVector4(30, 10, 45));
  60. root.SetPostRotation (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (9, 10, 5));
  61. root.SetRotationPivot (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (5, 6, 7));
  62. root.SetScalingPivot (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (1, 2, 1));
  63. root.SetRotationOffset (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (0.6, 8, 0.3));
  64. root.SetScalingOffset (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (10, 4, 3));
  65. FbxNode[] children = new FbxNode[3];
  66. FbxDouble3[][] transforms = {
  67. new FbxDouble3[]{new FbxDouble3(1,1,1), new FbxDouble3(0,0,90), new FbxDouble3(2,2,2)},
  68. new FbxDouble3[]{new FbxDouble3(0,0,0), new FbxDouble3(180,5,0), new FbxDouble3(3,2,1)},
  69. new FbxDouble3[]{new FbxDouble3(5,6,20), new FbxDouble3(0,10,0), new FbxDouble3(1,0.5,1)}
  70. };
  71. for (int i = 0; i < children.Length; i++) {
  72. children [i] = FbxNode.Create (scene, "Child" + i);
  73. // set the fbxNode's node attribute
  74. children[i].SetNodeAttribute (ExportNull (scene));
  75. children[i].SetShadingMode (FbxNode.EShadingMode.eWireFrame);
  76. // set the transform
  77. children [i].LclTranslation.Set (transforms [i] [0]);
  78. children [i].LclRotation.Set (transforms [i] [1]);
  79. children [i].LclScaling.Set (transforms [i] [2]);
  80. // set some values to check against later (doesn't really matter what the values are)
  81. children [i].SetPreRotation(FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (i, i*2, i%3));
  82. children [i].SetPostRotation (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (i-1, i+5, i));
  83. children [i].SetRotationPivot (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (i/2, i, i+3));
  84. children [i].SetScalingPivot (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (i*5, i-1, i/4));
  85. children [i].SetRotationOffset (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (0.6*i, 8, i/2.0f));
  86. children [i].SetScalingOffset (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (i, i, i));
  87. }
  88. // Create the hierarchy
  89. scene.GetRootNode ().AddChild (root);
  90. root.AddChild (children [0]);
  91. root.AddChild (children [1]);
  92. children [1].AddChild (children [2]);
  93. return scene;
  94. }
  95. protected override void CheckScene (FbxScene scene)
  96. {
  97. FbxScene origScene = CreateScene (FbxManager);
  98. // Compare the hierarchy and transforms of the two scenes
  99. FbxNode origRoot = origScene.GetRootNode();
  100. FbxNode importRoot = scene.GetRootNode ();
  101. CheckSceneHelper (origRoot, importRoot);
  102. }
  103. // compare the hierarchy and transform of two nodes
  104. private void CheckSceneHelper(FbxNode node1, FbxNode node2)
  105. {
  106. if (node1 == null && node2 == null) {
  107. return;
  108. }
  109. Assert.IsNotNull (node1);
  110. Assert.IsNotNull (node2);
  111. Assert.AreEqual (node1.GetChildCount (), node2.GetChildCount ());
  112. // compare the transforms
  113. Assert.AreEqual (node1.LclTranslation.Get(), node2.LclTranslation.Get());
  114. Assert.AreEqual (node1.LclRotation.Get(), node2.LclRotation.Get());
  115. Assert.AreEqual (node1.LclScaling.Get(), node2.LclScaling.Get());
  116. Assert.AreEqual (node1.GetPreRotation (FbxNode.EPivotSet.eSourcePivot),
  117. node2.GetPreRotation (FbxNode.EPivotSet.eSourcePivot));
  118. Assert.AreEqual (node1.GetPostRotation(FbxNode.EPivotSet.eSourcePivot),
  119. node2.GetPostRotation(FbxNode.EPivotSet.eSourcePivot));
  120. Assert.AreEqual (node1.GetRotationPivot(FbxNode.EPivotSet.eSourcePivot),
  121. node2.GetRotationPivot(FbxNode.EPivotSet.eSourcePivot));
  122. Assert.AreEqual (node1.GetScalingPivot(FbxNode.EPivotSet.eSourcePivot),
  123. node2.GetScalingPivot(FbxNode.EPivotSet.eSourcePivot));
  124. Assert.AreEqual (node1.GetRotationOffset(FbxNode.EPivotSet.eSourcePivot),
  125. node2.GetRotationOffset(FbxNode.EPivotSet.eSourcePivot));
  126. Assert.AreEqual (node1.GetScalingOffset(FbxNode.EPivotSet.eSourcePivot),
  127. node2.GetScalingOffset(FbxNode.EPivotSet.eSourcePivot));
  128. Assert.AreEqual (node1.GetName (), node2.GetName ());
  129. for (int i = 0; i < node1.GetChildCount (); i++) {
  130. // recurse through the hierarchy
  131. CheckSceneHelper (node1.GetChild (i), node2.GetChild (i));
  132. }
  133. }
  134. }
  135. }