From dec0a22ffe959b740903f09210ecc9ae704e4450 Mon Sep 17 00:00:00 2001 From: Perfare Date: Mon, 24 Dec 2018 08:28:26 +0800 Subject: [PATCH] Fixed #297 --- AssetStudio/IImported.cs | 12 +++- AssetStudioFBX/AssetStudioFBX.h | 4 +- AssetStudioFBX/AssetStudioFBXExporter.cpp | 86 ++++++++++++----------- AssetStudioUtility/ModelConverter.cs | 49 ++++++------- 4 files changed, 78 insertions(+), 73 deletions(-) diff --git a/AssetStudio/IImported.cs b/AssetStudio/IImported.cs index e489c63..e7d0db7 100644 --- a/AssetStudio/IImported.cs +++ b/AssetStudio/IImported.cs @@ -168,9 +168,15 @@ namespace AssetStudio public Color4 Specular { get; set; } public Color4 Emissive { get; set; } public float Power { get; set; } - public string[] Textures { get; set; } - public Vector2[] TexOffsets { get; set; } - public Vector2[] TexScales { get; set; } + public List Textures { get; set; } + } + + public class ImportedMaterialTexture + { + public string Name { get; set; } + public int Dest { get; set; } + public Vector2 Offset { get; set; } + public Vector2 Scale { get; set; } } public class ImportedTexture diff --git a/AssetStudioFBX/AssetStudioFBX.h b/AssetStudioFBX/AssetStudioFBX.h index be9b2b7..0e04841 100644 --- a/AssetStudioFBX/AssetStudioFBX.h +++ b/AssetStudioFBX/AssetStudioFBX.h @@ -69,7 +69,7 @@ namespace AssetStudio { Exporter(String^ path, IImported^ imported, bool allFrames, bool allBones, bool skins, float boneSize, float scaleFactor, int versionIndex, bool isAscii, bool normals); ~Exporter(); - void Exporter::LinkTexture(ImportedMaterial^ mat, int attIndex, FbxFileTexture* pTexture, FbxProperty& prop); + void Exporter::LinkTexture(ImportedMaterialTexture^ texture, FbxFileTexture* pTexture, FbxProperty& prop); void SetJointsNode(FbxNode* pNode, HashSet^ boneNames, bool allBones); HashSet^ SearchHierarchy(); void SearchHierarchy(ImportedFrame^ frame, HashSet^ exportFrames); @@ -77,7 +77,7 @@ namespace AssetStudio { void ExportFrame(FbxNode* pParentNode, ImportedFrame^ frame); void ExportMesh(FbxNode* pFrameNode, ImportedMesh^ meshList, bool normals); FbxNode* FindNodeByPath(String ^ path, bool recursive); - FbxFileTexture* ExportTexture(ImportedTexture^ matTex, FbxMesh* pMesh); + FbxFileTexture* ExportTexture(ImportedTexture^ matTex); void ExportAnimations(bool eulerFilter, float filterValue, bool flatInbetween); void ExportKeyframedAnimation(ImportedKeyframedAnimation^ parser, FbxString& kTakeName, FbxAnimCurveFilterUnroll* eulerFilter, float filterPrecision, bool flatInbetween); void ExportMorphs(IImported^ imported, bool morphMask, bool flatInbetween); diff --git a/AssetStudioFBX/AssetStudioFBXExporter.cpp b/AssetStudioFBX/AssetStudioFBXExporter.cpp index a824a4e..3fbe8f2 100644 --- a/AssetStudioFBX/AssetStudioFBXExporter.cpp +++ b/AssetStudioFBX/AssetStudioFBXExporter.cpp @@ -474,39 +474,49 @@ namespace AssetStudio pMeshNode->AddMaterial(pMat); bool hasTexture = false; - FbxFileTexture* pTextureDiffuse = ExportTexture(ImportedHelpers::FindTexture(mat->Textures[0], imported->TextureList), pMesh); - if (pTextureDiffuse != NULL) - { - LinkTexture(mat, 0, pTextureDiffuse, pMat->Diffuse); - hasTexture = true; - } - FbxFileTexture* pTextureAmbient = ExportTexture(ImportedHelpers::FindTexture(mat->Textures[1], imported->TextureList), pMesh); - if (pTextureAmbient != NULL) + for each (ImportedMaterialTexture^ texture in mat->Textures) { - LinkTexture(mat, 1, pTextureAmbient, pMat->Ambient); - hasTexture = true; - } - - FbxFileTexture* pTextureEmissive = ExportTexture(ImportedHelpers::FindTexture(mat->Textures[2], imported->TextureList), pMesh); - if (pTextureEmissive != NULL) - { - LinkTexture(mat, 2, pTextureEmissive, pMat->Emissive); - hasTexture = true; - } - - FbxFileTexture* pTextureSpecular = ExportTexture(ImportedHelpers::FindTexture(mat->Textures[3], imported->TextureList), pMesh); - if (pTextureSpecular != NULL) - { - LinkTexture(mat, 3, pTextureSpecular, pMat->Specular); - hasTexture = true; - } - - FbxFileTexture* pTextureBump = ExportTexture(ImportedHelpers::FindTexture(mat->Textures[4], imported->TextureList), pMesh); - if (pTextureBump != NULL) - { - LinkTexture(mat, 4, pTextureBump, pMat->Bump); - hasTexture = true; + if (texture->Dest == 0) + { + FbxFileTexture* pTextureDiffuse = ExportTexture(ImportedHelpers::FindTexture(texture->Name, imported->TextureList)); + if (pTextureDiffuse != NULL) + { + LinkTexture(texture, pTextureDiffuse, pMat->Diffuse); + hasTexture = true; + } + } + else if (texture->Dest == 1) + { + FbxFileTexture* pTextureEmissive = ExportTexture(ImportedHelpers::FindTexture(texture->Name, imported->TextureList)); + if (pTextureEmissive != NULL) + { + LinkTexture(texture, pTextureEmissive, pMat->Emissive); + hasTexture = true; + } + } + else if (texture->Dest == 2) + { + FbxFileTexture* pTextureSpecular = ExportTexture(ImportedHelpers::FindTexture(texture->Name, imported->TextureList)); + if (pTextureSpecular != NULL) + { + LinkTexture(texture, pTextureSpecular, pMat->Specular); + hasTexture = true; + } + } + else if (texture->Dest == 3) + { + FbxFileTexture* pTextureBump = ExportTexture(ImportedHelpers::FindTexture(texture->Name, imported->TextureList)); + if (pTextureBump != NULL) + { + LinkTexture(texture, pTextureBump, pMat->Bump); + hasTexture = true; + } + } + else + { + ExportTexture(ImportedHelpers::FindTexture(texture->Name, imported->TextureList)); + } } if (hasTexture) @@ -650,7 +660,7 @@ namespace AssetStudio return lNode; } - FbxFileTexture* Fbx::Exporter::ExportTexture(ImportedTexture^ matTex, FbxMesh* pMesh) + FbxFileTexture* Fbx::Exporter::ExportTexture(ImportedTexture^ matTex) { FbxFileTexture* pTex = NULL; @@ -714,16 +724,10 @@ namespace AssetStudio return pTex; } - void Fbx::Exporter::LinkTexture(ImportedMaterial^ mat, int attIndex, FbxFileTexture* pTexture, FbxProperty& prop) + void Fbx::Exporter::LinkTexture(ImportedMaterialTexture^ texture, FbxFileTexture* pTexture, FbxProperty& prop) { - if (mat->TexOffsets != nullptr) - { - pTexture->SetTranslation(mat->TexOffsets[attIndex].X, mat->TexOffsets[attIndex].Y); - } - if (mat->TexScales != nullptr) - { - pTexture->SetScale(mat->TexScales[attIndex].X, mat->TexScales[attIndex].Y); - } + pTexture->SetTranslation(texture->Offset.X, texture->Offset.Y); + pTexture->SetScale(texture->Scale.X, texture->Scale.Y); prop.ConnectSrcObject(pTexture); } diff --git a/AssetStudioUtility/ModelConverter.cs b/AssetStudioUtility/ModelConverter.cs index 3f4e111..2a257a7 100644 --- a/AssetStudioUtility/ModelConverter.cs +++ b/AssetStudioUtility/ModelConverter.cs @@ -587,38 +587,32 @@ namespace AssetStudio } //textures - iMat.Textures = new string[5]; - iMat.TexOffsets = new Vector2[5]; - iMat.TexScales = new Vector2[5]; + iMat.Textures = new List(); foreach (var texEnv in mat.m_SavedProperties.m_TexEnvs) { - Texture2D m_Texture2D = null; - if (texEnv.Value.m_Texture.TryGet(out var m_Texture)) //TODO other Texture - { - m_Texture2D = m_Texture; - } - - if (m_Texture2D == null) + if (!texEnv.Value.m_Texture.TryGet(out var m_Texture2D)) //TODO other Texture { continue; } + + var texture = new ImportedMaterialTexture(); + iMat.Textures.Add(texture); + int dest = -1; if (texEnv.Key == "_MainTex") dest = 0; else if (texEnv.Key == "_BumpMap") - dest = 4; - else if (texEnv.Key.Contains("Spec")) - dest = 2; - else if (texEnv.Key.Contains("Norm")) dest = 3; - if (dest < 0 || iMat.Textures[dest] != null) - { - continue; - } + else if (texEnv.Key.Contains("Specular")) + dest = 2; + else if (texEnv.Key.Contains("Normal")) + dest = 1; - if (textureNameDictionary.TryGetValue(m_Texture, out var textureName)) + texture.Dest = dest; + + if (textureNameDictionary.TryGetValue(m_Texture2D, out var textureName)) { - iMat.Textures[dest] = textureName; + texture.Name = textureName; } else if (ImportedHelpers.FindTexture(m_Texture2D.m_Name + ".png", TextureList) != null) //已有相同名字的图片 { @@ -627,20 +621,21 @@ namespace AssetStudio var name = m_Texture2D.m_Name + $" ({i}).png"; if (ImportedHelpers.FindTexture(name, TextureList) == null) { - iMat.Textures[dest] = name; - textureNameDictionary.Add(m_Texture, name); + texture.Name = name; + textureNameDictionary.Add(m_Texture2D, name); break; } } } else { - iMat.Textures[dest] = m_Texture2D.m_Name + ".png"; - textureNameDictionary.Add(m_Texture, iMat.Textures[dest]); + texture.Name = m_Texture2D.m_Name + ".png"; + textureNameDictionary.Add(m_Texture2D, texture.Name); } - iMat.TexOffsets[dest] = texEnv.Value.m_Offset; - iMat.TexScales[dest] = texEnv.Value.m_Scale; - ConvertTexture2D(m_Texture2D, iMat.Textures[dest]); + + texture.Offset = texEnv.Value.m_Offset; + texture.Scale = texEnv.Value.m_Scale; + ConvertTexture2D(m_Texture2D, texture.Name); } MaterialList.Add(iMat);