JSONSceneImporter.h 16 KB

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