CCSprite3DMaterial.h 5.0 KB

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