JSONSceneImporter.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. int GetLightmapIndex() const
  316. {
  317. return lightmapIndex_;
  318. }
  319. const Vector4& GetLightmapTilingOffset() const
  320. {
  321. return lightmapTilingOffset_;
  322. }
  323. protected:
  324. JSONMesh* mesh_;
  325. bool castShadows_;
  326. bool receiveShadows_;
  327. int lightmapIndex_;
  328. Vector4 lightmapTilingOffset_;
  329. PODVector<JSONMaterial*> materials_;
  330. };
  331. class JSONSkinnedMeshRenderer: public JSONMeshRenderer
  332. {
  333. public:
  334. JSONSkinnedMeshRenderer(JSONSceneImporter* importer, const rapidjson::Value& value);
  335. };
  336. class JSONTimeOfDay : public JSONComponent
  337. {
  338. public:
  339. JSONTimeOfDay(JSONSceneImporter* importer, const rapidjson::Value& value);
  340. float GetTimeOn() const
  341. {
  342. return timeOn_;
  343. }
  344. void SetTimeOn(float value)
  345. {
  346. timeOn_ = value;
  347. }
  348. float GetTimeOff() const
  349. {
  350. return timeOff_;
  351. }
  352. void SetTimeOff(float value)
  353. {
  354. timeOff_ = value;
  355. }
  356. private:
  357. float timeOn_;
  358. float timeOff_;
  359. };
  360. class JSONLight : public JSONComponent
  361. {
  362. public:
  363. JSONLight(JSONSceneImporter* importer, const rapidjson::Value& value);
  364. float GetRange() const
  365. {
  366. return range_;
  367. }
  368. void SetRange(float range)
  369. {
  370. range_ = range;
  371. }
  372. void SetLightType(const String& lightType)
  373. {
  374. lightType_ = lightType;
  375. }
  376. const String& GetLightType() const
  377. {
  378. return lightType_;
  379. }
  380. void SetColor(const Color& color)
  381. {
  382. color_ = color;
  383. }
  384. const Color& GetColor() const
  385. {
  386. return color_;
  387. }
  388. void SetCastsShadows(bool castsShadows)
  389. {
  390. castsShadows_ = castsShadows;
  391. }
  392. bool GetCastsShadows() const
  393. {
  394. return castsShadows_;
  395. }
  396. void SetRealtime(bool realtime)
  397. {
  398. realtime_ = realtime;
  399. }
  400. bool GetRealtime() const
  401. {
  402. return realtime_;
  403. }
  404. private:
  405. String lightType_;
  406. float range_;
  407. Color color_;
  408. bool castsShadows_;
  409. bool realtime_;
  410. };
  411. class JSONRigidBody : public JSONComponent
  412. {
  413. public:
  414. JSONRigidBody(JSONSceneImporter* importer, const rapidjson::Value& value);
  415. float GetMass() const
  416. {
  417. return mass_;
  418. }
  419. void SetMass(float mass)
  420. {
  421. mass_ = mass;
  422. }
  423. private:
  424. float mass_;
  425. };
  426. class JSONMeshCollider : public JSONComponent
  427. {
  428. public:
  429. JSONMeshCollider(JSONSceneImporter* importer, const rapidjson::Value& value);
  430. };
  431. class JSONBoxCollider : public JSONComponent
  432. {
  433. public:
  434. JSONBoxCollider(JSONSceneImporter* importer, const rapidjson::Value& value);
  435. const Vector3& GetCenter() const
  436. {
  437. return center_;
  438. }
  439. const Vector3& GetSize() const
  440. {
  441. return size_;
  442. }
  443. void SetCenter(const Vector3& center)
  444. {
  445. center_ = center;
  446. }
  447. void SetSize(const Vector3& size)
  448. {
  449. size_ = size;
  450. }
  451. private:
  452. Vector3 center_;
  453. Vector3 size_;
  454. };
  455. class JSONAnimation : public JSONComponent
  456. {
  457. public:
  458. class Keyframe
  459. {
  460. public:
  461. Vector3 pos_;
  462. Vector3 scale_;
  463. Quaternion rot_;
  464. float time_;
  465. };
  466. class AnimationNode
  467. {
  468. public:
  469. String name_;
  470. Vector<Keyframe*> keyframes_;
  471. };
  472. class AnimationClip
  473. {
  474. public:
  475. String name_;
  476. Vector<AnimationNode*> nodes_;
  477. float GetDuration() const
  478. {
  479. float maxTime = -1.0f;
  480. for (unsigned i = 0 ; i < nodes_.Size(); i++)
  481. {
  482. if (nodes_[i]->keyframes_.Size())
  483. if (nodes_[i]->keyframes_.Back()->time_ > maxTime)
  484. maxTime = nodes_[i]->keyframes_.Back()->time_;
  485. }
  486. return maxTime;
  487. }
  488. };
  489. const Vector<AnimationClip*>& GetClips() const
  490. {
  491. return clips_;
  492. }
  493. JSONAnimation(JSONSceneImporter* importer, const rapidjson::Value& value);
  494. private:
  495. Vector<AnimationClip*> clips_;
  496. };
  497. class JSONTerrain: public JSONComponent
  498. {
  499. public:
  500. JSONTerrain(JSONSceneImporter* importer, const rapidjson::Value& value);
  501. int GetHeightMapWidth() const
  502. {
  503. return heightmapWidth_;
  504. }
  505. int GetHeightMapHeight() const
  506. {
  507. return heightmapHeight_;
  508. }
  509. int GetHeightMapResolution() const
  510. {
  511. return heightmapResolution_;
  512. }
  513. const Vector3& GetHeightMapScale() const
  514. {
  515. return heightmapScale_;
  516. }
  517. const Vector3& GetHeightMapSize() const
  518. {
  519. return size_;
  520. }
  521. int GetAlphaMapWidth() const
  522. {
  523. return alphamapWidth_;
  524. }
  525. int GetAlphaMapHeight() const
  526. {
  527. return alphamapHeight_;
  528. }
  529. int GetAlphaMapLayers() const
  530. {
  531. return alphamapLayers_;
  532. }
  533. const float* GetHeightMap(unsigned& length) const
  534. {
  535. length = heightMapLength_;
  536. return heightMap_.Get();
  537. }
  538. const float* GetAlphaMap(unsigned& length) const
  539. {
  540. length = alphaMapLength_;
  541. return alphaMap_.Get();
  542. }
  543. private:
  544. int heightmapHeight_;
  545. int heightmapWidth_;
  546. int heightmapResolution_;
  547. Vector3 heightmapScale_;
  548. Vector3 size_;
  549. int alphamapWidth_;
  550. int alphamapHeight_;
  551. int alphamapLayers_;
  552. SharedArrayPtr<float> heightMap_;
  553. unsigned heightMapLength_;
  554. SharedArrayPtr<float> alphaMap_;
  555. unsigned alphaMapLength_;
  556. };
  557. class JSONCamera: public JSONComponent
  558. {
  559. public:
  560. JSONCamera(JSONSceneImporter* importer, const rapidjson::Value& value);
  561. private:
  562. };
  563. class JSONNode
  564. {
  565. public:
  566. JSONNode(JSONSceneImporter* importer, const rapidjson::Value& value);
  567. const String& GetName() const
  568. {
  569. return name_;
  570. }
  571. const PODVector<JSONComponent*>& GetComponents() const
  572. {
  573. return components_;
  574. }
  575. const PODVector<JSONNode*>& GetChildren() const
  576. {
  577. return children_;
  578. }
  579. void AddChild(JSONNode* child)
  580. {
  581. children_.Push(child);
  582. }
  583. private:
  584. String name_;
  585. JSONSceneImporter* importer_;
  586. PODVector<JSONComponent*> components_;
  587. PODVector<JSONNode*> children_;
  588. };
  589. class Importer: public Object
  590. {
  591. ATOMIC_OBJECT(Importer, Object);
  592. public:
  593. Importer(Context* context) : Object(context) {}
  594. private:
  595. };
  596. class JSONSceneImporter: public Importer
  597. {
  598. ATOMIC_OBJECT(JSONSceneImporter, Importer);
  599. public:
  600. JSONSceneImporter(Context* context);
  601. bool Import(const String& path);
  602. void ReadVector2FromArray(const rapidjson::Value& value, Vector2& v);
  603. void ReadVector3FromArray(const rapidjson::Value& value, Vector3& v);
  604. void ReadVector4FromArray(const rapidjson::Value& value, Vector4& v);
  605. void ReadQuaternionFromArray(const rapidjson::Value& value, Quaternion& q);
  606. void ReadMatrix4FromArray(const rapidjson::Value& value, Matrix4& m);
  607. void ReadColorFromArray(const rapidjson::Value& value, Color& color);
  608. JSONMesh* GetMesh(const String& name)
  609. {
  610. for (unsigned i = 0; i < meshes_.Size(); i++)
  611. {
  612. if (meshes_[i]->GetName() == name)
  613. return meshes_[i];
  614. }
  615. return NULL;
  616. }
  617. JSONMaterial* GetMaterial(const String& name)
  618. {
  619. for (unsigned i = 0; i < materials_.Size(); i++)
  620. {
  621. if (materials_[i]->GetName() == name)
  622. return materials_[i];
  623. }
  624. return NULL;
  625. }
  626. const PODVector<JSONTexture*>& GetTexture()
  627. {
  628. return textures_;
  629. }
  630. const PODVector<JSONLightmap*>& GetLightmaps()
  631. {
  632. return lightmaps_;
  633. }
  634. const PODVector<JSONShader*>& GetShaders()
  635. {
  636. return shaders_;
  637. }
  638. const PODVector<JSONMaterial*>& GetMaterials()
  639. {
  640. return materials_;
  641. }
  642. const PODVector<JSONTexture*>& GetTextures()
  643. {
  644. return textures_;
  645. }
  646. const PODVector<JSONNode*>& GetHierarchy() const
  647. {
  648. return hierarchy_;
  649. }
  650. PODVector<JSONMesh*>& GetMeshes()
  651. {
  652. return meshes_;
  653. }
  654. const String& GetSceneName() const
  655. {
  656. return sceneName_;
  657. }
  658. virtual ~JSONSceneImporter();
  659. private:
  660. void AddTexture(JSONTexture* texture)
  661. {
  662. textures_.Push(texture);
  663. }
  664. void AddLightmap(JSONLightmap* lightmap)
  665. {
  666. lightmaps_.Push(lightmap);
  667. }
  668. void AddShader(JSONShader* shader)
  669. {
  670. shaders_.Push(shader);
  671. }
  672. void AddMaterial(JSONMaterial* material)
  673. {
  674. materials_.Push(material);
  675. }
  676. void AddMesh(JSONMesh* mesh)
  677. {
  678. meshes_.Push(mesh);
  679. }
  680. bool ParseShaders(const rapidjson::Value& value);
  681. bool ParseTextures(const rapidjson::Value& value);
  682. bool ParseLightmaps(const rapidjson::Value& value);
  683. bool ParseMaterials(const rapidjson::Value& value);
  684. bool ParseMeshes(const rapidjson::Value& value);
  685. bool ParseResources(const rapidjson::Value& value);
  686. bool ParseHierarchy(const rapidjson::Value& value);
  687. rapidjson::Document* document_;
  688. String sceneName_;
  689. PODVector<JSONTexture*> textures_;
  690. PODVector<JSONLightmap*> lightmaps_;
  691. PODVector<JSONShader*> shaders_;
  692. PODVector<JSONMaterial*> materials_;
  693. PODVector<JSONMesh*> meshes_;
  694. PODVector<JSONNode*> hierarchy_;
  695. };
  696. }