| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943 |
- //
- // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #pragma once
- #include <rapidjson/document.h>
- #include <Atomic/Container/List.h>
- #include <Atomic/Core/Object.h>
- using namespace Atomic;
- namespace ToolCore
- {
- class JSONSceneImporter;
- class JSONResource
- {
- public:
- void SetName(const String& name)
- {
- name_ = name;
- }
- const String& GetName() const
- {
- return name_;
- }
- protected:
- JSONResource(String name) : name_(name) {}
- private:
- String name_;
- };
- class JSONTexture : public JSONResource
- {
- public:
- JSONTexture(const String& name) : JSONResource(name)
- {
- }
- unsigned char* GetPNGPixels(unsigned& length) const
- {
- length = length_;
- return pngPixels_.Get();
- }
- void SetPNGPixels(SharedArrayPtr<unsigned char>& pngPixels, unsigned length)
- {
- length_ = length;
- pngPixels_ = pngPixels;
- }
- private:
- SharedArrayPtr<unsigned char> pngPixels_;
- unsigned length_;
- };
- class JSONLightmap : public JSONResource
- {
- public:
- JSONLightmap(const String& name) : JSONResource(name)
- {
- }
- unsigned char* GetPNGPixels(unsigned& length) const
- {
- length = length_;
- return pngPixels_.Get();
- }
- void SetPNGPixels(SharedArrayPtr<unsigned char>& pngPixels, unsigned length)
- {
- length_ = length;
- pngPixels_ = pngPixels;
- }
- private:
- SharedArrayPtr<unsigned char> pngPixels_;
- unsigned length_;
- };
- class JSONShader: public JSONResource
- {
- public:
- JSONShader(const String& name, int renderQueue) :
- JSONResource(name), renderQueue_(renderQueue)
- {
- }
- private:
- int renderQueue_;
- };
- class JSONMaterial : public JSONResource
- {
- public:
- JSONMaterial(const String& name) :
- JSONResource(name),
- mainTextureOffset_(0.0f, 0.0f),
- mainTextureScale_(1.0f, 1.0f),
- passCount_(1),
- color_(1, 1, 1, 1),
- renderQueue_(0)
- {
- }
- const String& GetShader() const
- {
- return shader_;
- }
- void SetShader(const String& shader)
- {
- shader_ = shader;
- }
- const String& GetMainTexture() const
- {
- return mainTexture_;
- }
- void SetMainTexture(const String& mainTexture)
- {
- mainTexture_ = mainTexture;
- }
- void SetMainTextureOffset(const Vector2& offset)
- {
- mainTextureOffset_ = offset;
- }
- void SetMainTextureScale(const Vector2& scale)
- {
- mainTextureScale_ = scale;
- }
- void SetColor(const Color& color)
- {
- color_ = color;
- }
- void SetPassCount(int count)
- {
- passCount_ = count;
- }
- void SetRenderQueue(int renderQueue)
- {
- renderQueue_ = renderQueue;
- }
- private:
- String shader_;
- String mainTexture_;
- Vector2 mainTextureOffset_;
- Vector2 mainTextureScale_;
- int passCount_;
- Color color_;
- int renderQueue_;
- List<String> shaderKeywords_;
- };
- class JSONMesh : public JSONResource
- {
- public:
- struct BoneWeight
- {
- int indexes_[4];
- float weights_[4];
- };
- class Bone
- {
- public:
- Vector3 pos_;
- Vector3 scale_;
- Quaternion rot_;
- String name_;
- String parentName_;
- };
- JSONMesh(const String& name) :
- JSONResource(name)
- {
- }
- PODVector<int>& AddSubMesh()
- {
- triangles_.Resize(triangles_.Size() + 1);
- return triangles_.Back();
- }
- unsigned GetSubMeshCount()
- {
- return triangles_.Size();
- }
- PODVector<int>& GetSubMesh(unsigned index)
- {
- return triangles_.At(index);
- }
- unsigned GetVertexCount() const
- {
- return vertexPositions_.Size();
- }
- PODVector<Vector3>& GetVertexPositions()
- {
- return vertexPositions_;
- }
- PODVector<Vector3>& GetVertexNormals()
- {
- return vertexNormals_;
- }
- PODVector<Vector4>& GetVertexTangents()
- {
- return vertexTangents_;
- }
- unsigned GetNumUVSets() const
- {
- return vertexUV_.Size();
- }
- PODVector<Vector2>& GetUVSet(int idx)
- {
- while (vertexUV_.Size() <= idx)
- {
- AddUVSet();
- }
- return vertexUV_.At(idx);
- }
- PODVector<BoneWeight>& GetBoneWeights()
- {
- return boneWeights_;
- }
- Vector<Matrix4>& GetBindPoses()
- {
- return bindPoses_;
- }
- Vector<Bone>& GetBones()
- {
- return bones_;
- }
- const String& GetRootBone() const
- {
- return rootBone_;
- }
- void SetRootBone(const String& rootBone)
- {
- rootBone_ = rootBone;
- }
- private:
- PODVector<Vector2>& AddUVSet()
- {
- vertexUV_.Resize(vertexUV_.Size() + 1);
- return vertexUV_.Back();
- }
- PODVector<Vector3> vertexPositions_;
- PODVector<Vector3> vertexNormals_;
- PODVector<Vector4> vertexTangents_;
- Vector<PODVector<Vector2> > vertexUV_;
- Vector<Matrix4> bindPoses_;
- Vector<Bone> bones_;
- PODVector<BoneWeight> boneWeights_;
- String rootBone_;
- //broken into submeshes
- Vector<PODVector<int> > triangles_;
- };
- class JSONComponent
- {
- public:
- const String& GetType() const
- {
- return type_;
- }
- protected:
- JSONComponent(JSONSceneImporter* importer, const String& type) : type_(type), enabled_(true)
- {
- }
- protected:
- bool Parse(const rapidjson::Value& value);
- String type_;
- JSONSceneImporter* importer_;
- bool enabled_;
- };
- class JSONTransform : public JSONComponent
- {
- public:
- JSONTransform(JSONSceneImporter* importer, const rapidjson::Value& value);
- const Vector3& GetLocalPosition() const
- {
- return localPosition_;
- }
- const Vector3& GetLocalScale() const
- {
- return localScale_;
- }
- const Quaternion& GetLocalRotation() const
- {
- return localRotation_;
- }
- private:
- Vector3 localPosition_;
- Vector3 localScale_;
- Quaternion localRotation_;
- };
- class JSONMeshRenderer: public JSONComponent
- {
- public:
- JSONMeshRenderer(JSONSceneImporter* importer, const rapidjson::Value& value, const char *type = "MeshRenderer");
- const JSONMesh* GetMesh() const
- {
- return mesh_;
- }
- bool GetCastShadows() const
- {
- return castShadows_;
- }
- bool GetReceiveShadows() const
- {
- return receiveShadows_;
- }
- unsigned GetNumMaterials() const
- {
- return materials_.Size();
- }
- const JSONMaterial* GetMaterial(unsigned index) const
- {
- return materials_.At(index);
- }
- int GetLightmapIndex() const
- {
- return lightmapIndex_;
- }
- const Vector4& GetLightmapTilingOffset() const
- {
- return lightmapTilingOffset_;
- }
- protected:
- JSONMesh* mesh_;
- bool castShadows_;
- bool receiveShadows_;
- int lightmapIndex_;
- Vector4 lightmapTilingOffset_;
- PODVector<JSONMaterial*> materials_;
- };
- class JSONSkinnedMeshRenderer: public JSONMeshRenderer
- {
- public:
- JSONSkinnedMeshRenderer(JSONSceneImporter* importer, const rapidjson::Value& value);
- };
- class JSONTimeOfDay : public JSONComponent
- {
- public:
- JSONTimeOfDay(JSONSceneImporter* importer, const rapidjson::Value& value);
- float GetTimeOn() const
- {
- return timeOn_;
- }
- void SetTimeOn(float value)
- {
- timeOn_ = value;
- }
- float GetTimeOff() const
- {
- return timeOff_;
- }
- void SetTimeOff(float value)
- {
- timeOff_ = value;
- }
- private:
- float timeOn_;
- float timeOff_;
- };
- class JSONLight : public JSONComponent
- {
- public:
- JSONLight(JSONSceneImporter* importer, const rapidjson::Value& value);
- float GetRange() const
- {
- return range_;
- }
- void SetRange(float range)
- {
- range_ = range;
- }
- void SetLightType(const String& lightType)
- {
- lightType_ = lightType;
- }
- const String& GetLightType() const
- {
- return lightType_;
- }
- void SetColor(const Color& color)
- {
- color_ = color;
- }
- const Color& GetColor() const
- {
- return color_;
- }
- void SetCastsShadows(bool castsShadows)
- {
- castsShadows_ = castsShadows;
- }
- bool GetCastsShadows() const
- {
- return castsShadows_;
- }
- void SetRealtime(bool realtime)
- {
- realtime_ = realtime;
- }
- bool GetRealtime() const
- {
- return realtime_;
- }
- private:
- String lightType_;
- float range_;
- Color color_;
- bool castsShadows_;
- bool realtime_;
- };
- class JSONRigidBody : public JSONComponent
- {
- public:
- JSONRigidBody(JSONSceneImporter* importer, const rapidjson::Value& value);
- float GetMass() const
- {
- return mass_;
- }
- void SetMass(float mass)
- {
- mass_ = mass;
- }
- private:
- float mass_;
- };
- class JSONMeshCollider : public JSONComponent
- {
- public:
- JSONMeshCollider(JSONSceneImporter* importer, const rapidjson::Value& value);
- };
- class JSONBoxCollider : public JSONComponent
- {
- public:
- JSONBoxCollider(JSONSceneImporter* importer, const rapidjson::Value& value);
- const Vector3& GetCenter() const
- {
- return center_;
- }
- const Vector3& GetSize() const
- {
- return size_;
- }
- void SetCenter(const Vector3& center)
- {
- center_ = center;
- }
- void SetSize(const Vector3& size)
- {
- size_ = size;
- }
- private:
- Vector3 center_;
- Vector3 size_;
- };
- class JSONAnimation : public JSONComponent
- {
- public:
- class Keyframe
- {
- public:
- Vector3 pos_;
- Vector3 scale_;
- Quaternion rot_;
- float time_;
- };
- class AnimationNode
- {
- public:
- String name_;
- Vector<Keyframe*> keyframes_;
- };
- class AnimationClip
- {
- public:
- String name_;
- Vector<AnimationNode*> nodes_;
- float GetDuration() const
- {
- float maxTime = -1.0f;
- for (unsigned i = 0 ; i < nodes_.Size(); i++)
- {
- if (nodes_[i]->keyframes_.Size())
- if (nodes_[i]->keyframes_.Back()->time_ > maxTime)
- maxTime = nodes_[i]->keyframes_.Back()->time_;
- }
- return maxTime;
- }
- };
- const Vector<AnimationClip*>& GetClips() const
- {
- return clips_;
- }
- JSONAnimation(JSONSceneImporter* importer, const rapidjson::Value& value);
- private:
- Vector<AnimationClip*> clips_;
- };
- class JSONTerrain: public JSONComponent
- {
- public:
- JSONTerrain(JSONSceneImporter* importer, const rapidjson::Value& value);
- int GetHeightMapWidth() const
- {
- return heightmapWidth_;
- }
- int GetHeightMapHeight() const
- {
- return heightmapHeight_;
- }
- int GetHeightMapResolution() const
- {
- return heightmapResolution_;
- }
- const Vector3& GetHeightMapScale() const
- {
- return heightmapScale_;
- }
- const Vector3& GetHeightMapSize() const
- {
- return size_;
- }
- int GetAlphaMapWidth() const
- {
- return alphamapWidth_;
- }
- int GetAlphaMapHeight() const
- {
- return alphamapHeight_;
- }
- int GetAlphaMapLayers() const
- {
- return alphamapLayers_;
- }
- const float* GetHeightMap(unsigned& length) const
- {
- length = heightMapLength_;
- return heightMap_.Get();
- }
- const float* GetAlphaMap(unsigned& length) const
- {
- length = alphaMapLength_;
- return alphaMap_.Get();
- }
- private:
- int heightmapHeight_;
- int heightmapWidth_;
- int heightmapResolution_;
- Vector3 heightmapScale_;
- Vector3 size_;
- int alphamapWidth_;
- int alphamapHeight_;
- int alphamapLayers_;
- SharedArrayPtr<float> heightMap_;
- unsigned heightMapLength_;
- SharedArrayPtr<float> alphaMap_;
- unsigned alphaMapLength_;
- };
- class JSONCamera: public JSONComponent
- {
- public:
- JSONCamera(JSONSceneImporter* importer, const rapidjson::Value& value);
- private:
- };
- class JSONNode
- {
- public:
- JSONNode(JSONSceneImporter* importer, const rapidjson::Value& value);
- const String& GetName() const
- {
- return name_;
- }
- const PODVector<JSONComponent*>& GetComponents() const
- {
- return components_;
- }
- const PODVector<JSONNode*>& GetChildren() const
- {
- return children_;
- }
- void AddChild(JSONNode* child)
- {
- children_.Push(child);
- }
- private:
- String name_;
- JSONSceneImporter* importer_;
- PODVector<JSONComponent*> components_;
- PODVector<JSONNode*> children_;
- };
- class Importer: public Object
- {
- ATOMIC_OBJECT(Importer, Object);
- public:
- Importer(Context* context) : Object(context) {}
- private:
- };
- class JSONSceneImporter: public Importer
- {
- ATOMIC_OBJECT(JSONSceneImporter, Importer);
- public:
- JSONSceneImporter(Context* context);
- bool Import(const String& path);
- void ReadVector2FromArray(const rapidjson::Value& value, Vector2& v);
- void ReadVector3FromArray(const rapidjson::Value& value, Vector3& v);
- void ReadVector4FromArray(const rapidjson::Value& value, Vector4& v);
- void ReadQuaternionFromArray(const rapidjson::Value& value, Quaternion& q);
- void ReadMatrix4FromArray(const rapidjson::Value& value, Matrix4& m);
- void ReadColorFromArray(const rapidjson::Value& value, Color& color);
- JSONMesh* GetMesh(const String& name)
- {
- for (unsigned i = 0; i < meshes_.Size(); i++)
- {
- if (meshes_[i]->GetName() == name)
- return meshes_[i];
- }
- return NULL;
- }
- JSONMaterial* GetMaterial(const String& name)
- {
- for (unsigned i = 0; i < materials_.Size(); i++)
- {
- if (materials_[i]->GetName() == name)
- return materials_[i];
- }
- return NULL;
- }
- const PODVector<JSONTexture*>& GetTexture()
- {
- return textures_;
- }
- const PODVector<JSONLightmap*>& GetLightmaps()
- {
- return lightmaps_;
- }
- const PODVector<JSONShader*>& GetShaders()
- {
- return shaders_;
- }
- const PODVector<JSONMaterial*>& GetMaterials()
- {
- return materials_;
- }
- const PODVector<JSONTexture*>& GetTextures()
- {
- return textures_;
- }
- const PODVector<JSONNode*>& GetHierarchy() const
- {
- return hierarchy_;
- }
- PODVector<JSONMesh*>& GetMeshes()
- {
- return meshes_;
- }
- const String& GetSceneName() const
- {
- return sceneName_;
- }
- virtual ~JSONSceneImporter();
- private:
- void AddTexture(JSONTexture* texture)
- {
- textures_.Push(texture);
- }
- void AddLightmap(JSONLightmap* lightmap)
- {
- lightmaps_.Push(lightmap);
- }
- void AddShader(JSONShader* shader)
- {
- shaders_.Push(shader);
- }
- void AddMaterial(JSONMaterial* material)
- {
- materials_.Push(material);
- }
- void AddMesh(JSONMesh* mesh)
- {
- meshes_.Push(mesh);
- }
- bool ParseShaders(const rapidjson::Value& value);
- bool ParseTextures(const rapidjson::Value& value);
- bool ParseLightmaps(const rapidjson::Value& value);
- bool ParseMaterials(const rapidjson::Value& value);
- bool ParseMeshes(const rapidjson::Value& value);
- bool ParseResources(const rapidjson::Value& value);
- bool ParseHierarchy(const rapidjson::Value& value);
- rapidjson::Document* document_;
- String sceneName_;
- PODVector<JSONTexture*> textures_;
- PODVector<JSONLightmap*> lightmaps_;
- PODVector<JSONShader*> shaders_;
- PODVector<JSONMaterial*> materials_;
- PODVector<JSONMesh*> meshes_;
- PODVector<JSONNode*> hierarchy_;
- };
- }
|