Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fbx binary writer #33

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ the different 3D formats.

### MeshIO.FBX

- Read FBX binary files
- Read/Write FBX binary files
- Read/Write FBX ASCII files
- Process and add custom `FbxProperties`

Expand Down
40 changes: 36 additions & 4 deletions src/MeshIO.FBX.Tests/FbxWriterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public void WriteEmptyAsciiStream(FbxVersion version)
using (FbxWriter writer = new FbxWriter(new MemoryStream(), new Scene(), options))
{
writer.OnNotification += this.onNotification;
writer.Write(new FbxWriterOptions() { IsBinaryFormat = false });
writer.Write(options);
}
}

[Theory(Skip = "Not implemented")]
[Theory]
[MemberData(nameof(Versions))]
public void WriteEmptyBinaryStream(FbxVersion version)
{
Expand All @@ -43,7 +43,7 @@ public void WriteEmptyBinaryStream(FbxVersion version)
using (FbxWriter writer = new FbxWriter(new MemoryStream(), new Scene()))
{
writer.OnNotification += this.onNotification;
writer.Write(new FbxWriterOptions() { IsBinaryFormat = true });
writer.Write(options);
}
}

Expand All @@ -70,7 +70,39 @@ public void WriteAsciiFbxWithMesh(FbxVersion version)
using (FbxWriter writer = new FbxWriter(path, scene, options))
{
writer.OnNotification += this.onNotification;
writer.Write(new FbxWriterOptions() { IsBinaryFormat = false });
writer.Write(options);
}
}

[Theory]
[MemberData(nameof(Versions))]
public void WriteBinaryFbxWithMesh(FbxVersion version)
{
FbxWriterOptions options = new FbxWriterOptions
{
IsBinaryFormat = true,
Version = version,
};

string path = Path.Combine(FolderPath.OutFilesFbx, $"box_{version}_binary.fbx");

Scene scene = new Scene();

Node box = new Node("my_node");
Mesh mesh = new Box("my_box").CreateMesh();
box.Entities.Add(mesh);

scene.RootNode.Nodes.Add(box);

using (FbxWriter writer = new FbxWriter(path, scene, options))
{
writer.OnNotification += this.onNotification;
writer.Write(options);
}

using (FbxReader reader = new FbxReader(path))
{
var s = reader.Read();
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/MeshIO.FBX.Tests/MeshIO.FBX.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

<!-- Enable the MSTest runner, this is an opt-in feature -->
<EnableMSTestRunner>true</EnableMSTestRunner>
<!-- We need to produce an executable and not a DLL -->
<OutputType>Exe</OutputType>

<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
36 changes: 18 additions & 18 deletions src/MeshIO.FBX/FbxPropertyTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ private static Dictionary<string, FbxProperty> getFbxNodeTemplate()
new FbxProperty("TranslationMaxZ", "bool", "", PropertyFlags.None,0),
new FbxProperty("RotationOrder", "enum", "", PropertyFlags.None,0),
new FbxProperty("RotationSpaceForLimitOnly", "bool", "", PropertyFlags.None,0),
new FbxProperty("RotationStiffnessX", "double", "Number", PropertyFlags.None,0),
new FbxProperty("RotationStiffnessY", "double", "Number", PropertyFlags.None,0),
new FbxProperty("RotationStiffnessZ", "double", "Number", PropertyFlags.None,0),
new FbxProperty("RotationStiffnessX", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("RotationStiffnessY", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("RotationStiffnessZ", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("AxisLen", "double", "Number", PropertyFlags.None,10),
new FbxProperty("PreRotation", "Vector3D", "Vector", PropertyFlags.None,XYZ.Zero),
new FbxProperty("PostRotation", "Vector3D", "Vector", PropertyFlags.None,XYZ.Zero),
Expand All @@ -125,21 +125,21 @@ private static Dictionary<string, FbxProperty> getFbxNodeTemplate()
new FbxProperty("GeometricTranslation", "Vector3D", "Vector", PropertyFlags.None,XYZ.Zero),
new FbxProperty("GeometricRotation", "Vector3D", "Vector", PropertyFlags.None,XYZ.Zero),
new FbxProperty("GeometricScaling", "Vector3D", "Vector", PropertyFlags.None,new XYZ(1,1,1)),
new FbxProperty("MinDampRangeX", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MinDampRangeY", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MinDampRangeZ", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MaxDampRangeX", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MaxDampRangeY", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MaxDampRangeZ", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MinDampStrengthX", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MinDampStrengthY", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MinDampStrengthZ", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MaxDampStrengthX", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MaxDampStrengthY", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MaxDampStrengthZ", "double", "Number", PropertyFlags.None,0),
new FbxProperty("PreferedAngleX", "double", "Number", PropertyFlags.None,0),
new FbxProperty("PreferedAngleY", "double", "Number", PropertyFlags.None,0),
new FbxProperty("PreferedAngleZ", "double", "Number", PropertyFlags.None,0),
new FbxProperty("MinDampRangeX", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MinDampRangeY", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MinDampRangeZ", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MaxDampRangeX", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MaxDampRangeY", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MaxDampRangeZ", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MinDampStrengthX", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MinDampStrengthY", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MinDampStrengthZ", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MaxDampStrengthX", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MaxDampStrengthY", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("MaxDampStrengthZ", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("PreferedAngleX", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("PreferedAngleY", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("PreferedAngleZ", "double", "Number", PropertyFlags.None,0.0d),
new FbxProperty("LookAtProperty", "object", "", PropertyFlags.None,null),
new FbxProperty("UpVectorProperty", "object", "", PropertyFlags.None ,null),
new FbxProperty("Show", "bool", "", PropertyFlags.None,1),
Expand Down
6 changes: 4 additions & 2 deletions src/MeshIO.FBX/Writers/FbxBinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ public void Write()
this.stream.Write((int)this.Root.Version);
// TODO: Do we write a top level node or not? Maybe check the version?
this.nodePath.Clear();

foreach (var node in this.Root.Nodes)
{
this.WriteNode(this.Root, node);
}

this.WriteNode(this.Root, null);
this.stream.Write(GenerateFooterCode(this.Root));
this.WriteFooter(this.stream, (int)this.Root.Version);
Expand Down Expand Up @@ -285,7 +289,5 @@ void WriteNode(FbxRootNode document, FbxNode node)
this.nodePath.Pop();
}
}


}
}
14 changes: 12 additions & 2 deletions src/MeshIO.FBX/Writers/FbxFileWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public void Write()
if (this.Options.IsBinaryFormat)
{
//Seems to need some extra fields
Random random = new Random();
var fileId = new byte[16];
random.NextBytes(fileId);

_writer.WritePairNodeValue("FileId", fileId);
_writer.WritePairNodeValue("CreationTime", "1970-01-01 10:00:00:000");
_writer.WritePairNodeValue("Creator", "MeshIO.FBX");
}

this.writeGlobalSettings();
Expand Down Expand Up @@ -266,7 +273,9 @@ private void writeDocuments()
this.WriteProperties(Scene.Properties);

_writer.WritePairNodeValue(FbxFileToken.RootNode, 0L);

_writer.WriteCloseBracket();
_writer.WriteEmptyLine();

foreach (Scene s in Scene.SubScenes)
{
Expand All @@ -279,11 +288,10 @@ private void writeDocuments()
this.WriteProperties(s.Properties);

_writer.WriteCloseBracket();

}

_writer.WriteCloseBracket();

_writer.WriteEmptyLine();
}

private void writeReferences()
Expand Down Expand Up @@ -330,6 +338,8 @@ private void writeDefinitions()
this.WriteProperties(template.Properties.Values);

_writer.WriteCloseBracket();
_writer.WriteEmptyLine();

_writer.WriteCloseBracket();
_writer.WriteEmptyLine();
}
Expand Down
4 changes: 2 additions & 2 deletions src/MeshIO.FBX/Writers/StreamWriter/FbxAsciiStreamWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ internal sealed class FbxAsciiStreamWriter : FbxStreamWriterBase

private System.IO.StreamWriter _streamWriter;

public FbxAsciiStreamWriter(Stream stream, Encoding encoding)
public FbxAsciiStreamWriter(Stream stream, FbxWriterOptions options) : base(options)
{
_streamWriter = new System.IO.StreamWriter(stream, encoding);
_streamWriter = new System.IO.StreamWriter(stream, this._encoding);
}

public override void Dispose()
Expand Down
85 changes: 85 additions & 0 deletions src/MeshIO.FBX/Writers/StreamWriter/FbxBinaryStreamWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace MeshIO.FBX.Writers.StreamWriter
{
internal sealed class FbxBinaryStreamWriter : FbxStreamWriterBase
{
public FbxBinaryStreamWriter(Stream stream, FbxWriterOptions options) : base(options)
{
}

public override void Dispose()
{
throw new NotImplementedException();
}

public override void WriteArray<T>(IEnumerable<T> arr)
{
throw new NotImplementedException();
}

public override void WriteByteArray(IEnumerable<byte> arr)
{
throw new NotImplementedException();
}

public override void WriteCloseBracket()
{
throw new NotImplementedException();
}

public override void WriteEmptyLine()
{
throw new NotImplementedException();
}

public override void WriteName(string value)
{
throw new NotImplementedException();
}

public override void WriteOpenBracket()
{
throw new NotImplementedException();
}

public override void WriteValue(string value)
{
throw new NotImplementedException();
}

public override void WriteValue(int value)
{
throw new NotImplementedException();
}

public override void WriteValue(long value)
{
throw new NotImplementedException();
}

public override void WriteValue(ulong value)
{
throw new NotImplementedException();
}

public override void WriteValue(char value)
{
throw new NotImplementedException();
}

public override void WriteValue(double value)
{
throw new NotImplementedException();
}

public override void WriteValue(float value)
{
throw new NotImplementedException();
}
}
}
14 changes: 11 additions & 3 deletions src/MeshIO.FBX/Writers/StreamWriter/FbxStreamWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ public static IFbxStreamWriter Create(FbxWriterOptions options, Stream stream)
{
if (options.IsBinaryFormat)
{
throw new NotImplementedException();
return new FbxBinaryStreamWriter(stream, options);
}
else
{
Encoding encoding = new UTF8Encoding(false);
return new FbxAsciiStreamWriter(stream, encoding);
return new FbxAsciiStreamWriter(stream, options);
}
}

protected FbxWriterOptions _options;
protected Encoding _encoding;

protected FbxStreamWriterBase(FbxWriterOptions options)
{
this._options = options;
this._encoding = new UTF8Encoding(false);
}

public virtual void WriteComment(string comment)
{
}
Expand Down
Loading