Skip to content

Commit e1e4343

Browse files
committed
Add option to manually bind UV map types
1 parent e8ca265 commit e1e4343

File tree

17 files changed

+384
-352
lines changed

17 files changed

+384
-352
lines changed

AssetStudioCLI/Exporter.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,12 @@ public static void ExportGameObject(GameObject gameObject, string exportPath, Li
140140

141141
private static void ExportFbx(IImported convert, string exportPath)
142142
{
143-
var eulerFilter = true;
144-
var filterPrecision = 0.25f;
145-
var exportAllNodes = true;
146-
var exportSkins = true;
147-
var exportAnimations = true;
148-
var exportBlendShape = true;
149-
var castToBone = false;
150-
var boneSize = CLIOptions.o_fbxBoneSize.Value;
151-
var exportAllUvsAsDiffuseMaps = false;
152-
var scaleFactor = CLIOptions.o_fbxScaleFactor.Value;
153-
var fbxVersion = 3;
154-
var fbxFormat = 0;
155-
ModelExporter.ExportFbx(exportPath, convert, eulerFilter, filterPrecision,
156-
exportAllNodes, exportSkins, exportAnimations, exportBlendShape, castToBone, boneSize, exportAllUvsAsDiffuseMaps, scaleFactor, fbxVersion, fbxFormat == 1);
143+
var fbxSettings = new Fbx.Settings
144+
{
145+
BoneSize = CLIOptions.o_fbxBoneSize.Value,
146+
ScaleFactor = CLIOptions.o_fbxScaleFactor.Value,
147+
};
148+
ModelExporter.ExportFbx(exportPath, convert, fbxSettings);
157149
}
158150

159151
public static bool ExportRawFile(AssetItem item, string exportPath)
-16.7 KB
Binary file not shown.
-80 Bytes
Binary file not shown.
-96 Bytes
Binary file not shown.

AssetStudioFBXNative/api.cpp

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -462,26 +462,52 @@ AS_API(void) AsFbxMeshCreateElementNormal(FbxMesh* pMesh)
462462
pNormal->SetReferenceMode(FbxGeometryElement::eDirect);
463463
}
464464

465-
AS_API(void) AsFbxMeshCreateDiffuseUV(FbxMesh* pMesh, int32_t uv)
465+
AS_API(void) AsFbxMeshCreateUVMap(FbxMesh* pMesh, int32_t uvIndex, int32_t uvType)
466466
{
467467
if (pMesh == nullptr)
468468
{
469469
return;
470470
}
471471

472-
auto pUV = pMesh->CreateElementUV(FbxString("UV") + FbxString(uv), FbxLayerElement::eTextureDiffuse);
473-
pUV->SetMappingMode(FbxGeometryElement::eByControlPoint);
474-
pUV->SetReferenceMode(FbxGeometryElement::eDirect);
475-
}
476-
477-
AS_API(void) AsFbxMeshCreateNormalMapUV(FbxMesh* pMesh, int32_t uv)
478-
{
479-
if (pMesh == nullptr)
480-
{
481-
return;
482-
}
483-
484-
auto pUV = pMesh->CreateElementUV(FbxString("UV") + FbxString(uv), FbxLayerElement::eTextureNormalMap);
472+
FbxLayerElement::EType layerElement;
473+
switch (uvType)
474+
{
475+
case 0:
476+
layerElement = FbxLayerElement::eTextureDiffuse;
477+
break;
478+
case 1:
479+
layerElement = FbxLayerElement::eTextureNormalMap;
480+
break;
481+
case 2:
482+
layerElement = FbxLayerElement::eTextureDisplacement;
483+
break;
484+
case 3:
485+
layerElement = FbxLayerElement::eTextureSpecular;
486+
break;
487+
case 4:
488+
layerElement = FbxLayerElement::eTextureBump;
489+
break;
490+
case 5:
491+
layerElement = FbxLayerElement::eTextureEmissive;
492+
break;
493+
case 6:
494+
layerElement = FbxLayerElement::eTextureAmbient;
495+
break;
496+
case 7:
497+
layerElement = FbxLayerElement::eTextureShininess;
498+
break;
499+
case 8:
500+
layerElement = FbxLayerElement::eTextureReflection;
501+
break;
502+
case 9:
503+
layerElement = FbxLayerElement::eTextureTransparency;
504+
break;
505+
default:
506+
layerElement = FbxLayerElement::eTextureDiffuse;
507+
break;
508+
}
509+
510+
auto pUV = pMesh->CreateElementUV(FbxString("UV") + FbxString(uvIndex), layerElement);
485511
pUV->SetMappingMode(FbxGeometryElement::eByControlPoint);
486512
pUV->SetReferenceMode(FbxGeometryElement::eDirect);
487513
}

AssetStudioFBXNative/api.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ AS_API(void) AsFbxMeshInitControlPoints(fbxsdk::FbxMesh* pMesh, int32_t vertexCo
6666

6767
AS_API(void) AsFbxMeshCreateElementNormal(fbxsdk::FbxMesh* pMesh);
6868

69-
AS_API(void) AsFbxMeshCreateDiffuseUV(fbxsdk::FbxMesh* pMesh, int32_t uv);
70-
71-
AS_API(void) AsFbxMeshCreateNormalMapUV(fbxsdk::FbxMesh* pMesh, int32_t uv);
69+
AS_API(void) AsFbxMeshCreateUVMap(fbxsdk::FbxMesh* pMesh, int32_t uvIndex, int32_t uvType);
7270

7371
AS_API(void) AsFbxMeshCreateElementTangent(fbxsdk::FbxMesh* pMesh);
7472

AssetStudioFBXWrapper/Fbx.cs

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using AssetStudio.FbxInterop;
22
using System.IO;
3+
using System.Text.Json;
4+
using System.Collections.Generic;
35

46
#if NETFRAMEWORK
57
using AssetStudio.PInvoke;
@@ -30,9 +32,7 @@ public static Quaternion EulerToQuaternion(Vector3 v)
3032

3133
public static class Exporter
3234
{
33-
34-
public static void Export(string path, IImported imported, bool eulerFilter, float filterPrecision,
35-
bool allNodes, bool skins, bool animation, bool blendShape, bool castToBone, float boneSize, bool exportAllUvsAsDiffuseMaps, float scaleFactor, int versionIndex, bool isAscii)
35+
public static void Export(string path, IImported imported, Settings fbxSettings)
3636
{
3737
var file = new FileInfo(path);
3838
var dir = file.Directory;
@@ -47,16 +47,88 @@ public static void Export(string path, IImported imported, bool eulerFilter, flo
4747

4848
var name = Path.GetFileName(path);
4949

50-
using (var exporter = new FbxExporter(name, imported, allNodes, skins, castToBone, boneSize, exportAllUvsAsDiffuseMaps, scaleFactor, versionIndex, isAscii))
50+
using (var exporter = new FbxExporter(name, imported, fbxSettings))
5151
{
52-
exporter.Initialize();
53-
exporter.ExportAll(blendShape, animation, eulerFilter, filterPrecision);
52+
exporter.ExportAll();
5453
}
5554

5655
Directory.SetCurrentDirectory(currentDir);
5756
}
58-
5957
}
6058

59+
public sealed class Settings
60+
{
61+
public bool EulerFilter { get; set; }
62+
public float FilterPrecision { get; set; }
63+
public bool ExportAllNodes { get; set; }
64+
public bool ExportSkins { get; set; }
65+
public bool ExportAnimations { get; set; }
66+
public bool ExportBlendShape { get; set; }
67+
public bool CastToBone { get; set; }
68+
public float BoneSize { get; set; }
69+
public bool ExportAllUvsAsDiffuseMaps { get; set; }
70+
public float ScaleFactor { get; set; }
71+
public int FbxVersionIndex { get; set; }
72+
public int FbxFormat { get; set; }
73+
public Dictionary<int,int> UvBindings { get; set; }
74+
public bool IsAscii => FbxFormat == 1;
75+
76+
public Settings()
77+
{
78+
Init();
79+
}
80+
81+
public Settings(bool eulerFilter, float filterPrecision, bool exportAllNodes, bool exportSkins, bool exportAnimations, bool exportBlendShape, bool castToBone, float boneSize,
82+
bool exportAllUvsAsDiffuseMaps, float scaleFactor, int fbxVersionIndex, int fbxFormat, Dictionary<int, int> uvBindings)
83+
{
84+
EulerFilter = eulerFilter;
85+
FilterPrecision = filterPrecision;
86+
ExportAllNodes = exportAllNodes;
87+
ExportSkins = exportSkins;
88+
ExportAnimations = exportAnimations;
89+
ExportBlendShape = exportBlendShape;
90+
CastToBone = castToBone;
91+
BoneSize = (int)boneSize;
92+
ExportAllUvsAsDiffuseMaps = exportAllUvsAsDiffuseMaps;
93+
ScaleFactor = scaleFactor;
94+
FbxVersionIndex = fbxVersionIndex;
95+
FbxFormat = fbxFormat;
96+
UvBindings = uvBindings;
97+
}
98+
99+
public void Init()
100+
{
101+
var uvDict = new Dictionary<int, int>();
102+
for (var i = 0; i < 8; i++)
103+
{
104+
uvDict[i] = i + 1;
105+
}
106+
107+
EulerFilter = true;
108+
FilterPrecision = 0.25f;
109+
ExportAllNodes = true;
110+
ExportSkins = true;
111+
ExportAnimations = true;
112+
ExportBlendShape = true;
113+
CastToBone = false;
114+
ExportAllUvsAsDiffuseMaps = false;
115+
BoneSize = 10;
116+
ScaleFactor = 1.0f;
117+
FbxFormat = 0;
118+
FbxVersionIndex = 3;
119+
UvBindings = uvDict;
120+
}
121+
122+
public static Settings FromBase64(string base64String)
123+
{
124+
var settingsData = System.Convert.FromBase64String(base64String);
125+
return JsonSerializer.Deserialize<Settings>(settingsData);
126+
}
127+
128+
public string ToBase64()
129+
{
130+
return System.Convert.ToBase64String(JsonSerializer.SerializeToUtf8Bytes(this));
131+
}
132+
}
61133
}
62134
}

AssetStudioFBXWrapper/FbxExporter.cs

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,15 @@ internal sealed class FbxExporter : IDisposable
1111

1212
private readonly string _fileName;
1313
private readonly IImported _imported;
14-
private readonly bool _allNodes;
15-
private readonly bool _exportSkins;
16-
private readonly bool _castToBone;
17-
private readonly float _boneSize;
18-
private readonly bool _exportAllUvsAsDiffuseMaps;
19-
private readonly float _scaleFactor;
20-
private readonly int _versionIndex;
21-
private readonly bool _isAscii;
14+
private readonly Fbx.Settings _settings;
2215

23-
internal FbxExporter(string fileName, IImported imported, bool allNodes, bool exportSkins, bool castToBone, float boneSize, bool exportAllUvsAsDiffuseMaps, float scaleFactor, int versionIndex, bool isAscii)
16+
internal FbxExporter(string fileName, IImported imported, Fbx.Settings fbxSettings)
2417
{
2518
_context = new FbxExporterContext();
2619

2720
_fileName = fileName;
2821
_imported = imported;
29-
_allNodes = allNodes;
30-
_exportSkins = exportSkins;
31-
_castToBone = castToBone;
32-
_boneSize = boneSize;
33-
_exportAllUvsAsDiffuseMaps = exportAllUvsAsDiffuseMaps;
34-
_scaleFactor = scaleFactor;
35-
_versionIndex = versionIndex;
36-
_isAscii = isAscii;
22+
_settings = fbxSettings;
3723
}
3824

3925
~FbxExporter()
@@ -64,22 +50,24 @@ private void Dispose(bool disposing)
6450
IsDisposed = true;
6551
}
6652

67-
internal void Initialize()
53+
private void Initialize()
6854
{
6955
var is60Fps = _imported.AnimationList.Count > 0 && _imported.AnimationList[0].SampleRate.Equals(60.0f);
7056

71-
_context.Initialize(_fileName, _scaleFactor, _versionIndex, _isAscii, is60Fps);
57+
_context.Initialize(_fileName, _settings, is60Fps);
7258

73-
if (!_allNodes)
59+
if (!_settings.ExportAllNodes)
7460
{
7561
var framePaths = SearchHierarchy();
7662

7763
_context.SetFramePaths(framePaths);
7864
}
7965
}
8066

81-
internal void ExportAll(bool blendShape, bool animation, bool eulerFilter, float filterPrecision)
67+
internal void ExportAll()
8268
{
69+
Initialize();
70+
8371
var meshFrames = new List<ImportedFrame>();
8472

8573
ExportRootFrame(meshFrames);
@@ -97,16 +85,14 @@ internal void ExportAll(bool blendShape, bool animation, bool eulerFilter, float
9785
SetJointsNode(_imported.RootFrame, null, true);
9886
}
9987

100-
101-
102-
if (blendShape)
88+
if (_settings.ExportBlendShape)
10389
{
10490
ExportMorphs();
10591
}
10692

107-
if (animation)
93+
if (_settings.ExportAnimations)
10894
{
109-
ExportAnimations(eulerFilter, filterPrecision);
95+
ExportAnimations(_settings.EulerFilter, _settings.FilterPrecision);
11096
}
11197

11298
ExportScene();
@@ -134,7 +120,7 @@ private void ExportScene()
134120

135121
private void SetJointsFromImportedMeshes()
136122
{
137-
if (!_exportSkins)
123+
if (!_settings.ExportSkins)
138124
{
139125
return;
140126
}
@@ -156,12 +142,12 @@ private void SetJointsFromImportedMeshes()
156142
}
157143
}
158144

159-
SetJointsNode(_imported.RootFrame, bonePaths, _castToBone);
145+
SetJointsNode(_imported.RootFrame, bonePaths, _settings.CastToBone);
160146
}
161147

162148
private void SetJointsNode(ImportedFrame rootFrame, HashSet<string> bonePaths, bool castToBone)
163149
{
164-
_context.SetJointsNode(rootFrame, bonePaths, castToBone, _boneSize);
150+
_context.SetJointsNode(rootFrame, bonePaths, castToBone, _settings.BoneSize);
165151
}
166152

167153
private void PrepareMaterials()
@@ -173,7 +159,7 @@ private void ExportMeshFrames(ImportedFrame rootFrame, List<ImportedFrame> meshF
173159
{
174160
foreach (var meshFrame in meshFrames)
175161
{
176-
_context.ExportMeshFromFrame(rootFrame, meshFrame, _imported.MeshList, _imported.MaterialList, _imported.TextureList, _exportSkins, _exportAllUvsAsDiffuseMaps);
162+
_context.ExportMeshFromFrame(rootFrame, meshFrame, _imported.MeshList, _imported.MaterialList, _imported.TextureList, _settings);
177163
}
178164
}
179165

AssetStudioFBXWrapper/FbxExporterContext.PInvoke.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ private static IntPtr AsFbxCreateTexture(IntPtr context, string matTexName)
142142
private static extern void AsFbxMeshCreateElementNormal(IntPtr mesh);
143143

144144
[DllImport(FbxDll.DllName, CallingConvention = CallingConvention.Winapi)]
145-
private static extern void AsFbxMeshCreateDiffuseUV(IntPtr mesh, int uv);
146-
147-
[DllImport(FbxDll.DllName, CallingConvention = CallingConvention.Winapi)]
148-
private static extern void AsFbxMeshCreateNormalMapUV(IntPtr mesh, int uv);
145+
private static extern void AsFbxMeshCreateUVMap(IntPtr mesh, int uvIndex, int uvType);
149146

150147
[DllImport(FbxDll.DllName, CallingConvention = CallingConvention.Winapi)]
151148
private static extern void AsFbxMeshCreateElementTangent(IntPtr mesh);

0 commit comments

Comments
 (0)