colladaAppMaterial.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. specularColor = LinearColorF::ONE;
  55. smoothness = 0.0f;
  56. metalness = 0.0f;
  57. doubleSided = false;
  58. }
  59. ColladaAppMaterial::ColladaAppMaterial(const domMaterial *pMat)
  60. : mat(pMat),
  61. diffuseColor(LinearColorF::ONE),
  62. specularColor(LinearColorF::ONE),
  63. smoothness(0.0f),
  64. metalness(0.0f),
  65. doubleSided(false)
  66. {
  67. // Get the effect element for this material
  68. effect = daeSafeCast<domEffect>(mat->getInstance_effect()->getUrl().getElement());
  69. effectExt = new ColladaExtension_effect(effect);
  70. // Get the <profile_COMMON>, <diffuse> and <specular> elements
  71. const domProfile_COMMON* commonProfile = ColladaUtils::findEffectCommonProfile(effect);
  72. const domCommon_color_or_texture_type_complexType* domDiffuse = findEffectDiffuse(effect);
  73. const domCommon_color_or_texture_type_complexType* domSpecular = findEffectSpecular(effect);
  74. // Wrap flags
  75. if (effectExt->wrapU)
  76. flags |= TSMaterialList::S_Wrap;
  77. if (effectExt->wrapV)
  78. flags |= TSMaterialList::T_Wrap;
  79. // Set material attributes
  80. if (commonProfile) {
  81. F32 transparency = 0.0f;
  82. if (commonProfile->getTechnique()->getConstant()) {
  83. const domProfile_COMMON::domTechnique::domConstant* constant = commonProfile->getTechnique()->getConstant();
  84. diffuseColor.set(1.0f, 1.0f, 1.0f, 1.0f);
  85. resolveColor(constant->getReflective(), &specularColor);
  86. resolveFloat(constant->getReflectivity(), &smoothness);
  87. resolveTransparency(constant, &transparency);
  88. }
  89. else if (commonProfile->getTechnique()->getLambert()) {
  90. const domProfile_COMMON::domTechnique::domLambert* lambert = commonProfile->getTechnique()->getLambert();
  91. resolveColor(lambert->getDiffuse(), &diffuseColor);
  92. resolveColor(lambert->getReflective(), &specularColor);
  93. resolveFloat(lambert->getReflectivity(), &smoothness);
  94. resolveTransparency(lambert, &transparency);
  95. }
  96. else if (commonProfile->getTechnique()->getPhong()) {
  97. const domProfile_COMMON::domTechnique::domPhong* phong = commonProfile->getTechnique()->getPhong();
  98. resolveColor(phong->getDiffuse(), &diffuseColor);
  99. resolveColor(phong->getSpecular(), &specularColor);
  100. resolveFloat(phong->getShininess(), &metalness);
  101. resolveTransparency(phong, &transparency);
  102. }
  103. else if (commonProfile->getTechnique()->getBlinn()) {
  104. const domProfile_COMMON::domTechnique::domBlinn* blinn = commonProfile->getTechnique()->getBlinn();
  105. resolveColor(blinn->getDiffuse(), &diffuseColor);
  106. resolveColor(blinn->getSpecular(), &specularColor);
  107. resolveFloat(blinn->getShininess(), &metalness);
  108. resolveTransparency(blinn, &transparency);
  109. }
  110. // Normalize specularPower (1-128). Values > 1 are assumed to be
  111. // already normalized.
  112. if (smoothness <= 1.0f)
  113. smoothness *= 128;
  114. smoothness = mClampF(smoothness, 1.0f, 128.0f);
  115. // Set translucency
  116. if (transparency != 0.0f) {
  117. flags |= TSMaterialList::Translucent;
  118. if (transparency > 1.0f) {
  119. flags |= TSMaterialList::Additive;
  120. diffuseColor.alpha = transparency - 1.0f;
  121. }
  122. else if (transparency < 0.0f) {
  123. flags |= TSMaterialList::Subtractive;
  124. diffuseColor.alpha = -transparency;
  125. }
  126. else {
  127. diffuseColor.alpha = transparency;
  128. }
  129. }
  130. else
  131. diffuseColor.alpha = 1.0f;
  132. }
  133. // Double-sided flag
  134. doubleSided = effectExt->double_sided;
  135. // Get the paths for the various textures => Collada indirection at its finest!
  136. // <texture>.<newparam>.<sampler2D>.<source>.<newparam>.<surface>.<init_from>.<image>.<init_from>
  137. diffuseMap = getSamplerImagePath(effect, getTextureSampler(effect, domDiffuse));
  138. specularMap = getSamplerImagePath(effect, getTextureSampler(effect, domSpecular));
  139. normalMap = getSamplerImagePath(effect, effectExt->bumpSampler);
  140. // Set the material name
  141. name = ColladaUtils::getOptions().matNamePrefix;
  142. if ( ColladaUtils::getOptions().useDiffuseNames )
  143. {
  144. Torque::Path diffusePath( diffuseMap );
  145. name += diffusePath.getFileName();
  146. }
  147. else
  148. {
  149. name += _GetNameOrId(mat);
  150. }
  151. }
  152. void ColladaAppMaterial::resolveFloat(const domCommon_float_or_param_type* value, F32* dst)
  153. {
  154. if (value && value->getFloat()) {
  155. *dst = value->getFloat()->getValue();
  156. }
  157. }
  158. void ColladaAppMaterial::resolveColor(const domCommon_color_or_texture_type* value, LinearColorF* dst)
  159. {
  160. if (value && value->getColor()) {
  161. dst->red = value->getColor()->getValue()[0];
  162. dst->green = value->getColor()->getValue()[1];
  163. dst->blue = value->getColor()->getValue()[2];
  164. dst->alpha = value->getColor()->getValue()[3];
  165. }
  166. }
  167. // Generate a new Material object
  168. Material *ColladaAppMaterial::createMaterial(const Torque::Path& path) const
  169. {
  170. // The filename and material name are used as TorqueScript identifiers, so
  171. // clean them up first
  172. String cleanFile = cleanString(TSShapeLoader::getShapePath().getFileName());
  173. String cleanName = cleanString(getName());
  174. // Prefix the material name with the filename (if not done already by TSShapeConstructor prefix)
  175. if (!cleanName.startsWith(cleanFile))
  176. cleanName = cleanFile + "_" + cleanName;
  177. // Determine the blend operation for this material
  178. Material::BlendOp blendOp = (flags & TSMaterialList::Translucent) ? Material::LerpAlpha : Material::None;
  179. if (flags & TSMaterialList::Additive)
  180. blendOp = Material::Add;
  181. else if (flags & TSMaterialList::Subtractive)
  182. blendOp = Material::Sub;
  183. // Create the Material definition
  184. const String oldScriptFile = Con::getVariable("$Con::File");
  185. Con::setVariable("$Con::File", path.getFullPath()); // modify current script path so texture lookups are correct
  186. Material *newMat = MATMGR->allocateAndRegister( cleanName, getName() );
  187. Con::setVariable("$Con::File", oldScriptFile); // restore script path
  188. newMat->mDiffuseMapFilename[0] = diffuseMap;
  189. newMat->mNormalMapFilename[0] = normalMap;
  190. newMat->mSpecularMapFilename[0] = specularMap;
  191. newMat->mDiffuse[0] = diffuseColor;
  192. newMat->mSmoothness[0] = smoothness;
  193. newMat->mMetalness[0] = metalness;
  194. newMat->mDoubleSided = doubleSided;
  195. newMat->mTranslucent = (bool)(flags & TSMaterialList::Translucent);
  196. newMat->mTranslucentBlendOp = blendOp;
  197. return newMat;
  198. }