[FBX] Material Index Problem

Discussions Regarding Coding (C/C++/Objective-C Mainly)

[FBX] Material Index Problem

Postby Michael » Tue Oct 29, 2013 2:58 pm

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 :
Code: Select all
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;
}

Code: Select all
// 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
Michael
I Have a Question
 
Posts: 4
Joined: Wed Feb 20, 2013 6:14 pm

Re: [FBX] Material Index Problem

Postby L. Spiro » Thu Nov 28, 2013 11:35 pm

If you have not solved this yet I will reply with an answer later when I get home.
Sorry for the delay.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them.
- L. Spiro 2011
L. Spiro
Site Admin
 
Posts: 54
Joined: Thu Jul 21, 2011 2:59 pm
Location: Tokyo, Japan

Re: [FBX] Material Index Problem

Postby Michael » Fri Mar 14, 2014 12:05 pm

It's solved, the problem was not the material ID code but the optimize who didn't take care of subsets.
Michael
I Have a Question
 
Posts: 4
Joined: Wed Feb 20, 2013 6:14 pm


Return to Coding

Who is online

Users browsing this forum: No registered users and 0 guests

cron