void FbxLoader::FbxLoader::ProcessMaterialTexture2D(FbxSurfaceMaterial* mat, FMaterial& material) { int Texture2DIndex = 0; FbxProperty prop; FBXSDK_FOR_EACH_TEXTURE(Texture2DIndex) { prop = mat->FindProperty(FbxLayerElement::sTextureChannelNames[Texture2DIndex]); if(prop.IsValid()) { const int Texture2DCount = prop.GetSrcObjectCount<FbxTexture>(); for(int i = 0; i < Texture2DCount; i++) { FbxTexture* texture = prop.GetSrcObject<FbxTexture>(i); if(texture) { std::string textureType = prop.GetNameAsCStr(); FbxFileTexture* fileTexture = FbxCast<FbxFileTexture>(texture); if(fileTexture) { if(textureType == "DiffuseColor") material.diffuseMapName = fileTexture->GetRelativeFileName(); else if(textureType == "SpecularColor") material.specularMapName = fileTexture->GetRelativeFileName(); else if(textureType == "NormalMap") { material.normalMapName = fileTexture->GetRelativeFileName(); useNormalMap = true; } } } } const int layeredTextureCount = prop.GetSrcObjectCount<FbxLayeredTexture>(); for(int i = 0; i < layeredTextureCount; i++) { auto layeredTexture = prop.GetSrcObject<FbxLayeredTexture>(i); const int fileCount = layeredTexture->GetSrcObjectCount<FbxFileTexture>(); for(int j = 0; j < fileCount; j++) { auto fileTexture = layeredTexture->GetSrcObject<FbxFileTexture>(j); std::string Texture2DType = prop.GetNameAsCStr(); if(fileTexture) { if(Texture2DType == "DiffuseColor") material.diffuseMapName = fileTexture->GetRelativeFileName(); else if(Texture2DType == "SpecularColor") material.specularMapName = fileTexture->GetRelativeFileName(); else if(Texture2DType == "NormalMap") { material.normalMapName = fileTexture->GetRelativeFileName(); useNormalMap = true; } } } } } } if(material.diffuseMapName != "") { std::string n = FbxPathUtils::GetFileName(material.diffuseMapName.c_str(), false).Buffer(); material.name = n; mat->SetName(n.c_str()); } }
const char* FbxMaterialStore::RegisterMaterial(FbxSurfaceMaterial* material){ auto found = _materials.find(material); if (found != _materials.end()){ return found->second.id.c_str(); } babylon_material result; if (material){ result.id = std::to_string(_materials.size()); result.id.append(material->GetName()); result.name = result.id; result.ambient = extractColorAndTexture(material->FindProperty(FbxSurfaceMaterial::sAmbient), material->FindProperty(FbxSurfaceMaterial::sAmbientFactor), result.ambientTexture, _textureFormat, _inputFolder, _outputFolder); result.diffuse = extractColorAndTexture(material->FindProperty(FbxSurfaceMaterial::sDiffuse), material->FindProperty(FbxSurfaceMaterial::sDiffuseFactor), result.diffuseTexture, _textureFormat, _inputFolder, _outputFolder); result.emissive = extractColorAndTexture(material->FindProperty(FbxSurfaceMaterial::sEmissive), material->FindProperty(FbxSurfaceMaterial::sEmissiveFactor), result.emissiveTexture, _textureFormat, _inputFolder, _outputFolder); if (material->FindProperty(FbxSurfaceMaterial::sNormalMap).IsValid()){ auto texture = material->FindProperty(FbxSurfaceMaterial::sNormalMap).GetSrcObject<FbxFileTexture>(); if (texture){ result.bumpTexture.reset(new babylon_texture()); result.bumpTexture->name = texture->GetRelativeFileName(); result.bumpTexture->hasAlpha = texture->GetAlphaSource() != FbxTexture::eNone; } } result.alpha = 1; result.backFaceCulling = true; result.specular.x = 0; result.specular.y = 0; result.specular.z = 0; result.specularPower = 0; result.specular = extractColorAndTexture(material->FindProperty(FbxSurfaceMaterial::sSpecular), material->FindProperty(FbxSurfaceMaterial::sSpecularFactor), result.specularTexture, _textureFormat, _inputFolder, _outputFolder); if (material->FindProperty(FbxSurfaceMaterial::sShininess).IsValid()){ result.specularPower = (float) material->FindProperty(FbxSurfaceMaterial::sShininess).Get<FbxDouble>(); } } else{ result.name = "default material"; result.id = std::to_string(_materials.size()); result.id.append(result.name); result.ambient = babylon_vector3(0.05f, 0.05f, 0.05f); result.diffuse = babylon_vector3(0.8f, 0.8f, 0.8f); result.emissive = babylon_vector3(0.0f, 0.0f, 0.0f); result.alpha = 1; result.backFaceCulling = true; result.specular.x = 1; result.specular.y = 1; result.specular.z = 1; result.specularPower = 8; } return _materials.insert(std::pair<FbxSurfaceMaterial*, babylon_material>(material, result)).first->second.id.c_str(); }
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; }
// //////////////////////////////////////////////////////////////////////////////// // @global 将文件写入至 zip // void WriteFileToZip(WIN32_FIND_DATAW ffd, zipFile& zf, LPCWSTR wzFullPath, LPCWSTR wzParentPath, PBYTE buf) { FILE * fin; zip_fileinfo zi = {0}; FILETIME ftLocal; FileTimeToLocalFileTime(&(ffd.ftLastWriteTime),&ftLocal); FileTimeToDosDateTime(&ftLocal,((LPWORD)&zi.dosDate)+1,((LPWORD)&zi.dosDate)+0); WCHAR wzRelativeName[MAX_PATH] = {0}; GetRelativeFileName(wzFullPath, wzParentPath, wzRelativeName); CHAR szRelativeName[1024] = {0}; WideCharToMultiByte(CP_ACP, 0, wzRelativeName, -1, szRelativeName, 1024, NULL, NULL); int err = zipOpenNewFileInZip3_64( zf, szRelativeName, &zi, NULL,0,NULL,0,NULL, Z_DEFLATED, 9,0, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY, NULL, 0, 0); if ( ZIP_OK != err ) { DebugTools::OutputDebugPrintfW( L"[ZipTools] [AddFileToZip] Error In Opening File [%s] \r\n", wzRelativeName); } fin = _wfopen(wzRelativeName, L"rb"); if ( NULL == fin ) { DebugTools::OutputDebugPrintfW( L"[ZipTools] [AddFileToZip] Error In Opening %s For Reading.\r\n", wzRelativeName); } unsigned long size_read = 0; unsigned long size_buf = WRITEBUFFERSIZE; do { err = ZIP_OK; size_read = (int)fread(buf,1,size_buf,fin); if ( size_read > 0 ) { err = zipWriteInFileInZip (zf,buf,size_read); if ( err < 0 ) { DebugTools::OutputDebugPrintfW( L"[ZipTools] [AddFileToZip] Error In Writing %s In the Zipfile.\r\n", wzRelativeName); } } } while ((err == ZIP_OK) && (size_read>0)); fclose(fin); zipCloseFileInZip(zf); }