void FBXSceneEncoder::loadMaterialTextures(FbxSurfaceMaterial* fbxMaterial, Material* material) { FbxProperty fbxProperty; int textureIndex; FBXSDK_FOR_EACH_TEXTURE(textureIndex) { fbxProperty = fbxMaterial->FindProperty(FbxLayerElement::sTextureChannelNames[textureIndex]); //FindAndDisplayTextureInfoByProperty(fbxProperty, lDisplayHeader, lMaterialIndex); if ( fbxProperty.IsValid() ) { int textureCount = fbxProperty.GetSrcObjectCount<FbxTexture>(); for (int j = 0; j < textureCount; ++j) { FbxLayeredTexture *layeredTexture = fbxProperty.GetSrcObject<FbxLayeredTexture>(j); if (layeredTexture) { //DisplayInt(" Layered Texture: ", j); FbxLayeredTexture *layeredTexture = fbxProperty.GetSrcObject<FbxLayeredTexture>(j); int lNbTextures = layeredTexture->GetSrcObjectCount<FbxTexture>(); for (int k = 0; k<lNbTextures; ++k) { FbxTexture* fbxTexture = layeredTexture->GetSrcObject<FbxTexture>(k); if (fbxTexture) { /* if (pDisplayHeader){ DisplayInt(" Textures connected to Material ", pMaterialIndex); pDisplayHeader = false; } */ //NOTE the blend mode is ALWAYS on the LayeredTexture and NOT the one on the texture. //Why is that? because one texture can be shared on different layered textures and might //have different blend modes. FbxLayeredTexture::EBlendMode lBlendMode; layeredTexture->GetTextureBlendMode(k, lBlendMode); //DisplayString(" Textures for ", pProperty.GetName()); //DisplayInt(" Texture ", k); //DisplayTextureInfo(fbxTexture, (int) lBlendMode); } } } else if (FbxTexture* fbxTexture = fbxProperty.GetSrcObject<FbxTexture>(j)) { //no layered texture simply get on the property if (FbxFileTexture* fileTexture = FbxCast<FbxFileTexture>(fbxTexture)) { loadMaterialFileTexture(fileTexture, material); } } } } } }
std::vector<std::string>* findAndGetTextureInfoByProperty(FbxProperty fbxTextureProperty, int midx) { std::vector<std::string>* textureFileNames = new std::vector<std::string>(); if( fbxTextureProperty.IsValid() ) { int numTextures = fbxTextureProperty.GetSrcObjectCount<FbxTexture>(); for (int j = 0; j < numTextures; ++j) { // Here we have to check if it's layeredtextures, or just textures: FbxLayeredTexture *fbxLayeredTexture = fbxTextureProperty.GetSrcObject<FbxLayeredTexture>(j); if (fbxLayeredTexture) { int numLayeredTextures = fbxLayeredTexture->GetSrcObjectCount<FbxTexture>(); for (int k = 0; k < numLayeredTextures; ++k) { FbxTexture* fbxTexture = fbxLayeredTexture->GetSrcObject<FbxTexture>(k); if (fbxTexture) { // NOTE the blend mode is ALWAYS on the LayeredTexture and NOT the one on the texture. // Why is that? because one texture can be shared on different layered textures and might // have different blend modes. FbxLayeredTexture::EBlendMode lBlendMode; fbxLayeredTexture->GetTextureBlendMode(k, lBlendMode); std::string textureFileName = getTextureFileName(fbxTexture); textureFileNames->push_back(textureFileName); } } } else { // No layered texture simply get on the property FbxTexture* fbxTexture = fbxTextureProperty.GetSrcObject<FbxTexture>(j); if (fbxTexture) { // Display connected Material header only at the first time std::string textureFileName = getTextureFileName(fbxTexture); textureFileNames->push_back(textureFileName); } } } } return textureFileNames; }
//------------------------------------------------------------------------------------------------------------------------------------------- void transferTextureByProperty(FbxProperty fbxProperty, int matId, Material* modelMaterial){ if (fbxProperty.IsValid()){ int textureCount = fbxProperty.GetSrcObjectCount<FbxTexture>(); for (int texId = 0; texId < textureCount; texId++){ FbxLayeredTexture* layeredTexture = fbxProperty.GetSrcObject<FbxLayeredTexture>(texId); if (layeredTexture){ int numTexture = layeredTexture->GetSrcObjectCount<FbxTexture>(); for (int k = 0; k < numTexture; k++){ FbxTexture* texture = layeredTexture->GetSrcObject<FbxTexture>(k); if (texture){ LOG_DEBUG << " Textures connected to material " << matId; Texture* modelTexture = new Texture(); FbxLayeredTexture::EBlendMode blendMode; layeredTexture->GetTextureBlendMode(k, blendMode); transferTextureInfo(texture, (int)blendMode, modelTexture); modelMaterial->Textures.push_back(modelTexture); } } } else { // There is no layered texture FbxTexture* texture = fbxProperty.GetSrcObject<FbxTexture>(texId); if (texture){ LOG_DEBUG << " Textures connected to material " << matId; Texture* modelTexture = new Texture(); transferTextureInfo(texture, -1, modelTexture); modelMaterial->Textures.push_back(modelTexture); } } } } }
void UnFbx::FFbxImporter::ImportTexturesFromNode(FbxNode* Node) { FbxProperty Property; int32 NbMat = Node->GetMaterialCount(); // visit all materials int32 MaterialIndex; for (MaterialIndex = 0; MaterialIndex < NbMat; MaterialIndex++) { FbxSurfaceMaterial *Material = Node->GetMaterial(MaterialIndex); //go through all the possible textures if(Material) { int32 TextureIndex; FBXSDK_FOR_EACH_TEXTURE(TextureIndex) { Property = Material->FindProperty(FbxLayerElement::sTextureChannelNames[TextureIndex]); if( Property.IsValid() ) { FbxTexture * lTexture= NULL; //Here we have to check if it's layered textures, or just textures: int32 LayeredTextureCount = Property.GetSrcObjectCount(FbxLayeredTexture::ClassId); FbxString PropertyName = Property.GetName(); if(LayeredTextureCount > 0) { for(int32 LayerIndex=0; LayerIndex<LayeredTextureCount; ++LayerIndex) { FbxLayeredTexture *lLayeredTexture = FbxCast <FbxLayeredTexture>(Property.GetSrcObject(FbxLayeredTexture::ClassId, LayerIndex)); int32 NbTextures = lLayeredTexture->GetSrcObjectCount(FbxTexture::ClassId); for(int32 TexIndex =0; TexIndex<NbTextures; ++TexIndex) { FbxFileTexture* Texture = FbxCast <FbxFileTexture> (lLayeredTexture->GetSrcObject(FbxFileTexture::ClassId,TexIndex)); if(Texture) { ImportTexture(Texture, PropertyName == FbxSurfaceMaterial::sNormalMap || PropertyName == FbxSurfaceMaterial::sBump); } } } } else { //no layered texture simply get on the property int32 NbTextures = Property.GetSrcObjectCount(FbxTexture::ClassId); for(int32 TexIndex =0; TexIndex<NbTextures; ++TexIndex) { FbxFileTexture* Texture = FbxCast <FbxFileTexture> (Property.GetSrcObject(FbxFileTexture::ClassId,TexIndex)); if(Texture) { ImportTexture(Texture, PropertyName == FbxSurfaceMaterial::sNormalMap || PropertyName == FbxSurfaceMaterial::sBump); } } } } } }//end if(Material) }// end for MaterialIndex }
void Tools::DisplayMaterial::DisplayMaterial( FbxGeometry *i_geometry ) { DisplayCommon::DisplayString( "\n\n--------------------\nMaterial\n--------------------" ); int materialCount = 0; FbxNode *node = NULL; if( i_geometry ) { node = i_geometry->GetNode(); if( node ) materialCount = node->GetMaterialCount(); } if( materialCount > 0 ) { FbxPropertyT<FbxDouble3> double3; FbxPropertyT<FbxDouble> double1; FbxColor theColor; for( int ctr = 0; ctr < materialCount; ctr++ ) { DisplayCommon::DisplayInt( " Material ", ctr ); FbxSurfaceMaterial *material = node->GetMaterial( ctr ); DisplayCommon::DisplayString( " Name: \"", (char *) material->GetName(), "\"" ); #ifdef DISPLAY_HARDWARE_SHADER_INFORMATION //Get the implementation to see if it's a hardware shader. // Note:: this cause memory leak const FbxImplementation* implementation = GetImplementation( material, FBXSDK_IMPLEMENTATION_HLSL ); FbxString implementationType = "HLSL"; if( !implementation ) { implementation = GetImplementation( material, FBXSDK_IMPLEMENTATION_CGFX ); implementationType = "CGFX"; } if( implementation ) { //Now we have a hardware shader, let's read it FBXSDK_printf( " Hardware Shader Type: %s\n", implemenationType.Buffer() ); DisplayCommon::DisplayString( " Hardware Shader Type: ", implemenationType ); const FbxBindingTable* rootTable = implementation->GetRootTable(); FbxString fileName = rootTable->DescAbsoluteURL.Get(); FbxString techniqueName = rootTable->DescTAG.Get(); const FbxBindingTable* table = implementation->GetRootTable(); size_t entryNum = table->GetEntryCount(); for( int i = 0; i < (int)entryNum; i++ ) { const FbxBindingTableEntry& entry = table->GetEntry( i ); const char *entrySrcType = entry.GetEntryType( true ); FbxProperty fbxProp; FbxString test = entry.GetSource(); FBXSDK_printf( " Entry: %s\n", test.Buffer() ); DisplayCommon::DisplayString( " Entry: %s\n", test ); if ( strcmp( FbxPropertyEntryView::sEntryType, entrySrcType ) == 0 ) { fbxProp = material->FindPropertyHierarchical(entry.GetSource()); if( !fbxProp.IsValid() ) { fbxProp = material->RootProperty.FindHierarchical( entry.GetSource() ); } } else if( strcmp( FbxConstantEntryView::sEntryType, entrySrcType ) == 0 ) { fbxProp = implementation->GetConstants().FindHierarchical( entry.GetSource() ); } if( fbxProp.IsValid() ) { if( fbxProp.GetSrcObjectCount<FbxTexture>() > 0 ) { //do what you want with the textures for( int j = 0; j < fbxProp.GetSrcObjectCount<FbxFileTexture>(); j++ ) { FbxFileTexture *tex = fbxProp.GetSrcObject<FbxFileTexture>( j ); FBXSDK_printf( " File Texture: %s\n", tex->GetFileName() ); DisplayCommon::DisplayString( " File Texture: %s\n", tex->GetFileName() ); } for( int j = 0; j < fbxProp.GetSrcObjectCount<FbxLayeredTexture>(); j++ ) { FbxLayeredTexture *tex = fbxProp.GetSrcObject<FbxLayeredTexture>( j ); FBXSDK_printf( " Layered Texture: %s\n", tex->GetName() ); DisplayCommon::DisplayString( " Layered Texture: %s\n", tex->GetName() ); } for( int j = 0; j < fbxProp.GetSrcObjectCount<FbxProceduralTexture>(); j++ ) { FbxProceduralTexture *tex = fbxProp.GetSrcObject<FbxProceduralTexture>( j ); FBXSDK_printf( " Procedural Texture: %s\n", tex->GetName() ); DisplayCommon::DisplayString( " Procedural Texture: %s\n", tex->GetName() ); } } else { FbxDataType fbxType = fbxProp.GetPropertyDataType(); FbxString fbxName = fbxType.GetName(); if( FbxBoolDT == fbxType ) { DisplayCommon::DisplayBool( " Bool: ", fbxProp.Get<FbxBool>() ); } else if( FbxIntDT == fbxType || FbxEnumDT == fbxType ) { DisplayCommon::DisplayInt( " Int: ", fbxProp.Get<FbxInt>() ); } else if( FbxFloatDT == fbxType ) { DisplayCommon::DisplayDouble( " Float: ", fbxProp.Get<FbxFloat>() ); } else if( FbxDoubleDT == fbxType ) { DisplayCommon::DisplayDouble( " Double: ", fbxProp.Get<FbxDouble>() ); } else if( FbxStringDT == fbxType || FbxUrlDT == fbxType || FbxXRefUrlDT == fbxType ) { DisplayCommon::DisplayString( " String: ", fbxProp.Get<FbxString>().Buffer() ); } else if( FbxDouble2DT == fbxType ) { FbxDouble2 double2 = fbxProp.Get<FbxDouble2>(); FbxVector2 vector; vector[0] = double2[0]; vector[1] = double2[1]; DisplayCommon::Display2DVector( " 2D vector: ", vector ); } else if( (FbxDouble3DT == fbxType) || (FbxColor3DT == fbxType) ) { FbxDouble3 double3 = fbxProp.Get<FbxDouble3>(); FbxVector4 vector; vector[0] = double3[0]; vector[1] = double3[1]; vector[2] = double3[2]; DisplayCommon::Display3DVector( " 3D vector: ", vector ); } else if( (FbxDouble4DT == fbxType) || (FbxColor4DT == fbxType) ) { FbxDouble4 double4 = fbxProp.Get<FbxDouble4>(); FbxVector4 vector; vector[0] = double4[0]; vector[1] = double4[1]; vector[2] = double4[2]; vector[3] = double4[3]; DisplayCommon::Display4DVector( " 4D vector: ", vector ); } else if( FbxDouble4x4DT == fbxType ) { FbxDouble4x4 double44 = fbxProp.Get<FbxDouble4x4>(); for( int j = 0; j < 4; j++ ) { FbxVector4 vector; vector[0] = double44[j][0]; vector[1] = double44[j][1]; vector[2] = double44[j][2]; vector[3] = double44[j][3]; DisplayCommon::Display4DVector( " 4x4D vector: ", vector ); } } } } } } else #endif // #ifdef DISPLAY_HARDWARE_SHADER_INFORMATION if( material->GetClassId().Is(FbxSurfacePhong::ClassId) ) { // We found a Phong material. Display its properties. // Display the Ambient Color double3 = ((FbxSurfacePhong *) material)->Ambient; theColor.Set( double3.Get()[0], double3.Get()[1], double3.Get()[2] ); DisplayCommon::DisplayColor( " Ambient: ", theColor ); // Display the Diffuse Color double3 = ((FbxSurfacePhong *) material)->Diffuse; theColor.Set( double3.Get()[0], double3.Get()[1], double3.Get()[2] ); DisplayCommon::DisplayColor( " Diffuse: ", theColor ); // Display the Specular Color (unique to Phong materials) double3 = ((FbxSurfacePhong *) material)->Specular; theColor.Set( double3.Get()[0], double3.Get()[1], double3.Get()[2] ); DisplayCommon::DisplayColor( " Specular: ", theColor ); // Display the Emissive Color double3 = ((FbxSurfacePhong *) material)->Emissive; theColor.Set( double3.Get()[0], double3.Get()[1], double3.Get()[2] ); DisplayCommon::DisplayColor( " Emissive: ", theColor ); //Opacity is Transparency factor now double1 = ((FbxSurfacePhong *) material)->TransparencyFactor; DisplayCommon::DisplayDouble( " Opacity: ", 1.0-double1.Get() ); // Display the Shininess double1 = ((FbxSurfacePhong *) material)->Shininess; DisplayCommon::DisplayDouble( " Shininess: ", double1.Get() ); // Display the Reflectivity double1 = ((FbxSurfacePhong *) material)->ReflectionFactor; DisplayCommon::DisplayDouble( " Reflectivity: ", double1.Get() ); } else if( material->GetClassId().Is(FbxSurfaceLambert::ClassId) ) { // We found a Lambert material. Display its properties. // Display the Ambient Color double3 = ((FbxSurfaceLambert *)material)->Ambient; theColor.Set( double3.Get()[0], double3.Get()[1], double3.Get()[2] ); DisplayCommon::DisplayColor( " Ambient: ", theColor ); // Display the Diffuse Color double3 = ((FbxSurfaceLambert *)material)->Diffuse; theColor.Set( double3.Get()[0], double3.Get()[1], double3.Get()[2] ); DisplayCommon::DisplayColor( " Diffuse: ", theColor ); // Display the Emissive double3 = ((FbxSurfaceLambert *)material)->Emissive; theColor.Set( double3.Get()[0], double3.Get()[1], double3.Get()[2] ); DisplayCommon::DisplayColor( " Emissive: ", theColor ); // Display the Opacity double1 = ((FbxSurfaceLambert *)material)->TransparencyFactor; DisplayCommon::DisplayDouble( " Opacity: ", 1.0-double1.Get() ); } else DisplayCommon::DisplayString( "Unknown type of Material" ); FbxPropertyT<FbxString> string; string = material->ShadingModel; DisplayCommon::DisplayString( " Shading Model: ", string.Get() ); } } }