assimpAppMaterial.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "ts/loader/appSequence.h"
  24. #include "ts/assimp/assimpAppMaterial.h"
  25. #include "ts/assimp/assimpAppMesh.h"
  26. #include "materials/materialManager.h"
  27. #include "ts/tsMaterialList.h"
  28. // assimp include files.
  29. #include <assimp/cimport.h>
  30. #include <assimp/scene.h>
  31. #include <assimp/postprocess.h>
  32. #include <assimp/types.h>
  33. String AppMaterial::cleanString(const String& str)
  34. {
  35. String cleanStr(str);
  36. // Replace invalid characters with underscores
  37. const String badChars(" -,.+=*/");
  38. for (String::SizeType i = 0; i < badChars.length(); i++)
  39. cleanStr.replace(badChars[i], '_');
  40. // Prefix with an underscore if string starts with a number
  41. if ((cleanStr[0] >= '0') && (cleanStr[0] <= '9'))
  42. cleanStr.insert(0, '_');
  43. return cleanStr;
  44. }
  45. AssimpAppMaterial::AssimpAppMaterial(const char* matName)
  46. {
  47. name = matName;
  48. diffuseColor = LinearColorF::ONE;
  49. specularColor = LinearColorF::ONE;
  50. specularPower = 0.8f;
  51. doubleSided = false;
  52. // Set some defaults
  53. flags |= TSMaterialList::S_Wrap;
  54. flags |= TSMaterialList::T_Wrap;
  55. }
  56. AssimpAppMaterial::AssimpAppMaterial(const struct aiMaterial* mtl)
  57. {
  58. aiString matName;
  59. mtl->Get(AI_MATKEY_NAME, matName);
  60. name = matName.C_Str();
  61. if ( name.isEmpty() )
  62. name = "defaultMaterial";
  63. Con::printf("[ASSIMP] Loaded Material: %s", matName.C_Str());
  64. // Opacity
  65. F32 opacity = 0.0f;
  66. mtl->Get(AI_MATKEY_OPACITY, opacity);
  67. // Diffuse color
  68. aiColor3D diff_color (0.f, 0.f, 0.f);
  69. mtl->Get(AI_MATKEY_COLOR_DIFFUSE, diff_color);
  70. diffuseColor = LinearColorF(diff_color.r, diff_color.g, diff_color.b, opacity);
  71. // Spec Color color
  72. aiColor3D spec_color (0.f, 0.f, 0.f);
  73. mtl->Get(AI_MATKEY_COLOR_DIFFUSE, spec_color );
  74. specularColor = LinearColorF(spec_color.r, spec_color.g, spec_color.b, 1.0f);
  75. // Specular Power
  76. mtl->Get(AI_MATKEY_SHININESS_STRENGTH, specularPower);
  77. // Double-Sided
  78. S32 dbl_sided = 0;
  79. mtl->Get(AI_MATKEY_TWOSIDED, dbl_sided);
  80. doubleSided = (dbl_sided != 0);
  81. // Set some defaults
  82. flags |= TSMaterialList::S_Wrap;
  83. flags |= TSMaterialList::T_Wrap;
  84. }
  85. Material* AssimpAppMaterial::createMaterial(const Torque::Path& path) const
  86. {
  87. // The filename and material name are used as TorqueScript identifiers, so
  88. // clean them up first
  89. String cleanFile = cleanString(TSShapeLoader::getShapePath().getFileName());
  90. String cleanName = cleanString(getName());
  91. // Prefix the material name with the filename (if not done already by TSShapeConstructor prefix)
  92. //if (!cleanName.startsWith(cleanFile))
  93. // cleanName = cleanFile + "_" + cleanName;
  94. // Determine the blend operation for this material
  95. Material::BlendOp blendOp = (flags & TSMaterialList::Translucent) ? Material::LerpAlpha : Material::None;
  96. if (flags & TSMaterialList::Additive)
  97. blendOp = Material::Add;
  98. else if (flags & TSMaterialList::Subtractive)
  99. blendOp = Material::Sub;
  100. // Create the Material definition
  101. const String oldScriptFile = Con::getVariable("$Con::File");
  102. Con::setVariable("$Con::File", path.getFullPath()); // modify current script path so texture lookups are correct
  103. Material *newMat = MATMGR->allocateAndRegister( cleanName, getName() );
  104. Con::setVariable("$Con::File", oldScriptFile); // restore script path
  105. newMat->mDiffuseMapFilename[0] = "";
  106. newMat->mNormalMapFilename[0] = "";
  107. newMat->mSpecularMapFilename[0] = "";
  108. newMat->mDiffuse[0] = diffuseColor;
  109. //newMat->mSpecular[0] = specularColor;
  110. //newMat->mSpecularPower[0] = specularPower;
  111. newMat->mDoubleSided = doubleSided;
  112. newMat->mTranslucent = (bool)(flags & TSMaterialList::Translucent);
  113. newMat->mTranslucentBlendOp = blendOp;
  114. return newMat;
  115. }