CCLight.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 __CCLIGHT_H__
  22. #define __CCLIGHT_H__
  23. #include "2d/CCNode.h"
  24. NS_CC_BEGIN
  25. enum class LightType
  26. {
  27. DIRECTIONAL = 0,
  28. POINT = 1,
  29. SPOT = 2,
  30. AMBIENT = 3,
  31. };
  32. enum class LightFlag
  33. {
  34. LIGHT0 = 1,
  35. LIGHT1 = 1 << 1,
  36. LIGHT2 = 1 << 2,
  37. LIGHT3 = 1 << 3,
  38. LIGHT4 = 1 << 4,
  39. LIGHT5 = 1 << 5,
  40. LIGHT6 = 1 << 6,
  41. LIGHT7 = 1 << 7,
  42. LIGHT8 = 1 << 8,
  43. LIGHT9 = 1 << 9,
  44. LIGHT10 = 1 << 10,
  45. LIGHT11 = 1 << 11,
  46. LIGHT12 = 1 << 12,
  47. LIGHT13 = 1 << 13,
  48. LIGHT14 = 1 << 14,
  49. LIGHT15 = 1 << 15,
  50. };
  51. /**
  52. @js NA
  53. */
  54. class CC_DLL BaseLight : public Node
  55. {
  56. public:
  57. /**
  58. * Get the light type,light type MUST be one of LightType::DIRECTIONAL ,
  59. * LightType::POINT, LightType::SPOT, LightType::AMBIENT.
  60. */
  61. virtual LightType getLightType() const = 0;
  62. /** intensity getter and setter */
  63. float getIntensity() const { return _intensity; }
  64. void setIntensity(float intensity);
  65. /**light flag getter and setter*/
  66. LightFlag getLightFlag() const { return _lightFlag; }
  67. void setLightFlag(LightFlag flag) { _lightFlag = flag; }
  68. /**
  69. * light enabled getter and setter.
  70. */
  71. void setEnabled(bool enabled) { _enabled = enabled; }
  72. bool isEnabled() const { return _enabled; }
  73. //override
  74. virtual void onEnter() override;
  75. virtual void onExit() override;
  76. CC_CONSTRUCTOR_ACCESS:
  77. BaseLight();
  78. virtual ~BaseLight();
  79. protected:
  80. void setRotationFromDirection( const Vec3 &direction );
  81. protected:
  82. float _intensity;
  83. LightFlag _lightFlag;
  84. bool _enabled;
  85. };
  86. /**
  87. @js NA
  88. */
  89. class CC_DLL DirectionLight : public BaseLight
  90. {
  91. public:
  92. /**
  93. * Creates a direction light.
  94. * @param direction The light's direction
  95. * @param color The light's color.
  96. *
  97. * @return The new direction light.
  98. */
  99. static DirectionLight* create(const Vec3 &direction, const Color3B &color);
  100. //get light type
  101. virtual LightType getLightType() const override { return LightType::DIRECTIONAL; }
  102. /**
  103. * Sets the Direction in parent.
  104. *
  105. * @param dir The Direction in parent.
  106. */
  107. void setDirection(const Vec3 &dir);
  108. /**
  109. * Returns the Direction in parent.
  110. */
  111. Vec3 getDirection() const;
  112. /**
  113. * Returns direction in world.
  114. */
  115. Vec3 getDirectionInWorld() const;
  116. CC_CONSTRUCTOR_ACCESS:
  117. DirectionLight();
  118. virtual ~DirectionLight();
  119. };
  120. /**
  121. @js NA
  122. */
  123. class CC_DLL PointLight : public BaseLight
  124. {
  125. public:
  126. /**
  127. * Creates a point light.
  128. * @param position The light's position
  129. * @param color The light's color.
  130. * @param range The light's range.
  131. *
  132. * @return The new point light.
  133. */
  134. static PointLight* create(const Vec3 &position, const Color3B &color, float range);
  135. //get light type
  136. virtual LightType getLightType() const override { return LightType::POINT; }
  137. /** get or set range */
  138. float getRange() const { return _range; }
  139. void setRange(float range) { _range = range; }
  140. CC_CONSTRUCTOR_ACCESS:
  141. PointLight();
  142. virtual ~PointLight();
  143. protected:
  144. float _range;
  145. };
  146. /**
  147. @js NA
  148. */
  149. class CC_DLL SpotLight : public BaseLight
  150. {
  151. public:
  152. /**
  153. * Creates a spot light.
  154. * @param direction The light's direction
  155. * @param position The light's position
  156. * @param color The light's color.
  157. * @param innerAngle The light's inner angle (in radians).
  158. * @param outerAngle The light's outer angle (in radians).
  159. * @param range The light's range.
  160. *
  161. * @return The new spot light.
  162. */
  163. static SpotLight* create(const Vec3 &direction, const Vec3 &position, const Color3B &color, float innerAngle, float outerAngle, float range);
  164. //get light type
  165. virtual LightType getLightType() const override { return LightType::SPOT; }
  166. /**
  167. * Sets the Direction in parent.
  168. *
  169. * @param dir The Direction in parent.
  170. */
  171. void setDirection(const Vec3 &dir);
  172. /**
  173. * Returns the Direction in parent.
  174. */
  175. Vec3 getDirection() const;
  176. /**
  177. * Returns direction in world.
  178. */
  179. Vec3 getDirectionInWorld() const;
  180. /**
  181. * Sets the range of point or spot light.
  182. *
  183. * @param range The range of point or spot light.
  184. */
  185. void setRange(float range) { _range = range; }
  186. /**
  187. * Returns the range of point or spot light.
  188. *
  189. * @return The range of the point or spot light.
  190. */
  191. float getRange() const { return _range; }
  192. /**
  193. * Sets the inner angle of a spot light (in radians).
  194. *
  195. * @param angle The angle of spot light (in radians).
  196. */
  197. void setInnerAngle(float angle);
  198. /**
  199. * Returns the inner angle the spot light (in radians).
  200. */
  201. float getInnerAngle() const { return _innerAngle; }
  202. /** get cos innerAngle */
  203. float getCosInnerAngle() const { return _cosInnerAngle; }
  204. /**
  205. * Sets the outer angle of a spot light (in radians).
  206. *
  207. * @param outerAngle The angle of spot light (in radians).
  208. */
  209. void setOuterAngle(float outerAngle);
  210. /**
  211. * Returns the outer angle of the spot light (in radians).
  212. */
  213. float getOuterAngle() const { return _outerAngle; }
  214. /** get cos outAngle */
  215. float getCosOuterAngle() const { return _cosOuterAngle; }
  216. CC_CONSTRUCTOR_ACCESS:
  217. SpotLight();
  218. virtual ~SpotLight();
  219. protected:
  220. float _range;
  221. float _innerAngle;
  222. float _cosInnerAngle;
  223. float _outerAngle;
  224. float _cosOuterAngle;
  225. };
  226. /**
  227. @js NA
  228. */
  229. class CC_DLL AmbientLight : public BaseLight
  230. {
  231. public:
  232. /**
  233. * Creates a ambient light.
  234. * @param color The light's color.
  235. *
  236. * @return The new ambient light.
  237. */
  238. static AmbientLight* create(const Color3B &color);
  239. //get light type
  240. virtual LightType getLightType() const override { return LightType::AMBIENT; }
  241. CC_CONSTRUCTOR_ACCESS:
  242. AmbientLight();
  243. virtual ~AmbientLight();
  244. };
  245. NS_CC_END
  246. #endif