CCSprite3DMaterial.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CCSPRIT3DMATERIAL_H__
  22. #define __CCSPRIT3DMATERIAL_H__
  23. #include <string>
  24. #include <unordered_map>
  25. #include "base/ccTypes.h"
  26. #include "renderer/CCMaterial.h"
  27. #include "3d/CCBundle3DData.h"
  28. NS_CC_BEGIN
  29. /**
  30. * @addtogroup _3d
  31. * @{
  32. */
  33. class Texture2D;
  34. /**
  35. * @brief Sprite3DMaterial: Material for Sprite3D.
  36. */
  37. class CC_DLL Sprite3DMaterial : public Material
  38. {
  39. public:
  40. /**
  41. * Material type, there are mainly two types of materials. Built in materials and Custom material
  42. */
  43. enum class MaterialType
  44. {
  45. //Built in material
  46. UNLIT, //unlit material
  47. UNLIT_NOTEX, //unlit material without texture
  48. VERTEX_LIT, // vertex lit
  49. DIFFUSE, // diffuse (pixel lighting)
  50. DIFFUSE_NOTEX, //diffuse (without texture)
  51. BUMPED_DIFFUSE, //bumped diffuse
  52. //Custom material
  53. CUSTOM, //Create from material file
  54. };
  55. /**
  56. * Get material type
  57. * @return Material type
  58. */
  59. MaterialType getMaterialType() const { return _type; }
  60. /**
  61. * Create built in material from material type
  62. * @param type Material type
  63. * @param skinned Has skin?
  64. * @return Created material
  65. */
  66. static Sprite3DMaterial* createBuiltInMaterial(MaterialType type, bool skinned);
  67. /**
  68. * Create material with file name, it creates material from cache if it is previously loaded
  69. * @param path Path of material file
  70. * @return Created material
  71. */
  72. static Sprite3DMaterial* createWithFilename(const std::string& path);
  73. /**
  74. * Create material with GLProgramState
  75. * @param programState GLProgramState instance
  76. * @return Created material
  77. */
  78. static Sprite3DMaterial* createWithGLStateProgram(GLProgramState* programState);
  79. void setTexture(Texture2D* tex, NTextureData::Usage usage);
  80. /**
  81. * Create all build in materials
  82. */
  83. static void createBuiltInMaterial();
  84. /**
  85. * Release all built in materials
  86. */
  87. static void releaseBuiltInMaterial();
  88. /**
  89. * Release all cached materials
  90. */
  91. static void releaseCachedMaterial();
  92. /**
  93. * Clone material
  94. */
  95. virtual Material* clone() const override;
  96. protected:
  97. MaterialType _type;
  98. static std::unordered_map<std::string, Sprite3DMaterial*> _materials; //cached material
  99. static Sprite3DMaterial* _unLitMaterial;
  100. static Sprite3DMaterial* _unLitNoTexMaterial;
  101. static Sprite3DMaterial* _vertexLitMaterial;
  102. static Sprite3DMaterial* _diffuseMaterial;
  103. static Sprite3DMaterial* _diffuseNoTexMaterial;
  104. static Sprite3DMaterial* _bumpedDiffuseMaterial;
  105. static Sprite3DMaterial* _unLitMaterialSkin;
  106. static Sprite3DMaterial* _vertexLitMaterialSkin;
  107. static Sprite3DMaterial* _diffuseMaterialSkin;
  108. static Sprite3DMaterial* _bumpedDiffuseMaterialSkin;
  109. };
  110. /**
  111. * @brief the sprite3D material is only texture for now
  112. * @js NA
  113. * @lua NA
  114. */
  115. class Sprite3DMaterialCache
  116. {
  117. public:
  118. /**get & destroy cache*/
  119. static Sprite3DMaterialCache* getInstance();
  120. /**destroy the instance*/
  121. static void destroyInstance();
  122. /**add to cache*/
  123. bool addSprite3DMaterial(const std::string& key, Texture2D* tex);
  124. /**get material from cache*/
  125. Texture2D* getSprite3DMaterial(const std::string& key);
  126. /**remove all spritematerial*/
  127. void removeAllSprite3DMaterial();
  128. /**remove unused spritematerial*/
  129. void removeUnusedSprite3DMaterial();
  130. CC_CONSTRUCTOR_ACCESS:
  131. Sprite3DMaterialCache();
  132. ~Sprite3DMaterialCache();
  133. protected:
  134. static Sprite3DMaterialCache* _cacheInstance;//instance
  135. std::unordered_map<std::string, Texture2D*> _materials; //cached material
  136. };
  137. // end of 3d group
  138. /// @}
  139. NS_CC_END
  140. #endif // __CCSPRIT3DMATERIAL_H__