// FbxAMatrix /// Does not do any coordinate-convention switching. public static void ToTRS( this FbxAMatrix input, out Vector3 translation, out Quaternion rotation, out Vector3 scale) { translation = input.GetT().ToUVector3(); rotation = input.GetQ().ToUQuaternion(); scale = input.GetS().ToUVector3(); }
public void TestBasics() { Assert.That(!string.IsNullOrEmpty(ModelExporter.GetVersionFromReadme())); // Test GetOrCreateLayer using (var fbxManager = FbxManager.Create()) { var fbxMesh = FbxMesh.Create(fbxManager, "name"); var layer0 = ModelExporter.GetOrCreateLayer(fbxMesh); Assert.That(layer0, Is.Not.Null); Assert.That(ModelExporter.GetOrCreateLayer(fbxMesh), Is.EqualTo(layer0)); var layer5 = ModelExporter.GetOrCreateLayer(fbxMesh, layer: 5); Assert.That(layer5, Is.Not.Null); Assert.That(layer5, Is.Not.EqualTo(layer0)); } // Test axis conversion: a x b in left-handed is the same as b x a // in right-handed (that's why we need to flip the winding order). var a = new Vector3(1, 0, 0); var b = new Vector3(0, 0, 1); var crossLeft = Vector3.Cross(a, b); Assert.That(ModelExporter.DefaultMaterial); // Test non-static functions. using (var fbxManager = FbxManager.Create()) { var fbxScene = FbxScene.Create(fbxManager, "scene"); var fbxNode = FbxNode.Create(fbxScene, "node"); var exporter = new ModelExporter(); // Test ExportMaterial: it exports and it re-exports bool result = exporter.ExportMaterial(ModelExporter.DefaultMaterial, fbxScene, fbxNode); Assert.IsTrue(result); var fbxMaterial = fbxNode.GetMaterial(0); Assert.That(fbxMaterial, Is.Not.Null); result = exporter.ExportMaterial(ModelExporter.DefaultMaterial, fbxScene, fbxNode); var fbxMaterial2 = fbxNode.GetMaterial(1); Assert.AreEqual(fbxMaterial, fbxMaterial2); // Test ExportTexture: it finds the same texture for the default-material (it doesn't create a new one) var fbxMaterialNew = FbxSurfaceLambert.Create(fbxScene, "lambert"); exporter.ExportTexture(ModelExporter.DefaultMaterial, "_MainTex", fbxMaterialNew, FbxSurfaceLambert.sBump); Assert.AreEqual( fbxMaterial.FindProperty(FbxSurfaceLambert.sDiffuse).GetSrcObject(), fbxMaterialNew.FindProperty(FbxSurfaceLambert.sBump).GetSrcObject() ); // Test ExportMesh: make sure we exported a mesh with welded vertices. var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); var cubeNode = FbxNode.Create(fbxScene, "cube"); exporter.ExportMesh(cube.GetComponent <MeshFilter>().sharedMesh, cubeNode); Assert.That(cubeNode.GetMesh(), Is.Not.Null); Assert.That(cubeNode.GetMesh().GetControlPointsCount(), Is.EqualTo(8)); } // Test exporting a skinned-mesh. Make sure it doesn't leak (it did at one point) { var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); var character = new GameObject(); var smr = character.AddComponent <SkinnedMeshRenderer>(); smr.sharedMesh = cube.GetComponent <MeshFilter>().sharedMesh; var meshCount = Object.FindObjectsOfType <Mesh>().Length; ModelExporter.ExportObject(GetRandomFbxFilePath(), character); Assert.AreEqual(meshCount, Object.FindObjectsOfType <Mesh>().Length); } // Test euler to quaternion conversion { // EulerToQuaternionZXY var v = new Vector3(50, 45, 190); var quat = ModelExporter.EulerToQuaternionZXY(v); var unityQuat = Quaternion.Euler(v); Assert.That((float)quat.X, Is.EqualTo(unityQuat.x)); Assert.That((float)quat.Y, Is.EqualTo(unityQuat.y)); Assert.That((float)quat.Z, Is.EqualTo(unityQuat.z)); Assert.That((float)quat.W, Is.EqualTo(unityQuat.w)); // EulerToQuaternionXYZ var fbxV = new FbxVector4(v.x, v.y, v.z); var xyzQuat = ModelExporter.EulerToQuaternionXYZ(fbxV); // get the vector from the quaternion FbxAMatrix m = new FbxAMatrix(); m.SetR(fbxV); var actualQuat = m.GetQ(); // since this quaternion is XYZ instead of ZXY, it should not match the quaternion // created with EulerToQuaternionZXY Assert.That(xyzQuat, Is.Not.EqualTo(quat)); Assert.That(xyzQuat, Is.EqualTo(actualQuat)); } }
/// <summary> /// Process transformation data and setup Transform component /// </summary> private void ProcessTransform(FbxNode fbxNode, GameObject unityGo) { FbxVector4 lclTrs = new FbxVector4(); FbxQuaternion lclRot = new FbxQuaternion(); FbxVector4 lclScl = new FbxVector4(1.0f, 1.0f, 1.0f); #if UNI_18844 // Construct rotation matrices FbxVector4 fbxRotation = new FbxVector4(fbxNode.LclRotation.Get()); FbxAMatrix fbxRotationM = new FbxAMatrix(); fbxRotationM.SetR(fbxRotation); FbxVector4 fbxPreRotation = new FbxVector4(fbxNode.PreRotation.Get()); FbxAMatrix fbxPreRotationM = new FbxAMatrix(); fbxPreRotationM.SetR(fbxPreRotation); FbxVector4 fbxPostRotation = new FbxVector4(fbxNode.PostRotation.Get()); FbxAMatrix fbxPostRotationM = new FbxAMatrix(); fbxPostRotationM.SetR(fbxPostRotation); // Construct translation matrix FbxAMatrix fbxTranslationM = new FbxAMatrix(); FbxVector4 fbxTranslation = new FbxVector4(fbxNode.LclTranslation.Get()); fbxTranslationM.SetT(fbxTranslation); // Construct scaling matrix FbxAMatrix fbxScalingM = new FbxAMatrix(); FbxVector4 fbxScaling = new FbxVector4(fbxNode.LclScaling.Get()); fbxScalingM.SetS(fbxScaling); // Construct offset and pivot matrices FbxAMatrix fbxRotationOffsetM = new FbxAMatrix(); FbxVector4 fbxRotationOffset = fbxNode.RotationOffset.Get(); fbxRotationOffsetM.SetT(fbxRotationOffset); FbxAMatrix fbxRotationPivotM = new FbxAMatrix(); FbxVector4 fbxRotationPivot = fbxNode.RotationPivot.Get(); fbxRotationPivotM.SetT(fbxRotationPivot); FbxAMatrix fbxScalingOffsetM = new FbxAMatrix(); FbxVector4 fbxScalingOffset = fbxNode.ScalingOffset.Get(); fbxScalingOffsetM.SetT(fbxScalingOffset); FbxAMatrix fbxScalingPivotM = new FbxAMatrix(); FbxVector4 fbxScalingPivot = fbxNode.ScalingPivot.Get(); fbxScalingPivotM.SetT(fbxScalingPivot); FbxAMatrix fbxTransform = fbxTranslationM * fbxRotationOffsetM * fbxRotationPivotM * fbxPreRotationM * fbxRotationM * fbxPostRotationM * fbxRotationPivotM.Inverse() * fbxScalingOffsetM * fbxScalingPivotM * fbxScalingM * fbxScalingPivotM.Inverse(); FbxVector4 lclTrs = fbxTransform.GetT(); FbxQuaternion lclRot = fbxTransform.GetQ(); FbxVector4 lclScl = fbxTransform.GetS(); #endif Debug.Log(string.Format("processing {3} Lcl : T({0}) R({1}) S({2})", lclTrs.ToString(), lclRot.ToString(), lclScl.ToString(), fbxNode.GetName())); unityGo.transform.localPosition = new Vector3((float)lclTrs[0], (float)lclTrs[1], (float)lclTrs[2]); unityGo.transform.localRotation = new Quaternion((float)lclRot[0], (float)lclRot[1], (float)lclRot[2], (float)lclRot[3]); unityGo.transform.localScale = new Vector3((float)lclScl[0], (float)lclScl[1], (float)lclScl[2]); }
/// <summary> /// Process fbx scene by doing nothing /// </summary> public void ProcessNode(FbxNode fbxNode, GameObject unityParentObj = null, bool constructTransform = false) { string name = fbxNode.GetName(); GameObject unityGo = new GameObject(name); NumNodes++; if (unityParentObj != null) { unityGo.transform.parent = unityParentObj.transform; } FbxAMatrix fbxTransform = null; if (constructTransform) { #if UNI_18844 // Construct rotation matrices FbxVector4 fbxRotation = new FbxVector4(fbxNode.LclRotation.Get()); FbxAMatrix fbxRotationM = new FbxAMatrix(); fbxRotationM.SetR(fbxRotation); FbxVector4 fbxPreRotation = new FbxVector4(fbxNode.PreRotation.Get()); FbxAMatrix fbxPreRotationM = new FbxAMatrix(); fbxPreRotationM.SetR(fbxPreRotation); FbxVector4 fbxPostRotation = new FbxVector4(fbxNode.PostRotation.Get()); FbxAMatrix fbxPostRotationM = new FbxAMatrix(); fbxPostRotationM.SetR(fbxPostRotation); // Construct translation matrix FbxAMatrix fbxTranslationM = new FbxAMatrix(); FbxVector4 fbxTranslation = new FbxVector4(fbxNode.LclTranslation.Get()); fbxTranslationM.SetT(fbxTranslation); // Construct scaling matrix FbxAMatrix fbxScalingM = new FbxAMatrix(); FbxVector4 fbxScaling = new FbxVector4(fbxNode.LclScaling.Get()); fbxScalingM.SetS(fbxScaling); // Construct offset and pivot matrices FbxAMatrix fbxRotationOffsetM = new FbxAMatrix(); FbxVector4 fbxRotationOffset = fbxNode.RotationOffset.Get(); fbxRotationOffsetM.SetT(fbxRotationOffset); FbxAMatrix fbxRotationPivotM = new FbxAMatrix(); FbxVector4 fbxRotationPivot = fbxNode.RotationPivot.Get(); fbxRotationPivotM.SetT(fbxRotationPivot); FbxAMatrix fbxScalingOffsetM = new FbxAMatrix(); FbxVector4 fbxScalingOffset = fbxNode.ScalingOffset.Get(); fbxScalingOffsetM.SetT(fbxScalingOffset); FbxAMatrix fbxScalingPivotM = new FbxAMatrix(); FbxVector4 fbxScalingPivot = fbxNode.ScalingPivot.Get(); fbxScalingPivotM.SetT(fbxScalingPivot); fbxTransform = fbxTranslationM * fbxRotationOffsetM * fbxRotationPivotM * fbxPreRotationM * fbxRotationM * fbxPostRotationM * fbxRotationPivotM.Inverse() * fbxScalingOffsetM * fbxScalingPivotM * fbxScalingM * fbxScalingPivotM.Inverse(); lclTrs = fbxTransform.GetT(); lclRot = fbxTransform.GetQ(); lclScl = fbxTransform.GetS(); #endif } else { fbxTransform = fbxNode.EvaluateLocalTransform(); } if (fbxTransform == null) { Debug.LogError(string.Format("failed to retrieve transform for node : {0}", fbxNode.GetName())); return; } FbxVector4 lclTrs = fbxTransform.GetT(); FbxQuaternion lclRot = fbxTransform.GetQ(); FbxVector4 lclScl = fbxTransform.GetS(); Debug.Log(string.Format("processing {3} Lcl : T({0}) R({1}) S({2})", lclTrs.ToString(), lclRot.ToString(), lclScl.ToString(), fbxNode.GetName())); // check we can handle translation value Debug.Assert(lclTrs.X <= float.MaxValue && lclTrs.X >= float.MinValue); Debug.Assert(lclTrs.Y <= float.MaxValue && lclTrs.Y >= float.MinValue); Debug.Assert(lclTrs.Z <= float.MaxValue && lclTrs.Z >= float.MinValue); unityGo.transform.localPosition = new Vector3((float)lclTrs.X, (float)lclTrs.Y, (float)lclTrs.Z); unityGo.transform.localRotation = new Quaternion((float)lclRot[0], (float)lclRot[1], (float)lclRot[2], (float)lclRot[3]); unityGo.transform.localScale = new Vector3((float)lclScl.X, (float)lclScl.Y, (float)lclScl.Z); for (int i = 0; i < fbxNode.GetChildCount(); ++i) { ProcessNode(fbxNode.GetChild(i), unityGo); } }