colladaAppMaterial.cpp 8.4 KB

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