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 
  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_MVPMatrix * a_position;  \n"
 80         + "    gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position;  \n"
 81         + "    v_fragmentColor = a_color;             \n"
 82         + "}";
 83 
 84 // --------------------- Shader_PositionColorLengthTexture Shader source------------------------
 85 /**
 86  * @constant
 87  * @type {String}
 88  */
 89 cc.SHADER_POSITION_COLOR_LENGTH_TEXTURE_FRAG =
 90         "// #extension GL_OES_standard_derivatives : enable\n"
 91         + "varying mediump vec4 v_color;\n"
 92         + "varying mediump vec2 v_texcoord;\n"
 93         + "void main()	\n"
 94         + "{ \n"
 95         + "// #if defined GL_OES_standard_derivatives	\n"
 96         + "// gl_FragColor = v_color*smoothstep(0.0, length(fwidth(v_texcoord)), 1.0 - length(v_texcoord)); \n"
 97         + "// #else	\n"
 98         + "gl_FragColor = v_color * step(0.0, 1.0 - length(v_texcoord)); \n"
 99         + "// #endif \n"
100         + "}";
101 
102 /**
103  * @constant
104  * @type {String}
105  */
106 cc.SHADER_POSITION_COLOR_LENGTH_TEXTURE_VERT =
107         "attribute mediump vec4 a_position; \n"
108         + "attribute mediump vec2 a_texcoord; \n"
109         + "attribute mediump vec4 a_color;	\n"
110         + "varying mediump vec4 v_color; \n"
111         + "varying mediump vec2 v_texcoord;	\n"
112         + "void main() \n"
113         + "{ \n"
114         + "     v_color = a_color;//vec4(a_color.rgb * a_color.a, a_color.a); \n"
115         + "     v_texcoord = a_texcoord; \n"
116         //+ "    gl_Position = CC_MVPMatrix * a_position;  \n"
117         + "    gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position;  \n"
118         + "}";
119 
120 // ----------------------Shader_PositionTexture Shader Source-------------------------------------
121 /**
122  * @constant
123  * @type {String}
124  */
125 cc.SHADER_POSITION_TEXTURE_FRAG =
126         "precision lowp float;   \n"
127         + "varying vec2 v_texCoord;  \n"
128         + "uniform sampler2D CC_Texture0; \n"
129         + "void main() \n"
130         + "{  \n"
131         + "    gl_FragColor =  texture2D(CC_Texture0, v_texCoord);   \n"
132         + "}";
133 
134 /**
135  * @constant
136  * @type {String}
137  */
138 cc.SHADER_POSITION_TEXTURE_VERT =
139         "attribute vec4 a_position; \n"
140         + "attribute vec2 a_texCoord; \n"
141         + "varying mediump vec2 v_texCoord; \n"
142         + "void main() \n"
143         + "{ \n"
144         //+ "    gl_Position = CC_MVPMatrix * a_position;  \n"
145         + "    gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position;  \n"
146         + "    v_texCoord = a_texCoord;               \n"
147         + "}";
148 
149 // ------------------------Shader_PositionTexture_uColor Shader Source-------------------------------
150 /**
151  * @constant
152  * @type {String}
153  */
154 cc.SHADER_POSITION_TEXTURE_UCOLOR_FRAG =
155         "precision lowp float;  \n"
156         + "uniform vec4 u_color; \n"
157         + "varying vec2 v_texCoord; \n"
158         + "uniform sampler2D CC_Texture0;  \n"
159         + "void main() \n"
160         + "{  \n"
161         + "    gl_FragColor =  texture2D(CC_Texture0, v_texCoord) * u_color;    \n"
162         + "}";
163 
164 /**
165  * @constant
166  * @type {String}
167  */
168 cc.SHADER_POSITION_TEXTURE_UCOLOR_VERT =
169         "attribute vec4 a_position;\n"
170         + "attribute vec2 a_texCoord; \n"
171         + "varying mediump vec2 v_texCoord; \n"
172         + "void main() \n"
173         + "{ \n"
174         //+ "    gl_Position = CC_MVPMatrix * a_position;  \n"
175         + "    gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position;  \n"
176         + "    v_texCoord = a_texCoord;                 \n"
177         + "}";
178 
179 //---------------------Shader_PositionTextureA8Color Shader source-------------------------------
180 /**
181  * @constant
182  * @type {String}
183  */
184 cc.SHADER_POSITION_TEXTURE_A8COLOR_FRAG =
185         "precision lowp float;  \n"
186         + "varying vec4 v_fragmentColor; \n"
187         + "varying vec2 v_texCoord; \n"
188         + "uniform sampler2D CC_Texture0; \n"
189         + "void main() \n"
190         + "{ \n"
191         + "    gl_FragColor = vec4( v_fragmentColor.rgb,         \n"                            // RGB from uniform
192         + "        v_fragmentColor.a * texture2D(CC_Texture0, v_texCoord).a   \n"                  // A from texture and uniform
193         + "    ); \n"
194         + "}";
195 
196 /**
197  * @constant
198  * @type {String}
199  */
200 cc.SHADER_POSITION_TEXTURE_A8COLOR_VERT =
201         "attribute vec4 a_position; \n"
202         + "attribute vec2 a_texCoord; \n"
203         + "attribute vec4 a_color;  \n"
204         + "varying lowp vec4 v_fragmentColor; \n"
205         + "varying mediump vec2 v_texCoord; \n"
206         + "void main() \n"
207         + "{ \n"
208         //+ "    gl_Position = CC_MVPMatrix * a_position;  \n"
209         + "    gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position;  \n"
210         + "    v_fragmentColor = a_color; \n"
211         + "    v_texCoord = a_texCoord; \n"
212         + "}";
213 
214 // ------------------------Shader_PositionTextureColor Shader source------------------------------------
215 /**
216  * @constant
217  * @type {String}
218  */
219 cc.SHADER_POSITION_TEXTURE_COLOR_FRAG =
220         "precision lowp float;\n"
221         + "varying vec4 v_fragmentColor; \n"
222         + "varying vec2 v_texCoord; \n"
223         + "uniform sampler2D CC_Texture0; \n"
224         + "void main() \n"
225         + "{ \n"
226         + "    gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord); \n"
227         + "}";
228 
229 /**
230  * @constant
231  * @type {String}
232  */
233 cc.SHADER_POSITION_TEXTURE_COLOR_VERT =
234         "attribute vec4 a_position; \n"
235         + "attribute vec2 a_texCoord; \n"
236         + "attribute vec4 a_color;  \n"
237         + "varying lowp vec4 v_fragmentColor; \n"
238         + "varying mediump vec2 v_texCoord; \n"
239         + "void main() \n"
240         + "{ \n"
241         //+ "    gl_Position = CC_MVPMatrix * a_position;  \n"
242         + "    gl_Position = (CC_PMatrix * CC_MVMatrix) * a_position;  \n"
243         + "    v_fragmentColor = a_color; \n"
244         + "    v_texCoord = a_texCoord; \n"
245         + "}";
246 
247 //-----------------------Shader_PositionTextureColorAlphaTest_frag Shader Source----------------------------
248 /**
249  * @constant
250  * @type {String}
251  */
252 cc.SHADER_POSITION_TEXTURE_COLOR_ALPHATEST_FRAG =
253         "precision lowp float;   \n"
254         + "varying vec4 v_fragmentColor; \n"
255         + "varying vec2 v_texCoord;   \n"
256         + "uniform sampler2D CC_Texture0; \n"
257         + "uniform float CC_alpha_value; \n"
258         + "void main() \n"
259         + "{  \n"
260         + "    vec4 texColor = texture2D(CC_Texture0, v_texCoord);  \n"
261         // mimic: glAlphaFunc(GL_GREATER)
262         //pass if ( incoming_pixel >= CC_alpha_value ) => fail if incoming_pixel < CC_alpha_value
263         + "    if ( texColor.a <= CC_alpha_value )          \n"
264         + "        discard; \n"
265         + "    gl_FragColor = texColor * v_fragmentColor;  \n"
266         + "}";
267 
268 //-----------------------ShaderEx_SwitchMask_frag Shader Source----------------------------
269 /**
270  * @constant
271  * @type {String}
272  */
273 cc.SHADEREX_SWITCHMASK_FRAG =
274         "precision lowp float; \n"
275         + "varying vec4 v_fragmentColor; \n"
276         + "varying vec2 v_texCoord; \n"
277         + "uniform sampler2D u_texture;  \n"
278         + "uniform sampler2D   u_mask;   \n"
279         + "void main()  \n"
280         + "{  \n"
281         + "    vec4 texColor   = texture2D(u_texture, v_texCoord);  \n"
282         + "    vec4 maskColor  = texture2D(u_mask, v_texCoord); \n"
283         + "    vec4 finalColor = vec4(texColor.r, texColor.g, texColor.b, maskColor.a * texColor.a);        \n"
284         + "    gl_FragColor    = v_fragmentColor * finalColor; \n"
285         + "}";