Skip to content
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 @@ -353,6 +353,7 @@ MigrationBackup/
/.packages
/local
/samples/out
/samples/local
/samples/in/fbx/old

# Exceptions
Expand Down
19 changes: 19 additions & 0 deletions src/MeshIO.Tests/Formats/SceneReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ namespace MeshIO.Tests.Formats;
public class SceneReaderTests : IOTestsBase
{
public static TheoryData<FileModel> InputCases { get; } = new();
public static TheoryData<FileModel> LocalCases { get; } = new();

static SceneReaderTests()
{
loadSamples("stl", "stl", InputCases);
loadSamples("fbx", "fbx", InputCases);
loadSamples("glb", "glb", InputCases);

loadSamples("../local/in/fbx", string.Empty, "fbx", LocalCases);
}

public SceneReaderTests(ITestOutputHelper output)
Expand All @@ -32,4 +35,20 @@ public void ReadTest(FileModel test)
scene = reader.Read();
}
}

[Theory]
[MemberData(nameof(LocalCases))]
public void ReadLocalSamples(FileModel test)
{
if (string.IsNullOrEmpty(test.Path))
{
return;
}

Scene scene;
using (ISceneReader reader = FileFormat.GetReader(test.Path, onNotification))
{
scene = reader.Read();
}
}
}
2 changes: 1 addition & 1 deletion src/MeshIO/Entities/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Camera : Entity
/// too low may result in rendering artifacts due to depth buffer precision limitations.</remarks>
public double NearPlane { get; set; } = 0;

public XY OrtographicZoom { get; set; }
public XY OrthographicZoom { get; set; }

/// <summary>
/// Gets or sets the position represented by this instance.
Expand Down
6 changes: 3 additions & 3 deletions src/MeshIO/Formats/Fbx/Builders/FbxCameraBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected override void buildProperties(Dictionary<string, FbxProperty> properti

if (properties.Remove("OrthoZoom", out FbxProperty orthoZoom))
{
_element.OrtographicZoom = new XY((double)orthoZoom.ToProperty().Value);
_element.OrthographicZoom = new XY((double)orthoZoom.ToProperty().Value);
}

base.buildProperties(properties);
Expand All @@ -57,7 +57,7 @@ protected override bool setValue(FbxFileBuilderBase builder, FbxNode node)
case "AudioColor":
return true;
case FbxFileToken.CameraOrthoZoom:
_element.OrtographicZoom = new XY(node.GetValue<double>());
_element.OrthographicZoom = new XY(node.GetValue<double>());
return true;
case FbxFileToken.Position:
this._element.Position = this.nodeToXYZ(node);
Expand All @@ -72,4 +72,4 @@ protected override bool setValue(FbxFileBuilderBase builder, FbxNode node)
return base.setValue(builder, node);
}
}
}
}
79 changes: 79 additions & 0 deletions src/MeshIO/Formats/Fbx/Builders/FbxMaterialBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using MeshIO.Formats.Fbx.Readers;
using System.Collections.Generic;
using CSUtilities.Extensions;
using MeshIO.Materials;

namespace MeshIO.Formats.Fbx.Builders;

internal abstract class FbxMaterialBuilder<T> : FbxObjectBuilder<T>
where T : Material
{
public override string FbxObjectName { get { return string.Empty; } }

public override string FbxTypeName { get { return FbxFileToken.Material; } }

public FbxMaterialBuilder(FbxNode node, T material) : base(node, material)
{
}

protected override bool setValue(FbxFileBuilderBase builder, FbxNode node)
{
switch (node.Name)
{
case FbxFileToken.ShadingModel:
case FbxFileToken.MultiLayer:
return true;
default:
return base.setValue(builder, node);
}
}

protected override void buildProperties(Dictionary<string, FbxProperty> properties)
{
if (properties.Remove("EmissiveColor", out FbxProperty emissiveColor))
{
_element.EmissiveColor = emissiveColor.ToProperty().GetValue<Color>();
}

if (properties.Remove("EmissiveFactor", out FbxProperty emissiveFactor))
{
_element.EmissiveFactor = emissiveFactor.ToProperty().GetValue<double>();
}

if (properties.Remove("AmbientColor", out FbxProperty ambientColor))
{
_element.AmbientColor = ambientColor.ToProperty().GetValue<Color>();
}

if (properties.Remove("AmbientFactor", out FbxProperty ambientFactor))
{
_element.AmbientFactor = ambientFactor.ToProperty().GetValue<double>();
}

if (properties.Remove("DiffuseColor", out FbxProperty diffuseColor))
{
_element.DiffuseColor = diffuseColor.ToProperty().GetValue<Color>();
}

if (properties.Remove("DiffuseFactor", out FbxProperty diffuseFactor))
{
_element.DiffuseFactor = diffuseFactor.ToProperty().GetValue<double>();
}

base.buildProperties(properties);
}
}

internal class FbxShaderMaterialBuilder : FbxMaterialBuilder<ShaderMaterial>
{
public FbxShaderMaterialBuilder(FbxNode node) : base(node, new ShaderMaterial())
{
}
}

internal class FbxPhongMaterialBuilder : FbxMaterialBuilder<PhongMaterial>
{
public FbxPhongMaterialBuilder(FbxNode node) : base(node, new PhongMaterial())
{
}
}
2 changes: 1 addition & 1 deletion src/MeshIO/Formats/Fbx/Builders/FbxNodeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using MeshIO.Entities;
using MeshIO.Formats.Fbx.Connections;
using MeshIO.Formats.Fbx.Readers;
using MeshIO.Shaders;
using MeshIO.Materials;
using System.Collections.Generic;
using System.Linq;

Expand Down
4 changes: 4 additions & 0 deletions src/MeshIO/Formats/Fbx/FbxFileToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class FbxFileToken

public const string Model = "Model";

public const string MultiLayer = "MultiLayer";

public const string NodeAttribute = "NodeAttribute";

public const string NodeAttributeName = "NodeAttributeName";
Expand All @@ -90,6 +92,8 @@ public class FbxFileToken

public const string Shading = "Shading";

public const string ShadingModel = "ShadingModel";

public const string TypeFlags = "TypeFlags";

public const string Up = "Up";
Expand Down
2 changes: 1 addition & 1 deletion src/MeshIO/Formats/Fbx/FbxNodeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public abstract class FbxNodeCollection : IEnumerable<FbxNode>
/// </summary>
/// <param name="name"></param>
/// <returns>The child node, or null</returns>
public FbxNode this[string name] { get { return this.Nodes.Find(n => n != null && n.Name == name); } }
public FbxNode this[string name] { get { return this.Nodes.Find(n => n != null && n.Name.Equals(name, System.StringComparison.InvariantCultureIgnoreCase)); } }

/// <summary>
/// Add a note into the collection
Expand Down
11 changes: 0 additions & 11 deletions src/MeshIO/Formats/Fbx/FbxProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@ namespace MeshIO.Formats.Fbx;

public class FbxProperty : Property
{
public static class Geometry
{
public const string Color = "Color";
public const string BBoxMin = "BBoxMin";
public const string BBoxMax = "BBoxMax";
public const string PrimaryVisibility = "Primary Visibility";
public const string CastsShadows = "Casts Shadows";

//public static FbxProperty<bool> CreatePrimaryVisibility() => new FbxProperty<bool>(PrimaryVisibility, true);
}

/// <summary>
/// Fbx equivalent type name
/// </summary>
Expand Down
25 changes: 24 additions & 1 deletion src/MeshIO/Formats/Fbx/Readers/FbxFileBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Xml.Linq;

namespace MeshIO.Formats.Fbx.Readers;
Expand Down Expand Up @@ -149,7 +150,7 @@ public void Notify(string message, NotificationType notificationType = Notificat

public Dictionary<string, FbxProperty> ReadProperties(FbxNode node)
{
Dictionary<string, FbxProperty> properties = new Dictionary<string, FbxProperty>();
Dictionary<string, FbxProperty> properties = new(StringComparer.InvariantCultureIgnoreCase);
if (!node.TryGetNode(FbxFileToken.GetPropertiesName(this.Version), out FbxNode propertiesNode))
{
return properties;
Expand Down Expand Up @@ -392,6 +393,9 @@ protected void readObjects(FbxNode node)
case FbxFileToken.Geometry:
template = this.readGeometryNode(n);
break;
case FbxFileToken.Material:
template = this.readMaterial(n);
break;
case FbxFileToken.NodeAttribute:
var type = n.Properties.LastOrDefault().ToString();
switch (type)
Expand Down Expand Up @@ -425,6 +429,25 @@ protected void readObjects(FbxNode node)
}
}

private IFbxObjectBuilder readMaterial(FbxNode node)
{
if (!node.TryGetNode(FbxFileToken.ShadingModel, out FbxNode matType))
{
return new FbxShaderMaterialBuilder(node);
}

string name = matType.GetValue<string>().ToLower();
switch (name)
{
case "unknown":
return new FbxShaderMaterialBuilder(node);
case "phong":
return new FbxPhongMaterialBuilder(node);
default:
throw new System.NotImplementedException();
}
}

protected void readReferences(FbxNode node)
{
}
Expand Down
2 changes: 1 addition & 1 deletion src/MeshIO/Formats/Fbx/Templates/FbxNodeTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using MeshIO.Entities;
using MeshIO.Formats.Fbx.Writers;
using MeshIO.Shaders;
using MeshIO.Materials;

namespace MeshIO.Formats.Fbx.Templates;

Expand Down
6 changes: 3 additions & 3 deletions src/MeshIO/Formats/Gltf/Builders/GltfCameraBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ internal class GltfCameraBuilder : GltfObjectBuilder<GltfCamera>

public override void Build(GlbFileBuilder builder)
{
base.Build(builder);

this.Camera = new Camera(this.GltfObject.Name);

base.Build(builder);

switch (this.GltfObject.Type)
{
case GltfCamera.TypeEnum.perspective when this.GltfObject.Orthographic != null:
Expand Down Expand Up @@ -51,6 +51,6 @@ private void mapOrthographicCamera(GltfCameraOrthographic gltfCamera)
this.Camera.ProjectionType = ProjectionType.Orthographic;
this.Camera.NearPlane = gltfCamera.Znear;
this.Camera.FarPlane = gltfCamera.Zfar;
this.Camera.OrtographicZoom = new XY(gltfCamera.Xmag, gltfCamera.Ymag);
this.Camera.OrthographicZoom = new XY(gltfCamera.Xmag, gltfCamera.Ymag);
}
}
31 changes: 30 additions & 1 deletion src/MeshIO/Formats/Gltf/Builders/GltfMaterialBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using MeshIO.Formats.Gltf.Readers;
using MeshIO.Formats.Gltf.Schema.V2;
using MeshIO.Shaders;
using MeshIO.Materials;

namespace MeshIO.Formats.Gltf.Builders;

Expand All @@ -10,6 +10,35 @@ internal class GltfMaterialBuilder : GltfObjectBuilder<GltfMaterial>

public override void Build(GlbFileBuilder builder)
{
this.Material = new PbrMaterial(this.GltfObject.Name);
var pbrMat = this.Material as PbrMaterial;

base.Build(builder);

if (this.GltfObject.PbrMetallicRoughness != null)
{

}

if (this.GltfObject.NormalTexture != null
&& builder.TryGetBuilder(this.GltfObject.NormalTexture.Index,
out GltfTextureBuilder normalBuilder))
{
pbrMat.NormalTexture = normalBuilder.Texture;
}

if (this.GltfObject.OcclusionTexture != null
&& builder.TryGetBuilder(this.GltfObject.OcclusionTexture.Index,
out GltfTextureBuilder occlusionBuilder))
{
pbrMat.OcclusionTexture = occlusionBuilder.Texture;
}

if (this.GltfObject.EmissiveTexture != null
&& builder.TryGetBuilder(this.GltfObject.EmissiveTexture.Index,
out GltfTextureBuilder emissiveBuilder))
{
pbrMat.EmissiveTexture = emissiveBuilder.Texture;
}
}
}
4 changes: 1 addition & 3 deletions src/MeshIO/Formats/Gltf/Builders/GltfMeshBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using MeshIO.Entities.Geometries.Layers;
using MeshIO.Formats.Gltf.Readers;
using MeshIO.Formats.Gltf.Schema.V2;
using MeshIO.Shaders;
using MeshIO.Materials;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -88,8 +88,6 @@ public override void Build(GlbFileBuilder builder)
this.Materials.Add(material.Material);
var layer = new LayerElementMaterial();
mesh.Layers.Add(layer);

builder.Notify($"Material not implemented for mesh.", NotificationType.NotImplemented);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/MeshIO/Formats/Gltf/Builders/GltfTextureBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using MeshIO.Formats.Gltf.Readers;
using MeshIO.Formats.Gltf.Schema;
using MeshIO.Formats.Gltf.Schema.V2;
using MeshIO.Materials;

namespace MeshIO.Formats.Gltf.Builders;

internal class GltfTextureBuilder : GltfObjectBuilder<GltfTexture>
{
public Texture Texture { get; private set; }

public GltfTextureBuilder()
{ }

public override void Build(GlbFileBuilder builder)
{
this.Texture = new Texture(this.GltfObject.Name);

base.Build(builder);

if (builder.Samplers.TryGetValue(this.GltfObject.Sampler, out var sampler))
{
this.Texture.MagnificationFilter = ((int?)sampler.MagFilter).Convert();
this.Texture.MinificationFilter = ((int?)sampler.MinFilter).Convert(out TextureFilterType mipFilter);
this.Texture.MipFilter = mipFilter;

this.Texture.WrapModeT = ((int)sampler.WrapT).Convert();
this.Texture.WrapModeS = ((int)sampler.WrapS).Convert();
}

if (builder.Images.TryGetValue(this.GltfObject.Source, out var image))
{
}
}
}
Loading
Loading