123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- #ifndef __CCSPRITE3D_H__
- #define __CCSPRITE3D_H__
- #include <unordered_map>
- #include "base/CCVector.h"
- #include "base/ccTypes.h"
- #include "base/CCProtocols.h"
- #include "2d/CCNode.h"
- #include "renderer/CCMeshCommand.h"
- #include "renderer/CCGLProgramState.h"
- #include "3d/CCSkeleton3D.h"
- #include "3d/CCAABB.h"
- #include "3d/CCBundle3DData.h"
- #include "3d/CCMeshVertexIndexData.h"
- NS_CC_BEGIN
- class Mesh;
- class Texture2D;
- class MeshSkin;
- class AttachNode;
- struct NodeData;
- class CC_DLL Sprite3D : public Node, public BlendProtocol
- {
- public:
-
- static Sprite3D* create();
-
-
- static Sprite3D* create(const std::string &modelPath);
-
-
- static Sprite3D* create(const std::string &modelPath, const std::string &texturePath);
-
-
- static void createAsync(const std::string &modelPath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam);
-
- static void createAsync(const std::string &modelPath, const std::string &texturePath, const std::function<void(Sprite3D*, void*)>& callback, void* callbackparam);
-
-
- void setTexture(const std::string& texFile);
- void setTexture(Texture2D* texture);
-
-
- Mesh* getMeshByIndex(int index) const;
-
-
- Mesh* getMeshByName(const std::string& name) const;
-
-
- std::vector<Mesh*> getMeshArrayByName(const std::string& name) const;
-
- Mesh* getMesh() const;
-
-
- ssize_t getMeshCount() const { return _meshes.size(); }
-
-
- CC_DEPRECATED_ATTRIBUTE MeshSkin* getSkin() const;
-
- Skeleton3D* getSkeleton() const { return _skeleton; }
-
-
- AttachNode* getAttachNode(const std::string& boneName);
-
-
- void removeAttachNode(const std::string& boneName);
-
-
- void removeAllAttachNode();
-
- virtual void setBlendFunc(const BlendFunc &blendFunc) override;
- virtual const BlendFunc &getBlendFunc() const override;
-
-
-
- virtual void setGLProgramState(GLProgramState *glProgramState) override;
-
- virtual void setGLProgram(GLProgram *glprogram) override;
-
-
- const AABB& getAABB() const;
-
-
- AABB getAABBRecursively();
-
-
- virtual Action* runAction(Action* action) override;
-
-
- void setForceDepthWrite(bool value) { _forceDepthWrite = value; }
- bool isForceDepthWrite() const { return _forceDepthWrite;};
-
-
- virtual Rect getBoundingBox() const override;
-
- void setCullFace(GLenum cullFace);
-
- void setCullFaceEnabled(bool enable);
-
-
- void setLightMask(unsigned int mask) { _lightMask = mask; }
- unsigned int getLightMask() const { return _lightMask; }
-
-
- virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
-
- void setMaterial(Material* material);
-
- void setMaterial(Material* material, int meshIndex);
-
- Material* getMaterial(int meshIndex) const;
-
-
- void setForce2DQueue(bool force2D);
-
- const Vector<Mesh*>& getMeshes() const { return _meshes; }
- CC_CONSTRUCTOR_ACCESS:
-
- Sprite3D();
- virtual ~Sprite3D();
-
- virtual bool init() override;
-
- bool initWithFile(const std::string &path);
-
- bool initFrom(const NodeDatas& nodedatas, const MeshDatas& meshdatas, const MaterialDatas& materialdatas);
-
-
- bool loadFromCache(const std::string& path);
-
-
- bool loadFromFile(const std::string& path, NodeDatas* nodedatas, MeshDatas* meshdatas, MaterialDatas* materialdatas);
-
- virtual void visit(Renderer *renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
-
-
- void genMaterial(bool useLight = false);
- void createNode(NodeData* nodedata, Node* root, const MaterialDatas& materialdatas, bool singleSprite);
- void createAttachSprite3DNode(NodeData* nodedata, const MaterialDatas& materialdatas);
- Sprite3D* createSprite3DNode(NodeData* nodedata, ModelData* modeldata, const MaterialDatas& materialdatas);
-
- MeshIndexData* getMeshIndexData(const std::string& indexId) const;
-
- void addMesh(Mesh* mesh);
-
- void onAABBDirty() { _aabbDirty = true; }
-
- void afterAsyncLoad(void* param);
- static AABB getAABBRecursivelyImp(Node *node);
-
- protected:
- Skeleton3D* _skeleton;
-
- Vector<MeshVertexData*> _meshVertexDatas;
-
- std::unordered_map<std::string, AttachNode*> _attachments;
- BlendFunc _blend;
-
- Vector<Mesh*> _meshes;
- mutable AABB _aabb;
- mutable Mat4 _nodeToWorldTransform;
- mutable bool _aabbDirty;
- unsigned int _lightMask;
- bool _shaderUsingLight;
- bool _forceDepthWrite;
- bool _usingAutogeneratedGLProgram;
-
- struct AsyncLoadParam
- {
- std::function<void(Sprite3D*, void*)> afterLoadCallback;
- void* callbackParam;
- bool result;
- std::string modelPath;
- std::string texPath;
- MeshDatas* meshdatas;
- MaterialDatas* materialdatas;
- NodeDatas* nodeDatas;
- };
- AsyncLoadParam _asyncLoadParam;
- };
- class CC_DLL Sprite3DCache
- {
- public:
- struct Sprite3DData
- {
- Vector<MeshVertexData*> meshVertexDatas;
- Vector<GLProgramState*> glProgramStates;
- NodeDatas* nodedatas;
- MaterialDatas* materialdatas;
- ~Sprite3DData()
- {
- if (nodedatas)
- delete nodedatas;
- if (materialdatas)
- delete materialdatas;
- meshVertexDatas.clear();
- glProgramStates.clear();
- }
- };
-
-
- static Sprite3DCache* getInstance();
- static void destroyInstance();
-
- Sprite3DData* getSpriteData(const std::string& key) const;
-
-
- bool addSprite3DData(const std::string& key, Sprite3DData* spritedata);
-
-
- void removeSprite3DData(const std::string& key);
-
-
- void removeAllSprite3DData();
-
- CC_CONSTRUCTOR_ACCESS:
- Sprite3DCache();
- ~Sprite3DCache();
-
- protected:
-
-
- static Sprite3DCache* _cacheInstance;
- std::unordered_map<std::string, Sprite3DData*> _spriteDatas;
- };
- NS_CC_END
- #endif
|