json_exporter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. Assimp2Json
  3. Copyright (c) 2011, Alexander C. Gessler
  4. Licensed under a 3-clause BSD license. See the LICENSE file for more information.
  5. */
  6. #ifndef ASSIMP_BUILD_NO_EXPORT
  7. #ifndef ASSIMP_BUILD_NO_ASSJSON_EXPORTER
  8. #include <assimp/scene.h>
  9. #include <assimp/ai_assert.h>
  10. #include <assimp/Exporter.hpp>
  11. #include <assimp/IOStream.hpp>
  12. #include <assimp/IOSystem.hpp>
  13. #include <assimp/Importer.hpp>
  14. #include <assimp/Exceptional.h>
  15. #include <cassert>
  16. #include <limits>
  17. #include <memory>
  18. #include <sstream>
  19. #define CURRENT_FORMAT_VERSION 100
  20. #include "mesh_splitter.h"
  21. extern "C" {
  22. # include "cencode.h"
  23. }
  24. namespace Assimp {
  25. // Forward declarations
  26. void ExportAssimp2Json(const char *, Assimp::IOSystem *, const aiScene *, const Assimp::ExportProperties *);
  27. // small utility class to simplify serializing the aiScene to Json
  28. class JSONWriter {
  29. public:
  30. enum {
  31. Flag_DoNotIndent = 0x1,
  32. Flag_WriteSpecialFloats = 0x2,
  33. Flag_SkipWhitespaces = 0x4
  34. };
  35. JSONWriter(Assimp::IOStream &out, unsigned int flags = 0u) :
  36. out(out), indent (""), newline("\n"), space(" "), buff (), first(false), flags(flags) {
  37. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  38. buff.imbue(std::locale("C"));
  39. if (flags & Flag_SkipWhitespaces) {
  40. newline = "";
  41. space = "";
  42. }
  43. }
  44. ~JSONWriter() {
  45. Flush();
  46. }
  47. void Flush() {
  48. const std::string s = buff.str();
  49. out.Write(s.c_str(), s.length(), 1);
  50. buff.clear();
  51. }
  52. void PushIndent() {
  53. indent += '\t';
  54. }
  55. void PopIndent() {
  56. indent.erase(indent.end() - 1);
  57. }
  58. void Key(const std::string &name) {
  59. AddIndentation();
  60. Delimit();
  61. buff << '\"' + name + "\":" << space;
  62. }
  63. template <typename Literal>
  64. void Element(const Literal &name) {
  65. AddIndentation();
  66. Delimit();
  67. LiteralToString(buff, name) << newline;
  68. }
  69. template <typename Literal>
  70. void SimpleValue(const Literal &s) {
  71. LiteralToString(buff, s) << newline;
  72. }
  73. void SimpleValue(const void *buffer, size_t len) {
  74. base64_encodestate s;
  75. base64_init_encodestate(&s);
  76. char *const cur_out = new char[std::max(len * 2, static_cast<size_t>(16u))];
  77. const int n = base64_encode_block(reinterpret_cast<const char *>(buffer), static_cast<int>(len), cur_out, &s);
  78. cur_out[n + base64_encode_blockend(cur_out + n, &s)] = '\0';
  79. // base64 encoding may add newlines, but JSON strings may not contain 'real' newlines
  80. // (only escaped ones). Remove any newlines in out.
  81. for (char *cur = cur_out; *cur; ++cur) {
  82. if (*cur == '\n') {
  83. *cur = ' ';
  84. }
  85. }
  86. buff << '\"' << cur_out << "\"" << newline;
  87. delete[] cur_out;
  88. }
  89. void StartObj(bool is_element = false) {
  90. // if this appears as a plain array element, we need to insert a delimiter and we should also indent it
  91. if (is_element) {
  92. AddIndentation();
  93. if (!first) {
  94. buff << ',';
  95. }
  96. }
  97. first = true;
  98. buff << "{" << newline;
  99. PushIndent();
  100. }
  101. void EndObj() {
  102. PopIndent();
  103. AddIndentation();
  104. first = false;
  105. buff << "}" << newline;
  106. }
  107. void StartArray(bool is_element = false) {
  108. // if this appears as a plain array element, we need to insert a delimiter and we should also indent it
  109. if (is_element) {
  110. AddIndentation();
  111. if (!first) {
  112. buff << ',';
  113. }
  114. }
  115. first = true;
  116. buff << "[" << newline;
  117. PushIndent();
  118. }
  119. void EndArray() {
  120. PopIndent();
  121. AddIndentation();
  122. buff << "]" << newline;
  123. first = false;
  124. }
  125. void AddIndentation() {
  126. if (!(flags & Flag_DoNotIndent) && !(flags & Flag_SkipWhitespaces)) {
  127. buff << indent;
  128. }
  129. }
  130. void Delimit() {
  131. if (!first) {
  132. buff << ',';
  133. } else {
  134. buff << space;
  135. first = false;
  136. }
  137. }
  138. private:
  139. template <typename Literal>
  140. std::stringstream &LiteralToString(std::stringstream &stream, const Literal &s) {
  141. stream << s;
  142. return stream;
  143. }
  144. std::stringstream &LiteralToString(std::stringstream &stream, const aiString &s) {
  145. std::string t;
  146. // escape backslashes and single quotes, both would render the JSON invalid if left as is
  147. t.reserve(s.length);
  148. for (size_t i = 0; i < s.length; ++i) {
  149. if (s.data[i] == '\\' || s.data[i] == '\'' || s.data[i] == '\"') {
  150. t.push_back('\\');
  151. }
  152. t.push_back(s.data[i]);
  153. }
  154. stream << "\"";
  155. stream << t;
  156. stream << "\"";
  157. return stream;
  158. }
  159. std::stringstream &LiteralToString(std::stringstream &stream, float f) {
  160. if (!std::numeric_limits<float>::is_iec559) {
  161. // on a non IEEE-754 platform, we make no assumptions about the representation or existence
  162. // of special floating-point numbers.
  163. stream << f;
  164. return stream;
  165. }
  166. // JSON does not support writing Inf/Nan
  167. // [RFC 4672: "Numeric values that cannot be represented as sequences of digits
  168. // (such as Infinity and NaN) are not permitted."]
  169. // Nevertheless, many parsers will accept the special keywords Infinity, -Infinity and NaN
  170. if (std::numeric_limits<float>::infinity() == fabs(f)) {
  171. if (flags & Flag_WriteSpecialFloats) {
  172. stream << (f < 0 ? "\"-" : "\"") + std::string("Infinity\"");
  173. return stream;
  174. }
  175. // we should print this warning, but we can't - this is called from within a generic assimp exporter, we cannot use cerr
  176. // std::cerr << "warning: cannot represent infinite number literal, substituting 0 instead (use -i flag to enforce Infinity/NaN)" << std::endl;
  177. stream << "0.0";
  178. return stream;
  179. }
  180. // f!=f is the most reliable test for NaNs that I know of
  181. else if (f != f) {
  182. if (flags & Flag_WriteSpecialFloats) {
  183. stream << "\"NaN\"";
  184. return stream;
  185. }
  186. // we should print this warning, but we can't - this is called from within a generic assimp exporter, we cannot use cerr
  187. // std::cerr << "warning: cannot represent infinite number literal, substituting 0 instead (use -i flag to enforce Infinity/NaN)" << std::endl;
  188. stream << "0.0";
  189. return stream;
  190. }
  191. stream << f;
  192. return stream;
  193. }
  194. private:
  195. Assimp::IOStream &out;
  196. std::string indent;
  197. std::string newline;
  198. std::string space;
  199. std::stringstream buff;
  200. bool first;
  201. unsigned int flags;
  202. };
  203. static void Write(JSONWriter &out, const aiVector3D &ai, bool is_elem = true) {
  204. out.StartArray(is_elem);
  205. out.Element(ai.x);
  206. out.Element(ai.y);
  207. out.Element(ai.z);
  208. out.EndArray();
  209. }
  210. static void Write(JSONWriter &out, const aiQuaternion &ai, bool is_elem = true) {
  211. out.StartArray(is_elem);
  212. out.Element(ai.w);
  213. out.Element(ai.x);
  214. out.Element(ai.y);
  215. out.Element(ai.z);
  216. out.EndArray();
  217. }
  218. static void Write(JSONWriter &out, const aiColor3D &ai, bool is_elem = true) {
  219. out.StartArray(is_elem);
  220. out.Element(ai.r);
  221. out.Element(ai.g);
  222. out.Element(ai.b);
  223. out.EndArray();
  224. }
  225. static void Write(JSONWriter &out, const aiMatrix4x4 &ai, bool is_elem = true) {
  226. out.StartArray(is_elem);
  227. for (unsigned int x = 0; x < 4; ++x) {
  228. for (unsigned int y = 0; y < 4; ++y) {
  229. out.Element(ai[x][y]);
  230. }
  231. }
  232. out.EndArray();
  233. }
  234. static void Write(JSONWriter &out, const aiBone &ai, bool is_elem = true) {
  235. out.StartObj(is_elem);
  236. out.Key("name");
  237. out.SimpleValue(ai.mName);
  238. out.Key("offsetmatrix");
  239. Write(out, ai.mOffsetMatrix, false);
  240. out.Key("weights");
  241. out.StartArray();
  242. for (unsigned int i = 0; i < ai.mNumWeights; ++i) {
  243. out.StartArray(true);
  244. out.Element(ai.mWeights[i].mVertexId);
  245. out.Element(ai.mWeights[i].mWeight);
  246. out.EndArray();
  247. }
  248. out.EndArray();
  249. out.EndObj();
  250. }
  251. static void Write(JSONWriter &out, const aiFace &ai, bool is_elem = true) {
  252. out.StartArray(is_elem);
  253. for (unsigned int i = 0; i < ai.mNumIndices; ++i) {
  254. out.Element(ai.mIndices[i]);
  255. }
  256. out.EndArray();
  257. }
  258. static void Write(JSONWriter &out, const aiMesh &ai, bool is_elem = true) {
  259. out.StartObj(is_elem);
  260. out.Key("name");
  261. out.SimpleValue(ai.mName);
  262. out.Key("materialindex");
  263. out.SimpleValue(ai.mMaterialIndex);
  264. out.Key("primitivetypes");
  265. out.SimpleValue(ai.mPrimitiveTypes);
  266. out.Key("vertices");
  267. out.StartArray();
  268. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  269. out.Element(ai.mVertices[i].x);
  270. out.Element(ai.mVertices[i].y);
  271. out.Element(ai.mVertices[i].z);
  272. }
  273. out.EndArray();
  274. if (ai.HasNormals()) {
  275. out.Key("normals");
  276. out.StartArray();
  277. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  278. out.Element(ai.mNormals[i].x);
  279. out.Element(ai.mNormals[i].y);
  280. out.Element(ai.mNormals[i].z);
  281. }
  282. out.EndArray();
  283. }
  284. if (ai.HasTangentsAndBitangents()) {
  285. out.Key("tangents");
  286. out.StartArray();
  287. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  288. out.Element(ai.mTangents[i].x);
  289. out.Element(ai.mTangents[i].y);
  290. out.Element(ai.mTangents[i].z);
  291. }
  292. out.EndArray();
  293. out.Key("bitangents");
  294. out.StartArray();
  295. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  296. out.Element(ai.mBitangents[i].x);
  297. out.Element(ai.mBitangents[i].y);
  298. out.Element(ai.mBitangents[i].z);
  299. }
  300. out.EndArray();
  301. }
  302. if (ai.GetNumUVChannels()) {
  303. out.Key("numuvcomponents");
  304. out.StartArray();
  305. for (unsigned int n = 0; n < ai.GetNumUVChannels(); ++n) {
  306. out.Element(ai.mNumUVComponents[n]);
  307. }
  308. out.EndArray();
  309. out.Key("texturecoords");
  310. out.StartArray();
  311. for (unsigned int n = 0; n < ai.GetNumUVChannels(); ++n) {
  312. const unsigned int numc = ai.mNumUVComponents[n] ? ai.mNumUVComponents[n] : 2;
  313. out.StartArray(true);
  314. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  315. for (unsigned int c = 0; c < numc; ++c) {
  316. out.Element(ai.mTextureCoords[n][i][c]);
  317. }
  318. }
  319. out.EndArray();
  320. }
  321. out.EndArray();
  322. }
  323. if (ai.GetNumColorChannels()) {
  324. out.Key("colors");
  325. out.StartArray();
  326. for (unsigned int n = 0; n < ai.GetNumColorChannels(); ++n) {
  327. out.StartArray(true);
  328. for (unsigned int i = 0; i < ai.mNumVertices; ++i) {
  329. out.Element(ai.mColors[n][i].r);
  330. out.Element(ai.mColors[n][i].g);
  331. out.Element(ai.mColors[n][i].b);
  332. out.Element(ai.mColors[n][i].a);
  333. }
  334. out.EndArray();
  335. }
  336. out.EndArray();
  337. }
  338. if (ai.mNumBones) {
  339. out.Key("bones");
  340. out.StartArray();
  341. for (unsigned int n = 0; n < ai.mNumBones; ++n) {
  342. Write(out, *ai.mBones[n]);
  343. }
  344. out.EndArray();
  345. }
  346. out.Key("faces");
  347. out.StartArray();
  348. for (unsigned int n = 0; n < ai.mNumFaces; ++n) {
  349. Write(out, ai.mFaces[n]);
  350. }
  351. out.EndArray();
  352. out.EndObj();
  353. }
  354. static void Write(JSONWriter &out, const aiNode &ai, bool is_elem = true) {
  355. out.StartObj(is_elem);
  356. out.Key("name");
  357. out.SimpleValue(ai.mName);
  358. out.Key("transformation");
  359. Write(out, ai.mTransformation, false);
  360. if (ai.mNumMeshes) {
  361. out.Key("meshes");
  362. out.StartArray();
  363. for (unsigned int n = 0; n < ai.mNumMeshes; ++n) {
  364. out.Element(ai.mMeshes[n]);
  365. }
  366. out.EndArray();
  367. }
  368. if (ai.mNumChildren) {
  369. out.Key("children");
  370. out.StartArray();
  371. for (unsigned int n = 0; n < ai.mNumChildren; ++n) {
  372. Write(out, *ai.mChildren[n]);
  373. }
  374. out.EndArray();
  375. }
  376. out.EndObj();
  377. }
  378. static void Write(JSONWriter &out, const aiMaterial &ai, bool is_elem = true) {
  379. out.StartObj(is_elem);
  380. out.Key("properties");
  381. out.StartArray();
  382. for (unsigned int i = 0; i < ai.mNumProperties; ++i) {
  383. const aiMaterialProperty *const prop = ai.mProperties[i];
  384. out.StartObj(true);
  385. out.Key("key");
  386. out.SimpleValue(prop->mKey);
  387. out.Key("semantic");
  388. out.SimpleValue(prop->mSemantic);
  389. out.Key("index");
  390. out.SimpleValue(prop->mIndex);
  391. out.Key("type");
  392. out.SimpleValue(prop->mType);
  393. out.Key("value");
  394. switch (prop->mType) {
  395. case aiPTI_Float:
  396. if (prop->mDataLength / sizeof(float) > 1) {
  397. out.StartArray();
  398. for (unsigned int ii = 0; ii < prop->mDataLength / sizeof(float); ++ii) {
  399. out.Element(reinterpret_cast<float *>(prop->mData)[ii]);
  400. }
  401. out.EndArray();
  402. } else {
  403. out.SimpleValue(*reinterpret_cast<float *>(prop->mData));
  404. }
  405. break;
  406. case aiPTI_Double:
  407. if (prop->mDataLength / sizeof(double) > 1) {
  408. out.StartArray();
  409. for (unsigned int ii = 0; ii < prop->mDataLength / sizeof(double); ++ii) {
  410. out.Element(reinterpret_cast<double*>(prop->mData)[ii]);
  411. }
  412. out.EndArray();
  413. } else {
  414. out.SimpleValue(*reinterpret_cast<double*>(prop->mData));
  415. }
  416. break;
  417. case aiPTI_Integer:
  418. if (prop->mDataLength / sizeof(int) > 1) {
  419. out.StartArray();
  420. for (unsigned int ii = 0; ii < prop->mDataLength / sizeof(int); ++ii) {
  421. out.Element(reinterpret_cast<int *>(prop->mData)[ii]);
  422. }
  423. out.EndArray();
  424. } else {
  425. out.SimpleValue(*reinterpret_cast<int *>(prop->mData));
  426. }
  427. break;
  428. case aiPTI_String:
  429. {
  430. aiString s;
  431. aiGetMaterialString(&ai, prop->mKey.data, prop->mSemantic, prop->mIndex, &s);
  432. out.SimpleValue(s);
  433. }
  434. break;
  435. case aiPTI_Buffer:
  436. {
  437. // binary data is written as series of hex-encoded octets
  438. out.SimpleValue(prop->mData, prop->mDataLength);
  439. }
  440. break;
  441. default:
  442. ai_assert(false);
  443. }
  444. out.EndObj();
  445. }
  446. out.EndArray();
  447. out.EndObj();
  448. }
  449. static void Write(JSONWriter &out, const aiTexture &ai, bool is_elem = true) {
  450. out.StartObj(is_elem);
  451. out.Key("width");
  452. out.SimpleValue(ai.mWidth);
  453. out.Key("height");
  454. out.SimpleValue(ai.mHeight);
  455. out.Key("formathint");
  456. out.SimpleValue(aiString(ai.achFormatHint));
  457. out.Key("data");
  458. if (!ai.mHeight) {
  459. out.SimpleValue(ai.pcData, ai.mWidth);
  460. } else {
  461. out.StartArray();
  462. for (unsigned int y = 0; y < ai.mHeight; ++y) {
  463. out.StartArray(true);
  464. for (unsigned int x = 0; x < ai.mWidth; ++x) {
  465. const aiTexel &tx = ai.pcData[y * ai.mWidth + x];
  466. out.StartArray(true);
  467. out.Element(static_cast<unsigned int>(tx.r));
  468. out.Element(static_cast<unsigned int>(tx.g));
  469. out.Element(static_cast<unsigned int>(tx.b));
  470. out.Element(static_cast<unsigned int>(tx.a));
  471. out.EndArray();
  472. }
  473. out.EndArray();
  474. }
  475. out.EndArray();
  476. }
  477. out.EndObj();
  478. }
  479. static void Write(JSONWriter &out, const aiLight &ai, bool is_elem = true) {
  480. out.StartObj(is_elem);
  481. out.Key("name");
  482. out.SimpleValue(ai.mName);
  483. out.Key("type");
  484. out.SimpleValue(ai.mType);
  485. if (ai.mType == aiLightSource_SPOT || ai.mType == aiLightSource_UNDEFINED) {
  486. out.Key("angleinnercone");
  487. out.SimpleValue(ai.mAngleInnerCone);
  488. out.Key("angleoutercone");
  489. out.SimpleValue(ai.mAngleOuterCone);
  490. }
  491. out.Key("attenuationconstant");
  492. out.SimpleValue(ai.mAttenuationConstant);
  493. out.Key("attenuationlinear");
  494. out.SimpleValue(ai.mAttenuationLinear);
  495. out.Key("attenuationquadratic");
  496. out.SimpleValue(ai.mAttenuationQuadratic);
  497. out.Key("diffusecolor");
  498. Write(out, ai.mColorDiffuse, false);
  499. out.Key("specularcolor");
  500. Write(out, ai.mColorSpecular, false);
  501. out.Key("ambientcolor");
  502. Write(out, ai.mColorAmbient, false);
  503. if (ai.mType != aiLightSource_POINT) {
  504. out.Key("direction");
  505. Write(out, ai.mDirection, false);
  506. }
  507. if (ai.mType != aiLightSource_DIRECTIONAL) {
  508. out.Key("position");
  509. Write(out, ai.mPosition, false);
  510. }
  511. out.EndObj();
  512. }
  513. static void Write(JSONWriter &out, const aiNodeAnim &ai, bool is_elem = true) {
  514. out.StartObj(is_elem);
  515. out.Key("name");
  516. out.SimpleValue(ai.mNodeName);
  517. out.Key("prestate");
  518. out.SimpleValue(ai.mPreState);
  519. out.Key("poststate");
  520. out.SimpleValue(ai.mPostState);
  521. if (ai.mNumPositionKeys) {
  522. out.Key("positionkeys");
  523. out.StartArray();
  524. for (unsigned int n = 0; n < ai.mNumPositionKeys; ++n) {
  525. const aiVectorKey &pos = ai.mPositionKeys[n];
  526. out.StartArray(true);
  527. out.Element(pos.mTime);
  528. Write(out, pos.mValue);
  529. out.EndArray();
  530. }
  531. out.EndArray();
  532. }
  533. if (ai.mNumRotationKeys) {
  534. out.Key("rotationkeys");
  535. out.StartArray();
  536. for (unsigned int n = 0; n < ai.mNumRotationKeys; ++n) {
  537. const aiQuatKey &rot = ai.mRotationKeys[n];
  538. out.StartArray(true);
  539. out.Element(rot.mTime);
  540. Write(out, rot.mValue);
  541. out.EndArray();
  542. }
  543. out.EndArray();
  544. }
  545. if (ai.mNumScalingKeys) {
  546. out.Key("scalingkeys");
  547. out.StartArray();
  548. for (unsigned int n = 0; n < ai.mNumScalingKeys; ++n) {
  549. const aiVectorKey &scl = ai.mScalingKeys[n];
  550. out.StartArray(true);
  551. out.Element(scl.mTime);
  552. Write(out, scl.mValue);
  553. out.EndArray();
  554. }
  555. out.EndArray();
  556. }
  557. out.EndObj();
  558. }
  559. static void Write(JSONWriter &out, const aiAnimation &ai, bool is_elem = true) {
  560. out.StartObj(is_elem);
  561. out.Key("name");
  562. out.SimpleValue(ai.mName);
  563. out.Key("tickspersecond");
  564. out.SimpleValue(ai.mTicksPerSecond);
  565. out.Key("duration");
  566. out.SimpleValue(ai.mDuration);
  567. out.Key("channels");
  568. out.StartArray();
  569. for (unsigned int n = 0; n < ai.mNumChannels; ++n) {
  570. Write(out, *ai.mChannels[n]);
  571. }
  572. out.EndArray();
  573. out.EndObj();
  574. }
  575. static void Write(JSONWriter &out, const aiCamera &ai, bool is_elem = true) {
  576. out.StartObj(is_elem);
  577. out.Key("name");
  578. out.SimpleValue(ai.mName);
  579. out.Key("aspect");
  580. out.SimpleValue(ai.mAspect);
  581. out.Key("clipplanefar");
  582. out.SimpleValue(ai.mClipPlaneFar);
  583. out.Key("clipplanenear");
  584. out.SimpleValue(ai.mClipPlaneNear);
  585. out.Key("horizontalfov");
  586. out.SimpleValue(ai.mHorizontalFOV);
  587. out.Key("up");
  588. Write(out, ai.mUp, false);
  589. out.Key("lookat");
  590. Write(out, ai.mLookAt, false);
  591. out.EndObj();
  592. }
  593. static void WriteFormatInfo(JSONWriter &out) {
  594. out.StartObj();
  595. out.Key("format");
  596. out.SimpleValue("\"assimp2json\"");
  597. out.Key("version");
  598. out.SimpleValue(CURRENT_FORMAT_VERSION);
  599. out.EndObj();
  600. }
  601. static void Write(JSONWriter &out, const aiScene &ai) {
  602. out.StartObj();
  603. out.Key("__metadata__");
  604. WriteFormatInfo(out);
  605. out.Key("rootnode");
  606. Write(out, *ai.mRootNode, false);
  607. out.Key("flags");
  608. out.SimpleValue(ai.mFlags);
  609. if (ai.HasMeshes()) {
  610. out.Key("meshes");
  611. out.StartArray();
  612. for (unsigned int n = 0; n < ai.mNumMeshes; ++n) {
  613. Write(out, *ai.mMeshes[n]);
  614. }
  615. out.EndArray();
  616. }
  617. if (ai.HasMaterials()) {
  618. out.Key("materials");
  619. out.StartArray();
  620. for (unsigned int n = 0; n < ai.mNumMaterials; ++n) {
  621. Write(out, *ai.mMaterials[n]);
  622. }
  623. out.EndArray();
  624. }
  625. if (ai.HasAnimations()) {
  626. out.Key("animations");
  627. out.StartArray();
  628. for (unsigned int n = 0; n < ai.mNumAnimations; ++n) {
  629. Write(out, *ai.mAnimations[n]);
  630. }
  631. out.EndArray();
  632. }
  633. if (ai.HasLights()) {
  634. out.Key("lights");
  635. out.StartArray();
  636. for (unsigned int n = 0; n < ai.mNumLights; ++n) {
  637. Write(out, *ai.mLights[n]);
  638. }
  639. out.EndArray();
  640. }
  641. if (ai.HasCameras()) {
  642. out.Key("cameras");
  643. out.StartArray();
  644. for (unsigned int n = 0; n < ai.mNumCameras; ++n) {
  645. Write(out, *ai.mCameras[n]);
  646. }
  647. out.EndArray();
  648. }
  649. if (ai.HasTextures()) {
  650. out.Key("textures");
  651. out.StartArray();
  652. for (unsigned int n = 0; n < ai.mNumTextures; ++n) {
  653. Write(out, *ai.mTextures[n]);
  654. }
  655. out.EndArray();
  656. }
  657. out.EndObj();
  658. }
  659. void ExportAssimp2Json(const char *file, Assimp::IOSystem *io, const aiScene *scene, const Assimp::ExportProperties *pProperties) {
  660. std::unique_ptr<Assimp::IOStream> str(io->Open(file, "wt"));
  661. if (!str) {
  662. throw DeadlyExportError("could not open output file");
  663. }
  664. // get a copy of the scene so we can modify it
  665. aiScene *scenecopy_tmp;
  666. aiCopyScene(scene, &scenecopy_tmp);
  667. try {
  668. // split meshes so they fit into a 16 bit index buffer
  669. MeshSplitter splitter;
  670. splitter.SetLimit(1 << 16);
  671. splitter.Execute(scenecopy_tmp);
  672. // XXX Flag_WriteSpecialFloats is turned on by default, right now we don't have a configuration interface for exporters
  673. unsigned int flags = JSONWriter::Flag_WriteSpecialFloats;
  674. if (pProperties->GetPropertyBool("JSON_SKIP_WHITESPACES", false)) {
  675. flags |= JSONWriter::Flag_SkipWhitespaces;
  676. }
  677. JSONWriter s(*str, flags);
  678. Write(s, *scenecopy_tmp);
  679. } catch (...) {
  680. aiFreeScene(scenecopy_tmp);
  681. throw;
  682. }
  683. aiFreeScene(scenecopy_tmp);
  684. }
  685. } // namespace Assimp
  686. #endif // ASSIMP_BUILD_NO_ASSJSON_EXPORTER
  687. #endif // ASSIMP_BUILD_NO_EXPORT