colladaAppMaterial.h 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef _COLLADA_APP_MATERIAL_H_
  23. #define _COLLADA_APP_MATERIAL_H_
  24. #ifndef _APPMATERIAL_H_
  25. #include "ts/loader/appMaterial.h"
  26. #endif
  27. #ifndef _COLLADA_EXTENSIONS_H_
  28. #include "ts/collada/colladaExtensions.h"
  29. #endif
  30. class Material;
  31. class ColladaAppMaterial : public AppMaterial
  32. {
  33. public:
  34. const domMaterial* mat; ///< Collada <material> element
  35. domEffect* effect; ///< Collada <effect> element
  36. ColladaExtension_effect* effectExt; ///< effect extension
  37. String name; ///< Name of this material (cleaned)
  38. // Settings extracted from the Collada file, and optionally saved to materials.tscript
  39. String diffuseMap;
  40. String normalMap;
  41. LinearColorF diffuseColor;
  42. F32 roughness;
  43. F32 metalness;
  44. bool doubleSided;
  45. ColladaAppMaterial(const char* matName);
  46. ColladaAppMaterial(const domMaterial* pMat);
  47. ~ColladaAppMaterial() { delete effectExt; }
  48. String getName() const { return name; }
  49. void resolveFloat(const domCommon_float_or_param_type* value, F32* dst);
  50. void resolveColor(const domCommon_color_or_texture_type* value, LinearColorF* dst);
  51. // Determine the material transparency
  52. template<class T> void resolveTransparency(const T shader, F32* dst)
  53. {
  54. // Start out by getting the <transparency> value
  55. *dst = 1.0f;
  56. resolveFloat(shader->getTransparency(), dst);
  57. // Multiply the transparency by the transparent color
  58. LinearColorF transColor(1.0f, 1.0f, 1.0f, 1.0f);
  59. if (shader->getTransparent() && shader->getTransparent()->getColor()) {
  60. const domCommon_color_or_texture_type::domColor* color = shader->getTransparent()->getColor();
  61. transColor.set(color->getValue()[0], color->getValue()[1], color->getValue()[2], color->getValue()[3]);
  62. }
  63. if (!shader->getTransparent() || (shader->getTransparent()->getOpaque() == FX_OPAQUE_ENUM_A_ONE)) {
  64. // multiply by alpha value and invert (so 1.0 is fully opaque)
  65. *dst = 1.0f - (*dst * transColor.alpha);
  66. }
  67. else {
  68. // multiply by average of the RGB values
  69. F32 avg = (transColor.red + transColor.blue + transColor.green) / 3;
  70. *dst *= avg;
  71. }
  72. }
  73. Material *createMaterial(const Torque::Path& path) const;
  74. };
  75. #endif // _COLLADA_APP_MATERIAL_H_