CCShaders.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. //-----------------------Shader_Position_uColor Shader Source--------------------------
  23. /**
  24. * @constant
  25. * @type {String}
  26. */
  27. cc.SHADER_POSITION_UCOLOR_FRAG =
  28. "precision lowp float;\n"
  29. + "varying vec4 v_fragmentColor;\n"
  30. + "void main() \n"
  31. + "{ \n"
  32. + " gl_FragColor = v_fragmentColor; \n"
  33. + "}\n";
  34. /**
  35. * @constant
  36. * @type {String}
  37. */
  38. cc.SHADER_POSITION_UCOLOR_VERT =
  39. "attribute vec4 a_position;\n"
  40. + "uniform vec4 u_color;\n"
  41. + "uniform float u_pointSize;\n"
  42. + "varying lowp vec4 v_fragmentColor; \n"
  43. + "void main(void) \n"
  44. + "{\n"
  45. //+ " gl_Position = CC_MVPMatrix * a_position; \n"
  46. + " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
  47. + " gl_PointSize = u_pointSize; \n"
  48. + " v_fragmentColor = u_color; \n"
  49. + "}";
  50. //---------------------Shader_PositionColor Shader Source-----------------------
  51. /**
  52. * @constant
  53. * @type {String}
  54. */
  55. cc.SHADER_POSITION_COLOR_FRAG =
  56. "precision lowp float; \n"
  57. + "varying vec4 v_fragmentColor; \n"
  58. + "void main() \n"
  59. + "{ \n"
  60. + " gl_FragColor = v_fragmentColor; \n"
  61. + "} ";
  62. /**
  63. * @constant
  64. * @type {String}
  65. */
  66. cc.SHADER_POSITION_COLOR_VERT =
  67. "attribute vec4 a_position;\n"
  68. + "attribute vec4 a_color;\n"
  69. + "varying lowp vec4 v_fragmentColor;\n"
  70. + "void main()\n"
  71. + "{\n"
  72. //+ " gl_Position = CC_MVPMatrix * a_position; \n"
  73. + " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
  74. + " v_fragmentColor = a_color; \n"
  75. + "}";
  76. // --------------------- Shader_PositionColorLengthTexture Shader source------------------------
  77. /**
  78. * @constant
  79. * @type {String}
  80. */
  81. cc.SHADER_POSITION_COLOR_LENGTH_TEXTURE_FRAG =
  82. "// #extension GL_OES_standard_derivatives : enable\n"
  83. + "varying mediump vec4 v_color;\n"
  84. + "varying mediump vec2 v_texcoord;\n"
  85. + "void main() \n"
  86. + "{ \n"
  87. + "// #if defined GL_OES_standard_derivatives \n"
  88. + "// gl_FragColor = v_color*smoothstep(0.0, length(fwidth(v_texcoord)), 1.0 - length(v_texcoord)); \n"
  89. + "// #else \n"
  90. + "gl_FragColor = v_color * step(0.0, 1.0 - length(v_texcoord)); \n"
  91. + "// #endif \n"
  92. + "}";
  93. /**
  94. * @constant
  95. * @type {String}
  96. */
  97. cc.SHADER_POSITION_COLOR_LENGTH_TEXTURE_VERT =
  98. "attribute mediump vec4 a_position; \n"
  99. + "attribute mediump vec2 a_texcoord; \n"
  100. + "attribute mediump vec4 a_color; \n"
  101. + "varying mediump vec4 v_color; \n"
  102. + "varying mediump vec2 v_texcoord; \n"
  103. + "void main() \n"
  104. + "{ \n"
  105. + " v_color = a_color;//vec4(a_color.rgb * a_color.a, a_color.a); \n"
  106. + " v_texcoord = a_texcoord; \n"
  107. //+ " gl_Position = CC_MVPMatrix * a_position; \n"
  108. + " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
  109. + "}";
  110. // ----------------------Shader_PositionTexture Shader Source-------------------------------------
  111. /**
  112. * @constant
  113. * @type {String}
  114. */
  115. cc.SHADER_POSITION_TEXTURE_FRAG =
  116. "precision lowp float; \n"
  117. + "varying vec2 v_texCoord; \n"
  118. + "uniform sampler2D CC_Texture0; \n"
  119. + "void main() \n"
  120. + "{ \n"
  121. + " gl_FragColor = texture2D(CC_Texture0, v_texCoord); \n"
  122. + "}";
  123. /**
  124. * @constant
  125. * @type {String}
  126. */
  127. cc.SHADER_POSITION_TEXTURE_VERT =
  128. "attribute vec4 a_position; \n"
  129. + "attribute vec2 a_texCoord; \n"
  130. + "varying mediump vec2 v_texCoord; \n"
  131. + "void main() \n"
  132. + "{ \n"
  133. //+ " gl_Position = CC_MVPMatrix * a_position; \n"
  134. + " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
  135. + " v_texCoord = a_texCoord; \n"
  136. + "}";
  137. // ------------------------Shader_PositionTexture_uColor Shader Source-------------------------------
  138. /**
  139. * @constant
  140. * @type {String}
  141. */
  142. cc.SHADER_POSITION_TEXTURE_UCOLOR_FRAG =
  143. "precision lowp float; \n"
  144. + "uniform vec4 u_color; \n"
  145. + "varying vec2 v_texCoord; \n"
  146. + "uniform sampler2D CC_Texture0; \n"
  147. + "void main() \n"
  148. + "{ \n"
  149. + " gl_FragColor = texture2D(CC_Texture0, v_texCoord) * u_color; \n"
  150. + "}";
  151. /**
  152. * @constant
  153. * @type {String}
  154. */
  155. cc.SHADER_POSITION_TEXTURE_UCOLOR_VERT =
  156. "attribute vec4 a_position;\n"
  157. + "attribute vec2 a_texCoord; \n"
  158. + "varying mediump vec2 v_texCoord; \n"
  159. + "void main() \n"
  160. + "{ \n"
  161. //+ " gl_Position = CC_MVPMatrix * a_position; \n"
  162. + " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
  163. + " v_texCoord = a_texCoord; \n"
  164. + "}";
  165. //---------------------Shader_PositionTextureA8Color Shader source-------------------------------
  166. /**
  167. * @constant
  168. * @type {String}
  169. */
  170. cc.SHADER_POSITION_TEXTURE_A8COLOR_FRAG =
  171. "precision lowp float; \n"
  172. + "varying vec4 v_fragmentColor; \n"
  173. + "varying vec2 v_texCoord; \n"
  174. + "uniform sampler2D CC_Texture0; \n"
  175. + "void main() \n"
  176. + "{ \n"
  177. + " gl_FragColor = vec4( v_fragmentColor.rgb, \n" // RGB from uniform
  178. + " v_fragmentColor.a * texture2D(CC_Texture0, v_texCoord).a \n" // A from texture and uniform
  179. + " ); \n"
  180. + "}";
  181. /**
  182. * @constant
  183. * @type {String}
  184. */
  185. cc.SHADER_POSITION_TEXTURE_A8COLOR_VERT =
  186. "attribute vec4 a_position; \n"
  187. + "attribute vec2 a_texCoord; \n"
  188. + "attribute vec4 a_color; \n"
  189. + "varying lowp vec4 v_fragmentColor; \n"
  190. + "varying mediump vec2 v_texCoord; \n"
  191. + "void main() \n"
  192. + "{ \n"
  193. //+ " gl_Position = CC_MVPMatrix * a_position; \n"
  194. + " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
  195. + " v_fragmentColor = a_color; \n"
  196. + " v_texCoord = a_texCoord; \n"
  197. + "}";
  198. // ------------------------Shader_PositionTextureColor Shader source------------------------------------
  199. /**
  200. * @constant
  201. * @type {String}
  202. */
  203. cc.SHADER_POSITION_TEXTURE_COLOR_FRAG =
  204. "precision lowp float;\n"
  205. + "varying vec4 v_fragmentColor; \n"
  206. + "varying vec2 v_texCoord; \n"
  207. + "uniform sampler2D CC_Texture0; \n"
  208. + "void main() \n"
  209. + "{ \n"
  210. + " gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord); \n"
  211. + "}";
  212. /**
  213. * @constant
  214. * @type {String}
  215. */
  216. cc.SHADER_POSITION_TEXTURE_COLOR_VERT =
  217. "attribute vec4 a_position; \n"
  218. + "attribute vec2 a_texCoord; \n"
  219. + "attribute vec4 a_color; \n"
  220. + "varying lowp vec4 v_fragmentColor; \n"
  221. + "varying mediump vec2 v_texCoord; \n"
  222. + "void main() \n"
  223. + "{ \n"
  224. //+ " gl_Position = CC_MVPMatrix * a_position; \n"
  225. + " gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position; \n"
  226. + " v_fragmentColor = a_color; \n"
  227. + " v_texCoord = a_texCoord; \n"
  228. + "}";
  229. //-----------------------Shader_PositionTextureColorAlphaTest_frag Shader Source----------------------------
  230. /**
  231. * @constant
  232. * @type {String}
  233. */
  234. cc.SHADER_POSITION_TEXTURE_COLOR_ALPHATEST_FRAG =
  235. "precision lowp float; \n"
  236. + "varying vec4 v_fragmentColor; \n"
  237. + "varying vec2 v_texCoord; \n"
  238. + "uniform sampler2D CC_Texture0; \n"
  239. + "uniform float CC_alpha_value; \n"
  240. + "void main() \n"
  241. + "{ \n"
  242. + " vec4 texColor = texture2D(CC_Texture0, v_texCoord); \n"
  243. + " // mimic: glAlphaFunc(GL_GREATER) \n"
  244. + " //pass if ( incoming_pixel >= CC_alpha_value ) => fail if incoming_pixel < CC_alpha_value \n"
  245. + " if ( texColor.a <= CC_alpha_value ) \n"
  246. + " discard; \n"
  247. + " gl_FragColor = texColor * v_fragmentColor; \n"
  248. + "}";
  249. //-----------------------ShaderEx_SwitchMask_frag Shader Source----------------------------
  250. /**
  251. * @constant
  252. * @type {String}
  253. */
  254. cc.SHADEREX_SWITCHMASK_FRAG =
  255. "precision lowp float; \n"
  256. + "varying vec4 v_fragmentColor; \n"
  257. + "varying vec2 v_texCoord; \n"
  258. + "uniform sampler2D u_texture; \n"
  259. + "uniform sampler2D u_mask; \n"
  260. + "void main() \n"
  261. + "{ \n"
  262. + " vec4 texColor = texture2D(u_texture, v_texCoord); \n"
  263. + " vec4 maskColor = texture2D(u_mask, v_texCoord); \n"
  264. + " vec4 finalColor = vec4(texColor.r, texColor.g, texColor.b, maskColor.a * texColor.a); \n"
  265. + " gl_FragColor = v_fragmentColor * finalColor; \n"
  266. + "}";