JSONSceneImporter.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include <rapidjson/document.h>
  24. #include <Atomic/Container/List.h>
  25. #include <Atomic/Core/Object.h>
  26. using namespace Atomic;
  27. namespace ToolCore
  28. {
  29. class JSONSceneImporter;
  30. class JSONResource
  31. {
  32. public:
  33. void SetName(const String& name)
  34. {
  35. name_ = name;
  36. }
  37. const String& GetName() const
  38. {
  39. return name_;
  40. }
  41. protected:
  42. JSONResource(String name) : name_(name) {}
  43. private:
  44. String name_;
  45. };
  46. class JSONTexture : public JSONResource
  47. {
  48. public:
  49. JSONTexture(const String& name) : JSONResource(name)
  50. {
  51. }
  52. unsigned char* GetPNGPixels(unsigned& length) const
  53. {
  54. length = length_;
  55. return pngPixels_.Get();
  56. }
  57. void SetPNGPixels(SharedArrayPtr<unsigned char>& pngPixels, unsigned length)
  58. {
  59. length_ = length;
  60. pngPixels_ = pngPixels;
  61. }
  62. private:
  63. SharedArrayPtr<unsigned char> pngPixels_;
  64. unsigned length_;
  65. };
  66. class JSONLightmap : public JSONResource
  67. {
  68. public:
  69. JSONLightmap(const String& name) : JSONResource(name)
  70. {
  71. }
  72. unsigned char* GetPNGPixels(unsigned& length) const
  73. {
  74. length = length_;
  75. return pngPixels_.Get();
  76. }
  77. void SetPNGPixels(SharedArrayPtr<unsigned char>& pngPixels, unsigned length)
  78. {
  79. length_ = length;
  80. pngPixels_ = pngPixels;
  81. }
  82. private:
  83. SharedArrayPtr<unsigned char> pngPixels_;
  84. unsigned length_;
  85. };
  86. class JSONShader: public JSONResource
  87. {
  88. public:
  89. JSONShader(const String& name, int renderQueue) :
  90. JSONResource(name), renderQueue_(renderQueue)
  91. {
  92. }
  93. private:
  94. int renderQueue_;
  95. };
  96. class JSONMaterial : public JSONResource
  97. {
  98. public:
  99. JSONMaterial(const String& name) :
  100. JSONResource(name),
  101. mainTextureOffset_(0.0f, 0.0f),
  102. mainTextureScale_(1.0f, 1.0f),
  103. passCount_(1),
  104. color_(1, 1, 1, 1),
  105. renderQueue_(0)
  106. {
  107. }
  108. const String& GetShader() const
  109. {
  110. return shader_;
  111. }
  112. void SetShader(const String& shader)
  113. {
  114. shader_ = shader;
  115. }
  116. const String& GetMainTexture() const
  117. {
  118. return mainTexture_;
  119. }
  120. void SetMainTexture(const String& mainTexture)
  121. {
  122. mainTexture_ = mainTexture;
  123. }
  124. void SetMainTextureOffset(const Vector2& offset)
  125. {
  126. mainTextureOffset_ = offset;
  127. }
  128. void SetMainTextureScale(const Vector2& scale)
  129. {
  130. mainTextureScale_ = scale;
  131. }
  132. void SetColor(const Color& color)
  133. {
  134. color_ = color;
  135. }
  136. void SetPassCount(int count)
  137. {
  138. passCount_ = count;
  139. }
  140. void SetRenderQueue(int renderQueue)
  141. {
  142. renderQueue_ = renderQueue;
  143. }
  144. private:
  145. String shader_;
  146. String mainTexture_;
  147. Vector2 mainTextureOffset_;
  148. Vector2 mainTextureScale_;
  149. int passCount_;
  150. Color color_;
  151. int renderQueue_;
  152. List<String> shaderKeywords_;
  153. };
  154. class JSONMesh : public JSONResource
  155. {
  156. public:
  157. struct BoneWeight
  158. {
  159. int indexes_[4];
  160. float weights_[4];
  161. };
  162. class Bone
  163. {
  164. public:
  165. Vector3 pos_;
  166. Vector3 scale_;
  167. Quaternion rot_;
  168. String name_;
  169. String parentName_;
  170. };
  171. JSONMesh(const String& name) :
  172. JSONResource(name)
  173. {
  174. }
  175. PODVector<int>& AddSubMesh()
  176. {
  177. triangles_.Resize(triangles_.Size() + 1);
  178. return triangles_.Back();
  179. }
  180. unsigned GetSubMeshCount()
  181. {
  182. return triangles_.Size();
  183. }
  184. PODVector<int>& GetSubMesh(unsigned index)
  185. {
  186. return triangles_.At(index);
  187. }
  188. unsigned GetVertexCount() const
  189. {
  190. return vertexPositions_.Size();
  191. }
  192. PODVector<Vector3>& GetVertexPositions()
  193. {
  194. return vertexPositions_;
  195. }
  196. PODVector<Vector3>& GetVertexNormals()
  197. {
  198. return vertexNormals_;
  199. }
  200. PODVector<Vector4>& GetVertexTangents()
  201. {
  202. return vertexTangents_;
  203. }
  204. unsigned GetNumUVSets() const
  205. {
  206. return vertexUV_.Size();
  207. }
  208. PODVector<Vector2>& GetUVSet(int idx)
  209. {
  210. while (vertexUV_.Size() <= idx)
  211. {
  212. AddUVSet();
  213. }
  214. return vertexUV_.At(idx);
  215. }
  216. PODVector<BoneWeight>& GetBoneWeights()
  217. {
  218. return boneWeights_;
  219. }
  220. Vector<Matrix4>& GetBindPoses()
  221. {
  222. return bindPoses_;
  223. }
  224. Vector<Bone>& GetBones()
  225. {
  226. return bones_;
  227. }
  228. const String& GetRootBone() const
  229. {
  230. return rootBone_;
  231. }
  232. void SetRootBone(const String& rootBone)
  233. {
  234. rootBone_ = rootBone;
  235. }
  236. private:
  237. PODVector<Vector2>& AddUVSet()
  238. {
  239. vertexUV_.Resize(vertexUV_.Size() + 1);
  240. return vertexUV_.Back();
  241. }
  242. PODVector<Vector3> vertexPositions_;
  243. PODVector<Vector3> vertexNormals_;
  244. PODVector<Vector4> vertexTangents_;
  245. Vector<PODVector<Vector2> > vertexUV_;
  246. Vector<Matrix4> bindPoses_;
  247. Vector<Bone> bones_;
  248. PODVector<BoneWeight> boneWeights_;
  249. String rootBone_;
  250. //broken into submeshes
  251. Vector<PODVector<int> > triangles_;
  252. };
  253. class JSONComponent
  254. {
  255. public:
  256. const String& GetType() const
  257. {
  258. return type_;
  259. }
  260. protected:
  261. JSONComponent(JSONSceneImporter* importer, const String& type) : type_(type), enabled_(true)
  262. {
  263. }
  264. protected:
  265. bool Parse(const rapidjson::Value& value);
  266. String type_;
  267. JSONSceneImporter* importer_;
  268. bool enabled_;
  269. };
  270. class JSONTransform : public JSONComponent
  271. {
  272. public:
  273. JSONTransform(JSONSceneImporter* importer, const rapidjson::Value& value);
  274. const Vector3& GetLocalPosition() const
  275. {
  276. return localPosition_;
  277. }
  278. const Vector3& GetLocalScale() const
  279. {
  280. return localScale_;
  281. }
  282. const Quaternion& GetLocalRotation() const
  283. {
  284. return localRotation_;
  285. }
  286. private:
  287. Vector3 localPosition_;
  288. Vector3 localScale_;
  289. Quaternion localRotation_;
  290. };
  291. class JSONMeshRenderer: public JSONComponent
  292. {
  293. public:
  294. JSONMeshRenderer(JSONSceneImporter* importer, const rapidjson::Value& value, const char *type = "MeshRenderer");
  295. const JSONMesh* GetMesh() const
  296. {
  297. return mesh_;
  298. }
  299. bool GetCastShadows() const
  300. {
  301. return castShadows_;
  302. }
  303. bool GetReceiveShadows() const
  304. {
  305. return receiveShadows_;
  306. }
  307. unsigned GetNumMaterials() const
  308. {
  309. return materials_.Size();
  310. }
  311. const JSONMaterial* GetMaterial(unsigned index) const
  312. {
  313. return materials_.At(index);
  314. }
  315. bool GetLightmap() const
  316. {
  317. return lightmap_;
  318. }
  319. int GetLightmapIndex() const
  320. {
  321. return lightmapIndex_;
  322. }
  323. const Vector4& GetLightmapTilingOffset() const
  324. {
  325. return lightmapTilingOffset_;
  326. }
  327. protected:
  328. JSONMesh* mesh_;
  329. bool castShadows_;
  330. bool receiveShadows_;
  331. bool lightmap_;
  332. int lightmapIndex_;
  333. Vector4 lightmapTilingOffset_;
  334. PODVector<JSONMaterial*> materials_;
  335. };
  336. class JSONSkinnedMeshRenderer: public JSONMeshRenderer
  337. {
  338. public:
  339. JSONSkinnedMeshRenderer(JSONSceneImporter* importer, const rapidjson::Value& value);
  340. };
  341. class JSONTimeOfDay : public JSONComponent
  342. {
  343. public:
  344. JSONTimeOfDay(JSONSceneImporter* importer, const rapidjson::Value& value);
  345. float GetTimeOn() const
  346. {
  347. return timeOn_;
  348. }
  349. void SetTimeOn(float value)
  350. {
  351. timeOn_ = value;
  352. }
  353. float GetTimeOff() const
  354. {
  355. return timeOff_;
  356. }
  357. void SetTimeOff(float value)
  358. {
  359. timeOff_ = value;
  360. }
  361. private:
  362. float timeOn_;
  363. float timeOff_;
  364. };
  365. class JSONLight : public JSONComponent
  366. {
  367. public:
  368. JSONLight(JSONSceneImporter* importer, const rapidjson::Value& value);
  369. float GetRange() const
  370. {
  371. return range_;
  372. }
  373. void SetRange(float range)
  374. {
  375. range_ = range;
  376. }
  377. void SetLightType(const String& lightType)
  378. {
  379. lightType_ = lightType;
  380. }
  381. const String& GetLightType() const
  382. {
  383. return lightType_;
  384. }
  385. void SetColor(const Color& color)
  386. {
  387. color_ = color;
  388. }
  389. const Color& GetColor() const
  390. {
  391. return color_;
  392. }
  393. void SetCastsShadows(bool castsShadows)
  394. {
  395. castsShadows_ = castsShadows;
  396. }
  397. bool GetCastsShadows() const
  398. {
  399. return castsShadows_;
  400. }
  401. void SetRealtime(bool realtime)
  402. {
  403. realtime_ = realtime;
  404. }
  405. bool GetRealtime() const
  406. {
  407. return realtime_;
  408. }
  409. private:
  410. String lightType_;
  411. float range_;
  412. Color color_;
  413. bool castsShadows_;
  414. bool realtime_;
  415. };
  416. class JSONRigidBody : public JSONComponent
  417. {
  418. public:
  419. JSONRigidBody(JSONSceneImporter* importer, const rapidjson::Value& value);
  420. float GetMass() const
  421. {
  422. return mass_;
  423. }
  424. void SetMass(float mass)
  425. {
  426. mass_ = mass;
  427. }
  428. private:
  429. float mass_;
  430. };
  431. class JSONMeshCollider : public JSONComponent
  432. {
  433. public:
  434. JSONMeshCollider(JSONSceneImporter* importer, const rapidjson::Value& value);
  435. };
  436. class JSONBoxCollider : public JSONComponent
  437. {
  438. public:
  439. JSONBoxCollider(JSONSceneImporter* importer, const rapidjson::Value& value);
  440. const Vector3& GetCenter() const
  441. {
  442. return center_;
  443. }
  444. const Vector3& GetSize() const
  445. {
  446. return size_;
  447. }
  448. void SetCenter(const Vector3& center)
  449. {
  450. center_ = center;
  451. }
  452. void SetSize(const Vector3& size)
  453. {
  454. size_ = size;
  455. }
  456. private:
  457. Vector3 center_;
  458. Vector3 size_;
  459. };
  460. class JSONAnimation : public JSONComponent
  461. {
  462. public:
  463. class Keyframe
  464. {
  465. public:
  466. Vector3 pos_;
  467. Vector3 scale_;
  468. Quaternion rot_;
  469. float time_;
  470. };
  471. class AnimationNode
  472. {
  473. public:
  474. String name_;
  475. Vector<Keyframe*> keyframes_;
  476. };
  477. class AnimationClip
  478. {
  479. public:
  480. String name_;
  481. Vector<AnimationNode*> nodes_;
  482. float GetDuration() const
  483. {
  484. float maxTime = -1.0f;
  485. for (unsigned i = 0 ; i < nodes_.Size(); i++)
  486. {
  487. if (nodes_[i]->keyframes_.Size())
  488. if (nodes_[i]->keyframes_.Back()->time_ > maxTime)
  489. maxTime = nodes_[i]->keyframes_.Back()->time_;
  490. }
  491. return maxTime;
  492. }
  493. };
  494. const Vector<AnimationClip*>& GetClips() const
  495. {
  496. return clips_;
  497. }
  498. JSONAnimation(JSONSceneImporter* importer, const rapidjson::Value& value);
  499. private:
  500. Vector<AnimationClip*> clips_;
  501. };
  502. class JSONTerrain: public JSONComponent
  503. {
  504. public:
  505. JSONTerrain(JSONSceneImporter* importer, const rapidjson::Value& value);
  506. int GetHeightMapWidth() const
  507. {
  508. return heightmapWidth_;
  509. }
  510. int GetHeightMapHeight() const
  511. {
  512. return heightmapHeight_;
  513. }
  514. int GetHeightMapResolution() const
  515. {
  516. return heightmapResolution_;
  517. }
  518. const Vector3& GetHeightMapScale() const
  519. {
  520. return heightmapScale_;
  521. }
  522. const Vector3& GetHeightMapSize() const
  523. {
  524. return size_;
  525. }
  526. int GetAlphaMapWidth() const
  527. {
  528. return alphamapWidth_;
  529. }
  530. int GetAlphaMapHeight() const
  531. {
  532. return alphamapHeight_;
  533. }
  534. int GetAlphaMapLayers() const
  535. {
  536. return alphamapLayers_;
  537. }
  538. const float* GetHeightMap(unsigned& length) const
  539. {
  540. length = heightMapLength_;
  541. return heightMap_.Get();
  542. }
  543. const float* GetAlphaMap(unsigned& length) const
  544. {
  545. length = alphaMapLength_;
  546. return alphaMap_.Get();
  547. }
  548. private:
  549. int heightmapHeight_;
  550. int heightmapWidth_;
  551. int heightmapResolution_;
  552. Vector3 heightmapScale_;
  553. Vector3 size_;
  554. int alphamapWidth_;
  555. int alphamapHeight_;
  556. int alphamapLayers_;
  557. SharedArrayPtr<float> heightMap_;
  558. unsigned heightMapLength_;
  559. SharedArrayPtr<float> alphaMap_;
  560. unsigned alphaMapLength_;
  561. };
  562. class JSONCamera: public JSONComponent
  563. {
  564. public:
  565. JSONCamera(JSONSceneImporter* importer, const rapidjson::Value& value);
  566. private:
  567. };
  568. class JSONNode
  569. {
  570. public:
  571. JSONNode(JSONSceneImporter* importer, const rapidjson::Value& value);
  572. const String& GetName() const
  573. {
  574. return name_;
  575. }
  576. const PODVector<JSONComponent*>& GetComponents() const
  577. {
  578. return components_;
  579. }
  580. const PODVector<JSONNode*>& GetChildren() const
  581. {
  582. return children_;
  583. }
  584. void AddChild(JSONNode* child)
  585. {
  586. children_.Push(child);
  587. }
  588. private:
  589. String name_;
  590. JSONSceneImporter* importer_;
  591. PODVector<JSONComponent*> components_;
  592. PODVector<JSONNode*> children_;
  593. };
  594. class Importer: public Object
  595. {
  596. ATOMIC_OBJECT(Importer, Object);
  597. public:
  598. Importer(Context* context) : Object(context) {}
  599. private:
  600. };
  601. class JSONSceneImporter: public Importer
  602. {
  603. ATOMIC_OBJECT(JSONSceneImporter, Importer);
  604. public:
  605. JSONSceneImporter(Context* context);
  606. bool Import(const String& path);
  607. void ReadVector2FromArray(const rapidjson::Value& value, Vector2& v);
  608. void ReadVector3FromArray(const rapidjson::Value& value, Vector3& v);
  609. void ReadVector4FromArray(const rapidjson::Value& value, Vector4& v);
  610. void ReadQuaternionFromArray(const rapidjson::Value& value, Quaternion& q);
  611. void ReadMatrix4FromArray(const rapidjson::Value& value, Matrix4& m);
  612. void ReadColorFromArray(const rapidjson::Value& value, Color& color);
  613. JSONMesh* GetMesh(const String& name)
  614. {
  615. for (unsigned i = 0; i < meshes_.Size(); i++)
  616. {
  617. if (meshes_[i]->GetName() == name)
  618. return meshes_[i];
  619. }
  620. return NULL;
  621. }
  622. JSONMaterial* GetMaterial(const String& name)
  623. {
  624. for (unsigned i = 0; i < materials_.Size(); i++)
  625. {
  626. if (materials_[i]->GetName() == name)
  627. return materials_[i];
  628. }
  629. return NULL;
  630. }
  631. const PODVector<JSONTexture*>& GetTexture()
  632. {
  633. return textures_;
  634. }
  635. const PODVector<JSONLightmap*>& GetLightmaps()
  636. {
  637. return lightmaps_;
  638. }
  639. const PODVector<JSONShader*>& GetShaders()
  640. {
  641. return shaders_;
  642. }
  643. const PODVector<JSONMaterial*>& GetMaterials()
  644. {
  645. return materials_;
  646. }
  647. const PODVector<JSONTexture*>& GetTextures()
  648. {
  649. return textures_;
  650. }
  651. const PODVector<JSONNode*>& GetHierarchy() const
  652. {
  653. return hierarchy_;
  654. }
  655. PODVector<JSONMesh*>& GetMeshes()
  656. {
  657. return meshes_;
  658. }
  659. const String& GetSceneName() const
  660. {
  661. return sceneName_;
  662. }
  663. virtual ~JSONSceneImporter();
  664. private:
  665. void AddTexture(JSONTexture* texture)
  666. {
  667. textures_.Push(texture);
  668. }
  669. void AddLightmap(JSONLightmap* lightmap)
  670. {
  671. lightmaps_.Push(lightmap);
  672. }
  673. void AddShader(JSONShader* shader)
  674. {
  675. shaders_.Push(shader);
  676. }
  677. void AddMaterial(JSONMaterial* material)
  678. {
  679. materials_.Push(material);
  680. }
  681. void AddMesh(JSONMesh* mesh)
  682. {
  683. meshes_.Push(mesh);
  684. }
  685. bool ParseShaders(const rapidjson::Value& value);
  686. bool ParseTextures(const rapidjson::Value& value);
  687. bool ParseLightmaps(const rapidjson::Value& value);
  688. bool ParseMaterials(const rapidjson::Value& value);
  689. bool ParseMeshes(const rapidjson::Value& value);
  690. bool ParseResources(const rapidjson::Value& value);
  691. bool ParseHierarchy(const rapidjson::Value& value);
  692. rapidjson::Document* document_;
  693. String sceneName_;
  694. PODVector<JSONTexture*> textures_;
  695. PODVector<JSONLightmap*> lightmaps_;
  696. PODVector<JSONShader*> shaders_;
  697. PODVector<JSONMaterial*> materials_;
  698. PODVector<JSONMesh*> meshes_;
  699. PODVector<JSONNode*> hierarchy_;
  700. };
  701. }