選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

541 行
17 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. #include "AssimpPCH.h"
  37. #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER
  38. #include <functional>
  39. #include "FBXParser.h"
  40. #include "FBXDocument.h"
  41. #include "FBXImporter.h"
  42. #include "FBXImportSettings.h"
  43. #include "FBXDocumentUtil.h"
  44. namespace Assimp {
  45. namespace FBX {
  46. using namespace Util;
  47. // ------------------------------------------------------------------------------------------------
  48. Geometry::Geometry(uint64_t id, const Element& element, const std::string& name, const Document& doc)
  49. : Object(id, element,name)
  50. , skin()
  51. {
  52. const std::vector<const Connection*>& conns = doc.GetConnectionsByDestinationSequenced(ID(),"Deformer");
  53. BOOST_FOREACH(const Connection* con, conns) {
  54. const Skin* const sk = ProcessSimpleConnection<Skin>(*con, false, "Skin -> Geometry", element);
  55. if(sk) {
  56. skin = sk;
  57. break;
  58. }
  59. }
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. Geometry::~Geometry()
  63. {
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. MeshGeometry::MeshGeometry(uint64_t id, const Element& element, const std::string& name, const Document& doc)
  67. : Geometry(id, element,name, doc)
  68. {
  69. const Scope* sc = element.Compound();
  70. if (!sc) {
  71. DOMError("failed to read Geometry object (class: Mesh), no data scope found");
  72. }
  73. // must have Mesh elements:
  74. const Element& Vertices = GetRequiredElement(*sc,"Vertices",&element);
  75. const Element& PolygonVertexIndex = GetRequiredElement(*sc,"PolygonVertexIndex",&element);
  76. // optional Mesh elements:
  77. const ElementCollection& Layer = sc->GetCollection("Layer");
  78. std::vector<aiVector3D> tempVerts;
  79. ParseVectorDataArray(tempVerts,Vertices);
  80. if(tempVerts.empty()) {
  81. FBXImporter::LogWarn("encountered mesh with no vertices");
  82. return;
  83. }
  84. std::vector<int> tempFaces;
  85. ParseVectorDataArray(tempFaces,PolygonVertexIndex);
  86. if(tempFaces.empty()) {
  87. FBXImporter::LogWarn("encountered mesh with no faces");
  88. return;
  89. }
  90. vertices.reserve(tempFaces.size());
  91. faces.reserve(tempFaces.size() / 3);
  92. mapping_offsets.resize(tempVerts.size());
  93. mapping_counts.resize(tempVerts.size(),0);
  94. mappings.resize(tempFaces.size());
  95. const size_t vertex_count = tempVerts.size();
  96. // generate output vertices, computing an adjacency table to
  97. // preserve the mapping from fbx indices to *this* indexing.
  98. unsigned int count = 0;
  99. BOOST_FOREACH(int index, tempFaces) {
  100. const int absi = index < 0 ? (-index - 1) : index;
  101. if(static_cast<size_t>(absi) >= vertex_count) {
  102. DOMError("polygon vertex index out of range",&PolygonVertexIndex);
  103. }
  104. vertices.push_back(tempVerts[absi]);
  105. ++count;
  106. ++mapping_counts[absi];
  107. if (index < 0) {
  108. faces.push_back(count);
  109. count = 0;
  110. }
  111. }
  112. unsigned int cursor = 0;
  113. for (size_t i = 0, e = tempVerts.size(); i < e; ++i) {
  114. mapping_offsets[i] = cursor;
  115. cursor += mapping_counts[i];
  116. mapping_counts[i] = 0;
  117. }
  118. cursor = 0;
  119. BOOST_FOREACH(int index, tempFaces) {
  120. const int absi = index < 0 ? (-index - 1) : index;
  121. mappings[mapping_offsets[absi] + mapping_counts[absi]++] = cursor++;
  122. }
  123. // if settings.readAllLayers is true:
  124. // * read all layers, try to load as many vertex channels as possible
  125. // if settings.readAllLayers is false:
  126. // * read only the layer with index 0, but warn about any further layers
  127. for (ElementMap::const_iterator it = Layer.first; it != Layer.second; ++it) {
  128. const TokenList& tokens = (*it).second->Tokens();
  129. const char* err;
  130. const int index = ParseTokenAsInt(*tokens[0], err);
  131. if(err) {
  132. DOMError(err,&element);
  133. }
  134. if(doc.Settings().readAllLayers || index == 0) {
  135. const Scope& layer = GetRequiredScope(*(*it).second);
  136. ReadLayer(layer);
  137. }
  138. else {
  139. FBXImporter::LogWarn("ignoring additional geometry layers");
  140. }
  141. }
  142. }
  143. // ------------------------------------------------------------------------------------------------
  144. MeshGeometry::~MeshGeometry()
  145. {
  146. }
  147. // ------------------------------------------------------------------------------------------------
  148. void MeshGeometry::ReadLayer(const Scope& layer)
  149. {
  150. const ElementCollection& LayerElement = layer.GetCollection("LayerElement");
  151. for (ElementMap::const_iterator eit = LayerElement.first; eit != LayerElement.second; ++eit) {
  152. const Scope& elayer = GetRequiredScope(*(*eit).second);
  153. ReadLayerElement(elayer);
  154. }
  155. }
  156. // ------------------------------------------------------------------------------------------------
  157. void MeshGeometry::ReadLayerElement(const Scope& layerElement)
  158. {
  159. const Element& Type = GetRequiredElement(layerElement,"Type");
  160. const Element& TypedIndex = GetRequiredElement(layerElement,"TypedIndex");
  161. const std::string& type = ParseTokenAsString(GetRequiredToken(Type,0));
  162. const int typedIndex = ParseTokenAsInt(GetRequiredToken(TypedIndex,0));
  163. const Scope& top = GetRequiredScope(element);
  164. const ElementCollection candidates = top.GetCollection(type);
  165. for (ElementMap::const_iterator it = candidates.first; it != candidates.second; ++it) {
  166. const int index = ParseTokenAsInt(GetRequiredToken(*(*it).second,0));
  167. if(index == typedIndex) {
  168. ReadVertexData(type,typedIndex,GetRequiredScope(*(*it).second));
  169. return;
  170. }
  171. }
  172. FBXImporter::LogError(Formatter::format("failed to resolve vertex layer element: ")
  173. << type << ", index: " << typedIndex);
  174. }
  175. // ------------------------------------------------------------------------------------------------
  176. void MeshGeometry::ReadVertexData(const std::string& type, int index, const Scope& source)
  177. {
  178. const std::string& MappingInformationType = ParseTokenAsString(GetRequiredToken(
  179. GetRequiredElement(source,"MappingInformationType"),0)
  180. );
  181. const std::string& ReferenceInformationType = ParseTokenAsString(GetRequiredToken(
  182. GetRequiredElement(source,"ReferenceInformationType"),0)
  183. );
  184. if (type == "LayerElementUV") {
  185. if(index >= AI_MAX_NUMBER_OF_TEXTURECOORDS) {
  186. FBXImporter::LogError(Formatter::format("ignoring UV layer, maximum number of UV channels exceeded: ")
  187. << index << " (limit is " << AI_MAX_NUMBER_OF_TEXTURECOORDS << ")" );
  188. return;
  189. }
  190. const Element* Name = source["Name"];
  191. uvNames[index] = "";
  192. if(Name) {
  193. uvNames[index] = ParseTokenAsString(GetRequiredToken(*Name,0));
  194. }
  195. ReadVertexDataUV(uvs[index],source,
  196. MappingInformationType,
  197. ReferenceInformationType
  198. );
  199. }
  200. else if (type == "LayerElementMaterial") {
  201. if (materials.size() > 0) {
  202. FBXImporter::LogError("ignoring additional material layer");
  203. return;
  204. }
  205. std::vector<int> temp_materials;
  206. ReadVertexDataMaterials(temp_materials,source,
  207. MappingInformationType,
  208. ReferenceInformationType
  209. );
  210. // sometimes, there will be only negative entries. Drop the material
  211. // layer in such a case (I guess it means a default material should
  212. // be used). This is what the converter would do anyway, and it
  213. // avoids loosing the material if there are more material layers
  214. // coming of which at least one contains actual data (did observe
  215. // that with one test file).
  216. const size_t count_neg = std::count_if(temp_materials.begin(),temp_materials.end(),std::bind2nd(std::less<int>(),0));
  217. if(count_neg == temp_materials.size()) {
  218. FBXImporter::LogWarn("ignoring dummy material layer (all entries -1)");
  219. return;
  220. }
  221. std::swap(temp_materials, materials);
  222. }
  223. else if (type == "LayerElementNormal") {
  224. if (normals.size() > 0) {
  225. FBXImporter::LogError("ignoring additional normal layer");
  226. return;
  227. }
  228. ReadVertexDataNormals(normals,source,
  229. MappingInformationType,
  230. ReferenceInformationType
  231. );
  232. }
  233. else if (type == "LayerElementTangent") {
  234. if (tangents.size() > 0) {
  235. FBXImporter::LogError("ignoring additional tangent layer");
  236. return;
  237. }
  238. ReadVertexDataTangents(tangents,source,
  239. MappingInformationType,
  240. ReferenceInformationType
  241. );
  242. }
  243. else if (type == "LayerElementBinormal") {
  244. if (binormals.size() > 0) {
  245. FBXImporter::LogError("ignoring additional binormal layer");
  246. return;
  247. }
  248. ReadVertexDataBinormals(binormals,source,
  249. MappingInformationType,
  250. ReferenceInformationType
  251. );
  252. }
  253. else if (type == "LayerElementColor") {
  254. if(index >= AI_MAX_NUMBER_OF_COLOR_SETS) {
  255. FBXImporter::LogError(Formatter::format("ignoring vertex color layer, maximum number of color sets exceeded: ")
  256. << index << " (limit is " << AI_MAX_NUMBER_OF_COLOR_SETS << ")" );
  257. return;
  258. }
  259. ReadVertexDataColors(colors[index],source,
  260. MappingInformationType,
  261. ReferenceInformationType
  262. );
  263. }
  264. }
  265. // ------------------------------------------------------------------------------------------------
  266. // Lengthy utility function to read and resolve a FBX vertex data array - that is, the
  267. // output is in polygon vertex order. This logic is used for reading normals, UVs, colors,
  268. // tangents ..
  269. template <typename T>
  270. void ResolveVertexDataArray(std::vector<T>& data_out, const Scope& source,
  271. const std::string& MappingInformationType,
  272. const std::string& ReferenceInformationType,
  273. const char* dataElementName,
  274. const char* indexDataElementName,
  275. size_t vertex_count,
  276. const std::vector<unsigned int>& mapping_counts,
  277. const std::vector<unsigned int>& mapping_offsets,
  278. const std::vector<unsigned int>& mappings)
  279. {
  280. std::vector<T> tempUV;
  281. ParseVectorDataArray(tempUV,GetRequiredElement(source,dataElementName));
  282. // handle permutations of Mapping and Reference type - it would be nice to
  283. // deal with this more elegantly and with less redundancy, but right
  284. // now it seems unavoidable.
  285. if (MappingInformationType == "ByVertice" && ReferenceInformationType == "Direct") {
  286. data_out.resize(vertex_count);
  287. for (size_t i = 0, e = tempUV.size(); i < e; ++i) {
  288. const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i];
  289. for (unsigned int j = istart; j < iend; ++j) {
  290. data_out[mappings[j]] = tempUV[i];
  291. }
  292. }
  293. }
  294. else if (MappingInformationType == "ByVertice" && ReferenceInformationType == "IndexToDirect") {
  295. data_out.resize(vertex_count);
  296. std::vector<int> uvIndices;
  297. ParseVectorDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));
  298. for (size_t i = 0, e = uvIndices.size(); i < e; ++i) {
  299. const unsigned int istart = mapping_offsets[i], iend = istart + mapping_counts[i];
  300. for (unsigned int j = istart; j < iend; ++j) {
  301. if(static_cast<size_t>(uvIndices[i]) >= tempUV.size()) {
  302. DOMError("index out of range",&GetRequiredElement(source,indexDataElementName));
  303. }
  304. data_out[mappings[j]] = tempUV[uvIndices[i]];
  305. }
  306. }
  307. }
  308. else if (MappingInformationType == "ByPolygonVertex" && ReferenceInformationType == "Direct") {
  309. if (tempUV.size() != vertex_count) {
  310. FBXImporter::LogError(Formatter::format("length of input data unexpected for ByPolygon mapping: ")
  311. << tempUV.size() << ", expected " << vertex_count
  312. );
  313. return;
  314. }
  315. data_out.swap(tempUV);
  316. }
  317. else if (MappingInformationType == "ByPolygonVertex" && ReferenceInformationType == "IndexToDirect") {
  318. data_out.resize(vertex_count);
  319. std::vector<int> uvIndices;
  320. ParseVectorDataArray(uvIndices,GetRequiredElement(source,indexDataElementName));
  321. if (uvIndices.size() != vertex_count) {
  322. FBXImporter::LogError("length of input data unexpected for ByPolygonVertex mapping");
  323. return;
  324. }
  325. unsigned int next = 0;
  326. BOOST_FOREACH(int i, uvIndices) {
  327. if(static_cast<size_t>(i) >= tempUV.size()) {
  328. DOMError("index out of range",&GetRequiredElement(source,indexDataElementName));
  329. }
  330. data_out[next++] = tempUV[i];
  331. }
  332. }
  333. else {
  334. FBXImporter::LogError(Formatter::format("ignoring vertex data channel, access type not implemented: ")
  335. << MappingInformationType << "," << ReferenceInformationType);
  336. }
  337. }
  338. // ------------------------------------------------------------------------------------------------
  339. void MeshGeometry::ReadVertexDataNormals(std::vector<aiVector3D>& normals_out, const Scope& source,
  340. const std::string& MappingInformationType,
  341. const std::string& ReferenceInformationType)
  342. {
  343. ResolveVertexDataArray(normals_out,source,MappingInformationType,ReferenceInformationType,
  344. "Normals",
  345. "NormalsIndex",
  346. vertices.size(),
  347. mapping_counts,
  348. mapping_offsets,
  349. mappings);
  350. }
  351. // ------------------------------------------------------------------------------------------------
  352. void MeshGeometry::ReadVertexDataUV(std::vector<aiVector2D>& uv_out, const Scope& source,
  353. const std::string& MappingInformationType,
  354. const std::string& ReferenceInformationType)
  355. {
  356. ResolveVertexDataArray(uv_out,source,MappingInformationType,ReferenceInformationType,
  357. "UV",
  358. "UVIndex",
  359. vertices.size(),
  360. mapping_counts,
  361. mapping_offsets,
  362. mappings);
  363. }
  364. // ------------------------------------------------------------------------------------------------
  365. void MeshGeometry::ReadVertexDataColors(std::vector<aiColor4D>& colors_out, const Scope& source,
  366. const std::string& MappingInformationType,
  367. const std::string& ReferenceInformationType)
  368. {
  369. ResolveVertexDataArray(colors_out,source,MappingInformationType,ReferenceInformationType,
  370. "Colors",
  371. "ColorIndex",
  372. vertices.size(),
  373. mapping_counts,
  374. mapping_offsets,
  375. mappings);
  376. }
  377. // ------------------------------------------------------------------------------------------------
  378. void MeshGeometry::ReadVertexDataTangents(std::vector<aiVector3D>& tangents_out, const Scope& source,
  379. const std::string& MappingInformationType,
  380. const std::string& ReferenceInformationType)
  381. {
  382. ResolveVertexDataArray(tangents_out,source,MappingInformationType,ReferenceInformationType,
  383. "Tangent",
  384. "TangentIndex",
  385. vertices.size(),
  386. mapping_counts,
  387. mapping_offsets,
  388. mappings);
  389. }
  390. // ------------------------------------------------------------------------------------------------
  391. void MeshGeometry::ReadVertexDataBinormals(std::vector<aiVector3D>& binormals_out, const Scope& source,
  392. const std::string& MappingInformationType,
  393. const std::string& ReferenceInformationType)
  394. {
  395. ResolveVertexDataArray(binormals_out,source,MappingInformationType,ReferenceInformationType,
  396. "Binormal",
  397. "BinormalIndex",
  398. vertices.size(),
  399. mapping_counts,
  400. mapping_offsets,
  401. mappings);
  402. }
  403. // ------------------------------------------------------------------------------------------------
  404. void MeshGeometry::ReadVertexDataMaterials(std::vector<int>& materials_out, const Scope& source,
  405. const std::string& MappingInformationType,
  406. const std::string& ReferenceInformationType)
  407. {
  408. const size_t face_count = faces.size();
  409. ai_assert(face_count);
  410. // materials are handled separately. First of all, they are assigned per-face
  411. // and not per polyvert. Secondly, ReferenceInformationType=IndexToDirect
  412. // has a slightly different meaning for materials.
  413. ParseVectorDataArray(materials_out,GetRequiredElement(source,"Materials"));
  414. if (MappingInformationType == "AllSame") {
  415. // easy - same material for all faces
  416. if (materials_out.empty()) {
  417. FBXImporter::LogError(Formatter::format("expected material index, ignoring"));
  418. return;
  419. }
  420. else if (materials_out.size() > 1) {
  421. FBXImporter::LogWarn(Formatter::format("expected only a single material index, ignoring all except the first one"));
  422. materials_out.clear();
  423. }
  424. materials.assign(vertices.size(),materials_out[0]);
  425. }
  426. else if (MappingInformationType == "ByPolygon" && ReferenceInformationType == "IndexToDirect") {
  427. materials.resize(face_count);
  428. if(materials_out.size() != face_count) {
  429. FBXImporter::LogError(Formatter::format("length of input data unexpected for ByPolygon mapping: ")
  430. << materials_out.size() << ", expected " << face_count
  431. );
  432. return;
  433. }
  434. }
  435. else {
  436. FBXImporter::LogError(Formatter::format("ignoring material assignments, access type not implemented: ")
  437. << MappingInformationType << "," << ReferenceInformationType);
  438. }
  439. }
  440. } // !FBX
  441. } // !Assimp
  442. #endif