colladaAppMaterial.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. String cleanString(const String& str)
  30. {
  31. String cleanStr(str);
  32. // Replace invalid characters with underscores
  33. const String badChars(" -,.+=*/");
  34. for (String::SizeType i = 0; i < badChars.length(); i++)
  35. cleanStr.replace(badChars[i], '_');
  36. // Prefix with an underscore if string starts with a number
  37. if ((cleanStr[0] >= '0') && (cleanStr[0] <= '9'))
  38. cleanStr.insert(0, '_');
  39. return cleanStr;
  40. }
  41. //------------------------------------------------------------------------------
  42. ColladaAppMaterial::ColladaAppMaterial(const char* matName)
  43. : mat(0),
  44. effect(0),
  45. effectExt(0)
  46. {
  47. name = matName;
  48. // Set some defaults
  49. flags |= TSMaterialList::S_Wrap;
  50. flags |= TSMaterialList::T_Wrap;
  51. diffuseColor = LinearColorF::ONE;
  52. specularColor = LinearColorF::ONE;
  53. smoothness = 0.0f;
  54. metalness = 0.0f;
  55. doubleSided = false;
  56. }
  57. ColladaAppMaterial::ColladaAppMaterial(const domMaterial *pMat)
  58. : mat(pMat),
  59. diffuseColor(LinearColorF::ONE),
  60. specularColor(LinearColorF::ONE),
  61. smoothness(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. const domCommon_color_or_texture_type_complexType* domSpecular = findEffectSpecular(effect);
  72. // Wrap flags
  73. if (effectExt->wrapU)
  74. flags |= TSMaterialList::S_Wrap;
  75. if (effectExt->wrapV)
  76. flags |= TSMaterialList::T_Wrap;
  77. // Set material attributes
  78. if (commonProfile) {
  79. F32 transparency = 0.0f;
  80. if (commonProfile->getTechnique()->getConstant()) {
  81. const domProfile_COMMON::domTechnique::domConstant* constant = commonProfile->getTechnique()->getConstant();
  82. diffuseColor.set(1.0f, 1.0f, 1.0f, 1.0f);
  83. resolveColor(constant->getReflective(), &specularColor);
  84. resolveFloat(constant->getReflectivity(), &smoothness);
  85. resolveTransparency(constant, &transparency);
  86. }
  87. else if (commonProfile->getTechnique()->getLambert()) {
  88. const domProfile_COMMON::domTechnique::domLambert* lambert = commonProfile->getTechnique()->getLambert();
  89. resolveColor(lambert->getDiffuse(), &diffuseColor);
  90. resolveColor(lambert->getReflective(), &specularColor);
  91. resolveFloat(lambert->getReflectivity(), &smoothness);
  92. resolveTransparency(lambert, &transparency);
  93. }
  94. else if (commonProfile->getTechnique()->getPhong()) {
  95. const domProfile_COMMON::domTechnique::domPhong* phong = commonProfile->getTechnique()->getPhong();
  96. resolveColor(phong->getDiffuse(), &diffuseColor);
  97. resolveColor(phong->getSpecular(), &specularColor);
  98. resolveFloat(phong->getShininess(), &metalness);
  99. resolveTransparency(phong, &transparency);
  100. }
  101. else if (commonProfile->getTechnique()->getBlinn()) {
  102. const domProfile_COMMON::domTechnique::domBlinn* blinn = commonProfile->getTechnique()->getBlinn();
  103. resolveColor(blinn->getDiffuse(), &diffuseColor);
  104. resolveColor(blinn->getSpecular(), &specularColor);
  105. resolveFloat(blinn->getShininess(), &metalness);
  106. resolveTransparency(blinn, &transparency);
  107. }
  108. // Normalize specularPower (1-128). Values > 1 are assumed to be
  109. // already normalized.
  110. if (smoothness <= 1.0f)
  111. smoothness *= 128;
  112. smoothness = mClampF(smoothness, 1.0f, 128.0f);
  113. // Set translucency
  114. if (transparency != 0.0f) {
  115. flags |= TSMaterialList::Translucent;
  116. if (transparency > 1.0f) {
  117. flags |= TSMaterialList::Additive;
  118. diffuseColor.alpha = transparency - 1.0f;
  119. }
  120. else if (transparency < 0.0f) {
  121. flags |= TSMaterialList::Subtractive;
  122. diffuseColor.alpha = -transparency;
  123. }
  124. else {
  125. diffuseColor.alpha = transparency;
  126. }
  127. }
  128. else
  129. diffuseColor.alpha = 1.0f;
  130. }
  131. // Double-sided flag
  132. doubleSided = effectExt->double_sided;
  133. // Get the paths for the various textures => Collada indirection at its finest!
  134. // <texture>.<newparam>.<sampler2D>.<source>.<newparam>.<surface>.<init_from>.<image>.<init_from>
  135. diffuseMap = getSamplerImagePath(effect, getTextureSampler(effect, domDiffuse));
  136. specularMap = getSamplerImagePath(effect, getTextureSampler(effect, domSpecular));
  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->mSpecularMapFilename[0] = specularMap;
  189. newMat->mDiffuse[0] = diffuseColor;
  190. newMat->mSmoothness[0] = smoothness;
  191. newMat->mMetalness[0] = metalness;
  192. newMat->mDoubleSided = doubleSided;
  193. newMat->mTranslucent = (bool)(flags & TSMaterialList::Translucent);
  194. newMat->mTranslucentBlendOp = blendOp;
  195. return newMat;
  196. }