glTF.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. #include "glTF.h"
  2. #include "gfx/ModelInstance.h"
  3. #include "util/Json.h"
  4. #include "gfx/RenderDevice.h"
  5. #include "BFApp.h"
  6. USING_NS_BF;
  7. static bool IsWhitespace(char c)
  8. {
  9. return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
  10. }
  11. class GLTFPropsParser
  12. {
  13. public:
  14. enum NodeKind
  15. {
  16. NodeKind_None,
  17. NodeKind_End,
  18. NodeKind_LBrace,
  19. NodeKind_RBrace,
  20. NodeKind_LBracket,
  21. NodeKind_RBracket,
  22. NodeKind_Equals,
  23. NodeKind_Index,
  24. NodeKind_Integer,
  25. NodeKind_Float,
  26. NodeKind_String
  27. };
  28. struct Node
  29. {
  30. public:
  31. NodeKind mKind;
  32. int mStart;
  33. int mEnd;
  34. union
  35. {
  36. int mValueInt;
  37. float mValueFloat;
  38. };
  39. public:
  40. Node()
  41. {
  42. mKind = NodeKind_None;
  43. mStart = -1;
  44. mEnd = -1;
  45. mValueInt = 0;
  46. }
  47. };
  48. public:
  49. char* mStart;
  50. char* mPtr;
  51. char* mEnd;
  52. Node mNext;
  53. public:
  54. GLTFPropsParser(const StringImpl& str)
  55. {
  56. mStart = str.mPtr;
  57. mPtr = mStart;
  58. mEnd = mStart + str.mLength;
  59. }
  60. // double ParseLiteralDouble()
  61. // {
  62. // char buf[256];
  63. // int len = BF_MAX(mTokenEnd - mTokenStart, 255);
  64. //
  65. // memcpy(buf, &mSrc[mTokenStart], len);
  66. // char c = buf[len - 1];
  67. // if ((c == 'd') || (c == 'D') || (c == 'f') || (c == 'F'))
  68. // buf[len - 1] = '\0';
  69. // else
  70. // buf[len] = '\0';
  71. //
  72. // return strtod(buf, NULL);
  73. // }
  74. Node PeekNext()
  75. {
  76. if (mNext.mKind != NodeKind_None)
  77. return mNext;
  78. while (true)
  79. {
  80. if (mPtr >= mEnd)
  81. {
  82. mNext.mKind = NodeKind_End;
  83. return mNext;
  84. }
  85. char* start = mPtr;
  86. char c = *(mPtr++);
  87. if (c == '{')
  88. mNext.mKind = NodeKind_LBrace;
  89. else if (c == '}')
  90. mNext.mKind = NodeKind_RBrace;
  91. else if (c == '[')
  92. mNext.mKind = NodeKind_LBracket;
  93. else if (c == ']')
  94. mNext.mKind = NodeKind_RBracket;
  95. else if (c == '=')
  96. mNext.mKind = NodeKind_Equals;
  97. if (mNext.mKind != NodeKind_None)
  98. {
  99. mNext.mStart = (int)(mPtr - mStart - 1);
  100. mNext.mEnd = (int)(mPtr - mStart);
  101. return mNext;
  102. }
  103. if ((c >= '0') && (c <= '9'))
  104. {
  105. bool hadDot = false;
  106. while (mPtr < mEnd)
  107. {
  108. char c = *mPtr;
  109. if (c == '.')
  110. {
  111. mPtr++;
  112. hadDot = true;
  113. }
  114. else if ((c >= '0') && (c <= '9'))
  115. {
  116. mPtr++;
  117. }
  118. else
  119. break;
  120. }
  121. mNext.mStart = (int)(start - mStart);
  122. mNext.mEnd = (int)(mPtr - mStart);
  123. char buf[256];
  124. int len = BF_MIN((int)(mPtr - start), 255);
  125. memcpy(buf, start, len);
  126. char c = buf[len - 1];
  127. if ((c == 'd') || (c == 'D') || (c == 'f') || (c == 'F'))
  128. buf[len - 1] = '\0';
  129. else
  130. buf[len] = '\0';
  131. if (hadDot)
  132. {
  133. mNext.mKind = NodeKind_Float;
  134. mNext.mValueFloat = (float)strtod(buf, NULL);
  135. }
  136. else
  137. {
  138. mNext.mKind = NodeKind_Integer;
  139. mNext.mValueInt = atoi(buf);
  140. }
  141. return mNext;
  142. }
  143. if (!IsWhitespace(c))
  144. {
  145. char* lastCPtr = start;
  146. while (mPtr < mEnd)
  147. {
  148. char c = *mPtr;
  149. if ((c == '}') || (c == '=') || (c == '[') || (c == '\r') || (c == '\n'))
  150. break;
  151. if (c != ' ')
  152. lastCPtr = mPtr;
  153. mPtr++;
  154. }
  155. mPtr = lastCPtr + 1;
  156. mNext.mStart = (int)(start - mStart);
  157. mNext.mEnd = (int)(mPtr - mStart);
  158. mNext.mKind = NodeKind_String;
  159. return mNext;
  160. }
  161. }
  162. }
  163. Node GetNext()
  164. {
  165. auto node = PeekNext();
  166. mNext = Node();
  167. return node;
  168. }
  169. StringView GetStringView(const Node& node)
  170. {
  171. return StringView(mStart + node.mStart, node.mEnd - node.mStart);
  172. }
  173. StringView GetStringView(const Node& node, StringView& prefix)
  174. {
  175. auto stringView = StringView(mStart + node.mStart, node.mEnd - node.mStart);
  176. if (!stringView.EndsWith('\''))
  177. {
  178. prefix = "";
  179. return stringView;
  180. }
  181. int strStartIdx = (int)stringView.IndexOf('\'');
  182. prefix = StringView(stringView, 0, strStartIdx);
  183. return StringView(stringView, strStartIdx + 1, (int)stringView.mLength - strStartIdx - 2);
  184. }
  185. bool GetNextStringView(StringView& prefix, StringView& value)
  186. {
  187. auto node = GetNext();
  188. if (node.mKind != NodeKind_String)
  189. return false;
  190. auto stringView = StringView(mStart + node.mStart, node.mEnd - node.mStart);
  191. if (!stringView.EndsWith('\''))
  192. {
  193. prefix = "";
  194. value = stringView;
  195. return true;
  196. }
  197. int strStartIdx = (int)stringView.IndexOf('\'');
  198. prefix = StringView(stringView, 0, strStartIdx);
  199. value = StringView(stringView, strStartIdx + 1, (int)stringView.mLength - strStartIdx - 2);
  200. return true;
  201. }
  202. };
  203. enum ComponentType
  204. {
  205. Int8 = 5120,
  206. UInt8 = 5121,
  207. Int16 = 5122,
  208. UInt16 = 5123,
  209. UInt32 = 5125,
  210. Float = 5126,
  211. };
  212. BF_EXPORT void* BF_CALLTYPE Res_OpenGLTF(const char* fileName, const char* baseDir, void* vertexDefinition)
  213. {
  214. ModelDef* modelDef = new ModelDef();
  215. GLTFReader reader(modelDef);
  216. if (!reader.ReadFile(fileName, baseDir))
  217. {
  218. delete modelDef;
  219. return NULL;
  220. }
  221. return modelDef;
  222. }
  223. GLTFReader::GLTFReader(ModelDef* modelDef)
  224. {
  225. mModelDef = modelDef;
  226. }
  227. GLTFReader::~GLTFReader()
  228. {
  229. }
  230. struct DataSpan
  231. {
  232. uint8* mPtr;
  233. int mSize;
  234. };
  235. struct DataAccessor
  236. {
  237. uint8* mPtr;
  238. int mSize;
  239. int mCount;
  240. ComponentType mComponentType;
  241. };
  242. template <typename T>
  243. static void ReadBuffer(DataAccessor& dataAccessor, T* outPtr, int outStride)
  244. {
  245. for (int i = 0; i < dataAccessor.mCount; i++)
  246. *(T*)((uint8*)outPtr + i * outStride) = ((T*)dataAccessor.mPtr)[i];
  247. }
  248. template <typename T>
  249. static void ReadBuffer(DataAccessor& dataAccessor, T* outPtr, int outStride, int inStride)
  250. {
  251. for (int i = 0; i < dataAccessor.mCount; i++)
  252. *(T*)((uint8*)outPtr + i * outStride) = *(T*)(dataAccessor.mPtr + i * inStride);
  253. }
  254. static void TrySkipValue(GLTFPropsParser& propsParser)
  255. {
  256. auto nextNode = propsParser.PeekNext();
  257. if (nextNode.mKind == GLTFPropsParser::NodeKind_LBracket)
  258. {
  259. propsParser.GetNext();
  260. propsParser.GetNext();
  261. propsParser.GetNext();
  262. nextNode = propsParser.PeekNext();
  263. }
  264. if (nextNode.mKind == GLTFPropsParser::NodeKind_Equals)
  265. {
  266. propsParser.GetNext();
  267. int depth = 0;
  268. do
  269. {
  270. auto node = propsParser.GetNext();
  271. if (node.mKind == GLTFPropsParser::NodeKind_End)
  272. return;
  273. if (node.mKind == GLTFPropsParser::NodeKind_LBrace)
  274. depth++;
  275. else if (node.mKind == GLTFPropsParser::NodeKind_RBrace)
  276. depth--;
  277. if (node.mKind == GLTFPropsParser::NodeKind_LBracket)
  278. depth++;
  279. if (node.mKind == GLTFPropsParser::NodeKind_LBracket)
  280. depth--;
  281. } while (depth > 0);
  282. }
  283. }
  284. static bool ExpectIndex(GLTFPropsParser& propsParser, int& idx)
  285. {
  286. auto node = propsParser.GetNext();
  287. if (node.mKind != GLTFPropsParser::NodeKind_LBracket)
  288. return false;
  289. node = propsParser.GetNext();
  290. if (node.mKind != GLTFPropsParser::NodeKind_Integer)
  291. return false;
  292. idx = node.mValueInt;
  293. node = propsParser.GetNext();
  294. if (node.mKind != GLTFPropsParser::NodeKind_RBracket)
  295. return false;
  296. return true;
  297. };
  298. static bool ExpectOpen(GLTFPropsParser& propsParser)
  299. {
  300. if (propsParser.GetNext().mKind != GLTFPropsParser::NodeKind_LBrace)
  301. return false;
  302. return true;
  303. };
  304. static bool ExpectClose(GLTFPropsParser& propsParser)
  305. {
  306. if (propsParser.GetNext().mKind != GLTFPropsParser::NodeKind_RBrace)
  307. return false;
  308. return true;
  309. };
  310. static bool ExpectEquals(GLTFPropsParser& propsParser)
  311. {
  312. if (propsParser.GetNext().mKind != GLTFPropsParser::NodeKind_Equals)
  313. return false;
  314. return true;
  315. };
  316. bool GLTFReader::ParseMaterialDef(ModelMaterialDef* materialDef, const StringImpl& matText)
  317. {
  318. GLTFPropsParser propsParser(matText);
  319. while (true)
  320. {
  321. auto node = propsParser.GetNext();
  322. if (node.mKind == GLTFPropsParser::NodeKind_End)
  323. break;
  324. if (node.mKind == GLTFPropsParser::NodeKind_String)
  325. {
  326. auto key = propsParser.GetStringView(node);
  327. if (key == "Parent")
  328. {
  329. if (propsParser.GetNext().mKind != GLTFPropsParser::NodeKind_Equals)
  330. return false;
  331. auto valueNode = propsParser.GetNext();
  332. if (valueNode.mKind != GLTFPropsParser::NodeKind_String)
  333. return false;
  334. StringView prefix;
  335. StringView str = propsParser.GetStringView(valueNode, prefix);
  336. auto parentMaterialDef = LoadMaterial(str);
  337. }
  338. else if (key == "TextureParameterValues")
  339. {
  340. int count = 0;
  341. if (!ExpectIndex(propsParser, count))
  342. return false;
  343. if (!ExpectEquals(propsParser))
  344. return false;
  345. if (!ExpectOpen(propsParser))
  346. return false;
  347. while (true)
  348. {
  349. node = propsParser.GetNext();
  350. if (node.mKind == GLTFPropsParser::NodeKind_RBrace)
  351. break;
  352. if (node.mKind != GLTFPropsParser::NodeKind_String)
  353. return false;
  354. StringView prefix;
  355. StringView str = propsParser.GetStringView(node, prefix);
  356. if (str == "TextureParameterValues")
  357. {
  358. auto textureParamValue = materialDef->mTextureParameterValues.Alloc();
  359. int idx = 0;
  360. if (!ExpectIndex(propsParser, idx))
  361. return false;
  362. if (!ExpectEquals(propsParser))
  363. return false;
  364. if (!ExpectOpen(propsParser))
  365. return false;
  366. while (true)
  367. {
  368. node = propsParser.GetNext();
  369. if (node.mKind == GLTFPropsParser::NodeKind_RBrace)
  370. break;
  371. if (node.mKind != GLTFPropsParser::NodeKind_String)
  372. return false;
  373. str = propsParser.GetStringView(node, prefix);
  374. if (str == "ParameterInfo")
  375. {
  376. if (!ExpectEquals(propsParser))
  377. return false;
  378. if (!ExpectOpen(propsParser))
  379. return false;
  380. if (!propsParser.GetNextStringView(prefix, str))
  381. return false;
  382. if (!ExpectEquals(propsParser))
  383. return false;
  384. if (!propsParser.GetNextStringView(prefix, str))
  385. return false;
  386. textureParamValue->mName = str;
  387. if (!ExpectClose(propsParser))
  388. return false;
  389. }
  390. else if (str == "ParameterValue")
  391. {
  392. if (!ExpectEquals(propsParser))
  393. return false;
  394. if (!propsParser.GetNextStringView(prefix, str))
  395. return false;
  396. String path = mRootDir;
  397. path += str;
  398. int dotPos = (int)path.IndexOf('.');
  399. if (dotPos != -1)
  400. path.RemoveToEnd(dotPos);
  401. path += ".tga";
  402. textureParamValue->mTexturePath = path;
  403. // Texture* texture = gBFApp->mRenderDevice->LoadTexture(path, 0);
  404. // textureParamValue->mTexture = texture;
  405. }
  406. else
  407. TrySkipValue(propsParser);
  408. }
  409. }
  410. else
  411. {
  412. TrySkipValue(propsParser);
  413. }
  414. }
  415. }
  416. else
  417. {
  418. TrySkipValue(propsParser);
  419. }
  420. }
  421. }
  422. return true;
  423. }
  424. ModelMaterialDef* GLTFReader::LoadMaterial(const StringImpl& relPath)
  425. {
  426. String propsPath;
  427. if (relPath.StartsWith('/'))
  428. {
  429. propsPath = mRootDir + relPath;
  430. int dotPos = (int)propsPath.LastIndexOf('.');
  431. if (dotPos > 0)
  432. propsPath.RemoveToEnd(dotPos);
  433. propsPath += ".props.txt";
  434. }
  435. else if (mBasePathName.Contains("staticmesh"))
  436. propsPath = GetFileDir(mBasePathName) + "/" + relPath + ".props.txt";
  437. else
  438. propsPath = GetFileDir(mBasePathName) + "/materials/" + relPath + ".props.txt";
  439. ModelMaterialDef* materialDef = ModelMaterialDef::CreateOrGet("GLTF", propsPath);
  440. if (materialDef->mInitialized)
  441. return materialDef;
  442. materialDef->mInitialized = true;
  443. String propText;
  444. if (LoadTextData(propsPath, propText))
  445. {
  446. if (!ParseMaterialDef(materialDef, propText))
  447. {
  448. // Had error
  449. }
  450. }
  451. return materialDef;
  452. }
  453. bool GLTFReader::LoadModelProps(const StringImpl& propsPath)
  454. {
  455. String propText;
  456. if (!LoadTextData(propsPath, propText))
  457. return false;
  458. GLTFPropsParser propsParser(propText);
  459. while (true)
  460. {
  461. auto node = propsParser.GetNext();
  462. if (node.mKind == GLTFPropsParser::NodeKind_End)
  463. break;
  464. if (node.mKind == GLTFPropsParser::NodeKind_String)
  465. {
  466. auto key = propsParser.GetStringView(node);
  467. if (key == "StaticMaterials")
  468. {
  469. int count = 0;
  470. if (!ExpectIndex(propsParser, count))
  471. return false;
  472. if (!ExpectEquals(propsParser))
  473. return false;
  474. if (!ExpectOpen(propsParser))
  475. return false;
  476. while (true)
  477. {
  478. node = propsParser.GetNext();
  479. if (node.mKind == GLTFPropsParser::NodeKind_RBrace)
  480. break;
  481. if (node.mKind != GLTFPropsParser::NodeKind_String)
  482. return false;
  483. StringView prefix;
  484. StringView str = propsParser.GetStringView(node, prefix);
  485. if (str == "StaticMaterials")
  486. {
  487. StaticMaterial staticMaterial;
  488. int idx = 0;
  489. if (!ExpectIndex(propsParser, idx))
  490. return false;
  491. if (!ExpectEquals(propsParser))
  492. return false;
  493. if (!ExpectOpen(propsParser))
  494. return false;
  495. while (true)
  496. {
  497. node = propsParser.GetNext();
  498. if (node.mKind == GLTFPropsParser::NodeKind_RBrace)
  499. break;
  500. if (node.mKind != GLTFPropsParser::NodeKind_String)
  501. return false;
  502. str = propsParser.GetStringView(node, prefix);
  503. if (str == "MaterialSlotName")
  504. {
  505. if (!ExpectEquals(propsParser))
  506. return false;
  507. if (!propsParser.GetNextStringView(prefix, str))
  508. return false;
  509. staticMaterial.mMaterialSlotName = str;
  510. }
  511. else if (str == "MaterialInterface")
  512. {
  513. if (!ExpectEquals(propsParser))
  514. return false;
  515. if (!propsParser.GetNextStringView(prefix, str))
  516. return false;
  517. staticMaterial.mMaterialDef = LoadMaterial(str);
  518. }
  519. else
  520. TrySkipValue(propsParser);
  521. }
  522. mStaticMaterials.Add(staticMaterial);
  523. }
  524. else
  525. {
  526. TrySkipValue(propsParser);
  527. }
  528. }
  529. }
  530. else
  531. {
  532. TrySkipValue(propsParser);
  533. }
  534. }
  535. }
  536. return true;
  537. }
  538. bool GLTFReader::ReadFile(const StringImpl& filePath, const StringImpl& rootDir)
  539. {
  540. String basePathName;
  541. int dotPos = (int)filePath.LastIndexOf('.');
  542. if (dotPos > 0)
  543. basePathName = filePath.Substring(0, dotPos);
  544. else
  545. basePathName = basePathName;
  546. mBasePathName = basePathName;
  547. mRootDir = rootDir;
  548. String jsonPath = basePathName + ".gltf";
  549. char* textData = LoadTextData(jsonPath, NULL);
  550. if (textData == NULL)
  551. return false;
  552. defer({ delete textData; });
  553. Json* jRoot = Json::Parse(textData);
  554. if (jRoot == NULL)
  555. return false;
  556. defer({ delete jRoot; });
  557. LoadModelProps(basePathName + ".props.txt");
  558. Array<Array<uint8>> buffers;
  559. Array<DataSpan> bufferViews;
  560. Array<DataAccessor> dataAccessors;
  561. if (auto jBuffers = jRoot->GetObjectItem("buffers"))
  562. {
  563. for (auto jBuffer = jBuffers->mChild; jBuffer != NULL; jBuffer = jBuffer->mNext)
  564. {
  565. Array<uint8> data;
  566. if (auto jName = jBuffer->GetObjectItem("uri"))
  567. {
  568. if (jName->mValueString != NULL)
  569. {
  570. String dataPath = GetFileDir(basePathName) + "/" + jName->mValueString;
  571. int size = 0;
  572. uint8* rawData = LoadBinaryData(dataPath, &size);
  573. if (rawData != NULL)
  574. data.Insert(0, rawData, size);
  575. }
  576. }
  577. buffers.Add(data);
  578. }
  579. }
  580. if (auto jBufferViews = jRoot->GetObjectItem("bufferViews"))
  581. {
  582. for (auto jBufferView = jBufferViews->mChild; jBufferView != NULL; jBufferView = jBufferView->mNext)
  583. {
  584. int bufferIdx = 0;
  585. int byteOffset = 0;
  586. int byteLength = 0;
  587. if (auto jBufferIdx = jBufferView->GetObjectItem("buffer"))
  588. bufferIdx = jBufferIdx->mValueInt;
  589. if (auto jByteOffset = jBufferView->GetObjectItem("byteOffset"))
  590. byteOffset = jByteOffset->mValueInt;
  591. if (auto jByteLength = jBufferView->GetObjectItem("byteLength"))
  592. byteLength = jByteLength->mValueInt;
  593. bufferViews.Add(DataSpan{ buffers[bufferIdx].mVals + byteOffset, byteLength });
  594. }
  595. }
  596. if (auto jAccessors = jRoot->GetObjectItem("accessors"))
  597. {
  598. for (auto jAccessor = jAccessors->mChild; jAccessor != NULL; jAccessor = jAccessor->mNext)
  599. {
  600. DataAccessor dataAccessor = { 0 };
  601. if (auto jBufferIdx = jAccessor->GetObjectItem("bufferView"))
  602. {
  603. DataSpan& dataSpan = bufferViews[jBufferIdx->mValueInt];
  604. dataAccessor.mPtr = dataSpan.mPtr;
  605. dataAccessor.mSize = dataSpan.mSize;
  606. }
  607. if (auto jCount = jAccessor->GetObjectItem("count"))
  608. dataAccessor.mCount = jCount->mValueInt;
  609. if (auto jCount = jAccessor->GetObjectItem("componentType"))
  610. dataAccessor.mComponentType = (ComponentType)jCount->mValueInt;
  611. dataAccessors.Add(dataAccessor);
  612. }
  613. }
  614. auto _GetFloat3 = [&](Json* json, Vector3& vec)
  615. {
  616. int i = 0;
  617. for (auto jItem = json->mChild; jItem != NULL; jItem = jItem->mNext)
  618. {
  619. if (i == 0)
  620. vec.mX = (float)jItem->mValueDouble;
  621. if (i == 1)
  622. vec.mY = (float)jItem->mValueDouble;
  623. if (i == 2)
  624. vec.mZ = (float)jItem->mValueDouble;
  625. i++;
  626. }
  627. };
  628. auto _GetFloat4 = [&](Json* json, Vector4& vec)
  629. {
  630. int i = 0;
  631. for (auto jItem = json->mChild; jItem != NULL; jItem = jItem->mNext)
  632. {
  633. if (i == 0)
  634. vec.mX = (float)jItem->mValueDouble;
  635. if (i == 1)
  636. vec.mY = (float)jItem->mValueDouble;
  637. if (i == 2)
  638. vec.mZ = (float)jItem->mValueDouble;
  639. if (i == 3)
  640. vec.mW = (float)jItem->mValueDouble;
  641. i++;
  642. }
  643. };
  644. if (auto jMaterials = jRoot->GetObjectItem("materials"))
  645. {
  646. int materialIdx = 0;
  647. for (auto jMaterial = jMaterials->mChild; jMaterial != NULL; jMaterial = jMaterial->mNext)
  648. {
  649. ModelMaterialInstance modelMaterialInstance;
  650. if (auto jName = jMaterial->GetObjectItem("name"))
  651. {
  652. if (jName->mValueString != NULL)
  653. {
  654. modelMaterialInstance.mName = jName->mValueString;
  655. String matPath = jName->mValueString;
  656. if (materialIdx < mStaticMaterials.mSize)
  657. matPath = mStaticMaterials[materialIdx].mMaterialSlotName;
  658. ModelMaterialDef* materialDef = LoadMaterial(matPath);
  659. modelMaterialInstance.mDef = materialDef;
  660. }
  661. }
  662. if (auto jPBRMetallicRoughness = jMaterial->GetObjectItem("pbrMetallicRoughness"))
  663. {
  664. }
  665. mModelDef->mMaterials.Add(modelMaterialInstance);
  666. materialIdx++;
  667. }
  668. }
  669. if (auto jMeshes = jRoot->GetObjectItem("meshes"))
  670. {
  671. for (auto jMesh = jMeshes->mChild; jMesh != NULL; jMesh = jMesh->mNext)
  672. {
  673. ModelMesh modelMesh;
  674. if (auto jName = jMesh->GetObjectItem("name"))
  675. {
  676. if (jName->mValueString != NULL)
  677. modelMesh.mName = jName->mValueString;
  678. }
  679. if (auto jPrimitives = jMesh->GetObjectItem("primitives"))
  680. {
  681. modelMesh.mPrimitives.Resize(jPrimitives->GetArraySize());
  682. int primCount = 0;
  683. for (auto jPrimitive = jPrimitives->mChild; jPrimitive != NULL; jPrimitive = jPrimitive->mNext)
  684. {
  685. ModelPrimitives& modelPrimitives = modelMesh.mPrimitives[primCount];
  686. if (auto jIndices = jPrimitive->GetObjectItem("indices"))
  687. {
  688. auto& dataAccessor = dataAccessors[jIndices->mValueInt];
  689. modelPrimitives.mIndices.ResizeRaw(dataAccessor.mCount);
  690. for (int i = 0; i < dataAccessor.mCount; i++)
  691. modelPrimitives.mIndices[i] = *(uint16*)(dataAccessor.mPtr + i * 2);
  692. }
  693. if (auto jIndices = jPrimitive->GetObjectItem("material"))
  694. modelPrimitives.mMaterial = &mModelDef->mMaterials[jIndices->mValueInt];
  695. if (auto jAttributes = jPrimitive->GetObjectItem("attributes"))
  696. {
  697. if (auto jPosition = jAttributes->GetObjectItem("POSITION"))
  698. {
  699. auto& dataAccessor = dataAccessors[jPosition->mValueInt];
  700. modelPrimitives.mVertices.Resize(dataAccessor.mCount);
  701. ReadBuffer<Vector3>(dataAccessor, &modelPrimitives.mVertices[0].mPosition, sizeof(ModelVertex));
  702. }
  703. if (auto jNormal = jAttributes->GetObjectItem("NORMAL"))
  704. ReadBuffer<Vector3>(dataAccessors[jNormal->mValueInt], &modelPrimitives.mVertices[0].mNormal, sizeof(ModelVertex));
  705. if (auto jTangent = jAttributes->GetObjectItem("TANGENT"))
  706. ReadBuffer<Vector3>(dataAccessors[jTangent->mValueInt], &modelPrimitives.mVertices[0].mTangent, sizeof(ModelVertex), sizeof(Vector4));
  707. if (auto jColor = jAttributes->GetObjectItem("COLOR_0"))
  708. ReadBuffer<uint32>(dataAccessors[jColor->mValueInt], &modelPrimitives.mVertices[0].mColor, sizeof(ModelVertex));
  709. if (auto jTexCoords = jAttributes->GetObjectItem("TEXCOORD_0"))
  710. {
  711. ReadBuffer<TexCoords>(dataAccessors[jTexCoords->mValueInt], &modelPrimitives.mVertices[0].mTexCoords, sizeof(ModelVertex));
  712. for (auto& vertex : modelPrimitives.mVertices)
  713. {
  714. vertex.mTexCoords.mV = 1.0f - vertex.mTexCoords.mV;
  715. }
  716. }
  717. if (auto jTexCoords = jAttributes->GetObjectItem("TEXCOORD_1"))
  718. {
  719. ReadBuffer<TexCoords>(dataAccessors[jTexCoords->mValueInt], &modelPrimitives.mVertices[0].mTexCoords, sizeof(ModelVertex));
  720. for (auto& vertex : modelPrimitives.mVertices)
  721. {
  722. //vertex.mTexCoords.mU = 1.0f - vertex.mTexCoords.mU;
  723. vertex.mTexCoords.mV = 1.0f - vertex.mTexCoords.mV;
  724. }
  725. }
  726. else
  727. {
  728. for (auto& vertex : modelPrimitives.mVertices)
  729. vertex.mBumpTexCoords = vertex.mTexCoords;
  730. }
  731. }
  732. primCount++;
  733. }
  734. }
  735. mModelDef->mMeshes.Add(modelMesh);
  736. }
  737. }
  738. if (auto jNodes = jRoot->GetObjectItem("nodes"))
  739. {
  740. mModelDef->mNodes.Reserve(jNodes->GetArraySize());
  741. for (auto jNode = jNodes->mChild; jNode != NULL; jNode = jNode->mNext)
  742. {
  743. ModelNode modelNode;
  744. if (auto jName = jNode->GetObjectItem("name"))
  745. {
  746. if (jName->mValueString != NULL)
  747. modelNode.mName = jName->mValueString;
  748. }
  749. if (auto jChildren = jNode->GetObjectItem("children"))
  750. {
  751. for (auto jChild = jChildren->mChild; jChild != NULL; jChild = jChild->mNext)
  752. {
  753. int childIdx = jChild->mValueInt;
  754. modelNode.mChildren.Add(mModelDef->mNodes.mVals + childIdx);
  755. }
  756. }
  757. if (auto jTranslation = jNode->GetObjectItem("translation"))
  758. _GetFloat3(jTranslation, modelNode.mTranslation);
  759. if (auto jTranslation = jNode->GetObjectItem("rotation"))
  760. _GetFloat4(jTranslation, modelNode.mRotation);
  761. if (auto jMesh = jNode->GetObjectItem("mesh"))
  762. modelNode.mMesh = mModelDef->mMeshes.mVals + jMesh->mValueInt;
  763. mModelDef->mNodes.Add(modelNode);
  764. }
  765. }
  766. return true;
  767. }