colladaAppMaterial.cpp 8.5 KB

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