Skip to content

Commit 1f2abaa

Browse files
committed
code review fixes - add ToFbxDouble3()
also assert instead of checking if constraints are null
1 parent 5157f4e commit 1f2abaa

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,28 @@ public static FbxVector4 ConvertQuaternionToXYZEuler (FbxQuaternion quat)
11751175
return new FbxVector4 (vector4.X, -vector4.Y, -vector4.Z, vector4.W);
11761176
}
11771177

1178+
public static FbxDouble3 ToFbxDouble3(Vector3 v)
1179+
{
1180+
return new FbxDouble3(v.x, v.y, v.z);
1181+
}
1182+
1183+
public static FbxDouble3 ToFbxDouble3(FbxVector4 v)
1184+
{
1185+
return new FbxDouble3(v.X, v.Y, v.Z);
1186+
}
1187+
1188+
public static FbxVector4 ToFbxVector4(FbxDouble3 v)
1189+
{
1190+
return new FbxVector4(v.X, v.Y, v.Z);
1191+
}
1192+
1193+
public static FbxDouble3 ConvertToRightHandedEuler(Vector3 rot)
1194+
{
1195+
rot.y *= -1;
1196+
rot.z *= -1;
1197+
return ToFbxDouble3(rot);
1198+
}
1199+
11781200
/// <summary>
11791201
/// Euler to quaternion without axis conversion.
11801202
/// </summary>
@@ -1471,10 +1493,7 @@ protected void AddFbxNodeToConstraintsMapping<T>(FbxNode fbxNode, T fbxConstrain
14711493
protected bool ExportPositionConstraint(IConstraint uniConstraint, FbxScene fbxScene, FbxNode fbxNode)
14721494
{
14731495
var uniPosConstraint = uniConstraint as PositionConstraint;
1474-
if(uniPosConstraint == null)
1475-
{
1476-
return false;
1477-
}
1496+
Debug.Assert (uniPosConstraint == null);
14781497

14791498
FbxConstraintPosition fbxPosConstraint = FbxConstraintPosition.Create(fbxScene, fbxNode.GetName() + "_positionConstraint");
14801499
fbxPosConstraint.SetConstrainedObject(fbxNode);
@@ -1488,22 +1507,19 @@ protected bool ExportPositionConstraint(IConstraint uniConstraint, FbxScene fbxS
14881507
fbxPosConstraint.AffectZ.Set((uniAffectedAxes & Axis.Z) == Axis.Z);
14891508

14901509
var fbxTranslationOffset = ConvertToRightHanded(uniPosConstraint.translationOffset, UnitScaleFactor);
1491-
fbxPosConstraint.Translation.Set(new FbxDouble3(fbxTranslationOffset.X, fbxTranslationOffset.Y, fbxTranslationOffset.Z));
1510+
fbxPosConstraint.Translation.Set(ToFbxDouble3(fbxTranslationOffset));
14921511

14931512
// rest position is the position of the fbx node
14941513
var fbxRestTranslation = ConvertToRightHanded(uniPosConstraint.translationAtRest, UnitScaleFactor);
14951514
// set the local position of fbxNode
1496-
fbxNode.LclTranslation.Set(new FbxDouble3(fbxRestTranslation.X, fbxRestTranslation.Y, fbxRestTranslation.Z));
1515+
fbxNode.LclTranslation.Set(ToFbxDouble3(fbxRestTranslation));
14971516
return true;
14981517
}
14991518

15001519
protected bool ExportRotationConstraint(IConstraint uniConstraint, FbxScene fbxScene, FbxNode fbxNode)
15011520
{
15021521
var uniRotConstraint = uniConstraint as RotationConstraint;
1503-
if(uniRotConstraint == null)
1504-
{
1505-
return false;
1506-
}
1522+
Debug.Assert(uniRotConstraint == null);
15071523

15081524
FbxConstraintRotation fbxRotConstraint = FbxConstraintRotation.Create(fbxScene, fbxNode.GetName() + "_rotationConstraint");
15091525
fbxRotConstraint.SetConstrainedObject(fbxNode);
@@ -1518,7 +1534,7 @@ protected bool ExportRotationConstraint(IConstraint uniConstraint, FbxScene fbxS
15181534

15191535
// Not converting rotation offset to XYZ euler as it gives the incorrect result in both Maya and Unity.
15201536
var uniRotationOffset = uniRotConstraint.rotationOffset;
1521-
var fbxRotationOffset = new FbxDouble3(uniRotationOffset.x, -uniRotationOffset.y, -uniRotationOffset.z);
1537+
var fbxRotationOffset = ConvertToRightHandedEuler(uniRotationOffset);
15221538

15231539
fbxRotConstraint.Rotation.Set(fbxRotationOffset);
15241540

@@ -1533,10 +1549,7 @@ protected bool ExportRotationConstraint(IConstraint uniConstraint, FbxScene fbxS
15331549
protected bool ExportScaleConstraint(IConstraint uniConstraint, FbxScene fbxScene, FbxNode fbxNode)
15341550
{
15351551
var uniScaleConstraint = uniConstraint as ScaleConstraint;
1536-
if (uniScaleConstraint == null)
1537-
{
1538-
return false;
1539-
}
1552+
Debug.Assert(uniScaleConstraint == null);
15401553

15411554
FbxConstraintScale fbxScaleConstraint = FbxConstraintScale.Create(fbxScene, fbxNode.GetName() + "_scaleConstraint");
15421555
fbxScaleConstraint.SetConstrainedObject(fbxNode);
@@ -1550,12 +1563,12 @@ protected bool ExportScaleConstraint(IConstraint uniConstraint, FbxScene fbxScen
15501563
fbxScaleConstraint.AffectZ.Set((uniAffectedAxes & Axis.Z) == Axis.Z);
15511564

15521565
var uniScaleOffset = uniScaleConstraint.scaleOffset;
1553-
var fbxScalingOffset = new FbxDouble3(uniScaleOffset.x, uniScaleOffset.y, uniScaleOffset.z);
1566+
var fbxScalingOffset = ToFbxDouble3(uniScaleOffset);
15541567
fbxScaleConstraint.Scaling.Set(fbxScalingOffset);
15551568

15561569
// rest rotation is the rotation of the fbx node
15571570
var uniRestScale = uniScaleConstraint.scaleAtRest;
1558-
var fbxRestScale = new FbxDouble3(uniRestScale.x, uniRestScale.y, uniRestScale.z);
1571+
var fbxRestScale = ToFbxDouble3(uniRestScale);
15591572
// set the local rotation of fbxNode
15601573
fbxNode.LclScaling.Set(fbxRestScale);
15611574
return true;
@@ -1564,10 +1577,7 @@ protected bool ExportScaleConstraint(IConstraint uniConstraint, FbxScene fbxScen
15641577
protected bool ExportAimConstraint(IConstraint uniConstraint, FbxScene fbxScene, FbxNode fbxNode)
15651578
{
15661579
var uniAimConstraint = uniConstraint as AimConstraint;
1567-
if (uniAimConstraint == null)
1568-
{
1569-
return false;
1570-
}
1580+
Debug.Assert(uniAimConstraint == null);
15711581

15721582
FbxConstraintAim fbxAimConstraint = FbxConstraintAim.Create(fbxScene, fbxNode.GetName() + "_aimConstraint");
15731583
fbxAimConstraint.SetConstrainedObject(fbxNode);
@@ -1581,7 +1591,7 @@ protected bool ExportAimConstraint(IConstraint uniConstraint, FbxScene fbxScene,
15811591
fbxAimConstraint.AffectZ.Set((uniAffectedAxes & Axis.Z) == Axis.Z);
15821592

15831593
var uniRotationOffset = uniAimConstraint.rotationOffset;
1584-
var fbxRotationOffset = new FbxDouble3(uniRotationOffset.x, -uniRotationOffset.y, -uniRotationOffset.z);
1594+
var fbxRotationOffset = ConvertToRightHandedEuler(uniRotationOffset);
15851595
fbxAimConstraint.RotationOffset.Set(fbxRotationOffset);
15861596

15871597
// rest rotation is the rotation of the fbx node
@@ -1614,11 +1624,9 @@ protected bool ExportAimConstraint(IConstraint uniConstraint, FbxScene fbxScene,
16141624
fbxAimConstraint.WorldUpType.Set((int)fbxWorldUpType);
16151625

16161626
var uniAimVector = ConvertToRightHanded(uniAimConstraint.aimVector);
1617-
fbxAimConstraint.AimVector.Set(new FbxDouble3(uniAimVector.X, uniAimVector.Y, uniAimVector.Z));
1618-
var uniUpVector = uniAimConstraint.upVector;
1619-
fbxAimConstraint.UpVector.Set(new FbxDouble3(uniUpVector.x, uniUpVector.y, uniUpVector.z));
1620-
var uniWorldUpVector = uniAimConstraint.worldUpVector;
1621-
fbxAimConstraint.WorldUpVector.Set(new FbxDouble3(uniWorldUpVector.x, uniWorldUpVector.y, uniWorldUpVector.z));
1627+
fbxAimConstraint.AimVector.Set(ToFbxDouble3(uniAimVector));
1628+
fbxAimConstraint.UpVector.Set(ToFbxDouble3(uniAimConstraint.upVector));
1629+
fbxAimConstraint.WorldUpVector.Set(ToFbxDouble3(uniAimConstraint.worldUpVector));
16221630

16231631
if (uniAimConstraint.worldUpObject && MapUnityObjectToFbxNode.ContainsKey(uniAimConstraint.worldUpObject.gameObject))
16241632
{
@@ -1630,10 +1638,7 @@ protected bool ExportAimConstraint(IConstraint uniConstraint, FbxScene fbxScene,
16301638
protected bool ExportParentConstraint(IConstraint uniConstraint, FbxScene fbxScene, FbxNode fbxNode)
16311639
{
16321640
var uniParentConstraint = uniConstraint as ParentConstraint;
1633-
if (uniParentConstraint == null)
1634-
{
1635-
return false;
1636-
}
1641+
Debug.Assert(uniParentConstraint == null);
16371642

16381643
FbxConstraintParent fbxParentConstraint = FbxConstraintParent.Create(fbxScene, fbxNode.GetName() + "_parentConstraint");
16391644
fbxParentConstraint.SetConstrainedObject(fbxNode);
@@ -1651,7 +1656,7 @@ protected bool ExportParentConstraint(IConstraint uniConstraint, FbxScene fbxSce
16511656
var fbxTranslationOffset = ConvertToRightHanded(uniTranslationOffset, UnitScaleFactor);
16521657
fbxParentConstraint.SetTranslationOffset(uniSource.node, fbxTranslationOffset);
16531658

1654-
var fbxRotationOffset = new FbxVector4(uniRotationOffset.x, -uniRotationOffset.y, -uniRotationOffset.z);
1659+
var fbxRotationOffset = ToFbxVector4(ConvertToRightHandedEuler(uniRotationOffset));
16551660
fbxParentConstraint.SetRotationOffset(uniSource.node, fbxRotationOffset);
16561661
}
16571662
ExportCommonConstraintProperties(uniParentConstraint, fbxParentConstraint, fbxNode);
@@ -1669,7 +1674,7 @@ protected bool ExportParentConstraint(IConstraint uniConstraint, FbxScene fbxSce
16691674
// rest position is the position of the fbx node
16701675
var fbxRestTranslation = ConvertToRightHanded(uniParentConstraint.translationAtRest, UnitScaleFactor);
16711676
// set the local position of fbxNode
1672-
fbxNode.LclTranslation.Set(new FbxDouble3(fbxRestTranslation.X, fbxRestTranslation.Y, fbxRestTranslation.Z));
1677+
fbxNode.LclTranslation.Set(ToFbxDouble3(fbxRestTranslation));
16731678

16741679
// rest rotation is the rotation of the fbx node
16751680
var uniRestRotationQuat = Quaternion.Euler(uniParentConstraint.rotationAtRest);

0 commit comments

Comments
 (0)