Sign in to follow this  
Alundra

Unity [FBX] Material Index problem

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this