Пример #1
0
reMaterial* reFBXAsset::getMaterial( FbxMesh* fmesh, int i, reMaterialSet& set)
{
	reMaterial* mat = NULL;
	for (int l = 0; l < fmesh->GetElementMaterialCount(); l++)
	{		
		FbxGeometryElementMaterial* lMaterialElement = fmesh->GetElementMaterial(l);
		int lMatId = lMaterialElement->GetIndexArray().GetAt(i);
		if(lMatId >= 0)
		{
			if (mat = set.materialById(lMatId))
				return mat;
			mat = new reMaterial;
			mat->id = lMatId;
			set.addMetarial(mat);
			FbxSurfaceMaterial* lMaterial = fmesh->GetNode()->GetMaterial(lMaterialElement->GetIndexArray().GetAt(i));
			if (!lMaterial)
			{
				continue;
			}
			//////////////////////////////////////////////////////////////////////////
			FbxProperty lProperty = lMaterial->FindProperty(FbxSurfaceMaterial::sDiffuse);
			if (lMaterial->GetClassId().Is(FbxSurfacePhong::ClassId))
			{
				FbxDouble3 lFbxDouble3;
				lFbxDouble3 =((FbxSurfacePhong *)lMaterial)->Diffuse;
				mat->diffuseColor = reColor4(lFbxDouble3[0], lFbxDouble3[1], lFbxDouble3[2], 1);
			}
			if (lMaterial->GetClassId().Is(FbxSurfaceLambert::ClassId))
			{
				FbxDouble3 lFbxDouble3;
				lFbxDouble3 =((FbxSurfaceLambert *)lMaterial)->Diffuse;
				mat->diffuseColor = reColor4(lFbxDouble3[0], lFbxDouble3[1], lFbxDouble3[2], 1);
			}

			////////////////////////////////////////////////////////////////////////// read texture
			int lNbTextures = lProperty.GetSrcObjectCount(FbxTexture::ClassId);
			if (lNbTextures)
			{
				mat->diffuseTexture = new reTexture;
				FbxTexture* lTexture = FbxCast <FbxTexture> (lProperty.GetSrcObject(FbxTexture::ClassId,0));
				qDebug() << "map: " << lTexture->GetName();
				FbxFileTexture *lFileTexture = FbxCast<FbxFileTexture>(lTexture);
				if (lFileTexture)
				{
					mat->diffuseTexture->fileName(lFileTexture->GetFileName());
				}

			}
			//////////////////////////////////////////////////////////////////////////
		}
	}
	return mat;
}
Пример #2
0
Material* FBXSceneImporter::read_material(FbxNode *pNode, FbxSurfaceMaterial* material)
{
	std::string texture_names[Material::mtt_count];

	if (material != NULL)
	{
		std::string material_name = material->GetName();

		{
			FbxProperty prop = material->FindProperty(FbxSurfaceMaterial::sDiffuse);

			int textureCount = prop.GetSrcObjectCount<FbxTexture>();
			if (textureCount > 0)
			{
				FbxTexture* texture = FbxCast<FbxTexture>(prop.GetSrcObject<FbxTexture>(0));
				// Then, you can get all the properties of the texture, include its name
				const char* textureName = texture->GetName();
				FbxFileTexture *file_texture = (FbxFileTexture *)texture;

				std::string fileName = file_texture->GetFileName();
				texture_names[Material::mtt_diffuse] = fileName; file_texture->GetRelativeFileName();
			}
		}

		{
			FbxProperty prop = material->FindProperty(FbxSurfaceMaterial::sBump);

			int textureCount = prop.GetSrcObjectCount<FbxTexture>();
			if (textureCount > 0)
			{
				FbxTexture* texture = FbxCast<FbxTexture>(prop.GetSrcObject<FbxTexture>(0));
				// Then, you can get all the properties of the texture, include its name
				const char* textureName = texture->GetName();
				FbxFileTexture *file_texture = (FbxFileTexture *)texture;

				std::string fileName = file_texture->GetFileName();
				texture_names[Material::mtt_normal] = fileName; file_texture->GetRelativeFileName();
			}
		}

		{
			FbxProperty prop = material->FindProperty(FbxSurfaceMaterial::sBump);

			int textureCount = prop.GetSrcObjectCount<FbxTexture>();
			if (textureCount > 0)
			{
				FbxTexture* texture = FbxCast<FbxTexture>(prop.GetSrcObject<FbxTexture>(0));
				// Then, you can get all the properties of the texture, include its name
				const char* textureName = texture->GetName();
				FbxFileTexture *file_texture = (FbxFileTexture *)texture;

				std::string fileName = file_texture->GetFileName();
				texture_names[Material::mtt_specular] = fileName; file_texture->GetRelativeFileName();
			}
		}

		//diffuse color
		D3DXVECTOR4 diffuse_color = D3DXVECTOR4(1, 1, 1, 1);
		if (material->GetClassId().Is(FbxSurfaceLambert::ClassId))
		{
			FbxSurfaceLambert *lambert_material = (FbxSurfaceLambert *)material;
			FbxDouble3 diffuse = lambert_material->Diffuse.Get();
			diffuse_color = D3DXVECTOR4(diffuse[0], diffuse[1], diffuse[2], 1);
		}

		Material *new_material= new Material;
		new_material->create_from_file(texture_names, diffuse_color);
		return new_material;
	}

	return nullptr;
}
void MeshImporter::LoadMaterials(FbxNode* node, MeshEntry* mesh, ID3D11Device3* device,
	ID3D11DeviceContext3* context)
{
	int mcount = node->GetSrcObjectCount<FbxSurfaceMaterial>();

	for (int index = 0; index < mcount; index++)
	{
		FbxSurfaceMaterial *material =
			(FbxSurfaceMaterial*)node->GetSrcObject<FbxSurfaceMaterial>(index);

		if (material)
		{
			// This only gets the material of type sDiffuse, you 
			// probably need to traverse all Standard Material Property 
			// by its name to get all possible textures.
			FbxProperty prop = material->FindProperty(FbxSurfaceMaterial::sDiffuse);

			// Check if it's layeredtextures
			int layered_texture_count = prop.GetSrcObjectCount<FbxLayeredTexture>();

			if (layered_texture_count > 0)
			{
				for (int j = 0; j < layered_texture_count; j++)
				{
					FbxLayeredTexture* layered_texture = FbxCast<FbxLayeredTexture>(prop.GetSrcObject<FbxLayeredTexture>(j));
					int lcount = layered_texture->GetSrcObjectCount<FbxTexture>();

					for (int k = 0; k < lcount; k++)
					{
						FbxTexture* texture =
							FbxCast<FbxTexture>(layered_texture->GetSrcObject<FbxTexture>(k));
						// Then, you can get all the properties of the texture, include its name
						const char* texture_name = texture->GetName();

						// Load files
						LoadTexture(texture_name, mesh, device, context);

						PrintTab(to_string(layered_texture_count) + " Layered textures loaded!" +
							"Number of layers: " + to_string(lcount));
					}
				}
			}
			else
			{
				// Directly get textures
				int texture_count = prop.GetSrcObjectCount<FbxTexture>();

				for (int j = 0; j < texture_count; j++)
				{
					const FbxTexture* texture =
						FbxCast<FbxTexture>(prop.GetSrcObject<FbxTexture>(j));
					// Then, you can get all the properties of the texture, include its name
					const char* texture_name = texture->GetName();

					// Load file
					LoadTexture(texture_name, mesh, device, context);

					PrintTab(to_string(texture_count) + " Single texture loaded!");
				}
			}
		}
	}
}