FBXMeshGeometry.cpp 27 KB

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