Esempio n. 1
0
		void FbxLoader::FbxLoader::ProcessMaterialTexture2D(FbxSurfaceMaterial* mat, FMaterial& material)
		{
			int Texture2DIndex = 0;
			FbxProperty prop;

			FBXSDK_FOR_EACH_TEXTURE(Texture2DIndex)
			{
				prop = mat->FindProperty(FbxLayerElement::sTextureChannelNames[Texture2DIndex]);
				if(prop.IsValid()) {
					const int Texture2DCount = prop.GetSrcObjectCount<FbxTexture>();
					for(int i = 0; i < Texture2DCount; i++) {
						FbxTexture* texture = prop.GetSrcObject<FbxTexture>(i);
						if(texture) {
							std::string textureType = prop.GetNameAsCStr();
							FbxFileTexture* fileTexture = FbxCast<FbxFileTexture>(texture);
							if(fileTexture) {
								if(textureType == "DiffuseColor")
									material.diffuseMapName = fileTexture->GetRelativeFileName();
								else if(textureType == "SpecularColor")
									material.specularMapName = fileTexture->GetRelativeFileName();
								else if(textureType == "NormalMap") {
									material.normalMapName = fileTexture->GetRelativeFileName();
									useNormalMap = true;
								}
							}
						}
					}

					const int layeredTextureCount = prop.GetSrcObjectCount<FbxLayeredTexture>();
					for(int i = 0; i < layeredTextureCount; i++) {
						auto layeredTexture = prop.GetSrcObject<FbxLayeredTexture>(i);
						const int fileCount = layeredTexture->GetSrcObjectCount<FbxFileTexture>();
						for(int j = 0; j < fileCount; j++) {
							auto fileTexture = layeredTexture->GetSrcObject<FbxFileTexture>(j);
							std::string Texture2DType = prop.GetNameAsCStr();
							if(fileTexture) {
								if(Texture2DType == "DiffuseColor")
									material.diffuseMapName = fileTexture->GetRelativeFileName();
								else if(Texture2DType == "SpecularColor")
									material.specularMapName = fileTexture->GetRelativeFileName();
								else if(Texture2DType == "NormalMap") {
									material.normalMapName = fileTexture->GetRelativeFileName();
									useNormalMap = true;
								}
							}
						}
					}
				}
			}

			if(material.diffuseMapName != "") {
				std::string n = FbxPathUtils::GetFileName(material.diffuseMapName.c_str(), false).Buffer();
				material.name = n;
				mat->SetName(n.c_str());
			}
		}
Esempio n. 2
0
void Converter::getDiffuseTextures(FbxSurfaceMaterial *material, std::vector<std::string> &textures)
{
	FbxProperty property = material->FindProperty(FbxLayerElement::sTextureChannelNames[0]);
	if (property.IsValid()) {
		for (int j = 0; j < property.GetSrcObjectCount<FbxTexture>(); ++j) {
			if (!property.GetSrcObject<FbxLayeredTexture>(j)) {
				FbxTexture* texture = property.GetSrcObject<FbxTexture>(j);
				if (texture) {
					std::string textureType = property.GetNameAsCStr();
					FbxFileTexture* fileTexture = FbxCast<FbxFileTexture>(texture);
					if (fileTexture) {
						if (textureType == "DiffuseColor") {
							//std::cout << fileTexture->GetRelativeFileName() << "\n";
							textures.push_back(fileTexture->GetRelativeFileName());
						}
					}
				}
			}
		}
	}
}
Esempio n. 3
0
	std::shared_ptr<Material> FbxUtil::CreateMaterial(FbxSurfaceMaterial *fbxMaterial)
	{
		// a,d,s, Intensity(Factor), Shininess, Reflectivity, Transparency
		//GetImplementation(material, FBXSDK_IMP)

		FbxSurfacePhong *fbxPhong = static_cast<FbxSurfacePhong*>(fbxMaterial);
		Material::Ptr material = Material::Create(fbxPhong->GetName());

		// read uniforms
		material->SetUniform(Material::SHININESS, Uniform1f::Create({ (float)fbxPhong->Shininess }));
		material->SetUniform(Material::AMBIENT_FACTOR, Uniform1f::Create({ (float)fbxPhong->AmbientFactor }));
		material->SetUniform(Material::DIFFUSE_FACTOR, Uniform1f::Create({ (float)fbxPhong->DiffuseFactor }));
		material->SetUniform(Material::SPECULAR_FACTOR, Uniform1f::Create({ (float)fbxPhong->SpecularFactor }));

		auto GetUniform3f = [](FbxPropertyT<FbxDouble3> &data) -> UniformBase::Ptr
		{
			return Uniform3f::Create({ (float)data.Get()[0], (float)data.Get()[1], (float)data.Get()[2] });
		};

		// read colors
		material->SetUniform(Material::AMBIENT_COLOR, GetUniform3f(fbxPhong->Ambient));
		material->SetUniform(Material::DIFFUSE_COLOR, GetUniform3f(fbxPhong->Diffuse));
		material->SetUniform(Material::SPECULAR_COLOR, GetUniform3f(fbxPhong->Specular));

		material->SetUniform(Material::MATERIAL_ID, Uniform1ui::Create({ material->GetID() }));

		auto GetTexture = [&](FbxPropertyT<FbxDouble3> prop) -> Texture::Ptr
		{
			if (prop.IsValid() && prop.GetSrcObjectCount<FbxTexture>() > 0)
			{
				FbxTexture *fbxTexture = prop.GetSrcObject<FbxTexture>(0);

				FbxFileTexture *fileTexture = FbxCast<FbxFileTexture>(fbxTexture);
				if (fileTexture)
				{
					bool mipMap = (bool)fileTexture->UseMipMap;
					std::string filePath = FileUtil::Instance()->GetAbsPath(fileTexture->GetRelativeFileName(), true);

					auto texture = Texture::Create(fileTexture->GetName());
					texture->SetFilterMode(mipMap ? FilterMode::LINEAR_MIPMAP_LINEAR : FilterMode::LINEAR);
					texture->CreateFromImage(filePath, mipMap);

					return texture;
				}
				else
				{
					LOGW << "FbxProceduralTexture not supported!";
				}
			}

			return nullptr;
		};

		// read tetxures.
		if(m_Options & Options::DIFFUSE_MAP)
			material->SetTexture(Material::DIFFUSE_TEXTURE, GetTexture(fbxPhong->Diffuse));
		if(m_Options & Options::SPECULAR_MAP)
			material->SetTexture(Material::SPECULAR_TEXTURE, GetTexture(fbxPhong->Specular));
		if(m_Options & Options::NORMAL_MAP)
			material->SetTexture(Material::NORMAL_TEXTURE, GetTexture(fbxPhong->NormalMap));

		LOGD << fbxMaterial->GetName();

		return material;
	}
Esempio n. 4
0
//--------------------------------------------------------------------------------------
void FBXScene::ProcessMaterials(FbxScene* pScene)
{
	for( int i = 0; i < pScene->GetMaterialCount(); ++i )
	{
		Material* pMaterial = new Material(i);

		FbxSurfaceMaterial* pFBXMaterial = pScene->GetMaterial(i);

		FbxProperty diffuseTextureProperty = pFBXMaterial->FindProperty(FbxSurfaceMaterial::sDiffuse);
		if( diffuseTextureProperty.IsValid() )
		{
			FbxFileTexture* pDiffuseTexture = diffuseTextureProperty.GetSrcObject<FbxFileTexture>(0);

			if( pDiffuseTexture )
			{
				std::string strFileName = pDiffuseTexture->GetFileName();

				if( strFileName.length() == 0 )
					strFileName = pDiffuseTexture->GetRelativeFileName();

				strFileName = GetFileFromPath(strFileName);

				pMaterial->SetDiffuseTextureName(strFileName);
			}
		}
		
		FbxProperty normalTextureProperty = pFBXMaterial->FindProperty(FbxSurfaceMaterial::sNormalMap);
		if( normalTextureProperty.IsValid() )
		{
				FbxFileTexture* pNormalTexture = normalTextureProperty.GetSrcObject<FbxFileTexture>(0);

				if( pNormalTexture )
				{
					std::string strFileName = pNormalTexture->GetFileName();

					if( strFileName.length() == 0 )
						strFileName = pNormalTexture->GetRelativeFileName();
					
					strFileName = GetFileFromPath(strFileName);

					pMaterial->SetNormalTextureName(strFileName);
				}
		}

		FbxSurfaceLambert* pLambert = FbxCast<FbxSurfaceLambert>(pFBXMaterial);
		FbxSurfacePhong* pPhong = FbxCast<FbxSurfacePhong>(pFBXMaterial); 

		BTHFBX_VEC3 AmbientColor2;
		BTHFBX_VEC3 EmissiveColor2;
		BTHFBX_VEC3 DiffuseColor2;
		BTHFBX_VEC3 SpecularColor2;

		float fSpecularPower = 1.0f;
		float fTransparency = 1.0f;

		if( pLambert )
		{
			AmbientColor2 = GetMaterialColor2(pLambert->Ambient, pLambert->AmbientFactor);
			EmissiveColor2 = GetMaterialColor2(pLambert->Emissive, pLambert->EmissiveFactor);
			DiffuseColor2 = GetMaterialColor2(pLambert->Diffuse, pLambert->DiffuseFactor);

			FbxPropertyT<FbxDouble> FBXTransparencyProperty = pLambert->TransparencyFactor;

			if( FBXTransparencyProperty.IsValid() )
				fTransparency = (float)FBXTransparencyProperty.Get();
		}

		if( pPhong )
		{
			SpecularColor2 = GetMaterialColor2(pPhong->Specular, pPhong->SpecularFactor);

			FbxPropertyT<FbxDouble> FBXSpecularPowerProperty = pPhong->Shininess;

			if( FBXSpecularPowerProperty.IsValid() )
				fSpecularPower = (float)FBXSpecularPowerProperty.Get();
		}

		pMaterial->SetAmbientColor2(AmbientColor2);
		pMaterial->SetEmissiveColor2(EmissiveColor2);
		pMaterial->SetDiffuseColor2(DiffuseColor2);
		pMaterial->SetSpecularColor2(SpecularColor2);

		pMaterial->SetSpecularPower(fSpecularPower);
		pMaterial->SetTransparency(fTransparency);

		pMaterial->AddTexturePath( GetFilePath(this->mFilename) + "/" );

		m_Materials.push_back(pMaterial);
		m_FBXMaterials.push_back(pFBXMaterial);
	}
}
Esempio n. 5
0
FbxModelData* FBXLoader::loadModel( const char* aFile )
{
	FBX_LOG("FBXLoader Creating ModelData...");
	myLoadingModel = new FbxModelData;
	FBX_LOG("Success!");
	FBX_LOG("FBXLoader Creating TextureData...");
	myLoadingModel->myTextureData = new TextureData();
	FBX_LOG("Success!");
	FBX_LOG("FBXLoader Loading Scene...");
	auto scene = LoadScene(aFile);
	FBX_LOG("Successfully loaded scene!");

	FBX_LOG("FBXLoader Loading Textures...");
	//TextureData
	const int lTextureCount = scene->GetTextureCount();
	for (int lTextureIndex = 0; lTextureIndex < lTextureCount; ++lTextureIndex)
	{
		FbxTexture * lTexture = scene->GetTexture(lTextureIndex);
		FbxFileTexture * lFileTexture = reinterpret_cast<FbxFileTexture*>(lTexture);
		if (lFileTexture && !lFileTexture->GetUserDataPtr())
		{
			const FbxString lFileName = lFileTexture->GetFileName();
			
			unsigned int lTextureObject = 0;
			lTextureObject;
			bool lStatus = false;
			lStatus;
			
			const FbxString lAbsFbxFileName = FbxPathUtils::Resolve(aFile);
			const FbxString lAbsFolderName = FbxPathUtils::GetFolderName(lAbsFbxFileName);
			
			const FbxString lTextureFileName = lAbsFolderName + "\\" + lFileTexture->GetRelativeFileName();// FbxPathUtils::GetFileName(lFileName);
				
			const FbxString lResolvedFileName = lAbsFolderName + "\\" + FbxPathUtils::GetFileName(lFileName);// lFileTexture->GetRelativeFileName();;// FbxPathUtils::Bind(lAbsFolderName, lTextureFileName);
			TextureInfo info;
			info.myFileName = lResolvedFileName;
			//info.myFileName += "\\";
			info.myFileName = lFileTexture->GetRelativeFileName();
			myLoadingModel->myTextureData->myTextures.push_back(info);
			lFileTexture->SetFileName(lResolvedFileName);
		}
	}
	FBX_LOG("Success!");
	FBX_LOG("FBXLoader Loading Animations...");

	FbxArray<FbxString*> animationNames;
	FbxArray<FbxPose*> poses;
	scene->FillAnimStackNameArray(animationNames);
	scene->FillPoseArray(poses);
	FbxAnimStack * lCurrentAnimationStack = nullptr;
	FbxAnimLayer* lCurrentAnimLayer = nullptr;
	if(animationNames.GetCount() > 0)
	{
		lCurrentAnimationStack = scene->FindMember<FbxAnimStack>(animationNames[0]->Buffer());
		if (lCurrentAnimationStack != NULL)
		{
			lCurrentAnimLayer = lCurrentAnimationStack->GetMember<FbxAnimLayer>();
		}
	}
	//lCurrentAnimLayer->IsR
	myLoadingModel->myAnimation = new AnimationData();
	FbxPose* pose = nullptr;
	if(poses.GetCount() > 0)
	{
		pose = poses[0];
	}

	LoadAnimation(*myLoadingModel->myAnimation,scene->GetRootNode(),FbxAMatrix(),pose, lCurrentAnimLayer,-1);
	LoadNodeRecursive(myLoadingModel, *myLoadingModel->myAnimation, scene->GetRootNode(), FbxAMatrix(), pose, lCurrentAnimLayer, -1);
	FBX_LOG("Success!");
		
	return myLoadingModel;
}
Esempio n. 6
0
G2::Entity
FBXMesh::Builder::buildResource(bool importNormals, bool importTexCoords, bool importAnimations, bool flipTexU, bool flipTexV, TextureImporter* texImporte, G2::Entity* target)
{
	// create mesh
	G2::Entity meshLocal;
	G2::Entity* mesh = &meshLocal;
	if (target != nullptr)
	{
		mesh = target; // in case the user gave a valid pointer, we don't write into the local mesh
	}
	if(isAnimated && importAnimations)
	{
		// prepare animationData
		FBXAnimationData animData(cacheStart, cacheStop, animStackNameArray, 24.0);
		// prepare animation state
		FBXAnimationState animState(poseIndex, initialAnimLayer, initialAnimationStack, start, stop, false);
	
		auto* fbxAnimationComponent = mesh->addComponent<FBXAnimationComponent>(fbxScene, animState, animData);
	}

#ifdef USE_META_DATA
	// create a RenderComponent with the requested amount of VertexArrayObjects
	auto* renderComponent = mesh->addComponent<RenderComponent>();
	renderComponent->allocateVertexArrays((unsigned int)meshMetaData.size());

	// calculate the number of IndexArrayObjects we need
	unsigned int numIndexArrays = 0;
	for (unsigned int i = 0; i < meshMetaData.size() ; ++i) 
	{
		MeshMetaData const& meshData = meshMetaData[i];
		if(meshData.indices.size() > 0)
		{
			++numIndexArrays;
		}
	}
	renderComponent->allocateIndexArrays(numIndexArrays);

	// init the VAO
	unsigned int totalVertices = 0;
	unsigned int currentIndexArrayIndex = 0;
	for (unsigned int i = 0; i < meshMetaData.size() ; ++i) 
	{
		MeshMetaData const& meshData = meshMetaData[i];
		renderComponent->getVertexArray(i).resizeElementCount((unsigned int)meshData.vertices.size())
			.writeData(G2Core::Semantics::POSITION, &meshData.vertices[0]);
		totalVertices += (unsigned int)meshData.vertices.size();
		
		if(meshData.hasNormals && importNormals)
		{
			renderComponent->getVertexArray(i).writeData(G2Core::Semantics::NORMAL, &meshData.normals[0]);
		}
		if(meshData.hasUvs && importTexCoords)
		{
			if (flipTexU || flipTexV)
			{
				std::vector<glm::vec2> uvCopy = meshData.uvs; // copy
				std::transform(uvCopy.begin(), uvCopy.end(), uvCopy.begin(), [flipTexU,flipTexV](glm::vec2& uv) 
				{ 
					if (flipTexU)
					{
						uv.x = 1.0f - uv.x;
					}
					if (flipTexV)
					{
						uv.y = 1.0f - uv.y;
					}
					return uv; 
				});
				renderComponent->getVertexArray(i).writeData(G2Core::Semantics::TEXCOORD_0, &uvCopy[0]);
			}
			else
			{
				renderComponent->getVertexArray(i).writeData(G2Core::Semantics::TEXCOORD_0, &meshData.uvs[0]);
			}
		}
		if(meshData.indices.size() > 0)
		{
			IndexArrayObject& iao = renderComponent->getIndexArray(currentIndexArrayIndex);
			iao.writeIndices(&meshData.indices[0], (unsigned int)meshData.indices.size());
			
			++currentIndexArrayIndex;
		}
		// add a draw call
		renderComponent->addDrawCall(DrawCall()
			.setDrawMode(G2Core::DrawMode::TRIANGLES)
			.setEnabled(true)
			.setVaoIndex(i)
			.setModelSpaceAABB(meshData.modelSpaceAABB)
			.setIaoIndex(meshData.indices.size() > 0 ? currentIndexArrayIndex-1 : -1)
			.setAABBCalculationMode(AABBCalculationMode::MANUAL));
	}

	G2::logger << "[FBXMesh] Name: " << name << "\n\tIAO: " << renderComponent->getNumIndexArrays() << "\n\tVAO: " << renderComponent->getNumVertexArrays() << "\n\tVertices: " << totalVertices << "\n\tDraw-Calls: " << renderComponent->getNumDrawCalls() << "\n\tTriangles: " << renderComponent->getNumTriangles() << G2::endl;

#endif
	
	auto* nameComponent = mesh->addComponent<NameComponent>();
	nameComponent->name = name;

	if (texImporte != nullptr && renderComponent != nullptr)
	{
		for (int i = 0; i < meshTextures.size(); ++i)
		{
			FbxFileTexture* fbxTex = meshTextures[i];
			G2::Sampler::Name sampler = toSampler(fbxTex->GetTextureUse());

			if (sampler != G2::Sampler::SAMPLER_INVALID && renderComponent->material.getTexture(sampler).get() == nullptr)
			{
				// sampler is valid and RenderComponent has not yet set a texture at that sampler
				const FbxString lAbsFolderName = FbxPathUtils::GetFolderName(FbxPathUtils::Resolve(meshFilePath.c_str()));
				const FbxString lResolvedFileName = FbxPathUtils::Bind(lAbsFolderName, fbxTex->GetRelativeFileName());

				auto tex = texImporte->import(
					lResolvedFileName.Buffer(), 
					G2Core::DataFormat::Internal::R32G32B32A32_F, 
					G2Core::FilterMode::LINEAR, 
					G2Core::FilterMode::LINEAR, 
					false, 
					toWrapMode(fbxTex->GetWrapModeU()), 
					toWrapMode(fbxTex->GetWrapModeV())
				);
				if(tex.get() != nullptr)
				{
					renderComponent->material.setTexture(sampler, tex);
				}
			}
		}
	}

	return std::move(meshLocal);
}