1 /* 2 Copyright (c) 2008-2009 NetAllied Systems GmbH 3 4 This file is part of DAE2MA. 5 6 Licensed under the MIT Open Source License, 7 for details please see LICENSE file or the website 8 http://www.opensource.org/licenses/mit-license.php 9 */ 10 11 #include "DAE2MAStableHeaders.h" 12 #include "DAE2MAEffectImporter.h" 13 #include "DAE2MAImageImporter.h" 14 #include "DAE2MAMaterialImporter.h" 15 16 #include "COLLADAFWEffect.h" 17 18 #include <MayaDMPlace2dTexture.h> 19 #include <MayaDMPlace3dTexture.h> 20 #include <MayaDMDefaultTextureList.h> 21 #include <MayaDMCommands.h> 22 23 24 namespace DAE2MA 25 { 26 27 const String EffectImporter::EFFECT_NAME = "effect"; 28 const String EffectImporter::PLACE_2D_TEXTURE_NAME = "place2dTexture"; 29 const String EffectImporter::PLACE_3D_TEXTURE_NAME = "place3dTexture"; 30 const String EffectImporter::DEFAULT_TEXTURE_LIST = "defaultTextureList1"; 31 32 33 //------------------------------ EffectImporter(DocumentImporter * documentImporter)34 EffectImporter::EffectImporter ( DocumentImporter* documentImporter ) 35 : BaseImporter ( documentImporter ) 36 { 37 } 38 39 //------------------------------ ~EffectImporter()40 EffectImporter::~EffectImporter() 41 { 42 UniqueIdMayaEffectsMap::iterator it = mMayaEffectsMap.begin (); 43 while ( it != mMayaEffectsMap.end () ) 44 { 45 MayaEffectList mayaEffectList = it->second; 46 for ( size_t i=0; i<mayaEffectList.size (); ++i ) 47 { 48 MayaDM::Lambert* lambert = mayaEffectList[i]; 49 delete lambert; 50 } 51 ++it; 52 } 53 } 54 55 //------------------------------ importEffect(const COLLADAFW::Effect * effect)56 bool EffectImporter::importEffect ( const COLLADAFW::Effect* effect ) 57 { 58 // Check if the current effect is already imported. 59 const COLLADAFW::UniqueId& effectId = effect->getUniqueId (); 60 if ( findMayaEffects ( effectId ) != 0 ) return false; 61 62 // Check how many collada materials use this effect 63 // and create one maya material for every collada material. 64 const UniqueIdVec* materialIds = findMaterialId ( effectId ); 65 if ( materialIds == 0 ) 66 { 67 std::cerr << "No material for the current effect! " << std::endl; 68 return false; 69 } 70 size_t numMaterials = materialIds->size (); 71 for ( size_t i=0; i<numMaterials; ++i ) 72 { 73 const COLLADAFW::UniqueId& materialId = (*materialIds) [i]; 74 75 // Create the maya effect in depend on the shader type. 76 const COLLADAFW::CommonEffectPointerArray& commonEffects = effect->getCommonEffects (); 77 size_t numCommonEffects = commonEffects.getCount (); 78 if ( numCommonEffects > 1 ) 79 { 80 std::cerr << "Just one common effect can be imported!" << std::endl; 81 } 82 if ( numCommonEffects > 0 ) 83 { 84 for ( size_t i=0; i<1; ++i ) 85 { 86 COLLADAFW::EffectCommon* commonEffect = commonEffects [i]; 87 88 // Import shader data by type. 89 importShaderData ( effect, commonEffect, materialId ); 90 91 // Create the texture placement and push it to the image in a map. 92 createTexturePlacements ( effect, commonEffect ); 93 } 94 } 95 } 96 97 return true; 98 } 99 100 // -------------------------- importShaderData(const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect,const COLLADAFW::UniqueId & materialId)101 void EffectImporter::importShaderData ( 102 const COLLADAFW::Effect* effect, 103 const COLLADAFW::EffectCommon* commonEffect, 104 const COLLADAFW::UniqueId& materialId ) 105 { 106 String effectName = EMPTY_STRING; 107 108 const COLLADAFW::EffectCommon::ShaderType& shaderType = commonEffect->getShaderType (); 109 switch ( shaderType ) 110 { 111 case COLLADAFW::EffectCommon::SHADER_BLINN: 112 effectName = importBlinnShader ( effect, commonEffect, materialId ); 113 break; 114 case COLLADAFW::EffectCommon::SHADER_CONSTANT: 115 // Import as a lambert shader. 116 effectName = importLambertShader ( effect, commonEffect, materialId ); 117 break; 118 case COLLADAFW::EffectCommon::SHADER_PHONG: 119 effectName = importPhongShader ( effect, commonEffect, materialId ); 120 break; 121 case COLLADAFW::EffectCommon::SHADER_LAMBERT: 122 effectName = importLambertShader ( effect, commonEffect, materialId ); 123 break; 124 case COLLADAFW::EffectCommon::SHADER_UNKNOWN: 125 default: 126 // Import as a lambert shader. 127 std::cerr << "Unknown shader type!" << std::endl; 128 effectName = importLambertShader ( effect, commonEffect, materialId ); 129 break; 130 } 131 132 // Create the shader data and add the original collada ids to the new maya effect object. 133 if ( !effectName.empty () ) 134 { 135 // Adds the attributes with the original collada ids. 136 addOriginalColladaIds ( effect, materialId, commonEffect ); 137 138 // TODO 139 // // Add the effect attributes for all the extra tags to the maya effect. 140 // const COLLADAFW::ExtraDataArray& effectExtraDataArray = effect->getExtraDataArray (); 141 142 // Add the material attributes for all the extra tags to the maya effect. 143 const COLLADAFW::Material* material = getDocumentImporter ()->getMaterialById ( materialId ); 144 // const COLLADAFW::ExtraDataArray& materialExtraDataArray = material->getExtraDataArray (); 145 // 146 // // Generate an array with all the extra data elements. 147 // COLLADAFW::ExtraDataArray extraDataArray ( effectExtraDataArray ); 148 // extraDataArray.appendValues ( materialExtraDataArray ); 149 // 150 // // Set the extra data elements. 151 // setExtraData ( extraDataArray ); 152 153 // Create the shader data with the shading engine and the material info of the material. 154 MaterialImporter* materialImporter = getDocumentImporter ()->getMaterialImporter (); 155 String shadingEngineName = effectName + "SG"; 156 materialImporter->createShaderData ( materialId, shadingEngineName ); 157 } 158 } 159 160 //------------------------------ importBlinnShader(const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect,const COLLADAFW::UniqueId & materialId)161 String EffectImporter::importBlinnShader ( 162 const COLLADAFW::Effect* effect, 163 const COLLADAFW::EffectCommon* commonEffect, 164 const COLLADAFW::UniqueId& materialId ) 165 { 166 // Get the material name, which references this effect. 167 MaterialImporter* materialImporter = getDocumentImporter ()->getMaterialImporter (); 168 MayaNode* materialNode = materialImporter->findMayaMaterialNode ( materialId ); 169 String effectName = materialNode->getName (); 170 171 // // TODO Doesn't work on file import, just on file open! 172 // // Check, if the name of the material is "lambert1". This is just possible, if we create a 173 // // shared lambert shader! Otherwise we need another name! 174 // if ( COLLADABU::Utils::equals ( effectName, MaterialImporter::INITIAL_MATERIAL_NAME ) ) 175 // { 176 // // Generate a new name and set it into the maya node. 177 // effectName = generateUniqueDependNodeName ( effectName ); 178 // materialNode->setName ( effectName ); 179 // } 180 181 // Create the effect and write it into the maya ascii file. 182 FILE* file = getDocumentImporter ()->getFile (); 183 MayaDM::Blinn* blinn = new MayaDM::Blinn ( file, effectName ); 184 185 // Import the shader attributes. 186 importStandardShaderAttributes ( blinn, effect ); 187 importLambertShaderAttributes ( SHADER_BLINN, blinn, effect, commonEffect ); 188 importReflectShaderAttributes ( SHADER_BLINN, blinn, effect, commonEffect ); 189 importBlinnShaderAttributes ( blinn, effect, commonEffect ); 190 191 // Push it into the map. 192 const COLLADAFW::UniqueId& effectId = effect->getUniqueId (); 193 appendMayaEffect ( effectId, blinn ); 194 195 return effectName; 196 } 197 198 //------------------------------ importPhongShader(const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect,const COLLADAFW::UniqueId & materialId)199 String EffectImporter::importPhongShader ( 200 const COLLADAFW::Effect* effect, 201 const COLLADAFW::EffectCommon* commonEffect, 202 const COLLADAFW::UniqueId& materialId ) 203 { 204 // Get the material name, which references this effect. 205 MaterialImporter* materialImporter = getDocumentImporter ()->getMaterialImporter (); 206 MayaNode* materialNode = materialImporter->findMayaMaterialNode ( materialId ); 207 String effectName = materialNode->getName (); 208 209 // // TODO Doesn't work on file import, just on file open! 210 // // Check, if the name of the material is "lambert1". This is just possible, if we create a 211 // // shared lambert shader! Otherwise we need another name! 212 // if ( COLLADABU::Utils::equals ( effectName, MaterialImporter::INITIAL_MATERIAL_NAME ) ) 213 // { 214 // // Generate a new name and set it into the maya node. 215 // effectName = generateUniqueDependNodeName ( effectName ); 216 // materialNode->setName ( effectName ); 217 // } 218 219 // Write the effect into the maya ascii file. 220 FILE* file = getDocumentImporter ()->getFile (); 221 MayaDM::Phong* phong = new MayaDM::Phong ( file, effectName ); 222 223 // Import the shader attributes. 224 importStandardShaderAttributes ( phong, effect ); 225 importLambertShaderAttributes ( SHADER_PHONG, phong, effect, commonEffect ); 226 importReflectShaderAttributes ( SHADER_PHONG, phong, effect, commonEffect ); 227 importPhongShaderAttributes ( phong, effect, commonEffect ); 228 229 // Push it into the map. 230 const COLLADAFW::UniqueId& effectId = effect->getUniqueId (); 231 appendMayaEffect ( effectId, phong ); 232 233 return effectName; 234 } 235 236 //------------------------------ importLambertShader(const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect,const COLLADAFW::UniqueId & materialId)237 String EffectImporter::importLambertShader ( 238 const COLLADAFW::Effect* effect, 239 const COLLADAFW::EffectCommon* commonEffect, 240 const COLLADAFW::UniqueId& materialId ) 241 { 242 // Get the material name, which references this effect. 243 MaterialImporter* materialImporter = getDocumentImporter ()->getMaterialImporter (); 244 MayaNode* materialNode = materialImporter->findMayaMaterialNode ( materialId ); 245 String effectName = materialNode->getName (); 246 247 bool shared = false; 248 // // TODO Doesn't work on file import, just on file open! 249 // // Check, if we want to creat a shared shader! 250 // bool shared = false; 251 // if ( COLLADABU::Utils::equals ( effectName, MaterialImporter::INITIAL_MATERIAL_NAME ) ) shared = true; 252 253 // Write the effect into the maya ascii file. 254 FILE* file = getDocumentImporter ()->getFile (); 255 MayaDM::Lambert* lambert = new MayaDM::Lambert ( file, effectName, EMPTY_STRING, shared ); 256 257 // Import the shader attributes. 258 importStandardShaderAttributes ( lambert, effect ); 259 importLambertShaderAttributes ( SHADER_LAMBERT, lambert, effect, commonEffect ); 260 261 // Push it into the map. 262 const COLLADAFW::UniqueId& effectId = effect->getUniqueId (); 263 appendMayaEffect ( effectId, lambert ); 264 265 return effectName; 266 } 267 268 // -------------------------- importStandardShaderAttributes(MayaDM::Lambert * shaderNode,const COLLADAFW::Effect * effect)269 void EffectImporter::importStandardShaderAttributes ( 270 MayaDM::Lambert* shaderNode, 271 const COLLADAFW::Effect* effect ) 272 { 273 // Get the color and set it into the shader node (if it is a valid color). 274 const COLLADAFW::Color& standardColor = effect->getStandardColor (); 275 if ( standardColor.isValid () && standardColor != COLLADAFW::Color::GREY ) 276 { 277 shaderNode->setColor ( MayaDM::float3 ( (float)standardColor.getRed (), (float)standardColor.getGreen (), (float)standardColor.getBlue ()) ); 278 } 279 280 // Push the animation id in a map, if it exist. 281 const COLLADAFW::UniqueId& animationListId = standardColor.getAnimationList (); 282 if ( animationListId.isValid () ) 283 { 284 EffectAnimation effectAnimation; 285 effectAnimation.setAnimationListId ( animationListId ); 286 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 287 effectAnimation.setAnimatedValueType ( EffectAnimation::COLOR_OR_TEXTURE_STANDARD ); 288 289 mEffectAnimationMap [animationListId] = effectAnimation; 290 } 291 292 // Set the diffuse factor to one. 293 shaderNode->setDiffuse ( 1.0f ); 294 } 295 296 // -------------------------- importLambertShaderAttributes(const ShaderType & shaderType,MayaDM::Lambert * shaderNode,const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect)297 void EffectImporter::importLambertShaderAttributes ( 298 const ShaderType& shaderType, 299 MayaDM::Lambert* shaderNode, 300 const COLLADAFW::Effect* effect, 301 const COLLADAFW::EffectCommon* commonEffect ) 302 { 303 // Ambient color 304 const COLLADAFW::ColorOrTexture& ambientColor = commonEffect->getAmbient (); 305 if ( ambientColor.isColor () ) 306 { 307 // Get the color and set it into the shader node (if it is a valid color). 308 const COLLADAFW::Color& color = ambientColor.getColor (); 309 if ( color.isValid () && color != COLLADAFW::Color::BLACK ) 310 shaderNode->setAmbientColor ( MayaDM::float3 ( (float)color.getRed (), (float)color.getGreen (), (float)color.getBlue ()) ); 311 312 // Push the animation id in a map, if it exist. 313 const COLLADAFW::UniqueId& animationListId = color.getAnimationList (); 314 if ( animationListId.isValid () ) 315 { 316 EffectAnimation effectAnimation; 317 effectAnimation.setAnimationListId ( animationListId ); 318 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 319 effectAnimation.setAnimatedValueType ( EffectAnimation::COLOR_OR_TEXTURE_AMBIENT ); 320 321 mEffectAnimationMap [animationListId] = effectAnimation; 322 } 323 } 324 else if ( ambientColor.isTexture () ) 325 { 326 // Get the texure and the current shader attribute. 327 const COLLADAFW::Texture& texture = ambientColor.getTexture (); 328 ShaderAttribute shaderAttribute = ATTR_AMBIENT; 329 330 // Create a shader node attribute and append it on the list of shader node 331 // attributes to the current sampler file id. 332 appendTextureInfo ( effect, texture, shaderType, shaderAttribute, shaderNode ); 333 } 334 else 335 { 336 // The maya default color is grey. The framework/collada uses black. 337 shaderNode->setAmbientColor ( MayaDM::float3 ( 0.0f,0.0f,0.0f ) ); 338 } 339 340 // Diffuse color 341 const COLLADAFW::ColorOrTexture& diffuse = commonEffect->getDiffuse (); 342 if ( diffuse.isColor () ) 343 { 344 // Get the color and set it into the shader node (if it is a valid color). 345 const COLLADAFW::Color& color = diffuse.getColor (); 346 if ( color.isValid () && color != COLLADAFW::Color::BLACK ) 347 shaderNode->setColor ( MayaDM::float3 ( (float)color.getRed (), (float)color.getGreen (), (float)color.getBlue ()) ); 348 349 // Push the animation id in a map, if it exist. 350 const COLLADAFW::UniqueId& animationListId = color.getAnimationList (); 351 if ( animationListId.isValid () ) 352 { 353 EffectAnimation effectAnimation; 354 effectAnimation.setAnimationListId ( animationListId ); 355 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 356 effectAnimation.setAnimatedValueType ( EffectAnimation::COLOR_OR_TEXTURE_DIFFUSE ); 357 358 mEffectAnimationMap [animationListId] = effectAnimation; 359 } 360 } 361 else if ( diffuse.isTexture () ) 362 { 363 // Get the texure and the current shader attribute. 364 const COLLADAFW::Texture& texture = diffuse.getTexture (); 365 ShaderAttribute shaderAttribute = ATTR_COLOR; 366 367 // Create a shader node attribute and append it on the list of shader node 368 // attributes to the current sampler file id. 369 appendTextureInfo ( effect, texture, shaderType, shaderAttribute, shaderNode ); 370 } 371 else 372 { 373 // The maya default color is grey. The framework/collada uses black. 374 shaderNode->setColor ( MayaDM::float3 ( 0.0f,0.0f,0.0f ) ); 375 } 376 377 // Emission 378 const COLLADAFW::ColorOrTexture& emission = commonEffect->getEmission (); 379 if ( emission.isColor () ) 380 { 381 // Get the color and set it into the shader node (if it is a valid color). 382 const COLLADAFW::Color& color = emission.getColor (); 383 if ( color.isValid () && color != COLLADAFW::Color::BLACK ) 384 shaderNode->setIncandescence ( MayaDM::float3 ( (float)color.getRed (), (float)color.getGreen (), (float)color.getBlue ()) ); 385 386 // Push the animation id in a map, if it exist. 387 const COLLADAFW::UniqueId& animationListId = color.getAnimationList (); 388 if ( animationListId.isValid () ) 389 { 390 EffectAnimation effectAnimation; 391 effectAnimation.setAnimationListId ( animationListId ); 392 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 393 effectAnimation.setAnimatedValueType ( EffectAnimation::COLOR_OR_TEXTURE_EMISSION ); 394 395 mEffectAnimationMap [animationListId] = effectAnimation; 396 } 397 } 398 else if ( emission.isTexture () ) 399 { 400 // Get the texure and the current shader attribute. 401 const COLLADAFW::Texture& texture = emission.getTexture (); 402 ShaderAttribute shaderAttribute = ATTR_INCANDESCENE; 403 404 // Create a shader node attribute and append it on the list of shader node 405 // attributes to the current sampler file id. 406 appendTextureInfo ( effect, texture, shaderType, shaderAttribute, shaderNode ); 407 } 408 else 409 { 410 // The maya default color is grey. The framework/collada uses black. 411 shaderNode->setIncandescence ( MayaDM::float3 ( 0.0f,0.0f,0.0f ) ); 412 } 413 414 // Index of refraction 415 const COLLADAFW::FloatOrParam& indexOfRefractionFOP = commonEffect->getIndexOfRefraction (); 416 if ( indexOfRefractionFOP.getType () == COLLADAFW::FloatOrParam::FLOAT ) 417 { 418 float indexOfRefraction = indexOfRefractionFOP.getFloatValue (); 419 if ( indexOfRefraction > 0 ) 420 { 421 shaderNode->setRefractions ( true ); 422 shaderNode->setRefractiveIndex ( indexOfRefraction ); 423 424 // Push the animation id in a map, if it exist. 425 const COLLADAFW::UniqueId& animationListId = indexOfRefractionFOP.getAnimationList (); 426 if ( animationListId.isValid () ) 427 { 428 EffectAnimation effectAnimation; 429 effectAnimation.setAnimationListId ( animationListId ); 430 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 431 effectAnimation.setAnimatedValueType ( EffectAnimation::FLOAT_OR_PARAM_REFRACTIVE_INDEX ); 432 433 mEffectAnimationMap [animationListId] = effectAnimation; 434 } 435 } 436 } 437 438 // Opaque color 439 const COLLADAFW::ColorOrTexture& opaque = commonEffect->getOpacity(); 440 if ( opaque.isColor () ) 441 { 442 // Get the color and set it into the shader node (if it is a valid color). 443 const COLLADAFW::Color& color = opaque.getColor (); 444 if ( color.isValid () && color != COLLADAFW::Color::WHITE ) 445 { 446 // Maya handels the transparency, not the opaque, so we have to invert the color. 447 COLLADAFW::Color transparency ( 1-color.getRed (), 1-color.getGreen (), 1-color.getBlue (), color.getAlpha () ); 448 shaderNode->setTransparency ( MayaDM::float3 ( (float)transparency.getRed (), (float)transparency.getGreen (), (float)transparency.getBlue ()) ); 449 } 450 // Push the animation id in a map, if it exist. 451 const COLLADAFW::UniqueId& animationListId = color.getAnimationList (); 452 if ( animationListId.isValid () ) 453 { 454 EffectAnimation effectAnimation; 455 effectAnimation.setAnimationListId ( animationListId ); 456 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 457 effectAnimation.setAnimatedValueType ( EffectAnimation::COLOR_OR_TEXTURE_TRANSPARENCY ); 458 459 mEffectAnimationMap [animationListId] = effectAnimation; 460 } 461 } 462 else if ( opaque.isTexture () ) 463 { 464 // Get the texure and the current shader attribute. 465 const COLLADAFW::Texture& texture = opaque.getTexture (); 466 ShaderAttribute shaderAttribute = ATTR_TRANSPARENT; 467 468 // Create a shader node attribute and append it on the list of shader node 469 // attributes to the current sampler file id. 470 appendTextureInfo ( effect, texture, shaderType, shaderAttribute, shaderNode ); 471 } 472 else 473 { 474 // The maya default color is grey. The framework/collada uses black. 475 shaderNode->setTransparency ( MayaDM::float3 ( 0.0f,0.0f,0.0f ) ); 476 } 477 } 478 479 // -------------------------- importBlinnShaderAttributes(MayaDM::Blinn * shaderNode,const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect)480 void EffectImporter::importBlinnShaderAttributes ( 481 MayaDM::Blinn* shaderNode, 482 const COLLADAFW::Effect* effect, 483 const COLLADAFW::EffectCommon* commonEffect ) 484 { 485 // Shininess 486 const COLLADAFW::FloatOrParam& shininessFoP = commonEffect->getShininess (); 487 if ( shininessFoP.getType () == COLLADAFW::FloatOrParam::FLOAT ) 488 { 489 float shininess = shininessFoP.getFloatValue (); 490 if ( shininess > 0 ) 491 { 492 // Set the shininess to the blinn's eccentricity. 493 shaderNode->setEccentricity ( shininess ); 494 495 // Push the animation id in a map, if it exist. 496 const COLLADAFW::UniqueId& animationListId = shininessFoP.getAnimationList (); 497 if ( animationListId.isValid () ) 498 { 499 EffectAnimation effectAnimation; 500 effectAnimation.setAnimationListId ( animationListId ); 501 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 502 effectAnimation.setAnimatedValueType ( EffectAnimation::FLOAT_OR_PARAM_ECCENTRICITY ); 503 504 mEffectAnimationMap [animationListId] = effectAnimation; 505 } 506 } 507 } 508 } 509 510 // -------------------------- importPhongShaderAttributes(MayaDM::Phong * shaderNode,const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect)511 void EffectImporter::importPhongShaderAttributes ( 512 MayaDM::Phong* shaderNode, 513 const COLLADAFW::Effect* effect, 514 const COLLADAFW::EffectCommon* commonEffect ) 515 { 516 // Shininess 517 const COLLADAFW::FloatOrParam& shininessFoP = commonEffect->getShininess (); 518 if ( shininessFoP.getType () == COLLADAFW::FloatOrParam::FLOAT ) 519 { 520 float shininess = shininessFoP.getFloatValue (); 521 if ( shininess > 0 ) 522 { 523 // Set the shininess to the phong's cosine power. 524 shaderNode->setCosinePower ( shininess ); 525 526 // Push the animation id in a map, if it exist. 527 const COLLADAFW::UniqueId& animationListId = shininessFoP.getAnimationList (); 528 if ( animationListId.isValid () ) 529 { 530 EffectAnimation effectAnimation; 531 effectAnimation.setAnimationListId ( animationListId ); 532 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 533 effectAnimation.setAnimatedValueType ( EffectAnimation::FLOAT_OR_PARAM_COSINE_POWER ); 534 535 mEffectAnimationMap [animationListId] = effectAnimation; 536 } 537 } 538 } 539 } 540 541 // -------------------------- importReflectShaderAttributes(const ShaderType & shaderType,MayaDM::Reflect * shaderNode,const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect)542 void EffectImporter::importReflectShaderAttributes ( 543 const ShaderType& shaderType, 544 MayaDM::Reflect* shaderNode, 545 const COLLADAFW::Effect* effect, 546 const COLLADAFW::EffectCommon* commonEffect ) 547 { 548 // Reflective 549 const COLLADAFW::ColorOrTexture& reflective = commonEffect->getReflective (); 550 if ( reflective.isColor () ) 551 { 552 // Get the color and set it into the shader node (if it is a valid color). 553 const COLLADAFW::Color& color = reflective.getColor (); 554 if ( color.isValid () && color != COLLADAFW::Color::BLACK ) 555 shaderNode->setReflectedColor ( MayaDM::float3 ( (float)color.getRed (), (float)color.getGreen (), (float)color.getBlue ()) ); 556 557 // Push the animation id in a map, if it exist. 558 const COLLADAFW::UniqueId& animationListId = color.getAnimationList (); 559 if ( animationListId.isValid () ) 560 { 561 EffectAnimation effectAnimation; 562 effectAnimation.setAnimationListId ( animationListId ); 563 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 564 effectAnimation.setAnimatedValueType ( EffectAnimation::COLOR_OR_TEXTURE_REFLECTED ); 565 566 mEffectAnimationMap [animationListId] = effectAnimation; 567 } 568 } 569 else if ( reflective.isTexture () ) 570 { 571 // Get the texure and the current shader attribute. 572 const COLLADAFW::Texture& texture = reflective.getTexture (); 573 ShaderAttribute shaderAttribute = ATTR_REFLECTIVE; 574 575 // Create a shader node attribute and append it on the list of shader node 576 // attributes to the current sampler file id. 577 appendTextureInfo ( effect, texture, shaderType, shaderAttribute, shaderNode ); 578 } 579 else 580 { 581 // The maya default color is grey. The framework/collada uses black. 582 shaderNode->setReflectedColor ( MayaDM::float3 ( 0.0f,0.0f,0.0f ) ); 583 } 584 585 // Reflectivity 586 const COLLADAFW::FloatOrParam& reflectivityFoP = commonEffect->getReflectivity (); 587 if ( reflectivityFoP.getType () == COLLADAFW::FloatOrParam::FLOAT ) 588 { 589 float reflectivity = reflectivityFoP.getFloatValue (); 590 if ( reflectivity > 0 ) 591 { 592 shaderNode->setReflectivity ( reflectivity ); 593 594 // Push the animation id in a map, if it exist. 595 const COLLADAFW::UniqueId& animationListId = reflectivityFoP.getAnimationList (); 596 if ( animationListId.isValid () ) 597 { 598 EffectAnimation effectAnimation; 599 effectAnimation.setAnimationListId ( animationListId ); 600 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 601 effectAnimation.setAnimatedValueType ( EffectAnimation::FLOAT_OR_PARAM_REFLECTIVITY ); 602 603 mEffectAnimationMap [animationListId] = effectAnimation; 604 } 605 } 606 else 607 { 608 shaderNode->setReflectivity ( 0.0f ); 609 } 610 } 611 else if ( reflectivityFoP.getType () == COLLADAFW::FloatOrParam::PARAM ) 612 { 613 // TODO 614 } 615 else 616 { 617 std::cerr << "Unknown param type!" << std::endl; 618 } 619 620 // Specular 621 const COLLADAFW::ColorOrTexture& specular = commonEffect->getSpecular (); 622 if ( specular.isColor () ) 623 { 624 // Get the color and set it into the shader node (if it is a valid color). 625 const COLLADAFW::Color& color = specular.getColor (); 626 if ( color.isValid () && color != COLLADAFW::Color::GREY ) 627 shaderNode->setSpecularColor ( MayaDM::float3 ( (float)color.getRed (), (float)color.getGreen (), (float)color.getBlue ()) ); 628 629 // Push the animation id in a map, if it exist. 630 const COLLADAFW::UniqueId& animationListId = color.getAnimationList (); 631 if ( animationListId.isValid () ) 632 { 633 EffectAnimation effectAnimation; 634 effectAnimation.setAnimationListId ( animationListId ); 635 effectAnimation.setAnimationSourceId ( effect->getUniqueId () ); 636 effectAnimation.setAnimatedValueType ( EffectAnimation::COLOR_OR_TEXTURE_SPECULAR ); 637 638 mEffectAnimationMap [animationListId] = effectAnimation; 639 } 640 } 641 else if ( specular.isTexture () ) 642 { 643 // Get the texure and the current shader attribute. 644 const COLLADAFW::Texture& texture = specular.getTexture (); 645 ShaderAttribute shaderAttribute = ATTR_SPECULAR; 646 647 // Create a shader node attribute and append it on the list of shader node 648 // attributes to the current sampler file id. 649 appendTextureInfo ( effect, texture, shaderType, shaderAttribute, shaderNode ); 650 } 651 else 652 { 653 // The maya default color is grey. The framework/collada uses black. 654 shaderNode->setSpecularColor ( MayaDM::float3 ( 0.0f,0.0f,0.0f ) ); 655 } 656 } 657 658 //------------------------------ addOriginalColladaIds(const COLLADAFW::Effect * effect,const COLLADAFW::UniqueId & materialId,const COLLADAFW::EffectCommon * commonEffect)659 void EffectImporter::addOriginalColladaIds ( 660 const COLLADAFW::Effect* effect, 661 const COLLADAFW::UniqueId& materialId, 662 const COLLADAFW::EffectCommon* commonEffect ) 663 { 664 FILE* file = getDocumentImporter ()->getFile (); 665 666 // Add the original effect id attribute. 667 String colladaEffectId = effect->getOriginalId (); 668 if ( !colladaEffectId.empty () ) 669 { 670 MayaDM::addAttr ( file, COLLADA_EFFECT_ID_ATTRIBUTE_NAME, ATTRIBUTE_DATA_TYPE, ATTRIBUTE_TYPE_STRING ); 671 MayaDM::setAttr ( file, COLLADA_EFFECT_ID_ATTRIBUTE_NAME, ATTRIBUTE_TYPE, ATTRIBUTE_TYPE_STRING, colladaEffectId ); 672 } 673 674 // Add the original id attribute of the material. 675 MaterialImporter* materialImporter = getDocumentImporter ()->getMaterialImporter (); 676 String colladaMaterialId = materialImporter->findOriginalColladaId ( materialId ); 677 if ( !colladaMaterialId.empty () ) 678 { 679 MayaDM::addAttr ( file, COLLADA_MATERIAL_ID_ATTRIBUTE_NAME, ATTRIBUTE_DATA_TYPE, ATTRIBUTE_TYPE_STRING ); 680 MayaDM::setAttr ( file, COLLADA_MATERIAL_ID_ATTRIBUTE_NAME, ATTRIBUTE_TYPE, ATTRIBUTE_TYPE_STRING, colladaMaterialId ); 681 } 682 683 // Add the original id attribute of the effect common. 684 String colladaEffectCommonId = commonEffect->getOriginalId (); 685 if ( !colladaEffectCommonId.empty () ) 686 { 687 MayaDM::addAttr ( file, COLLADA_EFFECT_COMMON_ID_ATTRIBUTE_NAME, ATTRIBUTE_DATA_TYPE, ATTRIBUTE_TYPE_STRING ); 688 MayaDM::setAttr ( file, COLLADA_EFFECT_COMMON_ID_ATTRIBUTE_NAME, ATTRIBUTE_TYPE, ATTRIBUTE_TYPE_STRING, colladaEffectCommonId ); 689 } 690 } 691 692 // -------------------------- findMayaEffects(const COLLADAFW::UniqueId & effectId) const693 const EffectImporter::MayaEffectList* EffectImporter::findMayaEffects ( const COLLADAFW::UniqueId& effectId ) const 694 { 695 UniqueIdMayaEffectsMap::const_iterator it = mMayaEffectsMap.find ( effectId ); 696 if ( it != mMayaEffectsMap.end () ) 697 { 698 return &(it->second); 699 } 700 return 0; 701 } 702 703 // ----------------------------------- findMaterialId(const COLLADAFW::UniqueId & effectId)704 const EffectImporter::UniqueIdVec* EffectImporter::findMaterialId ( const COLLADAFW::UniqueId& effectId ) 705 { 706 UniqueIdUniqueIdsMap::const_iterator it = mEffectIdMaterialIdsMap.find ( effectId ); 707 if ( it == mEffectIdMaterialIdsMap.end () ) 708 { 709 return 0; 710 } 711 712 return &(it->second); 713 } 714 715 // -------------------------- appendMayaEffect(const COLLADAFW::UniqueId & effectId,MayaDM::Lambert * effectNode)716 void EffectImporter::appendMayaEffect ( 717 const COLLADAFW::UniqueId& effectId, 718 MayaDM::Lambert* effectNode ) 719 { 720 mMayaEffectsMap [effectId].push_back ( effectNode ); 721 } 722 723 // -------------------------- assignMaterial(const COLLADAFW::UniqueId & effectId,const COLLADAFW::UniqueId & materialId)724 void EffectImporter::assignMaterial ( 725 const COLLADAFW::UniqueId& effectId, 726 const COLLADAFW::UniqueId& materialId ) 727 { 728 mEffectIdMaterialIdsMap [effectId].push_back ( materialId ); 729 } 730 731 // -------------------------- appendTextureInfo(const COLLADAFW::Effect * effect,const COLLADAFW::Texture & texture,const ShaderType & shaderType,const ShaderAttribute & shaderAttribute,MayaDM::Lambert * shaderNode)732 void EffectImporter::appendTextureInfo ( 733 const COLLADAFW::Effect* effect, 734 const COLLADAFW::Texture &texture, 735 const ShaderType& shaderType, 736 const ShaderAttribute& shaderAttribute, 737 MayaDM::Lambert* shaderNode ) 738 { 739 // Get the effect id. 740 const COLLADAFW::UniqueId& effectId = effect->getUniqueId (); 741 742 // Get the current sampler id. 743 const COLLADAFW::SamplerID& samplerId = texture.getSamplerId (); 744 745 // Get the id of the texture map, that should be used by this texture. It must be bounded 746 // to a set of texture coordinates, when the material is bounded to the geometry. 747 const COLLADAFW::TextureMapId& textureMapId = texture.getTextureMapId (); 748 749 // Save the samplerId to this effect, so we can do the connection one time later. 750 TextureInfo textureInfo; 751 textureInfo.setEffectId ( effectId ); 752 textureInfo.setSamplerId ( samplerId ); 753 textureInfo.setShaderType ( shaderType ); 754 textureInfo.setShaderAttribute ( shaderAttribute ); 755 textureInfo.setShaderNode ( *shaderNode ); 756 textureInfo.setTextureMapId ( textureMapId ); 757 758 // Push the shader node attribute data in the map to the effect. 759 mEffectTextureInfosMap [ effectId ].push_back ( textureInfo ); 760 761 } 762 763 // -------------------------- createTexturePlacements(const COLLADAFW::Effect * effect,const COLLADAFW::EffectCommon * commonEffect)764 void EffectImporter::createTexturePlacements ( 765 const COLLADAFW::Effect* effect, 766 const COLLADAFW::EffectCommon* commonEffect ) 767 { 768 // Get the maya ascii file. 769 FILE* file = getDocumentImporter ()->getFile (); 770 771 // The effect id 772 const COLLADAFW::UniqueId& effectId = effect->getUniqueId (); 773 774 // Get the texture info attributes of the current effect. 775 std::vector<TextureInfo>* textureInfos = findTextureInfos ( effectId ); 776 if ( !textureInfos ) return; 777 778 // Get the list of samplers of the current effect. 779 const COLLADAFW::SamplerPointerArray& samplers = commonEffect->getSamplerPointerArray (); 780 size_t numSamplers = samplers.getCount (); 781 for ( size_t samplerId=0; samplerId<numSamplers; ++samplerId ) 782 { 783 COLLADAFW::Sampler* sampler = samplers [samplerId]; 784 785 const COLLADAFW::UniqueId& imageId = sampler->getSourceImage (); 786 COLLADAFW::Sampler::SamplerType samplerType = sampler->getSamplerType (); 787 788 // Get the texture info which use this sampler. 789 size_t numTextureInfos = textureInfos->size (); 790 for ( size_t i=0; i<numTextureInfos; ++i ) 791 { 792 // Check if the shader node uses the same sampler. 793 TextureInfo& textureInfo = (*textureInfos) [i]; 794 if ( textureInfo.getSamplerId () == samplerId ) 795 { 796 switch ( samplerType ) 797 { 798 case COLLADAFW::Sampler::SAMPLER_TYPE_2D: 799 { 800 // Create this in depend on the texture mapping of effects. 801 // createNode place2dTexture -n "place2dTexture1"; 802 String place2dTextureName = generateUniqueDependNodeName ( PLACE_2D_TEXTURE_NAME, true, true ); 803 MayaDM::Place2dTexture place2dTexture ( file, place2dTextureName ); 804 805 // TODO What to do with this attributes??? 806 sampler->getBorderColor (); 807 sampler->getMagFilter (); 808 sampler->getMinFilter (); 809 sampler->getMipFilter (); 810 sampler->getMipmapBias (); 811 sampler->getMipmapMaxlevel (); 812 //place2dTexture.setWrapU ( sampler->getWrapP () ); 813 //place2dTexture.setWrapV ( sampler->getWrapS () ); 814 sampler->getWrapT (); 815 816 // Set the image, the sampler type and the maya texture placement 817 // into the shader node. 818 textureInfo.setImageId ( imageId ); 819 textureInfo.setSamplerType ( samplerType ); 820 textureInfo.setTexturePlacementNode ( place2dTexture ); 821 822 break; 823 } 824 case COLLADAFW::Sampler::SAMPLER_TYPE_1D: 825 case COLLADAFW::Sampler::SAMPLER_TYPE_3D: 826 case COLLADAFW::Sampler::SAMPLER_TYPE_CUBE: 827 case COLLADAFW::Sampler::SAMPLER_TYPE_DEPTH: 828 case COLLADAFW::Sampler::SAMPLER_TYPE_RECT: 829 case COLLADAFW::Sampler::SAMPLER_TYPE_STATE: 830 case COLLADAFW::Sampler::SAMPLER_TYPE_UNSPECIFIED: 831 default: 832 std::cerr << "Sampler type not implemented: " << samplerType << std::endl; 833 break; 834 } 835 } 836 } 837 } 838 } 839 840 // -------------------------- findTextureInfos(const COLLADAFW::UniqueId & effectId)841 std::vector< EffectImporter::TextureInfo >* EffectImporter::findTextureInfos ( 842 const COLLADAFW::UniqueId& effectId ) 843 { 844 UniqueIdTextureInfosMap::iterator it = mEffectTextureInfosMap.find ( effectId ); 845 if ( it != mEffectTextureInfosMap.end () ) 846 return &it->second; 847 return 0; 848 } 849 850 // -------------------------- findTextureInfo(const COLLADAFW::UniqueId & effectId,const COLLADAFW::TextureMapId & textureMapId)851 const EffectImporter::TextureInfo* EffectImporter::findTextureInfo ( 852 const COLLADAFW::UniqueId& effectId, 853 const COLLADAFW::TextureMapId& textureMapId ) 854 { 855 const std::vector< EffectImporter::TextureInfo >* textureInfos = findTextureInfos ( effectId ); 856 if ( textureInfos == 0 ) return 0; 857 858 std::vector< EffectImporter::TextureInfo >::const_iterator it = textureInfos->begin (); 859 while ( it != textureInfos->end () ) 860 { 861 const EffectImporter::TextureInfo& textureInfo = *it; 862 if ( textureInfo.getTextureMapId () == textureMapId ) 863 return &textureInfo; 864 865 ++it; 866 } 867 return 0; 868 } 869 870 //------------------------------ findEffectAnimation(const COLLADAFW::UniqueId & animationListId)871 const EffectAnimation* EffectImporter::findEffectAnimation ( const COLLADAFW::UniqueId& animationListId ) 872 { 873 const EffectAnimationMap::const_iterator it = mEffectAnimationMap.find ( animationListId ); 874 if ( it != mEffectAnimationMap.end () ) 875 { 876 return &(it->second); 877 } 878 return 0; 879 } 880 881 //------------------------------ isDefaultTextureConnected(const String & imageName) const882 const bool EffectImporter::isDefaultTextureConnected ( const String& imageName ) const 883 { 884 std::vector<String>::const_iterator it = mDefaultTextureImageList.begin (); 885 while ( it != mDefaultTextureImageList.end () ) 886 { 887 if ( COLLADABU::Utils::equals ( imageName , *it ) ) 888 return true; 889 ++it; 890 } 891 return false; 892 } 893 894 // -------------------------- isMaterialConnected(const COLLADAFW::UniqueId & materialId,const String & imageName)895 const bool EffectImporter::isMaterialConnected ( 896 const COLLADAFW::UniqueId& materialId, 897 const String& imageName ) 898 { 899 std::map<COLLADAFW::UniqueId, std::vector<String> >::const_iterator it = mMaterialImagesMap.find ( materialId ); 900 if ( it != mMaterialImagesMap.end () ) 901 { 902 const std::vector<String>& imageNames = it->second; 903 std::vector<String>::const_iterator it2 = imageNames.begin (); 904 while ( it2!= imageNames.end () ) 905 { 906 if ( COLLADABU::Utils::equals ( *it2, imageName ) ) 907 return true; 908 ++it2; 909 } 910 } 911 return false; 912 } 913 914 // -------------------------- writeConnections()915 void EffectImporter::writeConnections () 916 { 917 // Connect the texture placements. 918 connectTexturePlacements (); 919 920 // Connect the file textures and the effects. 921 connectTextures (); 922 } 923 924 // -------------------------- connectTexturePlacements()925 void EffectImporter::connectTexturePlacements () 926 { 927 // Get the maya ascii file. 928 FILE* file = getDocumentImporter ()->getFile (); 929 930 // Writes the connections of the effect texture placements to the image files. 931 UniqueIdTextureInfosMap::const_iterator it = mEffectTextureInfosMap.begin (); 932 while ( it != mEffectTextureInfosMap.end () ) 933 { 934 const std::vector<TextureInfo>& textureInfos = it->second; 935 for ( size_t i=0; i<textureInfos.size (); ++i ) 936 { 937 const TextureInfo& textureInfo = textureInfos [i]; 938 939 const COLLADAFW::UniqueId& imageId = textureInfo.getImageId (); 940 const MayaDM::File& imageFile = textureInfo.getImageNode (); 941 if ( imageFile.getName ().empty () ) 942 { 943 std::cerr << "No image file for the shader node " << textureInfo.getShaderNode ().getName () << "!" << std::endl; 944 continue; 945 } 946 947 const size_t samplerId = textureInfo.getSamplerId (); 948 const COLLADAFW::Sampler::SamplerType& samplerType = textureInfo.getSamplerType (); 949 switch ( samplerType ) 950 { 951 case COLLADAFW::Sampler::SAMPLER_TYPE_2D: 952 { 953 const MayaDM::Place2dTexture& place2dTexture = textureInfo.getTexturePlacementNode (); 954 connectAttr ( file, place2dTexture.getOutUV (), imageFile.getUvCoord () ); 955 connectAttr ( file, place2dTexture.getOutUvFilterSize (), imageFile.getUvFilterSize () ); 956 connectAttr ( file, place2dTexture.getVertexUvOne (), imageFile.getVertexUvOne () ); 957 connectAttr ( file, place2dTexture.getVertexUvTwo (), imageFile.getVertexUvTwo () ); 958 connectAttr ( file, place2dTexture.getVertexUvThree (), imageFile.getVertexUvThree () ); 959 connectAttr ( file, place2dTexture.getVertexCameraOne (), imageFile.getVertexCameraOne () ); 960 connectAttr ( file, place2dTexture.getOffset (), imageFile.getOffset () ); 961 connectAttr ( file, place2dTexture.getStagger (), imageFile.getStagger () ); 962 connectAttr ( file, place2dTexture.getCoverage (), imageFile.getCoverage () ); 963 connectAttr ( file, place2dTexture.getTranslateFrame (), imageFile.getTranslateFrame () ); 964 connectAttr ( file, place2dTexture.getMirrorU (), imageFile.getMirrorU () ); 965 connectAttr ( file, place2dTexture.getMirrorV (), imageFile.getMirrorV () ); 966 connectAttr ( file, place2dTexture.getWrapU (), imageFile.getWrapU () ); 967 connectAttr ( file, place2dTexture.getWrapV (), imageFile.getWrapV () ); 968 connectAttr ( file, place2dTexture.getNoiseUV (), imageFile.getNoiseUV () ); 969 connectAttr ( file, place2dTexture.getRotateUV (), imageFile.getRotateUV () ); 970 connectAttr ( file, place2dTexture.getRepeatUV (), imageFile.getRepeatUV () ); 971 break; 972 } 973 case COLLADAFW::Sampler::SAMPLER_TYPE_1D: 974 case COLLADAFW::Sampler::SAMPLER_TYPE_3D: 975 case COLLADAFW::Sampler::SAMPLER_TYPE_CUBE: 976 case COLLADAFW::Sampler::SAMPLER_TYPE_DEPTH: 977 case COLLADAFW::Sampler::SAMPLER_TYPE_RECT: 978 case COLLADAFW::Sampler::SAMPLER_TYPE_STATE: 979 case COLLADAFW::Sampler::SAMPLER_TYPE_UNSPECIFIED: 980 default: 981 std::cerr << "Sampler type not implemented: " << samplerType << std::endl; 982 break; 983 } 984 } 985 ++it; 986 } 987 } 988 989 //------------------------------ connectTextures()990 void EffectImporter::connectTextures () 991 { 992 // Get the maya ascii file. 993 FILE* file = getDocumentImporter ()->getFile (); 994 995 // Get the current image importer. 996 ImageImporter* imageImporter = getDocumentImporter ()->getImageImporter (); 997 998 // Create the defaultTextureList object 999 MayaDM::DefaultTextureList defaultTextureList ( file, ":" + DEFAULT_TEXTURE_LIST, EMPTY_STRING, false, false ); 1000 1001 EffectImporter* effectImporter = getDocumentImporter ()->getEffectImporter (); 1002 const UniqueIdTextureInfosMap& textureInfos = effectImporter->getTextureInfosMap (); 1003 UniqueIdTextureInfosMap::const_iterator it = textureInfos.begin (); 1004 while ( it != textureInfos.end () ) 1005 { 1006 const COLLADAFW::UniqueId& effectId = it->first; 1007 1008 const std::vector<TextureInfo>& textureInfos = it->second; 1009 for ( size_t i=0; i<textureInfos.size (); ++i ) 1010 { 1011 // Get the current shader node attribute. 1012 const TextureInfo& textureInfo = textureInfos [i]; 1013 1014 // Get the current sampler id. 1015 const COLLADAFW::SamplerID& samplerId = textureInfo.getSamplerId (); 1016 1017 // Get the maya file object. 1018 const COLLADAFW::UniqueId& imageId = textureInfo.getImageId (); 1019 const MayaDM::File& imageFile = textureInfo.getImageNode (); 1020 if ( imageFile.getName ().empty () ) 1021 { 1022 std::cerr << "No image file for the shader node " << textureInfo.getShaderNode ().getName () << "!" << std::endl; 1023 continue; 1024 } 1025 1026 // Check if this connection already exist. 1027 String imageName = imageFile.getName (); 1028 // There can't be anymore some dublicate connections.. 1029 //if ( !isDefaultTextureConnected ( imageName ) ) 1030 { 1031 // connectAttr "file1.message" ":defaultTextureList1.textures" -nextAvailable; 1032 connectNextAttr ( file, imageFile.getMessage (), defaultTextureList.getTextures () ); 1033 //mDefaultTextureImageList.push_back ( imageName ); 1034 } 1035 1036 // Connect the image file out color with the material's texture attribute. 1037 // connectAttr "file1.outColor" "lambert2.color"; 1038 connectTextureAttribute ( textureInfo, &imageFile ); 1039 1040 // Get the current effect's material materialInfo object. 1041 const UniqueIdVec* materialIds = effectImporter->findMaterialId ( effectId ); 1042 if ( materialIds == 0 ) 1043 { 1044 std::cerr << "No material for the current effect! " << std::endl; 1045 continue; 1046 } 1047 size_t numMaterials = materialIds->size (); 1048 for ( size_t i=0; i<numMaterials; ++i ) 1049 { 1050 const COLLADAFW::UniqueId& materialId = (*materialIds) [i]; 1051 1052 // There can't be anymore some dublicate connections.. 1053 // Check if the connection between the image and the material already exist. 1054 //if ( isMaterialConnected ( materialId, imageName ) ) continue; 1055 //mMaterialImagesMap [materialId].push_back ( imageName ); 1056 1057 // Get the maya materialInfo object. 1058 MaterialImporter* materialImporter = getDocumentImporter ()->getMaterialImporter (); 1059 MaterialImporter::ShadingData* shadingData = materialImporter->findShaderData ( materialId ); 1060 if ( shadingData == 0 ) 1061 { 1062 std::cerr << "No material info for current material!" << std::endl; 1063 continue; 1064 } 1065 const MayaDM::MaterialInfo& materialInfo = shadingData->getMaterialInfo (); 1066 1067 // Connect the image file message with the materials materialInfo texture attribute. 1068 // connectAttr "file1.message" "materialInfo1.texture" -nextAvailable; 1069 connectNextAttr ( file, imageFile.getMessage (), materialInfo.getTexture () ); 1070 } 1071 } 1072 1073 ++it; 1074 } 1075 } 1076 1077 //------------------------------ connectTextureAttribute(const TextureInfo & textureInfo,const MayaDM::File * imageFile)1078 void EffectImporter::connectTextureAttribute ( 1079 const TextureInfo& textureInfo, 1080 const MayaDM::File* imageFile ) 1081 { 1082 // Get the maya ascii file. 1083 FILE* file = getDocumentImporter ()->getFile (); 1084 1085 const MayaDM::Lambert& shaderNode = textureInfo.getShaderNode (); 1086 const EffectImporter::ShaderAttribute& shaderAttribute = textureInfo.getShaderAttribute (); 1087 1088 switch ( shaderAttribute ) 1089 { 1090 case EffectImporter::ATTR_COLOR: 1091 { 1092 connectAttr ( file, imageFile->getOutColor (), shaderNode.getColor () ); 1093 break; 1094 } 1095 case EffectImporter::ATTR_AMBIENT: 1096 { 1097 connectAttr ( file, imageFile->getOutColor (), shaderNode.getAmbientColor () ); 1098 break; 1099 } 1100 case EffectImporter::ATTR_INCANDESCENE: 1101 { 1102 connectAttr ( file, imageFile->getOutColor(), shaderNode.getIncandescence () ); 1103 break; 1104 } 1105 case EffectImporter::ATTR_REFLECTIVE: 1106 { 1107 switch ( textureInfo.getShaderType () ) 1108 { 1109 case EffectImporter::SHADER_BLINN: 1110 { 1111 MayaDM::Blinn* blinnNode = ( MayaDM::Blinn* ) (&shaderNode); 1112 connectAttr ( file, imageFile->getOutColor (), blinnNode->getReflectedColor () ); 1113 break; 1114 } 1115 case EffectImporter::SHADER_PHONG: 1116 { 1117 MayaDM::Phong* phongNode = ( MayaDM::Phong* ) (&shaderNode); 1118 connectAttr ( file, imageFile->getOutColor (), phongNode->getReflectedColor () ); 1119 break; 1120 } 1121 default: 1122 std::cerr << "No valid shader type for shader node attribute ATTR_REFLECTIVE." << std::endl; 1123 break; 1124 } 1125 break; 1126 } 1127 case EffectImporter::ATTR_SPECULAR: 1128 { 1129 switch ( textureInfo.getShaderType () ) 1130 { 1131 case EffectImporter::SHADER_BLINN: 1132 { 1133 MayaDM::Blinn* blinnNode = ( MayaDM::Blinn* ) (&shaderNode); 1134 connectAttr ( file, imageFile->getOutColor (), blinnNode->getSpecularColor () ); 1135 break; 1136 } 1137 case EffectImporter::SHADER_PHONG: 1138 { 1139 MayaDM::Phong* phongNode = ( MayaDM::Phong* ) (&shaderNode); 1140 connectAttr ( file, imageFile->getOutColor (), phongNode->getSpecularColor () ); 1141 break; 1142 } 1143 default: 1144 std::cerr << "No valid shader type for shader node attribute ATTR_SPECULAR." << std::endl; 1145 break; 1146 } 1147 break; 1148 } 1149 case EffectImporter::ATTR_TRANSPARENT: 1150 { 1151 connectAttr ( file, imageFile->getOutTransparency (), shaderNode.getTransparency () ); 1152 break; 1153 } 1154 case EffectImporter::ATTR_UNKNOWN: 1155 default: 1156 { 1157 std::cerr << "No valid shader node attribute!" << std::endl; 1158 break; 1159 } 1160 } 1161 } 1162 1163 1164 } // namespace DAE2MA 1165