17 January 2016
版权声明:本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名elloop(包含链接)

#前言

本文把cocos2d-x 2.x版本中预定义的所有shader源码贴出来,便于集中参考。作者的引擎版本是2.2.3.

预定义shaders总览

cocos中的预定义shader是定义在ccShaders.h/cpp这对文件中的,下面是所有预定义shader的名字:

extern CC_DLL const GLchar * ccPosition_uColor_frag;
extern CC_DLL const GLchar * ccPosition_uColor_vert;

extern CC_DLL const GLchar * ccPositionColor_frag;
extern CC_DLL const GLchar * ccPositionColor_vert;

extern CC_DLL const GLchar * ccPositionTexture_frag;
extern CC_DLL const GLchar * ccPositionTexture_vert;

extern CC_DLL const GLchar * ccPositionTextureA8Color_frag;
extern CC_DLL const GLchar * ccPositionTextureA8Color_vert;

extern CC_DLL const GLchar * ccPositionTextureColor_frag;
extern CC_DLL const GLchar * ccPositionTextureColor_vert;

extern CC_DLL const GLchar * ccPositionTextureColorAlphaTest_frag;

extern CC_DLL const GLchar * ccPositionTexture_uColor_frag;
extern CC_DLL const GLchar * ccPositionTexture_uColor_vert;

extern CC_DLL const GLchar * ccPositionColorLengthTexture_frag;
extern CC_DLL const GLchar * ccPositionColorLengthTexture_vert;

extern CC_DLL const GLchar * ccExSwitchMask_frag;

下面以ccShaders.h中定义的片段着色器变量名作为标题,逐条列出。(为什么不用顶点着色器的变量名,因为有几个shader只有片段着色器变量名)

ccPosition_uColor_frag

ccShader_Position_uColor_vert.h

attribute vec4 a_position;							
uniform	vec4 u_color;								
uniform float u_pointSize;							
													
#ifdef GL_ES										
varying lowp vec4 v_fragmentColor;					
#else												
varying vec4 v_fragmentColor;						
#endif												
													
void main()											
{													
    gl_Position = CC_MVPMatrix * a_position;		
	gl_PointSize = u_pointSize;						
	v_fragmentColor = u_color;						
}													

ccShader_Position_uColor_frag.h

#ifdef GL_ES							
precision lowp float;					
#endif									
										
varying vec4 v_fragmentColor;			
										
void main()								
{										
	gl_FragColor = v_fragmentColor;		
}										

ccPositionColor_frag

ccShader_PositionColor_vert.h

attribute vec4 a_position;								
attribute vec4 a_color;									
#ifdef GL_ES											
varying lowp vec4 v_fragmentColor;						
#else													
varying vec4 v_fragmentColor;							
#endif													
														
void main()												
{														
    gl_Position = CC_MVPMatrix * a_position;			
	v_fragmentColor = a_color;							
}														

ccShader_PositionColor_frag.h

#ifdef GL_ES										
precision lowp float;								
#endif												
													
varying vec4 v_fragmentColor;						
													
void main()											
{													
	gl_FragColor = v_fragmentColor;					
}													

ccPositionTexture_frag

ccShader_PositionTexture_vert.h

attribute vec4 a_position;								
attribute vec2 a_texCoord;								
														
#ifdef GL_ES											
varying mediump vec2 v_texCoord;						
#else													
varying vec2 v_texCoord;								
#endif													
														
void main()												
{														
    gl_Position = CC_MVPMatrix * a_position;			
	v_texCoord = a_texCoord;							
}														

ccShader_PositionTexture_frag.h

#ifdef GL_ES															
precision lowp float;													
#endif																	
																		
varying vec2 v_texCoord;												
uniform sampler2D CC_Texture0;											
																		
void main()																
{																		
	gl_FragColor =  texture2D(CC_Texture0, v_texCoord);					
}																		

ccPositionTextureA8Color_frag

ccShader_PositionTextureA8Color_vert.h

attribute vec4 a_position;							
attribute vec2 a_texCoord;							
attribute vec4 a_color;								
													
#ifdef GL_ES										
varying lowp vec4 v_fragmentColor;					
varying mediump vec2 v_texCoord;					
#else												
varying vec4 v_fragmentColor;						
varying vec2 v_texCoord;							
#endif												
													
void main()											
{													
    gl_Position = CC_MVPMatrix * a_position;		
	v_fragmentColor = a_color;						
	v_texCoord = a_texCoord;						
}													

ccShader_PositionTextureA8Color_frag.h

#ifdef GL_ES										
precision lowp float;								
#endif												
													
varying vec4 v_fragmentColor;						
varying vec2 v_texCoord;							
uniform sampler2D CC_Texture0;						
													
void main()											
{													
	gl_FragColor = vec4( v_fragmentColor.rgb,										// RGB from uniform				
                         v_fragmentColor.a * texture2D(CC_Texture0, v_texCoord).a	// A from texture & uniform		
                       );							
}

ccPositionTextureColor_frag

ccShader_PositionTextureColor_vert.h

attribute vec4 a_position;							
attribute vec2 a_texCoord;							
attribute vec4 a_color;								
													
#ifdef GL_ES										
varying lowp vec4 v_fragmentColor;					
varying mediump vec2 v_texCoord;					
#else												
varying vec4 v_fragmentColor;						
varying vec2 v_texCoord;							
#endif												
													
void main()											
{													
    gl_Position = CC_MVPMatrix * a_position;		
	v_fragmentColor = a_color;						
	v_texCoord = a_texCoord;						
}													

ccShader_PositionTextureColor_frag.h

#ifdef GL_ES								
precision lowp float;						
#endif										
											
varying vec4 v_fragmentColor;				
varying vec2 v_texCoord;					
uniform sampler2D CC_Texture0;				
											
void main()									
{											
    gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);
}											

ccPositionTextureColorAlphaTest_frag

ccShader_PositionTextureColorAlphaTest_frag.h

#ifdef GL_ES												
precision lowp float;										
#endif														
															
varying vec4 v_fragmentColor;								
varying vec2 v_texCoord;									
uniform sampler2D CC_Texture0;								
uniform float CC_alpha_value;								
															
void main()													
{															
	vec4 texColor = texture2D(CC_Texture0, v_texCoord);		
															
	// mimic: glAlphaFunc(GL_GREATER)						
    // pass if ( incoming_pixel >= CC_alpha_value ) => fail if incoming_pixel < CC_alpha_value
															
	if ( texColor.a <= CC_alpha_value )						
		discard;											
															
	gl_FragColor = texColor * v_fragmentColor;				
}															

ccPositionTexture_uColor_frag

ccShader_PositionTexture_uColor_vert.h

attribute vec4 a_position;			
attribute vec2 a_texCoord;			
									
#ifdef GL_ES						
varying mediump vec2 v_texCoord;	
#else								
varying vec2 v_texCoord;			
#endif								
									
void main()							
{									
    gl_Position = CC_MVPMatrix * a_position;
	v_texCoord = a_texCoord;		
}									

ccShader_PositionTexture_uColor_frag.h

#ifdef GL_ES							
precision lowp float;					
#endif									
										
uniform		vec4 u_color;				
										
varying vec2 v_texCoord;				
										
uniform sampler2D CC_Texture0;			
										
void main()								
{										
    gl_FragColor =  texture2D(CC_Texture0, v_texCoord) * u_color;
}										

ccExSwitchMask_frag

ccShaderEx_SwitchMask_frag.h

#ifdef GL_ES                                     
precision lowp float;                            
#endif                                           
                                                 
varying vec4        v_fragmentColor;             
varying vec2        v_texCoord;                  
uniform sampler2D   u_texture;                   
uniform sampler2D   u_mask;                      
                                                 
void main()                                      
{                                                
    vec4 texColor   = texture2D(u_texture, v_texCoord);                                      
    vec4 maskColor  = texture2D(u_mask, v_texCoord);                                         
    vec4 finalColor = vec4(texColor.r, texColor.g, texColor.b, maskColor.a * texColor.a);    
    gl_FragColor    = v_fragmentColor * finalColor;                                          
}                                                                                            

ccPositionColorLengthTexture_frag

ccShader_PositionColorLengthTexture_vert.h

#ifdef GL_ES														
attribute mediump vec4 a_position;									
attribute mediump vec2 a_texcoord;									
attribute mediump vec4 a_color;										
																	
varying mediump vec4 v_color;										
varying mediump vec2 v_texcoord;									
																	
#else																
attribute vec4 a_position;											
attribute vec2 a_texcoord;											
attribute vec4 a_color;												
																	
varying vec4 v_color;												
varying vec2 v_texcoord;											
#endif																
																	
void main()															
{																	
	v_color = vec4(a_color.rgb * a_color.a, a_color.a);				
	v_texcoord = a_texcoord;										
																	
	gl_Position = CC_MVPMatrix * a_position;						
}																	

ccShader_PositionColorLengthTexture_frag.h

#ifdef GL_ES																												
// #extension GL_OES_standard_derivatives : enable																			
																															
varying mediump vec4 v_color;																								
varying mediump vec2 v_texcoord;																							
#else																														
varying vec4 v_color;																										
varying vec2 v_texcoord;																									
#endif																														
																															
void main()																													
{																															
// #if defined GL_OES_standard_derivatives																					
	// gl_FragColor = v_color*smoothstep(0.0, length(fwidth(v_texcoord)), 1.0 - length(v_texcoord));						
// #else																													
	gl_FragColor = v_color*step(0.0, 1.0 - length(v_texcoord));																
// #endif																													
}																															

在这里也能看到这篇文章:github博客, CSDN博客, 欢迎访问



分享到