diff --git a/tools/MayaExporter/MayaExporter/Material.cpp b/tools/MayaExporter/MayaExporter/Material.cpp new file mode 100644 index 00000000..53d029dc --- /dev/null +++ b/tools/MayaExporter/MayaExporter/Material.cpp @@ -0,0 +1,239 @@ +#include "Material.h" + +void Material::grabLambertProperties(MaterialNode& material_node, MFnDependencyNode& node) +{ + material_node.Name = node.name().asChar(); + + if (findColorTexture(material_node, node)) { + material_node.Color.fill(1.0f); + } + else { + m_Plug = node.findPlug("colorR"); + m_Plug.getValue(material_node.Color[0]); + m_Plug = node.findPlug("colorG"); + m_Plug.getValue(material_node.Color[1]); + m_Plug = node.findPlug("colorB"); + m_Plug.getValue(material_node.Color[2]); + + float TempTransp[3]; + m_Plug = node.findPlug("transparencyR"); + m_Plug.getValue(TempTransp[0]); + m_Plug = node.findPlug("transparencyG"); + m_Plug.getValue(TempTransp[1]); + m_Plug = node.findPlug("transparencyB"); + m_Plug.getValue(TempTransp[2]); + + MColor TranspNode(TempTransp[0], TempTransp[1], TempTransp[2]); + float DummyH, DummyS; + TranspNode.get(MColor::kHSV, DummyH, DummyS, material_node.Color[3]); + } + + if (findIncandescenceTexture(material_node, node)) { + material_node.Incandescence.fill(1.0f); + } + else { + m_Plug = node.findPlug("incandescenceR"); + m_Plug.getValue(material_node.Incandescence[0]); + m_Plug = node.findPlug("incandescenceG"); + m_Plug.getValue(material_node.Incandescence[1]); + m_Plug = node.findPlug("incandescenceB"); + m_Plug.getValue(material_node.Incandescence[2]); + } + + findNormalTexture(material_node, node); +} + +void Material::grabBlinnProperties(MaterialNode& material_node, MFnDependencyNode& node) +{ + if (findSpecularTexture(material_node, node)) { + material_node.Specular.fill(1.0f); + } + else { + m_Plug = node.findPlug("specularColorR"); + m_Plug.getValue(material_node.Specular[0]); + m_Plug = node.findPlug("specularColorG"); + m_Plug.getValue(material_node.Specular[1]); + m_Plug = node.findPlug("specularColorB"); + m_Plug.getValue(material_node.Specular[2]); + } + + m_Plug = node.findPlug("reflectivity"); + m_Plug.getValue(material_node.ReflectionFactor); + + m_Plug = node.findPlug("eccentricity"); + float TempEccent; + m_Plug.getValue(TempEccent); + + // Blinn works differently from Phong which is used in-game. + // This is some magic numbers and math to make a conversion estimate between the two. + // There is no exact conversion between the two, so there are errors. + + // Phong min/max is around Blinn 0.7/0.1 + TempEccent = std::max(std::min(TempEccent, 0.7f), 0.1f); + + material_node.SpecularExponent = std::max(std::min(((2.66f) + (427.0f) * exp((-14.8f) * TempEccent)), 100.0f), 2.0f); +} + +void Material::grabPhongProperties(MaterialNode& material_node, MFnDependencyNode& node) +{ + if (findSpecularTexture(material_node, node)) { + material_node.Specular.fill(1.0f); + } + else { + m_Plug = node.findPlug("specularColorR"); + m_Plug.getValue(material_node.Specular[0]); + m_Plug = node.findPlug("specularColorG"); + m_Plug.getValue(material_node.Specular[1]); + m_Plug = node.findPlug("specularColorB"); + m_Plug.getValue(material_node.Specular[2]); + } + + m_Plug = node.findPlug("reflectivity"); + m_Plug.getValue(material_node.ReflectionFactor); + + m_Plug = node.findPlug("cosinePower "); + m_Plug.getValue(material_node.SpecularExponent); +} + +bool Material::findColorTexture(MaterialNode& material_node, MFnDependencyNode& node) +{ + MPlugArray AllConnections; + + m_Plug = node.findPlug("color", true); + m_Plug.connectedTo(AllConnections, true, false); + + for (int i = 0; i < AllConnections.length(); i++) { + if (AllConnections[i].node().hasFn(MFn::kFileTexture)) { + MFnDependencyNode TextureNode(AllConnections[i].node()); + + std::string FullPath = TextureNode.findPlug("ftn").asString().asChar(); + m_TexturePaths.push_back(FullPath); + + material_node.ColorMapFile = FullPath.substr(FullPath.find_last_of("/")); + + return true; + } + } + + return false; +} + +bool Material::findNormalTexture(MaterialNode& material_node, MFnDependencyNode& node) +{ + MPlugArray AllConnections; + MPlugArray AllBumpConnections; + + m_Plug = node.findPlug("normalCamera", true); + m_Plug.connectedTo(AllConnections, true, false); + + for (int i = 0; i < AllConnections.length(); i++) { + if (AllConnections[i].node().apiType() == MFn::kBump) { + MFnDependencyNode BumpNode(AllConnections[i].node()); + + BumpNode.findPlug("bumpValue").connectedTo(AllBumpConnections, true, false); + for (int j = 0; j < AllBumpConnections.length(); j++) { + if (AllBumpConnections[j].node().hasFn(MFn::kFileTexture)) { + MFnDependencyNode TextureNode(AllBumpConnections[j].node()); + + std::string FullPath = TextureNode.findPlug("ftn").asString().asChar(); + m_TexturePaths.push_back(FullPath); + + material_node.NormalMapFile = FullPath.substr(FullPath.find_last_of("/")); + + return true; + } + } + } + } + + return false; +} + +bool Material::findSpecularTexture(MaterialNode& material_node, MFnDependencyNode& node) +{ + MPlugArray AllConnections; + + m_Plug = node.findPlug("specularColor", true); + m_Plug.connectedTo(AllConnections, true, false); + + for (int i = 0; i < AllConnections.length(); i++) { + if (AllConnections[i].node().hasFn(MFn::kFileTexture)) { + MFnDependencyNode TextureNode(AllConnections[i].node()); + + std::string FullPath = TextureNode.findPlug("ftn").asString().asChar(); + m_TexturePaths.push_back(FullPath); + + material_node.SpecularMapFile = FullPath.substr(FullPath.find_last_of("/")); + + return true; + } + } + return false; +} + +bool Material::findIncandescenceTexture(MaterialNode& material_node, MFnDependencyNode& node) +{ + MPlugArray AllConnections; + + m_Plug = node.findPlug("incandescence", true); + m_Plug.connectedTo(AllConnections, true, false); + + for (int i = 0; i < AllConnections.length(); i++) { + if (AllConnections[i].node().hasFn(MFn::kFileTexture)) { + MFnDependencyNode TextureNode(AllConnections[i].node()); + + std::string FullPath = TextureNode.findPlug("ftn").asString().asChar(); + m_TexturePaths.push_back(FullPath); + + material_node.SpecularMapFile = FullPath.substr(FullPath.find_last_of("/")); + + return true; + } + } + return false; +} + +// Returns the absolute path for all textures. Use for copying texture files. +std::vector* Material::TexturePaths() +{ + return &m_TexturePaths; +} + +// Traverse the DAG and grab all the materials +std::vector* Material::DoIt() +{ + // All materials we care about inherit from Lambert + MItDependencyNodes matIt(MFn::kLambert); + + while (!matIt.isDone()) { + MFnDependencyNode MaterialFnDN(matIt.thisNode()); + MaterialNode MaterialStorage; + + if (matIt.thisNode().hasFn(MFn::kPhong)) { + grabLambertProperties(MaterialStorage, MaterialFnDN); + grabPhongProperties(MaterialStorage, MaterialFnDN); + + m_AllMaterials.push_back(MaterialStorage); + } + else if (matIt.thisNode().hasFn(MFn::kBlinn)) { + grabLambertProperties(MaterialStorage, MaterialFnDN); + grabBlinnProperties(MaterialStorage, MaterialFnDN); + + m_AllMaterials.push_back(MaterialStorage); + } + else if (matIt.thisNode().hasFn(MFn::kLambert)) { + grabLambertProperties(MaterialStorage, MaterialFnDN); + + m_AllMaterials.push_back(MaterialStorage); + + MaterialStorage.Specular.fill(0.0f); + MaterialStorage.ReflectionFactor = 0.0f; + MaterialStorage.SpecularExponent = 0.0f; + } + + matIt.next(); + } + + return &m_AllMaterials; +} + diff --git a/tools/MayaExporter/MayaExporter/Material.h b/tools/MayaExporter/MayaExporter/Material.h new file mode 100644 index 00000000..1685453d --- /dev/null +++ b/tools/MayaExporter/MayaExporter/Material.h @@ -0,0 +1,46 @@ +#ifndef Material_Material_h__ +#define Material_Material_h__ + +#include +#include +#include +#include +#include "MayaIncludes.h" + +struct MaterialNode +{ + std::string Name; + std::array Color; + std::array Incandescence; + std::array Specular; + float ReflectionFactor; + float SpecularExponent; + std::string ColorMapFile; + std::string SpecularMapFile; + std::string NormalMapFile; + std::string IncandescenceMapFile; +}; + +class Material +{ +public: + Material() {}; + ~Material() {}; + std::vector* DoIt(); + std::vector* TexturePaths(); +private: + MPlug m_Plug; + + std::vector m_AllMaterials; + std::vector m_TexturePaths; + + bool findColorTexture(MaterialNode& material_node, MFnDependencyNode& node); + bool findNormalTexture(MaterialNode& material_node, MFnDependencyNode& node); + bool findSpecularTexture(MaterialNode& material_node, MFnDependencyNode& node); + bool findIncandescenceTexture(MaterialNode& material_node, MFnDependencyNode& node); + void grabLambertProperties(MaterialNode& material_node, MFnDependencyNode& node); + void grabBlinnProperties(MaterialNode& material_node, MFnDependencyNode& node); + void grabPhongProperties(MaterialNode& material_node, MFnDependencyNode& node); +}; + +#endif // Material_Material_h__ \ No newline at end of file diff --git a/tools/MayaExporter/MayaExporter/MayaExporter.vcxproj b/tools/MayaExporter/MayaExporter/MayaExporter.vcxproj index 42b1b45a..c8ce591c 100644 --- a/tools/MayaExporter/MayaExporter/MayaExporter.vcxproj +++ b/tools/MayaExporter/MayaExporter/MayaExporter.vcxproj @@ -152,6 +152,7 @@ + true @@ -213,6 +214,7 @@ $(QTDIR)\bin\moc.exe;%(FullPath);$(QTDIR)\bin\moc.exe;%(FullPath) + diff --git a/tools/MayaExporter/MayaExporter/MayaExporter.vcxproj.filters b/tools/MayaExporter/MayaExporter/MayaExporter.vcxproj.filters index d5bcfd00..4719620b 100644 --- a/tools/MayaExporter/MayaExporter/MayaExporter.vcxproj.filters +++ b/tools/MayaExporter/MayaExporter/MayaExporter.vcxproj.filters @@ -50,6 +50,9 @@ Source Files + + Source Files + @@ -69,5 +72,8 @@ Generated Files + + Header Files + \ No newline at end of file diff --git a/tools/MayaExporter/MayaExporter/MayaIncludes.h b/tools/MayaExporter/MayaExporter/MayaIncludes.h index 3573d30d..6c092052 100644 --- a/tools/MayaExporter/MayaExporter/MayaIncludes.h +++ b/tools/MayaExporter/MayaExporter/MayaIncludes.h @@ -30,6 +30,7 @@ #include #include #include +#include // Wrappers diff --git a/tools/MayaExporter/MayaExporter/Menu.cpp b/tools/MayaExporter/MayaExporter/Menu.cpp index 6af300aa..5d8af1d4 100644 --- a/tools/MayaExporter/MayaExporter/Menu.cpp +++ b/tools/MayaExporter/MayaExporter/Menu.cpp @@ -141,9 +141,9 @@ void Menu::CancelClicked(bool) void Menu::Button1Clicked(bool) { if(exportAnimationsButton->isChecked()) - cout << "1 checked!" << endl; + MGlobal::displayInfo("1 checked!"); else - cout << "1 unchecked!" << endl; + MGlobal::displayInfo("1 unchecked!"); } void Menu::Button2Clicked(bool) @@ -222,11 +222,16 @@ void Menu::GetMeshData(MObject object) } -void Menu::exportMaterial(MObject object) +void Menu::GetMaterialData() { - MItDependencyNodes matIt(MFn::kLambert); + this->MaterialHandler = new Material(); + // Traverse scene and return vector with all materials + std::vector* AllMaterials = MaterialHandler->DoIt(); + // Access the colorR component of one material (example) + cout << AllMaterials->at(0).Color[0] << endl; + MGlobal::displayInfo(MString() + AllMaterials->at(0).Color[0]); } Menu::~Menu() @@ -236,4 +241,5 @@ Menu::~Menu() //delete exportPath; //delete fileDialog; fileDialog->~QFileDialog(); + delete MaterialHandler; } \ No newline at end of file diff --git a/tools/MayaExporter/MayaExporter/Menu.h b/tools/MayaExporter/MayaExporter/Menu.h index 44eca702..b2731c0a 100644 --- a/tools/MayaExporter/MayaExporter/Menu.h +++ b/tools/MayaExporter/MayaExporter/Menu.h @@ -5,6 +5,7 @@ #include #include "MayaIncludes.h" +#include "Material.h" // Qt #pragma comment(lib, "QtCore4") #pragma comment(lib, "QtGui4") @@ -46,7 +47,7 @@ public: ~Menu(); void GetMeshData(MObject object); - void exportMaterial(MObject object); + void GetMaterialData(); private slots: void ExportSelected(bool checked); @@ -74,6 +75,7 @@ private: QFileDialog* fileDialog; QDialog* dialogPointer; + Material* MaterialHandler; }; #endif \ No newline at end of file