Skip to content

Commit 39ca36f

Browse files
committed
Refactor materials/textures to MeshIO.Materials namespace
Moved all material and texture classes from MeshIO.Shaders to a new MeshIO.Materials namespace. Introduced a Texture class with filter and wrap enums, and extended PbrMaterial to support texture properties. Added GltfTextureBuilder and GltfConverter for glTF texture handling. Updated GltfMaterialBuilder to assign textures, and rewrote GltfMaterialNormalTextureInfo to implement IGltfTextureInfo. GlbFileBuilder now manages images, samplers, and textures. Renamed Camera.OrtographicZoom to OrthographicZoom. Updated all references and usages to reflect these changes.
1 parent 7e0a4fa commit 39ca36f

21 files changed

Lines changed: 367 additions & 187 deletions

src/MeshIO/Entities/Camera.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class Camera : Entity
3737
/// too low may result in rendering artifacts due to depth buffer precision limitations.</remarks>
3838
public double NearPlane { get; set; } = 0;
3939

40-
public XY OrtographicZoom { get; set; }
40+
public XY OrthographicZoom { get; set; }
4141

4242
/// <summary>
4343
/// Gets or sets the position represented by this instance.

src/MeshIO/Formats/Fbx/Builders/FbxCameraBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected override void buildProperties(Dictionary<string, FbxProperty> properti
4141

4242
if (properties.Remove("OrthoZoom", out FbxProperty orthoZoom))
4343
{
44-
_element.OrtographicZoom = new XY((double)orthoZoom.ToProperty().Value);
44+
_element.OrthographicZoom = new XY((double)orthoZoom.ToProperty().Value);
4545
}
4646

4747
base.buildProperties(properties);
@@ -57,7 +57,7 @@ protected override bool setValue(FbxFileBuilderBase builder, FbxNode node)
5757
case "AudioColor":
5858
return true;
5959
case FbxFileToken.CameraOrthoZoom:
60-
_element.OrtographicZoom = new XY(node.GetValue<double>());
60+
_element.OrthographicZoom = new XY(node.GetValue<double>());
6161
return true;
6262
case FbxFileToken.Position:
6363
this._element.Position = this.nodeToXYZ(node);

src/MeshIO/Formats/Fbx/Builders/FbxMaterialBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using MeshIO.Formats.Fbx.Readers;
2-
using MeshIO.Shaders;
32
using System.Collections.Generic;
43
using CSUtilities.Extensions;
4+
using MeshIO.Materials;
55

66
namespace MeshIO.Formats.Fbx.Builders;
77

src/MeshIO/Formats/Fbx/Builders/FbxNodeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using MeshIO.Entities;
77
using MeshIO.Formats.Fbx.Connections;
88
using MeshIO.Formats.Fbx.Readers;
9-
using MeshIO.Shaders;
9+
using MeshIO.Materials;
1010
using System.Collections.Generic;
1111
using System.Linq;
1212

src/MeshIO/Formats/Fbx/Templates/FbxNodeTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using MeshIO.Entities;
66
using MeshIO.Formats.Fbx.Writers;
7-
using MeshIO.Shaders;
7+
using MeshIO.Materials;
88

99
namespace MeshIO.Formats.Fbx.Templates;
1010

src/MeshIO/Formats/Gltf/Builders/GltfCameraBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ internal class GltfCameraBuilder : GltfObjectBuilder<GltfCamera>
1111

1212
public override void Build(GlbFileBuilder builder)
1313
{
14-
base.Build(builder);
15-
1614
this.Camera = new Camera(this.GltfObject.Name);
1715

16+
base.Build(builder);
17+
1818
switch (this.GltfObject.Type)
1919
{
2020
case GltfCamera.TypeEnum.perspective when this.GltfObject.Orthographic != null:
@@ -51,6 +51,6 @@ private void mapOrthographicCamera(GltfCameraOrthographic gltfCamera)
5151
this.Camera.ProjectionType = ProjectionType.Orthographic;
5252
this.Camera.NearPlane = gltfCamera.Znear;
5353
this.Camera.FarPlane = gltfCamera.Zfar;
54-
this.Camera.OrtographicZoom = new XY(gltfCamera.Xmag, gltfCamera.Ymag);
54+
this.Camera.OrthographicZoom = new XY(gltfCamera.Xmag, gltfCamera.Ymag);
5555
}
5656
}

src/MeshIO/Formats/Gltf/Builders/GltfMaterialBuilder.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using MeshIO.Formats.Gltf.Readers;
22
using MeshIO.Formats.Gltf.Schema.V2;
3-
using MeshIO.Shaders;
3+
using MeshIO.Materials;
44

55
namespace MeshIO.Formats.Gltf.Builders;
66

@@ -10,6 +10,39 @@ internal class GltfMaterialBuilder : GltfObjectBuilder<GltfMaterial>
1010

1111
public override void Build(GlbFileBuilder builder)
1212
{
13+
this.Material = new PbrMaterial(this.GltfObject.Name);
14+
var pbrMat = this.Material as PbrMaterial;
15+
1316
base.Build(builder);
17+
18+
if (this.GltfObject.PbrMetallicRoughness != null)
19+
{
20+
}
21+
22+
if (this.GltfObject.NormalTexture != null
23+
&& builder.TryGetBuilder(this.GltfObject.NormalTexture.Index,
24+
out GltfTextureBuilder normalBuilder))
25+
{
26+
pbrMat.NormalTexture = normalBuilder.Texture;
27+
}
28+
29+
if (this.GltfObject.OcclusionTexture != null
30+
&& builder.TryGetBuilder(this.GltfObject.NormalTexture.Index,
31+
out GltfTextureBuilder occlusionBuilder))
32+
{
33+
pbrMat.OcclusionTexture = occlusionBuilder.Texture;
34+
}
35+
36+
if (this.GltfObject.EmissiveTexture != null
37+
&& builder.TryGetBuilder(this.GltfObject.NormalTexture.Index,
38+
out GltfTextureBuilder emissiveBuilder))
39+
{
40+
pbrMat.EmissiveTexture = emissiveBuilder.Texture;
41+
}
42+
}
43+
44+
private Texture processTexture(GlbFileBuilder builder)
45+
{
46+
throw new System.NotImplementedException();
1447
}
1548
}

src/MeshIO/Formats/Gltf/Builders/GltfMeshBuilder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using MeshIO.Entities.Geometries.Layers;
44
using MeshIO.Formats.Gltf.Readers;
55
using MeshIO.Formats.Gltf.Schema.V2;
6-
using MeshIO.Shaders;
6+
using MeshIO.Materials;
77
using System;
88
using System.Collections.Generic;
99

@@ -88,8 +88,6 @@ public override void Build(GlbFileBuilder builder)
8888
this.Materials.Add(material.Material);
8989
var layer = new LayerElementMaterial();
9090
mesh.Layers.Add(layer);
91-
92-
builder.Notify($"Material not implemented for mesh.", NotificationType.NotImplemented);
9391
}
9492
}
9593
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using MeshIO.Formats.Gltf.Readers;
2+
using MeshIO.Formats.Gltf.Schema;
3+
using MeshIO.Formats.Gltf.Schema.V2;
4+
using MeshIO.Materials;
5+
6+
namespace MeshIO.Formats.Gltf.Builders;
7+
8+
internal class GltfTextureBuilder : GltfObjectBuilder<GltfTexture>
9+
{
10+
public Texture Texture { get; private set; }
11+
12+
public GltfTextureBuilder()
13+
{ }
14+
15+
public override void Build(GlbFileBuilder builder)
16+
{
17+
this.Texture = new Texture(this.GltfObject.Name);
18+
19+
base.Build(builder);
20+
21+
if (builder.Samplers.TryGetValue(this.GltfObject.Sampler, out var sampler))
22+
{
23+
this.Texture.MagnificationFilter = ((int?)sampler.MagFilter).Convert();
24+
this.Texture.MinificationFilter = ((int?)sampler.MinFilter).Convert(out TextureFilterType mipFilter);
25+
this.Texture.MipFilter = mipFilter;
26+
27+
this.Texture.WrapModeT = ((int)sampler.WrapT).Convert();
28+
this.Texture.WrapModeS = ((int)sampler.WrapS).Convert();
29+
}
30+
31+
if (builder.Images.TryGetValue(this.GltfObject.Source, out var image))
32+
{
33+
}
34+
}
35+
}

src/MeshIO/Formats/Gltf/Readers/GlbFileBuilder.cs

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ internal class GlbFileBuilder : IGlbFileBuilder
1313
{
1414
public event NotificationEventHandler OnNotification;
1515

16+
public Dictionary<string, GltfImage> Images { get; } = new();
17+
18+
public Dictionary<string, GltfSampler> Samplers { get; } = new();
19+
1620
private readonly Dictionary<string, GltfAccessorBuilder> _accessors = new();
1721

1822
private readonly Dictionary<string, GltfBuffer> _buffers = new();
@@ -31,6 +35,8 @@ internal class GlbFileBuilder : IGlbFileBuilder
3135

3236
private readonly Dictionary<string, GltfSceneBuilder> _scenes = new();
3337

38+
private readonly Dictionary<string, GltfTextureBuilder> _textures = new();
39+
3440
private GltfRoot _root;
3541

3642
public GlbFileBuilder(GltfHeader header)
@@ -69,31 +75,13 @@ public Scene Build()
6975
this.createBuilders(_cameras, this._root.Cameras);
7076
this.createBuilders(_accessors, this._root.Accessors);
7177
this.createBuilders(_materials, this._root.Materials);
78+
this.createBuilders(_textures, this._root.Textures);
7279

73-
for (int i = 0; i < this._root.Buffers.Length; i++)
74-
{
75-
var gltf = this._root.Buffers[i];
76-
if (this._header.Version == GltfVersion.V1)
77-
{
78-
_buffers.Add(gltf.Name, gltf);
79-
}
80-
else
81-
{
82-
_buffers.Add(i.ToString(), gltf);
83-
}
84-
}
85-
for (int i = 0; i < this._root.BufferViews.Length; i++)
86-
{
87-
var gltf = this._root.BufferViews[i];
88-
if (this._header.Version == GltfVersion.V1)
89-
{
90-
_bufferViews.Add(gltf.Name, gltf);
91-
}
92-
else
93-
{
94-
_bufferViews.Add(i.ToString(), gltf);
95-
}
96-
}
80+
mapCollection(this._buffers, this._root.Buffers);
81+
mapCollection(this._bufferViews, this._root.BufferViews);
82+
83+
mapCollection(this.Samplers, this._root.Samplers);
84+
mapCollection(this.Images, this._root.Images);
9785

9886
sceneBuilder.Build(this);
9987

@@ -152,6 +140,10 @@ public bool TryGetBuilder<T>(string id, out T builder, bool notify = true)
152140
{
153141
dict = this._materials as Dictionary<string, T>;
154142
}
143+
else if (builderType == typeof(GltfTextureBuilder))
144+
{
145+
dict = this._textures as Dictionary<string, T>;
146+
}
155147
else
156148
{
157149
throw new InvalidOperationException();
@@ -195,4 +187,21 @@ private void createBuilders<Builder, Gltf>(Dictionary<string, Builder> collectio
195187
}
196188
}
197189
}
190+
191+
private void mapCollection<T>(Dictionary<string, T> map, T[] values)
192+
where T : IGltfNamedObject
193+
{
194+
for (int i = 0; i < values.Length; i++)
195+
{
196+
var gltf = values[i];
197+
if (this._header.Version == GltfVersion.V1)
198+
{
199+
map.Add(gltf.Name, gltf);
200+
}
201+
else
202+
{
203+
map.Add(i.ToString(), gltf);
204+
}
205+
}
206+
}
198207
}

0 commit comments

Comments
 (0)