FBXDeformer.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*************************************************************************/
  2. /* FBXDeformer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. /*
  31. Open Asset Import Library (assimp)
  32. ----------------------------------------------------------------------
  33. Copyright (c) 2006-2019, assimp team
  34. All rights reserved.
  35. Redistribution and use of this software in source and binary forms,
  36. with or without modification, are permitted provided that the
  37. following conditions are met:
  38. * Redistributions of source code must retain the above
  39. copyright notice, this list of conditions and the
  40. following disclaimer.
  41. * Redistributions in binary form must reproduce the above
  42. copyright notice, this list of conditions and the
  43. following disclaimer in the documentation and/or other
  44. materials provided with the distribution.
  45. * Neither the name of the assimp team, nor the names of its
  46. contributors may be used to endorse or promote products
  47. derived from this software without specific prior
  48. written permission of the assimp team.
  49. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  50. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  52. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  53. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  54. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  55. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  56. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  57. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. ----------------------------------------------------------------------
  61. */
  62. /** @file FBXNoteAttribute.cpp
  63. * @brief Assimp::FBX::NodeAttribute (and subclasses) implementation
  64. */
  65. #include "FBXDocument.h"
  66. #include "FBXDocumentUtil.h"
  67. #include "FBXMeshGeometry.h"
  68. #include "FBXParser.h"
  69. #include "core/math/math_funcs.h"
  70. #include "core/math/transform.h"
  71. #include <iostream>
  72. namespace FBXDocParser {
  73. using namespace Util;
  74. // ------------------------------------------------------------------------------------------------
  75. Deformer::Deformer(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name) :
  76. Object(id, element, name) {
  77. const ScopePtr sc = GetRequiredScope(element);
  78. const std::string &classname = ParseTokenAsString(GetRequiredToken(element, 2));
  79. props = GetPropertyTable(doc, "Deformer.Fbx" + classname, element, sc, true);
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. Deformer::~Deformer() {
  83. }
  84. Constraint::Constraint(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name) :
  85. Object(id, element, name) {
  86. const ScopePtr sc = GetRequiredScope(element);
  87. const std::string &classname = ParseTokenAsString(GetRequiredToken(element, 2));
  88. // used something.fbx as this is a cache name.
  89. props = GetPropertyTable(doc, "Something.Fbx" + classname, element, sc, true);
  90. }
  91. Constraint::~Constraint() {
  92. }
  93. // ------------------------------------------------------------------------------------------------
  94. Cluster::Cluster(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name) :
  95. Deformer(id, element, doc, name), valid_transformAssociateModel(false) {
  96. const ScopePtr sc = GetRequiredScope(element);
  97. // for( auto element : sc.Elements())
  98. // {
  99. // std::cout << "cluster element: " << element.first << std::endl;
  100. // }
  101. //
  102. // element: Indexes
  103. // element: Transform
  104. // element: TransformAssociateModel
  105. // element: TransformLink
  106. // element: UserData
  107. // element: Version
  108. // element: Weights
  109. const ElementPtr Indexes = sc->GetElement("Indexes");
  110. const ElementPtr Weights = sc->GetElement("Weights");
  111. const ElementPtr TransformAssociateModel = sc->GetElement("TransformAssociateModel");
  112. if (TransformAssociateModel != nullptr) {
  113. //Transform t = ReadMatrix(*TransformAssociateModel);
  114. link_mode = SkinLinkMode_Additive;
  115. valid_transformAssociateModel = true;
  116. } else {
  117. link_mode = SkinLinkMode_Normalized;
  118. valid_transformAssociateModel = false;
  119. }
  120. const ElementPtr Transform = GetRequiredElement(sc, "Transform", element);
  121. const ElementPtr TransformLink = GetRequiredElement(sc, "TransformLink", element);
  122. // todo: check if we need this
  123. //const Element& TransformAssociateModel = GetRequiredElement(sc, "TransformAssociateModel", &element);
  124. transform = ReadMatrix(Transform);
  125. transformLink = ReadMatrix(TransformLink);
  126. // it is actually possible that there be Deformer's with no weights
  127. if (!!Indexes != !!Weights) {
  128. DOMError("either Indexes or Weights are missing from Cluster", element);
  129. }
  130. if (Indexes) {
  131. ParseVectorDataArray(indices, Indexes);
  132. ParseVectorDataArray(weights, Weights);
  133. }
  134. if (indices.size() != weights.size()) {
  135. DOMError("sizes of index and weight array don't match up", element);
  136. }
  137. // read assigned node
  138. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "Model");
  139. for (const Connection *con : conns) {
  140. const Model *mod = ProcessSimpleConnection<Model>(*con, false, "Model -> Cluster", element);
  141. if (mod) {
  142. node = mod;
  143. break;
  144. }
  145. }
  146. if (!node) {
  147. DOMError("failed to read target Node for Cluster", element);
  148. node = nullptr;
  149. }
  150. }
  151. // ------------------------------------------------------------------------------------------------
  152. Cluster::~Cluster() {
  153. }
  154. // ------------------------------------------------------------------------------------------------
  155. Skin::Skin(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name) :
  156. Deformer(id, element, doc, name), accuracy(0.0f) {
  157. const ScopePtr sc = GetRequiredScope(element);
  158. // keep this it is used for debugging and any FBX format changes
  159. // for (auto element : sc.Elements()) {
  160. // std::cout << "skin element: " << element.first << std::endl;
  161. // }
  162. const ElementPtr Link_DeformAcuracy = sc->GetElement("Link_DeformAcuracy");
  163. if (Link_DeformAcuracy) {
  164. accuracy = ParseTokenAsFloat(GetRequiredToken(Link_DeformAcuracy, 0));
  165. }
  166. const ElementPtr SkinType = sc->GetElement("SkinningType");
  167. if (SkinType) {
  168. std::string skin_type = ParseTokenAsString(GetRequiredToken(SkinType, 0));
  169. if (skin_type == "Linear") {
  170. skinType = Skin_Linear;
  171. } else if (skin_type == "Rigid") {
  172. skinType = Skin_Rigid;
  173. } else if (skin_type == "DualQuaternion") {
  174. skinType = Skin_DualQuaternion;
  175. } else if (skin_type == "Blend") {
  176. skinType = Skin_Blend;
  177. } else {
  178. print_error("[doc:skin] could not find valid skin type: " + String(skin_type.c_str()));
  179. }
  180. }
  181. // resolve assigned clusters
  182. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "Deformer");
  183. //
  184. clusters.reserve(conns.size());
  185. for (const Connection *con : conns) {
  186. const Cluster *cluster = ProcessSimpleConnection<Cluster>(*con, false, "Cluster -> Skin", element);
  187. if (cluster) {
  188. clusters.push_back(cluster);
  189. continue;
  190. }
  191. }
  192. }
  193. // ------------------------------------------------------------------------------------------------
  194. Skin::~Skin() {
  195. }
  196. // ------------------------------------------------------------------------------------------------
  197. BlendShape::BlendShape(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name) :
  198. Deformer(id, element, doc, name) {
  199. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "Deformer");
  200. blendShapeChannels.reserve(conns.size());
  201. for (const Connection *con : conns) {
  202. const BlendShapeChannel *bspc = ProcessSimpleConnection<BlendShapeChannel>(*con, false, "BlendShapeChannel -> BlendShape", element);
  203. if (bspc) {
  204. blendShapeChannels.push_back(bspc);
  205. continue;
  206. }
  207. }
  208. }
  209. // ------------------------------------------------------------------------------------------------
  210. BlendShape::~BlendShape() {
  211. }
  212. // ------------------------------------------------------------------------------------------------
  213. BlendShapeChannel::BlendShapeChannel(uint64_t id, const ElementPtr element, const Document &doc, const std::string &name) :
  214. Deformer(id, element, doc, name) {
  215. const ScopePtr sc = GetRequiredScope(element);
  216. const ElementPtr DeformPercent = sc->GetElement("DeformPercent");
  217. if (DeformPercent) {
  218. percent = ParseTokenAsFloat(GetRequiredToken(DeformPercent, 0));
  219. }
  220. const ElementPtr FullWeights = sc->GetElement("FullWeights");
  221. if (FullWeights) {
  222. ParseVectorDataArray(fullWeights, FullWeights);
  223. }
  224. const std::vector<const Connection *> &conns = doc.GetConnectionsByDestinationSequenced(ID(), "Geometry");
  225. shapeGeometries.reserve(conns.size());
  226. for (const Connection *con : conns) {
  227. const ShapeGeometry *const sg = ProcessSimpleConnection<ShapeGeometry>(*con, false, "Shape -> BlendShapeChannel", element);
  228. if (sg) {
  229. shapeGeometries.push_back(sg);
  230. continue;
  231. }
  232. }
  233. }
  234. // ------------------------------------------------------------------------------------------------
  235. BlendShapeChannel::~BlendShapeChannel() {
  236. }
  237. // ------------------------------------------------------------------------------------------------
  238. } // namespace FBXDocParser