JSONSceneImporter.h 16 KB

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