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 writer fix #32

Merged
merged 26 commits into from
Jan 19, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,4 @@ MigrationBackup/
# Exceptions
!MeshIO.OBJ
/local
/src/Tests/outFiles
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ the different 3D formats.

### MeshIO.FBX

- Read/Write FBX binary files
- Read FBX binary files
- Read/Write FBX ASCII files
- Parse FBX files in a node base structure
- Process and add custom `FbxProperties`

For more [information](https://github.com/DomCR/MeshIO/tree/master/src/MeshIO.FBX).
Expand Down
21 changes: 20 additions & 1 deletion src/MeshIO.FBX.Tests/FbxReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void GetVersion(string path)
[MemberData(nameof(AsciiFiles))]
public void ReadAsciiTest(string test)
{
readFile(test);
Scene scene = readFile(test);
}

[Theory]
Expand All @@ -54,6 +54,25 @@ public void ReadBinaryTest(string test)
readFile(test);
}

[Theory]
[MemberData(nameof(AsciiFiles))]
public void ReadWriteAsciiTest(string test)
{
Scene scene = readFile(test);

if (scene == null)
return;

FbxWriterOptions options = new FbxWriterOptions
{
IsBinaryFormat = false,
};
using (FbxWriter writer = new FbxWriter(new MemoryStream(), scene, options))
{
writer.Write();
}
}

private Scene readFile(string path)
{
using (FbxReader reader = new FbxReader(path, ErrorLevel.Checked))
Expand Down
53 changes: 47 additions & 6 deletions src/MeshIO.FBX.Tests/FbxWriterTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using MeshIO.Tests.Shared;
using MeshIO.Entities.Geometries;
using MeshIO.Entities.Primitives;
using MeshIO.Tests.Shared;
using System.IO;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -15,21 +17,60 @@ public FbxWriterTest(ITestOutputHelper output) : base(output) { }
[MemberData(nameof(Versions))]
public void WriteEmptyAsciiStream(FbxVersion version)
{
using (FbxWriter writer = new FbxWriter(new MemoryStream(), new Scene(), version))
FbxWriterOptions options = new FbxWriterOptions
{
IsBinaryFormat = false,
Version = version,
};

using (FbxWriter writer = new FbxWriter(new MemoryStream(), new Scene(), options))
{
writer.OnNotification += this.onNotification;
writer.Write(FbxFileFormat.ASCII);
writer.Write(new FbxWriterOptions() { IsBinaryFormat = false });
}
}

[Theory]
[Theory(Skip = "Not implemented")]
[MemberData(nameof(Versions))]
public void WriteEmptyBinaryStream(FbxVersion version)
{
using (FbxWriter writer = new FbxWriter(new MemoryStream(), new Scene(), version))
FbxWriterOptions options = new FbxWriterOptions
{
IsBinaryFormat = true,
Version = version,
};

using (FbxWriter writer = new FbxWriter(new MemoryStream(), new Scene()))
{
writer.OnNotification += this.onNotification;
writer.Write(new FbxWriterOptions() { IsBinaryFormat = true });
}
}

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

string path = Path.Combine(FolderPath.OutFilesFbx, $"box_{version}_ascii.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(FbxFileFormat.Binary);
writer.Write(new FbxWriterOptions() { IsBinaryFormat = false });
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/MeshIO.FBX/Converters/Mappers/FbxObjectsMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class FbxObjectsMapper : FbxMapperBase

public FbxDefinitionsMapper DefinitionMap { get; set; } = new FbxDefinitionsMapper();

public Dictionary<ulong, Element3D> ObjectMap { get; } = new Dictionary<ulong, Element3D>();
public Dictionary<ulong, Element3D> ObjectMap { get; } = new();

public override void Map(FbxNode node)
{
Expand Down Expand Up @@ -107,16 +107,16 @@ private Element3D mapModel(FbxNode node)
{
switch (p.Name)
{
case FbxProperty.LclRotation:
case FbxPropertyOld.LclRotation:
model.Transform.EulerRotation = (XYZ)p.Value;
continue;
case FbxProperty.LclScaling:
case FbxPropertyOld.LclScaling:
model.Transform.Scale = (XYZ)p.Value;
continue;
case FbxProperty.LclTranslation:
case FbxPropertyOld.LclTranslation:
model.Transform.Translation = (XYZ)p.Value;
continue;
case FbxProperty.Show:
case FbxPropertyOld.Show:
model.IsVisible = (bool)p.Value;
continue;
}
Expand All @@ -143,7 +143,7 @@ private Element3D mapMaterial(FbxNode node)
switch (p.Name)
{
//TODO: finish material read
case FbxProperty.AmbientColor:
case FbxPropertyOld.AmbientColor:
material.AmbientColor = (Color)p.Value;
continue;
}
Expand Down Expand Up @@ -174,13 +174,13 @@ private Element3D mapGeometry(FbxNode node)
{
switch (p.Name)
{
case FbxProperty.PrimaryVisibility:
case FbxPropertyOld.PrimaryVisibility:
geometry.IsVisible = (bool)p.Value;
continue;
case FbxProperty.CastShadows:
case FbxPropertyOld.CastShadows:
geometry.CastShadows = (bool)p.Value;
continue;
case FbxProperty.ReceiveShadows:
case FbxPropertyOld.ReceiveShadows:
geometry.ReceiveShadows = (bool)p.Value;
continue;
}
Expand Down Expand Up @@ -350,7 +350,7 @@ private LayerElement mapLayerElementUV(FbxNode node)

if (node.TryGetNode("UVIndex", out FbxNode indices))
{
layer.Indices.AddRange(this.toArr<int>(indices.Value as IEnumerable));
layer.Indexes.AddRange(this.toArr<int>(indices.Value as IEnumerable));
}

return layer;
Expand Down Expand Up @@ -378,7 +378,7 @@ private LayerElement BuildLayerElementMaterial(FbxNode node)

if (node.TryGetNode("Materials", out FbxNode materials))
{
layer.Indices.AddRange(this.toArr<int>(materials.Value as IEnumerable));
layer.Indexes.AddRange(this.toArr<int>(materials.Value as IEnumerable));
}

return layer;
Expand Down
1 change: 0 additions & 1 deletion src/MeshIO.FBX/Converters/Mappers/FbxPropertyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class FbxPropertyMapper

public void MapNode()
{

}

public static List<Property> MapProperties(FbxNode node, NotificationEventHandler notify = null)
Expand Down
2 changes: 1 addition & 1 deletion src/MeshIO.FBX/Converters/NodeConverterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void assignSubNodes(Node node)
{
if (this.MapObjects.ObjectMap.TryGetValue(id, out Element3D sub))
{
node.Nodes.Add(sub);
node.Children.Add(sub);

if (sub is Node n)
{
Expand Down
33 changes: 21 additions & 12 deletions src/MeshIO.FBX/Converters/SceneConverterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected FbxNode buildDocumentsNode()

node.Nodes.Add(new FbxNode("Count", 1));

FbxNode doc = new FbxNode("Document", Utils.CreateId(), "", "Scene");
FbxNode doc = new FbxNode("Document", IdUtils.CreateId(), "", "Scene");

FbxNode properties = new FbxNode("Properties70");
properties.Nodes.Add(new FbxNode("P", "SourceObject", "object", "", ""));
Expand Down Expand Up @@ -161,7 +161,7 @@ protected void buildObjectsNode()
{
_objects = new FbxNode("Objects");

foreach (Element3D item in this._scene.RootNode.Nodes)
foreach (Element3D item in this._scene.RootNode.Children)
{
FbxNode c = buildElementNode(item);

Expand Down Expand Up @@ -212,15 +212,9 @@ protected FbxNode buildModel(Node n, FbxNode properties)
FbxNode node = new FbxNode("Model", n.Id, $"Model::{n.Name}", "Null");
node.Nodes.Add(new FbxNode("Version", 232));

if (n.MultiLayer.HasValue)
node.Nodes.Add(new FbxNode("MultiLayer", n.MultiLayer.Value ? 'T' : 'F'));
if (n.MultiTake.HasValue)
node.Nodes.Add(new FbxNode("MultiTake", n.MultiTake.Value ? 'T' : 'F'));

node.Nodes.Add(new FbxNode("Shading", n.Shading ? 'T' : 'F'));
node.Nodes.Add(new FbxNode("Culling", n.Culling));

foreach (Element3D item in n.Nodes)
foreach (Element3D item in n.Children)
{
buildElementNode(item);

Expand All @@ -232,6 +226,8 @@ protected FbxNode buildModel(Node n, FbxNode properties)

properties.Nodes.Add(buildProperty("Lcl Translation", n.Transform.Translation / n.Transform.Scale));
properties.Nodes.Add(buildProperty("Lcl Scaling", n.Transform.Scale));
properties.Nodes.Add(buildProperty("DefaultAttributeIndex", (int)0));
properties.Nodes.Add(buildProperty("InheritType", "enum", "1"));

return node;
}
Expand Down Expand Up @@ -401,7 +397,7 @@ public FbxNode buildLayerElementMaterial(LayerElementMaterial layer)
FbxNode node = new FbxNode("LayerElementMaterial", 0);
node.Nodes.Add(new FbxNode("Version", 101));
buildLayerElement(node, layer);
node.Nodes.Add(new FbxNode("Materials", layer.Indices.ToArray()));
node.Nodes.Add(new FbxNode("Materials", layer.Indexes.ToArray()));
return node;
}

Expand Down Expand Up @@ -429,7 +425,7 @@ public FbxNode buildLayerElementUV(LayerElementUV layer)
node.Nodes.Add(new FbxNode("Version", 101));
buildLayerElement(node, layer);
node.Nodes.Add(new FbxNode("UV", layer.UV.SelectMany(x => x.ToEnumerable()).ToArray()));
node.Nodes.Add(new FbxNode("UVIndex", layer.Indices.ToArray()));
node.Nodes.Add(new FbxNode("UVIndex", layer.Indexes.ToArray()));
return node;
}

Expand Down Expand Up @@ -470,7 +466,7 @@ private FbxNode buildProperty(Property property)
{
switch (property)
{
case FbxProperty:
case FbxPropertyOld:
case Property:
default:
break;
Expand All @@ -479,6 +475,19 @@ private FbxNode buildProperty(Property property)
return buildProperty(property.Name, property.Value);
}

private FbxNode buildProperty(string name, string typeName, string value)
{
FbxNode node = new FbxNode("P");
node.Properties.Add(name);

node.Properties.Add(typeName);
node.Properties.Add("");
node.Properties.Add("");
node.Properties.Add(value);

return node;
}

private FbxNode buildProperty(string name, object propValue)
{
FbxNode node = new FbxNode("P");
Expand Down
21 changes: 21 additions & 0 deletions src/MeshIO.FBX/Extensions/Element3DExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using MeshIO.Entities.Geometries;
using System;

namespace MeshIO.FBX.Extensions
{
internal static class Element3DExtensions
{
public static string GetFbxName(this Element3D element)
{
switch (element)
{
case Node:
return FbxFileToken.Model;
case Geometry:
return FbxFileToken.Geometry;
default:
throw new NotImplementedException($"Unknown Element3D : {element.GetType().FullName}");
}
}
}
}
55 changes: 55 additions & 0 deletions src/MeshIO.FBX/Extensions/LayerElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using MeshIO.Entities.Geometries.Layers;
using System;

namespace MeshIO.FBX.Extensions
{
internal static class LayerElementExtensions
{
public static string GetFbxName(this LayerElement layer)
{
switch (layer)
{
case LayerElementNormal:
return "LayerElementNormal";
case LayerElementUV:
return "LayerElementUV";
default:
throw new NotImplementedException($"Unknown LayerElement Type : {layer.GetType().FullName}");
}
}

public static string GetFbxName(this MappingMode mappingMode)
{
switch (mappingMode)
{
case MappingMode.ByVertex:
return "ByVertice";
case MappingMode.ByPolygonVertex:
return "ByPolygonVertex";
case MappingMode.ByPolygon:
return "ByPolygon";
case MappingMode.ByEdge:
return "ByEdge";
case MappingMode.AllSame:
return "AllSame";
default:
throw new ArgumentException($"Unknown MappingMode : {mappingMode}", nameof(mappingMode));
}
}

public static string GetFbxName(this ReferenceMode referenceMode)
{
switch (referenceMode)
{
case ReferenceMode.Direct:
return "Direct";
case ReferenceMode.Index:
return "Index";
case ReferenceMode.IndexToDirect:
return "IndexToDirect";
default:
throw new ArgumentException($"Unknown ReferenceMode : {referenceMode}", nameof(referenceMode));
}
}
}
}
5 changes: 4 additions & 1 deletion src/MeshIO.FBX/FbxFileFormat.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace MeshIO.FBX
using System;

namespace MeshIO.FBX
{
[Obsolete]
public enum FbxFileFormat
{
Binary,
Expand Down
Loading
Loading