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