babylon_vector3 extractColorAndTexture(const FbxPropertyT<FbxDouble3>& colorRGB, const FbxPropertyT<FbxDouble>& factorProp, std::shared_ptr<babylon_texture>& oTexture, TextureFormat desiredTextureFormat, const std::wstring& srcDir, const std::wstring& outdir){ babylon_vector3 color(0,0,0); if (colorRGB.IsValid()){ auto ambient = colorRGB.Get(); float factor = 1; if (factorProp.IsValid()){ factor = (float) factorProp.Get(); } color.x = (float) (ambient[0] * factor); color.y = (float) (ambient[1] * factor); color.z = (float) (ambient[2] * factor); auto texture = colorRGB.GetSrcObject<FbxFileTexture>(); if (texture){ std::string relativeFileName(texture->GetRelativeFileName()); std::wstring wsourceFile(srcDir); if (*wsourceFile.rbegin() != L'\\'){ wsourceFile.push_back(L'\\'); } wsourceFile.append(std::begin(relativeFileName), std::end(relativeFileName)); // check if file exists DWORD attrib = GetFileAttributes(wsourceFile.c_str()); if (attrib != 0xFFFFFFFF) { oTexture.reset(new babylon_texture()); std::transform(relativeFileName.begin(), relativeFileName.end(), relativeFileName.begin(), [](char c){return (char) tolower(c); }); oTexture->name = relativeFileName; oTexture->hasAlpha = texture->GetAlphaSource() != FbxTexture::eNone; TextureFormat sourceTextureFormat = getTextureFormatFromFileName(relativeFileName); if (sourceTextureFormat == desiredTextureFormat){ std::wstring wdestFile(outdir); if (*wdestFile.rbegin() != L'\\'){ wdestFile.push_back(L'\\'); } wdestFile.append(std::wstring(relativeFileName.begin(), relativeFileName.end())); CopyFile(wsourceFile.c_str(), wdestFile.c_str(), true); } else{ changeFileNameForFormat(relativeFileName, desiredTextureFormat); oTexture->name = relativeFileName; std::wstring wdestFile(outdir); if (*wdestFile.rbegin() != L'\\'){ wdestFile.push_back(L'\\'); } wdestFile.append(std::wstring(relativeFileName.begin(), relativeFileName.end())); convertTexture(wsourceFile, wdestFile, sourceTextureFormat, desiredTextureFormat, oTexture->hasAlpha); } } } } return color; }
BTHFBX_VEC3 FBXScene::GetMaterialColor2(FbxPropertyT<FbxDouble3> FBXColorProperty, FbxPropertyT<FbxDouble> FBXFactorProperty) { BTHFBX_VEC3 Color; if( FBXColorProperty.IsValid() ) { FbxDouble3 FBXColor = FBXColorProperty.Get(); Color = BTHFBX_VEC3( (float)FBXColor[0], (float)FBXColor[1], (float)FBXColor[2] ); if( FBXFactorProperty.IsValid() ) { float FBXFactor = (float)FBXFactorProperty.Get(); Color.x *= FBXFactor; Color.y *= FBXFactor; Color.z *= FBXFactor; } } return Color; }
//-------------------------------------------------------------------------------------- void FBXScene::ProcessMaterials(FbxScene* pScene) { for( int i = 0; i < pScene->GetMaterialCount(); ++i ) { Material* pMaterial = new Material(i); FbxSurfaceMaterial* pFBXMaterial = pScene->GetMaterial(i); FbxProperty diffuseTextureProperty = pFBXMaterial->FindProperty(FbxSurfaceMaterial::sDiffuse); if( diffuseTextureProperty.IsValid() ) { FbxFileTexture* pDiffuseTexture = diffuseTextureProperty.GetSrcObject<FbxFileTexture>(0); if( pDiffuseTexture ) { std::string strFileName = pDiffuseTexture->GetFileName(); if( strFileName.length() == 0 ) strFileName = pDiffuseTexture->GetRelativeFileName(); strFileName = GetFileFromPath(strFileName); pMaterial->SetDiffuseTextureName(strFileName); } } FbxProperty normalTextureProperty = pFBXMaterial->FindProperty(FbxSurfaceMaterial::sNormalMap); if( normalTextureProperty.IsValid() ) { FbxFileTexture* pNormalTexture = normalTextureProperty.GetSrcObject<FbxFileTexture>(0); if( pNormalTexture ) { std::string strFileName = pNormalTexture->GetFileName(); if( strFileName.length() == 0 ) strFileName = pNormalTexture->GetRelativeFileName(); strFileName = GetFileFromPath(strFileName); pMaterial->SetNormalTextureName(strFileName); } } FbxSurfaceLambert* pLambert = FbxCast<FbxSurfaceLambert>(pFBXMaterial); FbxSurfacePhong* pPhong = FbxCast<FbxSurfacePhong>(pFBXMaterial); BTHFBX_VEC3 AmbientColor2; BTHFBX_VEC3 EmissiveColor2; BTHFBX_VEC3 DiffuseColor2; BTHFBX_VEC3 SpecularColor2; float fSpecularPower = 1.0f; float fTransparency = 1.0f; if( pLambert ) { AmbientColor2 = GetMaterialColor2(pLambert->Ambient, pLambert->AmbientFactor); EmissiveColor2 = GetMaterialColor2(pLambert->Emissive, pLambert->EmissiveFactor); DiffuseColor2 = GetMaterialColor2(pLambert->Diffuse, pLambert->DiffuseFactor); FbxPropertyT<FbxDouble> FBXTransparencyProperty = pLambert->TransparencyFactor; if( FBXTransparencyProperty.IsValid() ) fTransparency = (float)FBXTransparencyProperty.Get(); } if( pPhong ) { SpecularColor2 = GetMaterialColor2(pPhong->Specular, pPhong->SpecularFactor); FbxPropertyT<FbxDouble> FBXSpecularPowerProperty = pPhong->Shininess; if( FBXSpecularPowerProperty.IsValid() ) fSpecularPower = (float)FBXSpecularPowerProperty.Get(); } pMaterial->SetAmbientColor2(AmbientColor2); pMaterial->SetEmissiveColor2(EmissiveColor2); pMaterial->SetDiffuseColor2(DiffuseColor2); pMaterial->SetSpecularColor2(SpecularColor2); pMaterial->SetSpecularPower(fSpecularPower); pMaterial->SetTransparency(fTransparency); pMaterial->AddTexturePath( GetFilePath(this->mFilename) + "/" ); m_Materials.push_back(pMaterial); m_FBXMaterials.push_back(pFBXMaterial); } }