JSONSceneImporter.h 17 KB

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