glTFExporter.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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. #ifndef ASSIMP_BUILD_NO_EXPORT
  34. #ifndef ASSIMP_BUILD_NO_GLTF_EXPORTER
  35. #include "glTFExporter.h"
  36. #include "Exceptional.h"
  37. #include "StringComparison.h"
  38. #include "ByteSwapper.h"
  39. #include "SplitLargeMeshes.h"
  40. #include "SceneCombiner.h"
  41. #include <assimp/version.h>
  42. #include <assimp/IOSystem.hpp>
  43. #include <assimp/Exporter.hpp>
  44. #include <assimp/material.h>
  45. #include <assimp/scene.h>
  46. // Header files, standart library.
  47. #include <memory>
  48. #include <inttypes.h>
  49. #include "glTFAssetWriter.h"
  50. #ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC
  51. // Header files, Open3DGC.
  52. # include <Open3DGC/o3dgcSC3DMCEncoder.h>
  53. #endif
  54. using namespace rapidjson;
  55. using namespace Assimp;
  56. using namespace glTF;
  57. namespace Assimp {
  58. // ------------------------------------------------------------------------------------------------
  59. // Worker function for exporting a scene to GLTF. Prototyped and registered in Exporter.cpp
  60. void ExportSceneGLTF(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
  61. {
  62. // invoke the exporter
  63. glTFExporter exporter(pFile, pIOSystem, pScene, pProperties, false);
  64. }
  65. // ------------------------------------------------------------------------------------------------
  66. // Worker function for exporting a scene to GLB. Prototyped and registered in Exporter.cpp
  67. void ExportSceneGLB(const char* pFile, IOSystem* pIOSystem, const aiScene* pScene, const ExportProperties* pProperties)
  68. {
  69. // invoke the exporter
  70. glTFExporter exporter(pFile, pIOSystem, pScene, pProperties, true);
  71. }
  72. } // end of namespace Assimp
  73. glTFExporter::glTFExporter(const char* filename, IOSystem* pIOSystem, const aiScene* pScene,
  74. const ExportProperties* pProperties, bool isBinary)
  75. : mFilename(filename)
  76. , mIOSystem(pIOSystem)
  77. , mProperties(pProperties)
  78. {
  79. aiScene* sceneCopy_tmp;
  80. SceneCombiner::CopyScene(&sceneCopy_tmp, pScene);
  81. std::unique_ptr<aiScene> sceneCopy(sceneCopy_tmp);
  82. SplitLargeMeshesProcess_Triangle tri_splitter;
  83. tri_splitter.SetLimit(0xffff);
  84. tri_splitter.Execute(sceneCopy.get());
  85. SplitLargeMeshesProcess_Vertex vert_splitter;
  86. vert_splitter.SetLimit(0xffff);
  87. vert_splitter.Execute(sceneCopy.get());
  88. mScene = sceneCopy.get();
  89. std::unique_ptr<Asset> asset();
  90. mAsset.reset( new glTF::Asset( pIOSystem ) );
  91. if (isBinary) {
  92. mAsset->SetAsBinary();
  93. }
  94. ExportMetadata();
  95. //for (unsigned int i = 0; i < pScene->mNumAnimations; ++i) {}
  96. //for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {}
  97. //for (unsigned int i = 0; i < pScene->mNumLights; ++i) {}
  98. ExportMaterials();
  99. ExportMeshes();
  100. //for (unsigned int i = 0; i < pScene->mNumTextures; ++i) {}
  101. if (mScene->mRootNode) {
  102. ExportNode(mScene->mRootNode);
  103. }
  104. ExportScene();
  105. glTF::AssetWriter writer(*mAsset);
  106. if (isBinary) {
  107. writer.WriteGLBFile(filename);
  108. } else {
  109. writer.WriteFile(filename);
  110. }
  111. }
  112. static void CopyValue(const aiMatrix4x4& v, glTF::mat4& o)
  113. {
  114. o[ 0] = v.a1; o[ 1] = v.b1; o[ 2] = v.c1; o[ 3] = v.d1;
  115. o[ 4] = v.a2; o[ 5] = v.b2; o[ 6] = v.c2; o[ 7] = v.d2;
  116. o[ 8] = v.a3; o[ 9] = v.b3; o[10] = v.c3; o[11] = v.d3;
  117. o[12] = v.a4; o[13] = v.b4; o[14] = v.c4; o[15] = v.d4;
  118. }
  119. inline Ref<Accessor> ExportData(Asset& a, std::string& meshName, Ref<Buffer>& buffer,
  120. unsigned int count, void* data, AttribType::Value typeIn, AttribType::Value typeOut, ComponentType compType, bool isIndices = false)
  121. {
  122. if (!count || !data) return Ref<Accessor>();
  123. unsigned int numCompsIn = AttribType::GetNumComponents(typeIn);
  124. unsigned int numCompsOut = AttribType::GetNumComponents(typeOut);
  125. unsigned int bytesPerComp = ComponentTypeSize(compType);
  126. size_t offset = buffer->byteLength;
  127. size_t length = count * numCompsOut * bytesPerComp;
  128. buffer->Grow(length);
  129. // bufferView
  130. Ref<BufferView> bv = a.bufferViews.Create(a.FindUniqueID(meshName, "view"));
  131. bv->buffer = buffer;
  132. bv->byteOffset = unsigned(offset);
  133. bv->byteLength = length; //! The target that the WebGL buffer should be bound to.
  134. bv->target = isIndices ? BufferViewTarget_ELEMENT_ARRAY_BUFFER : BufferViewTarget_ARRAY_BUFFER;
  135. // accessor
  136. Ref<Accessor> acc = a.accessors.Create(a.FindUniqueID(meshName, "accessor"));
  137. acc->bufferView = bv;
  138. acc->byteOffset = 0;
  139. acc->byteStride = 0;
  140. acc->componentType = compType;
  141. acc->count = count;
  142. acc->type = typeOut;
  143. // copy the data
  144. acc->WriteData(count, data, numCompsIn*bytesPerComp);
  145. return acc;
  146. }
  147. namespace {
  148. void GetMatScalar(const aiMaterial* mat, float& val, const char* propName, int type, int idx) {
  149. if (mat->Get(propName, type, idx, val) == AI_SUCCESS) {}
  150. }
  151. }
  152. void glTFExporter::GetMatColorOrTex(const aiMaterial* mat, glTF::TexProperty& prop, const char* propName, int type, int idx, aiTextureType tt)
  153. {
  154. aiString tex;
  155. aiColor4D col;
  156. if (mat->GetTextureCount(tt) > 0) {
  157. if (mat->Get(AI_MATKEY_TEXTURE(tt, 0), tex) == AI_SUCCESS) {
  158. std::string path = tex.C_Str();
  159. if (path.size() > 0) {
  160. if (path[0] != '*') {
  161. std::map<std::string, unsigned int>::iterator it = mTexturesByPath.find(path);
  162. if (it != mTexturesByPath.end()) {
  163. prop.texture = mAsset->textures.Get(it->second);
  164. }
  165. }
  166. if (!prop.texture) {
  167. std::string texId = mAsset->FindUniqueID("", "texture");
  168. prop.texture = mAsset->textures.Create(texId);
  169. mTexturesByPath[path] = prop.texture.GetIndex();
  170. std::string imgId = mAsset->FindUniqueID("", "image");
  171. prop.texture->source = mAsset->images.Create(imgId);
  172. if (path[0] == '*') { // embedded
  173. aiTexture* tex = mScene->mTextures[atoi(&path[1])];
  174. uint8_t* data = reinterpret_cast<uint8_t*>(tex->pcData);
  175. prop.texture->source->SetData(data, tex->mWidth, *mAsset);
  176. if (tex->achFormatHint[0]) {
  177. std::string mimeType = "image/";
  178. mimeType += (memcmp(tex->achFormatHint, "jpg", 3) == 0) ? "jpeg" : tex->achFormatHint;
  179. prop.texture->source->mimeType = mimeType;
  180. }
  181. }
  182. else {
  183. prop.texture->source->uri = path;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. if (mat->Get(propName, type, idx, col) == AI_SUCCESS) {
  190. prop.color[0] = col.r; prop.color[1] = col.g; prop.color[2] = col.b; prop.color[3] = col.a;
  191. }
  192. }
  193. void glTFExporter::ExportMaterials()
  194. {
  195. aiString aiName;
  196. for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
  197. const aiMaterial* mat = mScene->mMaterials[i];
  198. std::string name;
  199. if (mat->Get(AI_MATKEY_NAME, aiName) == AI_SUCCESS) {
  200. name = aiName.C_Str();
  201. }
  202. name = mAsset->FindUniqueID(name, "material");
  203. Ref<Material> m = mAsset->materials.Create(name);
  204. GetMatColorOrTex(mat, m->ambient, AI_MATKEY_COLOR_AMBIENT, aiTextureType_AMBIENT);
  205. GetMatColorOrTex(mat, m->diffuse, AI_MATKEY_COLOR_DIFFUSE, aiTextureType_DIFFUSE);
  206. GetMatColorOrTex(mat, m->specular, AI_MATKEY_COLOR_SPECULAR, aiTextureType_SPECULAR);
  207. GetMatColorOrTex(mat, m->emission, AI_MATKEY_COLOR_EMISSIVE, aiTextureType_EMISSIVE);
  208. GetMatScalar(mat, m->shininess, AI_MATKEY_SHININESS);
  209. }
  210. }
  211. void glTFExporter::ExportMeshes()
  212. {
  213. // Not for
  214. // using IndicesType = decltype(aiFace::mNumIndices);
  215. // But yes for
  216. // using IndicesType = unsigned short;
  217. // because "ComponentType_UNSIGNED_SHORT" used for indices. And it's a maximal type according to glTF specification.
  218. typedef unsigned short IndicesType;
  219. // Variables needed for compression. BEGIN.
  220. // Indices, not pointers - because pointer to buffer is changin while writing to it.
  221. size_t idx_srcdata_begin;// Index of buffer before writing mesh data. Also, index of begin of coordinates array in buffer.
  222. size_t idx_srcdata_normal = SIZE_MAX;// Index of begin of normals array in buffer. SIZE_MAX - mean that mesh has no normals.
  223. std::vector<size_t> idx_srcdata_tc;// Array of indices. Every index point to begin of texture coordinates array in buffer.
  224. size_t idx_srcdata_ind;// Index of begin of coordinates indices array in buffer.
  225. bool comp_allow;// Point that data of current mesh can be compressed.
  226. // Variables needed for compression. END.
  227. std::string fname = std::string(mFilename);
  228. std::string bufferIdPrefix = fname.substr(0, fname.find("."));
  229. std::string bufferId = mAsset->FindUniqueID("", bufferIdPrefix.c_str());
  230. Ref<Buffer> b = mAsset->GetBodyBuffer();
  231. if (!b) {
  232. b = mAsset->buffers.Create(bufferId);
  233. }
  234. for (unsigned int idx_mesh = 0; idx_mesh < mScene->mNumMeshes; ++idx_mesh) {
  235. const aiMesh* aim = mScene->mMeshes[idx_mesh];
  236. // Check if compressing requested and mesh can be encoded.
  237. #ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC
  238. comp_allow = mProperties->GetPropertyBool("extensions.Open3DGC.use", false);
  239. #else
  240. comp_allow = false;
  241. #endif
  242. if(comp_allow && (aim->mPrimitiveTypes == aiPrimitiveType_TRIANGLE) && (aim->mNumVertices > 0) && (aim->mNumFaces > 0))
  243. {
  244. idx_srcdata_tc.clear();
  245. idx_srcdata_tc.reserve(AI_MAX_NUMBER_OF_TEXTURECOORDS);
  246. }
  247. else
  248. {
  249. std::string msg;
  250. if(aim->mPrimitiveTypes != aiPrimitiveType_TRIANGLE)
  251. msg = "all primitives of the mesh must be a triangles.";
  252. else
  253. msg = "mesh must has vertices and faces.";
  254. DefaultLogger::get()->warn("GLTF: can not use Open3DGC-compression: " + msg);
  255. comp_allow = false;
  256. }
  257. std::string meshId = mAsset->FindUniqueID(aim->mName.C_Str(), "mesh");
  258. Ref<Mesh> m = mAsset->meshes.Create(meshId);
  259. m->primitives.resize(1);
  260. Mesh::Primitive& p = m->primitives.back();
  261. p.material = mAsset->materials.Get(aim->mMaterialIndex);
  262. /******************* Vertices ********************/
  263. // If compression is used then you need parameters of uncompressed region: begin and size. At this step "begin" is stored.
  264. if(comp_allow) idx_srcdata_begin = b->byteLength;
  265. Ref<Accessor> v = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mVertices, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
  266. if (v) p.attributes.position.push_back(v);
  267. /******************** Normals ********************/
  268. if(comp_allow && (aim->mNormals > 0)) idx_srcdata_normal = b->byteLength;// Store index of normals array.
  269. Ref<Accessor> n = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mNormals, AttribType::VEC3, AttribType::VEC3, ComponentType_FLOAT);
  270. if (n) p.attributes.normal.push_back(n);
  271. /************** Texture coordinates **************/
  272. for (int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) {
  273. // Flip UV y coords
  274. if (aim -> mNumUVComponents[i] > 1) {
  275. for (unsigned int j = 0; j < aim->mNumVertices; ++j) {
  276. aim->mTextureCoords[i][j].y = 1 - aim->mTextureCoords[i][j].y;
  277. }
  278. }
  279. if (aim->mNumUVComponents[i] > 0) {
  280. AttribType::Value type = (aim->mNumUVComponents[i] == 2) ? AttribType::VEC2 : AttribType::VEC3;
  281. if(comp_allow) idx_srcdata_tc.push_back(b->byteLength);// Store index of texture coordinates array.
  282. Ref<Accessor> tc = ExportData(*mAsset, meshId, b, aim->mNumVertices, aim->mTextureCoords[i], AttribType::VEC3, type, ComponentType_FLOAT, true);
  283. if (tc) p.attributes.texcoord.push_back(tc);
  284. }
  285. }
  286. /*************** Vertices indices ****************/
  287. idx_srcdata_ind = b->byteLength;// Store index of indices array.
  288. if (aim->mNumFaces > 0) {
  289. std::vector<IndicesType> indices;
  290. unsigned int nIndicesPerFace = aim->mFaces[0].mNumIndices;
  291. indices.resize(aim->mNumFaces * nIndicesPerFace);
  292. for (size_t i = 0; i < aim->mNumFaces; ++i) {
  293. for (size_t j = 0; j < nIndicesPerFace; ++j) {
  294. indices[i*nIndicesPerFace + j] = uint16_t(aim->mFaces[i].mIndices[j]);
  295. }
  296. }
  297. p.indices = ExportData(*mAsset, meshId, b, unsigned(indices.size()), &indices[0], AttribType::SCALAR, AttribType::SCALAR, ComponentType_UNSIGNED_SHORT, true);
  298. }
  299. switch (aim->mPrimitiveTypes) {
  300. case aiPrimitiveType_POLYGON:
  301. p.mode = PrimitiveMode_TRIANGLES; break; // TODO implement this
  302. case aiPrimitiveType_LINE:
  303. p.mode = PrimitiveMode_LINES; break;
  304. case aiPrimitiveType_POINT:
  305. p.mode = PrimitiveMode_POINTS; break;
  306. default: // aiPrimitiveType_TRIANGLE
  307. p.mode = PrimitiveMode_TRIANGLES;
  308. }
  309. /****************** Compression ******************/
  310. ///TODO: animation: weights, joints.
  311. if(comp_allow)
  312. {
  313. #ifdef ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC
  314. // Only one type of compression supported at now - Open3DGC.
  315. //
  316. o3dgc::BinaryStream bs;
  317. o3dgc::SC3DMCEncoder<IndicesType> encoder;
  318. o3dgc::IndexedFaceSet<IndicesType> comp_o3dgc_ifs;
  319. o3dgc::SC3DMCEncodeParams comp_o3dgc_params;
  320. //
  321. // Fill data for encoder.
  322. //
  323. // Quantization
  324. unsigned quant_coord = mProperties->GetPropertyInteger("extensions.Open3DGC.quantization.POSITION", 12);
  325. unsigned quant_normal = mProperties->GetPropertyInteger("extensions.Open3DGC.quantization.NORMAL", 10);
  326. unsigned quant_texcoord = mProperties->GetPropertyInteger("extensions.Open3DGC.quantization.TEXCOORD", 10);
  327. // Prediction
  328. o3dgc::O3DGCSC3DMCPredictionMode prediction_position = o3dgc::O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION;
  329. o3dgc::O3DGCSC3DMCPredictionMode prediction_normal = o3dgc::O3DGC_SC3DMC_SURF_NORMALS_PREDICTION;
  330. o3dgc::O3DGCSC3DMCPredictionMode prediction_texcoord = o3dgc::O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION;
  331. // IndexedFacesSet: "Crease angle", "solid", "convex" are set to default.
  332. comp_o3dgc_ifs.SetCCW(true);
  333. comp_o3dgc_ifs.SetIsTriangularMesh(true);
  334. comp_o3dgc_ifs.SetNumFloatAttributes(0);
  335. // Coordinates
  336. comp_o3dgc_params.SetCoordQuantBits(quant_coord);
  337. comp_o3dgc_params.SetCoordPredMode(prediction_position);
  338. comp_o3dgc_ifs.SetNCoord(aim->mNumVertices);
  339. comp_o3dgc_ifs.SetCoord((o3dgc::Real* const)&b->GetPointer()[idx_srcdata_begin]);
  340. // Normals
  341. if(idx_srcdata_normal != SIZE_MAX)
  342. {
  343. comp_o3dgc_params.SetNormalQuantBits(quant_normal);
  344. comp_o3dgc_params.SetNormalPredMode(prediction_normal);
  345. comp_o3dgc_ifs.SetNNormal(aim->mNumVertices);
  346. comp_o3dgc_ifs.SetNormal((o3dgc::Real* const)&b->GetPointer()[idx_srcdata_normal]);
  347. }
  348. // Texture coordinates
  349. for(size_t num_tc = 0; num_tc < idx_srcdata_tc.size(); num_tc++)
  350. {
  351. size_t num = comp_o3dgc_ifs.GetNumFloatAttributes();
  352. comp_o3dgc_params.SetFloatAttributeQuantBits(num, quant_texcoord);
  353. comp_o3dgc_params.SetFloatAttributePredMode(num, prediction_texcoord);
  354. comp_o3dgc_ifs.SetNFloatAttribute(num, aim->mNumVertices);// number of elements.
  355. comp_o3dgc_ifs.SetFloatAttributeDim(num, aim->mNumUVComponents[num_tc]);// components per element: aiVector3D => x * float
  356. comp_o3dgc_ifs.SetFloatAttributeType(num, o3dgc::O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_TEXCOORD);
  357. comp_o3dgc_ifs.SetFloatAttribute(num, (o3dgc::Real* const)&b->GetPointer()[idx_srcdata_tc[num_tc]]);
  358. comp_o3dgc_ifs.SetNumFloatAttributes(num + 1);
  359. }
  360. // Coordinates indices
  361. comp_o3dgc_ifs.SetNCoordIndex(aim->mNumFaces);
  362. comp_o3dgc_ifs.SetCoordIndex((IndicesType* const)&b->GetPointer()[idx_srcdata_ind]);
  363. // Prepare to enconding
  364. comp_o3dgc_params.SetNumFloatAttributes(comp_o3dgc_ifs.GetNumFloatAttributes());
  365. if(mProperties->GetPropertyBool("extensions.Open3DGC.binary", true))
  366. comp_o3dgc_params.SetStreamType(o3dgc::O3DGC_STREAM_TYPE_BINARY);
  367. else
  368. comp_o3dgc_params.SetStreamType(o3dgc::O3DGC_STREAM_TYPE_ASCII);
  369. comp_o3dgc_ifs.ComputeMinMax(o3dgc::O3DGC_SC3DMC_MAX_ALL_DIMS);
  370. //
  371. // Encoding
  372. //
  373. encoder.Encode(comp_o3dgc_params, comp_o3dgc_ifs, bs);
  374. // Replace data in buffer.
  375. b->ReplaceData(idx_srcdata_begin, b->byteLength - idx_srcdata_begin, bs.GetBuffer(), bs.GetSize());
  376. //
  377. // Add information about extension to mesh.
  378. //
  379. // Create extension structure.
  380. Mesh::SCompression_Open3DGC* ext = new Mesh::SCompression_Open3DGC;
  381. // Fill it.
  382. ext->Buffer = b->id;
  383. ext->Offset = idx_srcdata_begin;
  384. ext->Count = b->byteLength - idx_srcdata_begin;
  385. ext->Binary = mProperties->GetPropertyBool("extensions.Open3DGC.binary");
  386. ext->IndicesCount = comp_o3dgc_ifs.GetNCoordIndex() * 3;
  387. ext->VerticesCount = comp_o3dgc_ifs.GetNCoord();
  388. // And assign to mesh.
  389. m->Extension.push_back(ext);
  390. #endif
  391. }// if(comp_allow)
  392. }// for (unsigned int i = 0; i < mScene->mNumMeshes; ++i) {
  393. }
  394. unsigned int glTFExporter::ExportNode(const aiNode* n)
  395. {
  396. Ref<Node> node = mAsset->nodes.Create(mAsset->FindUniqueID(n->mName.C_Str(), "node"));
  397. if (!n->mTransformation.IsIdentity()) {
  398. node->matrix.isPresent = true;
  399. CopyValue(n->mTransformation, node->matrix.value);
  400. }
  401. for (unsigned int i = 0; i < n->mNumMeshes; ++i) {
  402. node->meshes.push_back(mAsset->meshes.Get(n->mMeshes[i]));
  403. }
  404. for (unsigned int i = 0; i < n->mNumChildren; ++i) {
  405. unsigned int idx = ExportNode(n->mChildren[i]);
  406. node->children.push_back(mAsset->nodes.Get(idx));
  407. }
  408. return node.GetIndex();
  409. }
  410. void glTFExporter::ExportScene()
  411. {
  412. const char* sceneName = "defaultScene";
  413. Ref<Scene> scene = mAsset->scenes.Create(sceneName);
  414. // root node will be the first one exported (idx 0)
  415. if (mAsset->nodes.Size() > 0) {
  416. scene->nodes.push_back(mAsset->nodes.Get(0u));
  417. }
  418. // set as the default scene
  419. mAsset->scene = scene;
  420. }
  421. void glTFExporter::ExportMetadata()
  422. {
  423. glTF::AssetMetadata& asset = mAsset->asset;
  424. asset.version = 1;
  425. char buffer[256];
  426. ai_snprintf(buffer, 256, "Open Asset Import Library (assimp v%d.%d.%d)",
  427. aiGetVersionMajor(), aiGetVersionMinor(), aiGetVersionRevision());
  428. asset.generator = buffer;
  429. }
  430. #endif // ASSIMP_BUILD_NO_GLTF_EXPORTER
  431. #endif // ASSIMP_BUILD_NO_EXPORT