Hi
I’ve noticed scene.AssetInfo.UnitScaleFactor no longer works in 18.9.0. When saving to FBX Ascii the FBX Property value is always 1. For an fbx file this results in 1 unit being 1 centimeter.
In 18.8.0 the behaviour was working as expected and by default the fbx property had a value of 100 saved in the file (100 = 100cm = 1 meter). This value was correctly changing when you adjusted scene.AssetInfo.UnitScaleFactor.
For example, if I want to work in millimeters within the Aspose.3D API, I would do this:
scene.AssetInfo.UnitScaleFactor = 0.001;
In 18.8.0 the FBX "UnitScaleFactor " property results in 0.1, which is correct.
In 18.9.0 the property is always 1.
Documentation:
UnitName Gets or sets the unit of length used in this asset. e.g. cm/m/km/inch/feet UnitScaleFactor Gets or sets the scale factor to real-world meter.
Unit Tests:
[ TestFixture ]
public class SceneFileScaleTests
{
[ Test ]
public void Aspose_Scene_MetersFileScale()
{
Scene scene = CreateSampleScene();
scene.AssetInfo.UnitName = "m";
scene.AssetInfo.UnitScaleFactor = 1;
double fileScale = GetSavedFileScale( scene );
// 1 meter should return 100 file scale
Assert.AreEqual(100, fileScale);
}
[ Test ]
public void Aspose_Scene_MillimetersFileScale()
{
Scene scene = CreateSampleScene();
scene.AssetInfo.UnitName = "mm";
scene.AssetInfo.UnitScaleFactor = 0.001;
double fileScale = GetSavedFileScale( scene );
Assert.AreEqual(0.1, fileScale);
}
[Test]
public void Aspose_Scene_InchesFileScale()
{
Scene scene = CreateSampleScene();
scene.AssetInfo.UnitName = "inch";
scene.AssetInfo.UnitScaleFactor = 0.0254;
double fileScale = GetSavedFileScale(scene);
Assert.AreEqual(2.54, fileScale);
}
/// <summary>
/// Creates a sample scene with a 1x1x1 cube.
/// </summary>
private static Scene CreateSampleScene()
{
Scene scene = new Scene();
var cubeNode = scene.RootNode.CreateChildNode( "Cube" );
cubeNode.AddEntity( new Box( 1, 1, 1 ).ToMesh() );
return scene;
}
/// <summary>
/// Saves the and return the line that contains the "UnitScaleFactor" property when saved
/// as an ASCII FBX.
/// </summary>
/// <param name="scene">The scene.</param>
/// <exception cref="Exception">Failed to locate UnitScaleFactor</exception>
private static string SaveAndReturnFileScaleLine( Scene scene )
{
byte[] bytes;
using ( var saveStream = new MemoryStream() )
{
scene.Save( saveStream, FileFormat.FBX7500ASCII );
bytes = saveStream.GetBuffer();
}
string unitScaleFactorLine = string.Empty;
// Read saved bytes as text
using ( var ms = new MemoryStream( bytes ) )
using ( var sr = new StreamReader( ms ) )
{
while ( !sr.EndOfStream )
{
string line = sr.ReadLine();
if ( line == null )
break;
if ( line.Contains( "\"UnitScaleFactor\"" ) )
{
unitScaleFactorLine = line;
break;
}
}
}
if ( string.IsNullOrEmpty( unitScaleFactorLine ) )
throw new Exception( "Failed to locate UnitScaleFactor" );
return unitScaleFactorLine;
}
private static double GetSavedFileScale( Scene scene )
{
var line = SaveAndReturnFileScaleLine( scene );
// Example line:
// P: "UnitScaleFactor", "double", "Number", "",100
// Index of 4
string fileScaleValue = line.Split( ',' )[4];
Debug.WriteLine($"Scene UnitScaleFactor: {scene.AssetInfo.UnitScaleFactor}");
Debug.WriteLine("FBX Property Line: " + SaveAndReturnFileScaleLine(scene) );
return double.Parse( fileScaleValue );
}
}
The above code passes in 18.8.0 but fails in 18.9.0.
From my understanding UnitName doesn’t matter which is fine, however UnitScaleFactor in 18.9.0 now makes it impossible to correct the units, and we are stuck in centimeters.
18.9.0 Output
Scene UnitScaleFactor: 0.0254
FBX Property Line: P: "UnitScaleFactor", "double", "Number", "",1
Exception thrown: 'NUnit.Framework.AssertionException' in nunit.framework.dll
An exception of type 'NUnit.Framework.AssertionException' occurred in nunit.framework.dll but was not handled in user code
Expected: 2.54d
But was: 1.0d
'JetBrains.ReSharper.TaskRunner.CLR45.x64.exe' (CLR v4.0.30319: domain-): Loaded 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\Remote Debugger\x64\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Scene UnitScaleFactor: 1
FBX Property Line: P: "UnitScaleFactor", "double", "Number", "",1
Exception thrown: 'NUnit.Framework.AssertionException' in nunit.framework.dll
An exception of type 'NUnit.Framework.AssertionException' occurred in nunit.framework.dll but was not handled in user code
Expected: 100
But was: 1.0d
Scene UnitScaleFactor: 0.001
FBX Property Line: P: "UnitScaleFactor", "double", "Number", "",1
Exception thrown: 'NUnit.Framework.AssertionException' in nunit.framework.dll
An exception of type 'NUnit.Framework.AssertionException' occurred in nunit.framework.dll but was not handled in user code
Expected: 0.10000000000000001d
But was: 1.0d
18.8.0 Output
Scene UnitScaleFactor: 0.0254
FBX Property Line: P: "UnitScaleFactor", "double", "Number", "",2.54
Scene UnitScaleFactor: 1
FBX Property Line: P: "UnitScaleFactor", "double", "Number", "",100
Scene UnitScaleFactor: 0.001
FBX Property Line: P: "UnitScaleFactor", "double", "Number", "",0.1