Beispiel #1
0
bool ExporterF3D::ExportMaterial(IGameMaterial* gameMaterial, MaterialPtr& material, STileUV& tile)
{
    if (!m_settings->isExportMaterials())
    {
        return true;
    }

    if (!gameMaterial)
    {
        LOG_WARNING("Material don't apply for this Node. Skipping Export Material");
        return true; //maybe need false
    }

    std::string materialName = TCHARToString(gameMaterial->GetMaterialName());
    std::string materialClass = TCHARToString(gameMaterial->GetMaterialClass());

    auto predMaterialExist = [&materialName](const MaterialPtr& material) -> bool
    {
        return material->getResourseName() == materialName;
    };

    const std::vector<MaterialPtr>& materials = m_scene->getMaterialList();
    std::vector<MaterialPtr>::const_iterator iter = std::find_if(materials.begin(), materials.end(), predMaterialExist);
    if (iter != materials.end())
    {
        material = (*iter);
        return true;
    }

    LOG_INFO("ExportMaterial : %s, class: %s", materialName.c_str(), materialClass.c_str());
    if (gameMaterial->IsMultiType())
    {
        LOG_DEBUG("ExportMaterial Export muliType");
        s32 countSubMaterial = gameMaterial->GetSubMaterialCount();
        LOG_INFO("Num Sub Materials : %d", countSubMaterial);
        for (u32 i = 0; i < countSubMaterial; ++i)
        {
            if (i == 0) //Use only first
            {
                IGameMaterial* subGameMaterial = gameMaterial->GetSubMaterial(i);

                s32 materialId = gameMaterial->GetMaterialID(i);
                LOG_INFO("Material ID : %d", materialId);

                return ExporterF3D::ExportMaterial(subGameMaterial, material, tile);
            }
        }
    }
    else
    {
        LOG_DEBUG("ExportMaterial Export singleType");
        material->setResourseName(materialName);

        //Texture
        for (u32 i = 0; i < gameMaterial->GetNumberOfTextureMaps(); ++i)
        {
            LOG_DEBUG("ExportMaterial Textures");
            IGameTextureMap* textureMap = gameMaterial->GetIGameTextureMap(i);

            std::string textureName = TCHARToString(textureMap->GetTextureName());
            LOG_INFO("Texture Name[%d] : %s", i, textureName.c_str());

            s32 textureType = textureMap->GetStdMapSlot();
            switch (textureType)
            {
            case ID_DI:
            {
                std::string diffuseMapName = getBitmapNameWithoutPath(TCHARToString(textureMap->GetBitmapFileName()));
                LOG_INFO("DiffuseMap Texture File : %s", diffuseMapName.c_str());

                CImage* texture = new CImage();
                texture->setResourseName(diffuseMapName);

                //material->setTexture(i, texture);
                m_scene->addTexture(material, texture);

                IGameUVGen* uvGen = textureMap->GetIGameUVGen();
                if (uvGen)
                {
                    IGameProperty* u = uvGen->GetUTilingData();
                    IGameProperty* v = uvGen->GetVTilingData();

                    if (u->GetType() == IGAME_FLOAT_PROP)
                    {
                        u->GetPropertyValue(tile._u);
                    }

                    if (v->GetType() == IGAME_FLOAT_PROP)
                    {
                        v->GetPropertyValue(tile._v);
                    }
                }
            }
                break;

            case ID_DP:
            {
                std::string heightMapName = getBitmapNameWithoutPath(TCHARToString(textureMap->GetBitmapFileName()));
                LOG_INFO("HeightMap Texture File : %s", heightMapName.c_str());

                CImage* texture = new CImage();
                texture->setResourseName(heightMapName);

               // material->setTexture(i, texture);
                m_scene->addTexture(material, texture);

            }
                break;


            case ID_BU:
            {
                std::string normalMapName = getBitmapNameWithoutPath(TCHARToString(textureMap->GetBitmapFileName()));
                LOG_INFO("NormalMap Texture File : %s", normalMapName.c_str());

                CImage* texture = new CImage();
                texture->setResourseName(normalMapName);

                //material->setTexture(i, texture);
                m_scene->addTexture(material, texture);

            }
                break;

            case ID_SP:
            case ID_OP:
            default:
                break;
            }
        }

        LOG_DEBUG("ExportMaterial Props");

        //Diffuse Color
        IGameProperty* propertyDiffuseColor = gameMaterial->GetDiffuseData();
        if (propertyDiffuseColor && propertyDiffuseColor->GetType() == IGAME_POINT3_PROP)
        {
            Point3 color;
            propertyDiffuseColor->GetPropertyValue(color);
            LOG_DEBUG("add DiffuseColor : (%f, %f, %f)", color.x, color.y, color.z);
            material->setDiffuseColor(convertPointToVector3(color));
        }

        //Ambient Color
        IGameProperty* propertyAmbientColor = gameMaterial->GetAmbientData();
        if (propertyAmbientColor && propertyAmbientColor->GetType() == IGAME_POINT3_PROP)
        {
            Point3 color;
            propertyAmbientColor->GetPropertyValue(color);
            LOG_DEBUG("add AmbientColor : (%f, %f, %f)", color.x, color.y, color.z);
            material->setAmbientColor(convertPointToVector3(color));
        }

        //Specular Color
        IGameProperty* propertySpecularColor = gameMaterial->GetSpecularData();
        if (propertySpecularColor && propertySpecularColor->GetType() == IGAME_POINT3_PROP)
        {
            Point3 color;
            propertySpecularColor->GetPropertyValue(color);
            LOG_DEBUG("add SpecularColor : (%f, %f, %f)", color.x, color.y, color.z);
            material->setSpecularColor(convertPointToVector3(color));
        }

        //Emission Color
        IGameProperty* propertyEmissionColor = gameMaterial->GetEmissiveData();
        if (propertyEmissionColor && propertyEmissionColor->GetType() == IGAME_POINT3_PROP)
        {
            Point3 color;
            propertyEmissionColor->GetPropertyValue(color);
            LOG_DEBUG("add EmissionColor : (%f, %f, %f)", color.x, color.y, color.z);
            material->setEmissionColor(convertPointToVector3(color));
        }

        //Opacity Color
        IGameProperty* propertyOpacity = gameMaterial->GetOpacityData();
        if (propertyOpacity && propertyOpacity->GetType() == IGAME_FLOAT_PROP)
        {
            f32 opacity;
            propertyOpacity->GetPropertyValue(opacity);
            LOG_DEBUG("add Transparency : %f", opacity);
            material->setTransparency(opacity);
        }

        //Specular Level
        IGameProperty* propertySpecularLevel = gameMaterial->GetSpecularLevelData();
        if (propertySpecularLevel && propertySpecularLevel->GetType() == IGAME_FLOAT_PROP)
        {
            f32 level;
            propertySpecularLevel->GetPropertyValue(level);
            LOG_DEBUG("add SpecularLevel : %f", level);
            material->setShininess(level);
        }

        //Glossiness
        IGameProperty* propertyGlossiness = gameMaterial->GetGlossinessData();
        if (propertyGlossiness && propertyGlossiness->GetType() == IGAME_FLOAT_PROP)
        {
            f32 glossiness;
            propertyGlossiness->GetPropertyValue(glossiness);
            LOG_DEBUG("add Glossiness : %f", glossiness);
            material->setGlossiness(glossiness);
        }
    }

    m_scene->addMaterial(material);

    return true;
}
//----------------------------------------------------------------------------------
// dump material textures
void DumpTexture(m_material *pMat, IGameMaterial *pGMaxMat)
{
	std::vector<tex_channel>		bk_tex_channel;
	std::vector<unsigned int>		bk_tex_idx;
    std::vector<MatTextureInfo>		TexInfos;

	int texCount = pGMaxMat->GetNumberOfTextureMaps();

	for (int i = 0; i < texCount; ++i)
	{
		IGameTextureMap * pGMaxTex = pGMaxMat->GetIGameTextureMap(i);

		int tex_type = pGMaxTex->GetStdMapSlot();

		if (pGMaxTex->IsEntitySupported() && tex_type >= 0)	//its a bitmap texture
		{
			MatTextureInfo TexInfo;
			tex_channel tc;

			TexInfo.mat_id = pMat->id;

			m_texture * pTex = new m_texture;

			std::string pathname = pGMaxTex->GetBitmapFileName();

			int idx = (int)pathname.rfind('\\');
			if (idx == INDEX_NONE){
				idx = (int)pathname.rfind('/');
			}
			
			std::string filename = pathname.substr(idx + 1, INDEX_NONE);

			pTex->name = filename;

			// set the texture xform...
			IGameUVGen *pUVGen = pGMaxTex->GetIGameUVGen();
			GMatrix UVMat = pUVGen->GetUVTransform();
            TexInfo.tex_mat = pTex->tex_mat = (scalar*)UVMat.GetAddr(); // save mapping matrix

          	// get the uv channel to use...
			Texmap *pTMap = pGMaxTex->GetMaxTexmap();
			BitmapTex *pBTex = (BitmapTex*)pTMap;
			StdUVGen *pStdUVGen = pBTex->GetUVGen();

			if (pStdUVGen){
				tc.channel = pStdUVGen->GetMapChannel() - 1;
			}

			IParamBlock2 *pUVWCropParam = (IParamBlock2*)(pBTex->GetReference(1));

			if (pUVWCropParam)
			{
				 pUVWCropParam->GetValue(0, ExporterMAX::GetExporter()->GetStaticFrame(), TexInfo.uv_offset.x, FOREVER);
				 pUVWCropParam->GetValue(1, ExporterMAX::GetExporter()->GetStaticFrame(), TexInfo.uv_offset.y, FOREVER);
				 pUVWCropParam->GetValue(2, ExporterMAX::GetExporter()->GetStaticFrame(), TexInfo.uv_scale.x,  FOREVER);
				 pUVWCropParam->GetValue(3, ExporterMAX::GetExporter()->GetStaticFrame(), TexInfo.uv_scale.y,  FOREVER);
			}

			// set the type of texture...
			pTex->type = texture_type[tex_type];

 			// if we have a bump map, we create a normal map with the convention
 			// that the filename will be the same name as the bump map + "_normal" 
 			// appended to it.
  			if (pTex->type == m_texture::BUMP)
 			{
 				std::string normal_map = pTex->name;
 				std::string::size_type pos = normal_map.rfind(".");
 				normal_map.insert(pos, "_normal");
 
 				m_texture  *pTexNormal = new m_texture;
 				*pTexNormal = *pTex;
 
 				pTexNormal->name = normal_map;
 				pTexNormal->type = m_texture::NORMAL;
 
 				tc.pTex = pTexNormal;
 
 				bk_tex_channel.push_back(tc);	// add the new texture to the local TOC
 				TexInfos.push_back(TexInfo);
 			}

			 tc.pTex = pTex;
						 
			 bk_tex_channel.push_back(tc);	// add the new texture to the local TOC
			 TexInfos.push_back(TexInfo);
		}
	}

	// lets check if we don't have them already in our global TOC...
	for (size_t index = 0; index < bk_tex_channel.size(); ++index)
	{
		m_texture * pTex = bk_tex_channel[index].pTex;

		unsigned int idx = ExporterMAX::GetExporter()->FindTexture(pTex);
		
		if (idx == INDEX_NONE){	
			idx = ExporterMAX::GetExporter()->AddTexture(pTex); // add the new texture to the TOC
		}

		bk_tex_idx.push_back(idx);

		pMat->tex_channel.push_back(bk_tex_channel[index].channel);

		TexInfos[index].base_channel = bk_tex_channel[index].channel;

		bk_texs.insert(IdxBKTexMapPair(idx, TexInfos[index]));
	}

	// set the texture indices...
	for (size_t index = 0; index < bk_tex_idx.size(); ++index){
		pMat->textures.push_back(bk_tex_idx[index]);
	}
}