FBXMeshGeometry.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file FBXMeshGeometry.cpp
  34. * @brief Assimp::FBX::MeshGeometry implementation
  35. */
  36. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  37. #include <functional>
  38. #include "FBXMeshGeometry.h"
  39. #include "FBXDocument.h"
  40. #include "FBXImporter.h"
  41. #include "FBXImportSettings.h"
  42. #include "FBXDocumentUtil.h"
  43. namespace Assimp {
  44. namespace FBX {
  45. using namespace Util;
  46. // ------------------------------------------------------------------------------------------------
  47. Geometry::Geometry(uint64_t id, const Element& element, const std::string& name, const Document& doc) :
  48. Object(id, element, name), skin() {
  49. const std::vector<const Connection*> &conns = doc.GetConnectionsByDestinationSequenced(ID(),"Deformer");
  50. for(const Connection* con : conns) {
  51. const Skin* const sk = ProcessSimpleConnection<Skin>(*con, false, "Skin -> Geometry", element);
  52. if(sk) {
  53. skin = sk;
  54. }
  55. const BlendShape* const bsp = ProcessSimpleConnection<BlendShape>(*con, false, "BlendShape -> Geometry", element);
  56. if (bsp) {
  57. auto pr = blendShapes.insert(bsp);
  58. if (!pr.second) {
  59. FBXImporter::LogWarn("there is the same blendShape id ", bsp->ID());
  60. }
  61. }
  62. }
  63. }
  64. // ------------------------------------------------------------------------------------------------
  65. const std::unordered_set<const BlendShape*>& Geometry::GetBlendShapes() const {
  66. return blendShapes;
  67. }
  68. // ------------------------------------------------------------------------------------------------
  69. const Skin* Geometry::DeformerSkin() const {
  70. return skin;
  71. }
  72. // ------------------------------------------------------------------------------------------------
  73. MeshGeometry::MeshGeometry(uint64_t id, const Element& element, const std::string& name, const Document& doc)
  74. : Geometry(id, element,name, doc)
  75. {
  76. const Scope* sc = element.Compound();
  77. if (!sc) {
  78. DOMError("failed to read Geometry object (class: Mesh), no data scope found");
  79. }
  80. // must have Mesh elements:
  81. const Element& Vertices = GetRequiredElement(*sc,"Vertices",&element);
  82. const Element& PolygonVertexIndex = GetRequiredElement(*sc,"PolygonVertexIndex",&element);
  83. // optional Mesh elements:
  84. const ElementCollection& Layer = sc->GetCollection("Layer");
  85. std::vector<aiVector3D> tempVerts;
  86. ParseVectorDataArray(tempVerts,Vertices);
  87. if(tempVerts.empty()) {
  88. FBXImporter::LogWarn("encountered mesh with no vertices");
  89. }
  90. std::vector<int> tempFaces;
  91. ParseVectorDataArray(tempFaces,PolygonVertexIndex);
  92. if(tempFaces.empty()) {
  93. FBXImporter::LogWarn("encountered mesh with no faces");
  94. }
  95. m_vertices.reserve(tempFaces.size());
  96. m_faces.reserve(tempFaces.size() / 3);
  97. m_mapping_offsets.resize(tempVerts.size());
  98. m_mapping_counts.resize(tempVerts.size(),0);
  99. m_mappings.resize(tempFaces.size());
  100. const size_t vertex_count = tempVerts.size();
  101. // generate output vertices, computing an adjacency table to
  102. // preserve the mapping from fbx indices to *this* indexing.
  103. unsigned int count = 0;
  104. for(int index : tempFaces) {
  105. const int absi = index < 0 ? (-index - 1) : index;
  106. if(static_cast<size_t>(absi) >= vertex_count) {
  107. DOMError("polygon vertex index out of range",&PolygonVertexIndex);
  108. }
  109. m_vertices.push_back(tempVerts[absi]);
  110. ++count;
  111. ++m_mapping_counts[absi];
  112. if (index < 0) {
  113. m_faces.push_back(count);
  114. count = 0;
  115. }
  116. }
  117. unsigned int cursor = 0;
  118. for (size_t i = 0, e = tempVerts.size(); i < e; ++i) {
  119. m_mapping_offsets[i] = cursor;
  120. cursor += m_mapping_counts[i];
  121. m_mapping_counts[i] = 0;
  122. }
  123. cursor = 0;
  124. for(int index : tempFaces) {
  125. const int absi = index < 0 ? (-index - 1) : index;
  126. m_mappings[m_mapping_offsets[absi] + m_mapping_counts[absi]++] = cursor++;
  127. }
  128. // if settings.readAllLayers is true:
  129. // * read all layers, try to load as many vertex channels as possible
  130. // if settings.readAllLayers is false:
  131. // * read only the layer with index 0, but warn about any further layers
  132. for (ElementMap::const_iterator it = Layer.first; it != Layer.second; ++it) {
  133. const TokenList& tokens = (*it).second->Tokens();
  134. const char* err;
  135. const int index = ParseTokenAsInt(*tokens[0], err);
  136. if(err) {
  137. DOMError(err,&element);
  138. }
  139. if(doc.Settings().readAllLayers || index == 0) {
  140. const Scope& layer = GetRequiredScope(*(*it).second);
  141. ReadLayer(layer);
  142. } else {
  143. FBXImporter::LogWarn("ignoring additional geometry layers");
  144. }
  145. }
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. const std::vector<aiVector3D>& MeshGeometry::GetVertices() const {
  149. return m_vertices;
  150. }
  151. // ------------------------------------------------------------------------------------------------
  152. const std::vector<aiVector3D>& MeshGeometry::GetNormals() const {
  153. return m_normals;
  154. }
  155. // ------------------------------------------------------------------------------------------------
  156. const std::vector<aiVector3D>& MeshGeometry::GetTangents() const {
  157. return m_tangents;
  158. }
  159. // ------------------------------------------------------------------------------------------------
  160. const std::vector<aiVector3D>& MeshGeometry::GetBinormals() const {
  161. return m_binormals;
  162. }
  163. // ------------------------------------------------------------------------------------------------
  164. const std::vector<unsigned int>& MeshGeometry::GetFaceIndexCounts() const {
  165. return m_faces;
  166. }
  167. // ------------------------------------------------------------------------------------------------
  168. const std::vector<aiVector2D>& MeshGeometry::GetTextureCoords( unsigned int index ) const {
  169. static const std::vector<aiVector2D> empty;
  170. return index >= AI_MAX_NUMBER_OF_TEXTURECOORDS ? empty : m_uvs[ index ];
  171. }
  172. std::string MeshGeometry::GetTextureCoordChannelName( unsigned int index ) const {
  173. return index >= AI_MAX_NUMBER_OF_TEXTURECOORDS ? "" : m_uvNames[ index ];
  174. }
  175. const std::vector<aiColor4D>& MeshGeometry::GetVertexColors( unsigned int index ) const {
  176. static const std::vector<aiColor4D> empty;
  177. return index >= AI_MAX_NUMBER_OF_COLOR_SETS ? empty : m_colors[ index ];
  178. }
  179. const MatIndexArray& MeshGeometry::GetMaterialIndices() const {
  180. return m_materials;
  181. }
  182. // ------------------------------------------------------------------------------------------------
  183. const unsigned int* MeshGeometry::ToOutputVertexIndex( unsigned int in_index, unsigned int& count ) const {
  184. if ( in_index >= m_mapping_counts.size() ) {
  185. return nullptr;
  186. }
  187. ai_assert( m_mapping_counts.size() == m_mapping_offsets.size() );
  188. count = m_mapping_counts[ in_index ];
  189. ai_assert( m_mapping_offsets[ in_index ] + count <= m_mappings.size() );
  190. return &m_mappings[ m_mapping_offsets[ in_index ] ];
  191. }
  192. // ------------------------------------------------------------------------------------------------
  193. unsigned int MeshGeometry::FaceForVertexIndex( unsigned int in_index ) const {
  194. ai_assert( in_index < m_vertices.size() );
  195. // in the current conversion pattern this will only be needed if
  196. // weights are present, so no need to always pre-compute this table
  197. if ( m_facesVertexStartIndices.empty() ) {
  198. m_facesVertexStartIndices.resize( m_faces.size() + 1, 0 );
  199. std::partial_sum( m_faces.begin(), m_faces.end(), m_facesVertexStartIndices.begin() + 1 );
  200. m_facesVertexStartIndices.pop_back();
  201. }
  202. ai_assert( m_facesVertexStartIndices.size() == m_faces.size() );
  203. const std::vector<unsigned int>::iterator it = std::upper_bound(
  204. m_facesVertexStartIndices.begin(),
  205. m_facesVertexStartIndices.end(),
  206. in_index
  207. );
  208. return static_cast< unsigned int >( std::distance( m_facesVertexStartIndices.begin(), it - 1 ) );
  209. }
  210. // ------------------------------------------------------------------------------------------------
  211. void MeshGeometry::ReadLayer(const Scope& layer)
  212. {
  213. const ElementCollection& LayerElement = layer.GetCollection("LayerElement");
  214. for (ElementMap::const_iterator eit = LayerElement.first; eit != LayerElement.second; ++eit) {
  215. const Scope& elayer = GetRequiredScope(*(*eit).second);
  216. ReadLayerElement(elayer);
  217. }
  218. }
  219. // ------------------------------------------------------------------------------------------------
  220. void MeshGeometry::ReadLayerElement(const Scope& layerElement)
  221. {
  222. const Element& Type = GetRequiredElement(layerElement,"Type");
  223. const Element& TypedIndex = GetRequiredElement(layerElement,"TypedIndex");
  224. const std::string& type = ParseTokenAsString(GetRequiredToken(Type,0));
  225. const int typedIndex = ParseTokenAsInt(GetRequiredToken(TypedIndex,0));
  226. const Scope& top = GetRequiredScope(element);
  227. const ElementCollection candidates = top.GetCollection(type);
  228. for (ElementMap::const_iterator it = candidates.first; it != candidates.second; ++it) {
  229. const int index = ParseTokenAsInt(GetRequiredToken(*(*it).second,0));
  230. if(index == typedIndex) {
  231. ReadVertexData(type,typedIndex,GetRequiredScope(*(*it).second));
  232. return;
  233. }
  234. }
  235. FBXImporter::LogError("failed to resolve vertex layer element: ",
  236. type, ", index: ", typedIndex);
  237. }
  238. // ------------------------------------------------------------------------------------------------
  239. void MeshGeometry::ReadVertexData(const std::string& type, int index, const Scope& source)
  240. {
  241. const std::string& MappingInformationType = ParseTokenAsString(GetRequiredToken(
  242. GetRequiredElement(source,"MappingInformationType"),0)
  243. );
  244. const std::string& ReferenceInformationType = ParseTokenAsString(GetRequiredToken(
  245. GetRequiredElement(source,"ReferenceInformationType"),0)
  246. );
  247. if (type == "LayerElementUV") {
  248. if(index >= AI_MAX_NUMBER_OF_TEXTURECOORDS) {
  249. FBXImporter::LogError("ignoring UV layer, maximum number of UV channels exceeded: ",
  250. index, " (limit is ", AI_MAX_NUMBER_OF_TEXTURECOORDS, ")" );
  251. return;
  252. }
  253. const Element* Name = source["Name"];
  254. m_uvNames[index] = std::string();
  255. if(Name) {
  256. m_uvNames[index] = ParseTokenAsString(GetRequiredToken(*Name,0));
  257. }
  258. ReadVertexDataUV(m_uvs[index],source,
  259. MappingInformationType,
  260. ReferenceInformationType
  261. );
  262. }
  263. else if (type == "LayerElementMaterial") {
  264. if (m_materials.size() > 0) {
  265. FBXImporter::LogError("ignoring additional material layer");
  266. return;
  267. }
  268. std::vector<int> temp_materials;
  269. ReadVertexDataMaterials(temp_materials,source,
  270. MappingInformationType,
  271. ReferenceInformationType
  272. );
  273. // sometimes, there will be only negative entries. Drop the material
  274. // layer in such a case (I guess it means a default material should
  275. // be used). This is what the converter would do anyway, and it
  276. // avoids losing the material if there are more material layers
  277. // coming of which at least one contains actual data (did observe
  278. // that with one test file).
  279. const size_t count_neg = std::count_if(temp_materials.begin(),temp_materials.end(),[](int n) { return n < 0; });
  280. if(count_neg == temp_materials.size()) {
  281. FBXImporter::LogWarn("ignoring dummy material layer (all entries -1)");
  282. return;
  283. }
  284. std::swap(temp_materials, m_materials);
  285. }
  286. else if (type == "LayerElementNormal") {
  287. if (m_normals.size() > 0) {
  288. FBXImporter::LogError("ignoring additional normal layer");
  289. return;
  290. }
  291. ReadVertexDataNormals(m_normals,source,
  292. MappingInformationType,
  293. ReferenceInformationType
  294. );
  295. }
  296. else if (type == "LayerElementTangent") {
  297. if (m_tangents.size() > 0) {
  298. FBXImporter::LogError("ignoring additional tangent layer");
  299. return;
  300. }
  301. ReadVertexDataTangents(m_tangents,source,
  302. MappingInformationType,
  303. ReferenceInformationType
  304. );
  305. }
  306. else if (type == "LayerElementBinormal") {
  307. if (m_binormals.size() > 0) {
  308. FBXImporter::LogError("ignoring additional binormal layer");
  309. return;
  310. }
  311. ReadVertexDataBinormals(m_binormals,source,
  312. MappingInformationType,
  313. ReferenceInformationType
  314. );
  315. }
  316. else if (type == "LayerElementColor") {
  317. if(index >= AI_MAX_NUMBER_OF_COLOR_SETS) {
  318. FBXImporter::LogError("ignoring vertex color layer, maximum number of color sets exceeded: ",
  319. index, " (limit is ", AI_MAX_NUMBER_OF_COLOR_SETS, ")" );
  320. return;
  321. }
  322. ReadVertexDataColors(m_colors[index],source,
  323. MappingInformationType,
  324. ReferenceInformationType
  325. );
  326. }
  327. }
  328. // ------------------------------------------------------------------------------------------------
  329. // Lengthy utility function to read and resolve a FBX vertex data array - that is, the
  330. // output is in polygon vertex order. This logic is used for reading normals, UVs, colors,
  331. // tangents ..
  332. template <typename T>
  333. void ResolveVertexDataArray(std::vector<T>& data_out, const Scope& source,
  334. const std::string& MappingInformationType,
  335. const std::string& ReferenceInformationType,
  336. const char* dataElementName,
  337. const char* indexDataElementName,
  338. size_t vertex_count,
  339. const std::vector<unsigned int>& mapping_counts,
  340. const std::vector<unsigned int>& mapping_offsets,
  341. const std::vector<unsigned int>& mappings)
  342. {
  343. bool isDirect = ReferenceInformationType == "Direct";
  344. bool isIndexToDirect = ReferenceInformationType == "IndexToDirect";
  345. const bool hasDataElement = HasElement(source, dataElementName);
  346. const bool hasIndexDataElement = HasElement(source, indexDataElementName);
  347. // fall-back to direct data if there is no index data element
  348. if (isIndexToDirect && !hasIndexDataElement) {
  349. isDirect = true;
  350. isIndexToDirect = false;
  351. }
  352. // handle permutations of Mapping and Reference type - it would be nice to
  353. // deal with this more elegantly and with less redundancy, but right
  354. // now it seems unavoidable.
  355. if (MappingInformationType == "ByVertice" && isDirect) {
  356. if (!hasDataElement) {
  357. FBXImporter::LogWarn("missing data element: ", dataElementName);
  358. return;
  359. }
  360. std::vector<T> tempData;
  361. ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName));
  362. if (tempData.size() != mapping_offsets.size()) {
  363. FBXImporter::LogError("length of input data unexpected for ByVertice mapping: ",
  364. tempData.size(), ", expected ", mapping_offsets.size());
  365. return;
  366. }
  367. data_out.resize(vertex_count);
  368. for (size_t i = 0, e = tempData.size(); i < e; ++i) {
  369. const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i];
  370. for (unsigned int j = istart; j < iend; ++j) {
  371. data_out[mappings[j]] = tempData[i];
  372. }
  373. }
  374. }
  375. else if (MappingInformationType == "ByVertice" && isIndexToDirect) {
  376. std::vector<T> tempData;
  377. if (!hasDataElement || !hasIndexDataElement) {
  378. if (!hasDataElement)
  379. FBXImporter::LogWarn("missing data element: ", dataElementName);
  380. if (!hasIndexDataElement)
  381. FBXImporter::LogWarn("missing index data element: ", indexDataElementName);
  382. return;
  383. }
  384. ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName));
  385. std::vector<int> uvIndices;
  386. ParseVectorDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));
  387. if (uvIndices.size() != mapping_offsets.size()) {
  388. FBXImporter::LogError("length of input data unexpected for ByVertice mapping: ",
  389. uvIndices.size(), ", expected ", mapping_offsets.size());
  390. return;
  391. }
  392. data_out.resize(vertex_count);
  393. for (size_t i = 0, e = uvIndices.size(); i < e; ++i) {
  394. const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i];
  395. for (unsigned int j = istart; j < iend; ++j) {
  396. if (static_cast<size_t>(uvIndices[i]) >= tempData.size()) {
  397. DOMError("index out of range",&GetRequiredElement(source,indexDataElementName));
  398. }
  399. data_out[mappings[j]] = tempData[uvIndices[i]];
  400. }
  401. }
  402. }
  403. else if (MappingInformationType == "ByPolygonVertex" && isDirect) {
  404. if (!hasDataElement) {
  405. FBXImporter::LogWarn("missing data element: ", dataElementName);
  406. return;
  407. }
  408. std::vector<T> tempData;
  409. ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName));
  410. if (tempData.size() != vertex_count) {
  411. FBXImporter::LogError("length of input data unexpected for ByPolygon mapping: ",
  412. tempData.size(), ", expected ", vertex_count
  413. );
  414. return;
  415. }
  416. data_out.swap(tempData);
  417. }
  418. else if (MappingInformationType == "ByPolygonVertex" && isIndexToDirect) {
  419. std::vector<T> tempData;
  420. if (!hasDataElement || !hasIndexDataElement) {
  421. if (!hasDataElement)
  422. FBXImporter::LogWarn("missing data element: ", dataElementName);
  423. if (!hasIndexDataElement)
  424. FBXImporter::LogWarn("missing index data element: ", indexDataElementName);
  425. return;
  426. }
  427. ParseVectorDataArray(tempData, GetRequiredElement(source, dataElementName));
  428. std::vector<int> uvIndices;
  429. ParseVectorDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));
  430. if (uvIndices.size() > vertex_count) {
  431. FBXImporter::LogWarn("trimming length of input array for ByPolygonVertex mapping: ",
  432. uvIndices.size(), ", expected ", vertex_count);
  433. uvIndices.resize(vertex_count);
  434. }
  435. if (uvIndices.size() != vertex_count) {
  436. FBXImporter::LogError("length of input data unexpected for ByPolygonVertex mapping: ",
  437. uvIndices.size(), ", expected ", vertex_count);
  438. return;
  439. }
  440. data_out.resize(vertex_count);
  441. const T empty;
  442. unsigned int next = 0;
  443. for(int i : uvIndices) {
  444. if ( -1 == i ) {
  445. data_out[ next++ ] = empty;
  446. continue;
  447. }
  448. if (static_cast<size_t>(i) >= tempData.size()) {
  449. DOMError("index out of range",&GetRequiredElement(source,indexDataElementName));
  450. }
  451. data_out[next++] = tempData[i];
  452. }
  453. }
  454. else {
  455. FBXImporter::LogError("ignoring vertex data channel, access type not implemented: ",
  456. MappingInformationType, ",", ReferenceInformationType);
  457. }
  458. }
  459. // ------------------------------------------------------------------------------------------------
  460. void MeshGeometry::ReadVertexDataNormals(std::vector<aiVector3D>& normals_out, const Scope& source,
  461. const std::string& MappingInformationType,
  462. const std::string& ReferenceInformationType)
  463. {
  464. ResolveVertexDataArray(normals_out,source,MappingInformationType,ReferenceInformationType,
  465. "Normals",
  466. "NormalsIndex",
  467. m_vertices.size(),
  468. m_mapping_counts,
  469. m_mapping_offsets,
  470. m_mappings);
  471. }
  472. // ------------------------------------------------------------------------------------------------
  473. void MeshGeometry::ReadVertexDataUV(std::vector<aiVector2D>& uv_out, const Scope& source,
  474. const std::string& MappingInformationType,
  475. const std::string& ReferenceInformationType)
  476. {
  477. ResolveVertexDataArray(uv_out,source,MappingInformationType,ReferenceInformationType,
  478. "UV",
  479. "UVIndex",
  480. m_vertices.size(),
  481. m_mapping_counts,
  482. m_mapping_offsets,
  483. m_mappings);
  484. }
  485. // ------------------------------------------------------------------------------------------------
  486. void MeshGeometry::ReadVertexDataColors(std::vector<aiColor4D>& colors_out, const Scope& source,
  487. const std::string& MappingInformationType,
  488. const std::string& ReferenceInformationType)
  489. {
  490. ResolveVertexDataArray(colors_out,source,MappingInformationType,ReferenceInformationType,
  491. "Colors",
  492. "ColorIndex",
  493. m_vertices.size(),
  494. m_mapping_counts,
  495. m_mapping_offsets,
  496. m_mappings);
  497. }
  498. // ------------------------------------------------------------------------------------------------
  499. static const char *TangentIndexToken = "TangentIndex";
  500. static const char *TangentsIndexToken = "TangentsIndex";
  501. void MeshGeometry::ReadVertexDataTangents(std::vector<aiVector3D>& tangents_out, const Scope& source,
  502. const std::string& MappingInformationType,
  503. const std::string& ReferenceInformationType)
  504. {
  505. const char * str = source.Elements().count( "Tangents" ) > 0 ? "Tangents" : "Tangent";
  506. const char * strIdx = source.Elements().count( "Tangents" ) > 0 ? TangentsIndexToken : TangentIndexToken;
  507. ResolveVertexDataArray(tangents_out,source,MappingInformationType,ReferenceInformationType,
  508. str,
  509. strIdx,
  510. m_vertices.size(),
  511. m_mapping_counts,
  512. m_mapping_offsets,
  513. m_mappings);
  514. }
  515. // ------------------------------------------------------------------------------------------------
  516. static const char * BinormalIndexToken = "BinormalIndex";
  517. static const char * BinormalsIndexToken = "BinormalsIndex";
  518. void MeshGeometry::ReadVertexDataBinormals(std::vector<aiVector3D>& binormals_out, const Scope& source,
  519. const std::string& MappingInformationType,
  520. const std::string& ReferenceInformationType)
  521. {
  522. const char * str = source.Elements().count( "Binormals" ) > 0 ? "Binormals" : "Binormal";
  523. const char * strIdx = source.Elements().count( "Binormals" ) > 0 ? BinormalsIndexToken : BinormalIndexToken;
  524. ResolveVertexDataArray(binormals_out,source,MappingInformationType,ReferenceInformationType,
  525. str,
  526. strIdx,
  527. m_vertices.size(),
  528. m_mapping_counts,
  529. m_mapping_offsets,
  530. m_mappings);
  531. }
  532. // ------------------------------------------------------------------------------------------------
  533. void MeshGeometry::ReadVertexDataMaterials(std::vector<int>& materials_out, const Scope& source,
  534. const std::string& MappingInformationType,
  535. const std::string& ReferenceInformationType)
  536. {
  537. const size_t face_count = m_faces.size();
  538. if( 0 == face_count )
  539. {
  540. return;
  541. }
  542. if (source["Materials"]) {
  543. // materials are handled separately. First of all, they are assigned per-face
  544. // and not per polyvert. Secondly, ReferenceInformationType=IndexToDirect
  545. // has a slightly different meaning for materials.
  546. ParseVectorDataArray(materials_out, GetRequiredElement(source, "Materials"));
  547. }
  548. if (MappingInformationType == "AllSame") {
  549. // easy - same material for all faces
  550. if (materials_out.empty()) {
  551. FBXImporter::LogError("expected material index, ignoring");
  552. return;
  553. } else if (materials_out.size() > 1) {
  554. FBXImporter::LogWarn("expected only a single material index, ignoring all except the first one");
  555. materials_out.clear();
  556. }
  557. materials_out.resize(m_vertices.size());
  558. std::fill(materials_out.begin(), materials_out.end(), materials_out.at(0));
  559. } else if (MappingInformationType == "ByPolygon" && ReferenceInformationType == "IndexToDirect") {
  560. materials_out.resize(face_count);
  561. if(materials_out.size() != face_count) {
  562. FBXImporter::LogError("length of input data unexpected for ByPolygon mapping: ",
  563. materials_out.size(), ", expected ", face_count
  564. );
  565. return;
  566. }
  567. } else {
  568. FBXImporter::LogError("ignoring material assignments, access type not implemented: ",
  569. MappingInformationType, ",", ReferenceInformationType);
  570. }
  571. }
  572. // ------------------------------------------------------------------------------------------------
  573. ShapeGeometry::ShapeGeometry(uint64_t id, const Element& element, const std::string& name, const Document& doc)
  574. : Geometry(id, element, name, doc) {
  575. const Scope *sc = element.Compound();
  576. if (nullptr == sc) {
  577. DOMError("failed to read Geometry object (class: Shape), no data scope found");
  578. }
  579. const Element& Indexes = GetRequiredElement(*sc, "Indexes", &element);
  580. const Element& Vertices = GetRequiredElement(*sc, "Vertices", &element);
  581. ParseVectorDataArray(m_indices, Indexes);
  582. ParseVectorDataArray(m_vertices, Vertices);
  583. if ((*sc)["Normals"]) {
  584. const Element& Normals = GetRequiredElement(*sc, "Normals", &element);
  585. ParseVectorDataArray(m_normals, Normals);
  586. }
  587. }
  588. // ------------------------------------------------------------------------------------------------
  589. ShapeGeometry::~ShapeGeometry() = default;
  590. // ------------------------------------------------------------------------------------------------
  591. const std::vector<aiVector3D>& ShapeGeometry::GetVertices() const {
  592. return m_vertices;
  593. }
  594. // ------------------------------------------------------------------------------------------------
  595. const std::vector<aiVector3D>& ShapeGeometry::GetNormals() const {
  596. return m_normals;
  597. }
  598. // ------------------------------------------------------------------------------------------------
  599. const std::vector<unsigned int>& ShapeGeometry::GetIndices() const {
  600. return m_indices;
  601. }
  602. // ------------------------------------------------------------------------------------------------
  603. LineGeometry::LineGeometry(uint64_t id, const Element& element, const std::string& name, const Document& doc)
  604. : Geometry(id, element, name, doc)
  605. {
  606. const Scope* sc = element.Compound();
  607. if (!sc) {
  608. DOMError("failed to read Geometry object (class: Line), no data scope found");
  609. }
  610. const Element& Points = GetRequiredElement(*sc, "Points", &element);
  611. const Element& PointsIndex = GetRequiredElement(*sc, "PointsIndex", &element);
  612. ParseVectorDataArray(m_vertices, Points);
  613. ParseVectorDataArray(m_indices, PointsIndex);
  614. }
  615. // ------------------------------------------------------------------------------------------------
  616. LineGeometry::~LineGeometry() = default;
  617. // ------------------------------------------------------------------------------------------------
  618. const std::vector<aiVector3D>& LineGeometry::GetVertices() const {
  619. return m_vertices;
  620. }
  621. // ------------------------------------------------------------------------------------------------
  622. const std::vector<int>& LineGeometry::GetIndices() const {
  623. return m_indices;
  624. }
  625. } // !FBX
  626. } // !Assimp
  627. #endif