colladaAppMaterial.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/tsShapeLoader.h"
  24. #include "ts/collada/colladaAppMaterial.h"
  25. #include "ts/collada/colladaUtils.h"
  26. #include "ts/tsMaterialList.h"
  27. #include "materials/materialManager.h"
  28. using namespace ColladaUtils;
  29. #ifndef TORQUE_ASSIMP
  30. String AppMaterial::cleanString(const String& str)
  31. {
  32. String cleanStr(str);
  33. // Replace invalid characters with underscores
  34. const String badChars(" -,.+=*/");
  35. for (String::SizeType i = 0; i < badChars.length(); i++)
  36. cleanStr.replace(badChars[i], '_');
  37. // Prefix with an underscore if string starts with a number
  38. if ((cleanStr[0] >= '0') && (cleanStr[0] <= '9'))
  39. cleanStr.insert(0, '_');
  40. return cleanStr;
  41. }
  42. #endif // !TORQUE_ASSIMP
  43. //------------------------------------------------------------------------------
  44. ColladaAppMaterial::ColladaAppMaterial(const char* matName)
  45. : mat(0),
  46. effect(0),
  47. effectExt(0)
  48. {
  49. name = matName;
  50. // Set some defaults
  51. flags |= TSMaterialList::S_Wrap;
  52. flags |= TSMaterialList::T_Wrap;
  53. diffuseColor = LinearColorF::ONE;
  54. roughness = 0.0f;
  55. metalness = 0.0f;
  56. doubleSided = false;
  57. }
  58. ColladaAppMaterial::ColladaAppMaterial(const domMaterial *pMat)
  59. : mat(pMat),
  60. diffuseColor(LinearColorF::ONE),
  61. roughness(0.0f),
  62. metalness(0.0f),
  63. doubleSided(false)
  64. {
  65. // Get the effect element for this material
  66. effect = daeSafeCast<domEffect>(mat->getInstance_effect()->getUrl().getElement());
  67. effectExt = new ColladaExtension_effect(effect);
  68. // Get the <profile_COMMON>, <diffuse> and <specular> elements
  69. const domProfile_COMMON* commonProfile = ColladaUtils::findEffectCommonProfile(effect);
  70. const domCommon_color_or_texture_type_complexType* domDiffuse = findEffectDiffuse(effect);
  71. // Wrap flags
  72. if (effectExt->wrapU)
  73. flags |= TSMaterialList::S_Wrap;
  74. if (effectExt->wrapV)
  75. flags |= TSMaterialList::T_Wrap;
  76. // Set material attributes
  77. if (commonProfile) {
  78. F32 transparency = 0.0f;
  79. if (commonProfile->getTechnique()->getConstant()) {
  80. const domProfile_COMMON::domTechnique::domConstant* constant = commonProfile->getTechnique()->getConstant();
  81. diffuseColor.set(1.0f, 1.0f, 1.0f, 1.0f);
  82. resolveFloat(constant->getReflectivity(), &metalness);
  83. resolveTransparency(constant, &transparency);
  84. }
  85. else if (commonProfile->getTechnique()->getLambert()) {
  86. const domProfile_COMMON::domTechnique::domLambert* lambert = commonProfile->getTechnique()->getLambert();
  87. resolveColor(lambert->getDiffuse(), &diffuseColor);
  88. resolveFloat(lambert->getReflectivity(), &metalness);
  89. resolveTransparency(lambert, &transparency);
  90. }
  91. else if (commonProfile->getTechnique()->getPhong()) {
  92. const domProfile_COMMON::domTechnique::domPhong* phong = commonProfile->getTechnique()->getPhong();
  93. resolveColor(phong->getDiffuse(), &diffuseColor);
  94. resolveFloat(phong->getShininess(), &roughness);
  95. resolveTransparency(phong, &transparency);
  96. }
  97. else if (commonProfile->getTechnique()->getBlinn()) {
  98. const domProfile_COMMON::domTechnique::domBlinn* blinn = commonProfile->getTechnique()->getBlinn();
  99. resolveColor(blinn->getDiffuse(), &diffuseColor);
  100. resolveFloat(blinn->getShininess(), &roughness);
  101. resolveTransparency(blinn, &transparency);
  102. }
  103. // Normalize specularPower (1-128). Values > 1 are assumed to be
  104. // already normalized.
  105. if (roughness <= 1.0f)
  106. roughness *= 128;
  107. roughness = mClampF(roughness, 1.0f, 128.0f);
  108. // Set translucency
  109. if (transparency != 0.0f) {
  110. flags |= TSMaterialList::Translucent;
  111. if (transparency > 1.0f) {
  112. flags |= TSMaterialList::Additive;
  113. diffuseColor.alpha = transparency - 1.0f;
  114. }
  115. else if (transparency < 0.0f) {
  116. flags |= TSMaterialList::Subtractive;
  117. diffuseColor.alpha = -transparency;
  118. }
  119. else {
  120. diffuseColor.alpha = transparency;
  121. }
  122. }
  123. else
  124. diffuseColor.alpha = 1.0f;
  125. }
  126. // Double-sided flag
  127. doubleSided = effectExt->double_sided;
  128. // Get the paths for the various textures => Collada indirection at its finest!
  129. // <texture>.<newparam>.<sampler2D>.<source>.<newparam>.<surface>.<init_from>.<image>.<init_from>
  130. diffuseMap = getSamplerImagePath(effect, getTextureSampler(effect, domDiffuse));
  131. normalMap = getSamplerImagePath(effect, effectExt->bumpSampler);
  132. // Set the material name
  133. name = ColladaUtils::getOptions().matNamePrefix;
  134. if ( ColladaUtils::getOptions().useDiffuseNames )
  135. {
  136. Torque::Path diffusePath( diffuseMap );
  137. name += diffusePath.getFileName();
  138. }
  139. else
  140. {
  141. name += _GetNameOrId(mat);
  142. }
  143. }
  144. void ColladaAppMaterial::resolveFloat(const domCommon_float_or_param_type* value, F32* dst)
  145. {
  146. if (value && value->getFloat()) {
  147. *dst = value->getFloat()->getValue();
  148. }
  149. }
  150. void ColladaAppMaterial::resolveColor(const domCommon_color_or_texture_type* value, LinearColorF* dst)
  151. {
  152. if (value && value->getColor()) {
  153. dst->red = value->getColor()->getValue()[0];
  154. dst->green = value->getColor()->getValue()[1];
  155. dst->blue = value->getColor()->getValue()[2];
  156. dst->alpha = value->getColor()->getValue()[3];
  157. }
  158. }
  159. // Generate a new Material object
  160. Material *ColladaAppMaterial::createMaterial(const Torque::Path& path) const
  161. {
  162. // The filename and material name are used as TorqueScript identifiers, so
  163. // clean them up first
  164. String cleanFile = cleanString(TSShapeLoader::getShapePath().getFileName());
  165. String cleanName = cleanString(getName());
  166. // Prefix the material name with the filename (if not done already by TSShapeConstructor prefix)
  167. if (!cleanName.startsWith(cleanFile))
  168. cleanName = cleanFile + "_" + cleanName;
  169. // Determine the blend operation for this material
  170. Material::BlendOp blendOp = (flags & TSMaterialList::Translucent) ? Material::LerpAlpha : Material::None;
  171. if (flags & TSMaterialList::Additive)
  172. blendOp = Material::Add;
  173. else if (flags & TSMaterialList::Subtractive)
  174. blendOp = Material::Sub;
  175. // Create the Material definition
  176. const String oldScriptFile = Con::getVariable("$Con::File");
  177. Con::setVariable("$Con::File", path.getFullPath()); // modify current script path so texture lookups are correct
  178. Material *newMat = MATMGR->allocateAndRegister( cleanName, getName() );
  179. Con::setVariable("$Con::File", oldScriptFile); // restore script path
  180. newMat->mDiffuseMapName[0] = diffuseMap;
  181. newMat->mNormalMapName[0] = normalMap;
  182. newMat->mDiffuse[0] = diffuseColor;
  183. newMat->mRoughness[0] = roughness;
  184. newMat->mMetalness[0] = metalness;
  185. newMat->mDoubleSided = doubleSided;
  186. newMat->mTranslucent = (bool)(flags & TSMaterialList::Translucent);
  187. newMat->mTranslucentBlendOp = blendOp;
  188. return newMat;
  189. }