Jump to content
  • Advertisement
Sign in to follow this  
Alundra

Unity [FBX] Material Index problem

This topic is 1730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I have a problem to get good material index with FBX, the problem is not the FBX because Unity open it good.

The problem is not the UV because I have rendered them and I see good UV on the mesh.

I show you the code I use to get the material index :

int GetMappingIndex( const FbxLayerElement::EMappingMode& MappingMode, const int PolygonIndex, const int PolygonVertexIndex, const int VertexIndex )
{
  switch( MappingMode )
  {
    case FbxLayerElement::eAllSame         : return 0;
    case FbxLayerElement::eByControlPoint  : return VertexIndex;
    case FbxLayerElement::eByPolygonVertex : return PolygonIndex * 3 + PolygonVertexIndex;
    case FbxLayerElement::eByPolygon       : return PolygonIndex;
  }
  return -1;
}


int GetMaterialIndex( FbxScene* Scene, FbxMesh* FBXMesh, const int LayerIndex, const int PolygonIndex, const int PolygonVertexIndex, const int VertexIndex )
{
  // Check for valid params.
  if( LayerIndex < 0 || LayerIndex > FBXMesh->GetLayerCount() )
    return -1;


  // Get the node of the mesh.
  FbxNode* Node = FBXMesh->GetNode();


  // Check if the node is valid.
  if( Node == NULL )
    return -1;


  // Get the layer element material.
  FbxLayerElementMaterial* FBXMaterial = FBXMesh->GetLayer( LayerIndex )->GetMaterials();


  // Check if the layer element material is valid.
  if( FBXMaterial )
  {
    // Get the mapping index.
    const int MappingIndex = GetMappingIndex( FBXMaterial->GetMappingMode(), PolygonIndex, 0, VertexIndex );


    // Check if the mapping index is valid.
    if( MappingIndex < 0 )
      return -1;


    // Get the reference mode.
    const FbxLayerElement::EReferenceMode ReferenceMode = FBXMaterial->GetReferenceMode();


    // Check the reference mode.
    if( ReferenceMode == FbxLayerElement::eDirect )
    {
      // Check if the mapping index is valid.
      if( MappingIndex < Node->GetMaterialCount() )
      {
        // Get the node material.
        FbxSurfaceMaterial* NodeMaterial = Node->GetMaterial( MappingIndex );


        // Find the node material in the scene.
        for( int i = 0; i < Scene->GetMaterialCount(); ++i )
        {
          FbxSurfaceMaterial* SceneMaterial = Scene->GetMaterial( i );
          if( SceneMaterial == NodeMaterial )
            return i;
        }
      }
    }
    else if( ReferenceMode == FbxLayerElement::eIndexToDirect )
    {
      // Get the material index array.
      const FbxLayerElementArrayTemplate< int >& MaterialIndexArray = FBXMaterial->GetIndexArray();


      // Check if the mapping index is valid.
      if( MappingIndex < MaterialIndexArray.GetCount() )
      {
        // Get the material index.
        const int Index = MaterialIndexArray.GetAt( MappingIndex );


        // Check if the index is valid.
        if( Index < Node->GetMaterialCount() )
        {
          // Get the node material.
          FbxSurfaceMaterial* NodeMaterial = Node->GetMaterial( Index );


          // Find the node material in the scene.
          for( int i = 0; i < Scene->GetMaterialCount(); ++i )
          {
            FbxSurfaceMaterial* SceneMaterial = Scene->GetMaterial( i );
            if( SceneMaterial == NodeMaterial )
              return i;
          }
        }
      }
    }
  }


  // Return invalid material index.
  return -1;
}
// Each polygon (triangle).
for( DE::Int32 p = 0; p < FBXMesh->GetPolygonCount(); ++p )
{
  // Each polygon vertex.
  for( DE::Int32 pv = 0; pv < 3; ++pv )
  {
    ...
    ...
  }

  // Set the face material ID.
  const DE::Int32 MaterialControlPointIndex = FBXMesh->GetPolygonVertex( p, 0 );
  NewFace.MaterialID = GetMaterialIndex( m_Scene, FBXMesh, 0, p, 0, MaterialControlPointIndex );
}

Thanks for the help

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!