好记性不如铅笔头

C && C++, cocos2dx, 编程

cocos2dx学习笔记:shaders

根据【 void CCShaderCache::loadDefaultShader(CCGLProgram *p, int type) 】这个函数和【 ccShaders.cpp 】这个文件,我们可以找到cocos2dx的自带的一些shader的代码。这里备注下学习笔记:

CONTENTS

参考文档:

http://blog.csdn.net/carl_zhong/article/details/12219485 】

http://blog.csdn.net/wangyuchun_799/article/details/7763373 】

http://blog.csdn.net/wangyuchun_799/article/details/7770500 】

Shaders:

kCCShaderType_PositionTextureColor:

ccShader_PositionTextureColor_vert.h:

"													\n\
attribute vec4 a_position;							\n\   //==>>顶点位置
attribute vec2 a_texCoord;							\n\   //==>>顶点纹理坐标
attribute vec4 a_color;								\n\     //==>>顶点颜色
													\n\
#ifdef GL_ES										\n\
varying lowp vec4 v_fragmentColor;					\n\
varying mediump vec2 v_texCoord;					\n\
#else												\n\
varying vec4 v_fragmentColor;						\n\
varying vec2 v_texCoord;							\n\
#endif												\n\
													\n\
void main()											\n\
{													\n\
    gl_Position = CC_MVPMatrix * a_position;		\n\
	v_fragmentColor = a_color;						\n\  //==>>将输入的顶点颜色传到片段着色器里面
	v_texCoord = a_texCoord;						\n\    //==>>将输入的顶点纹理坐标传到片段着色器里面
}													\n\
";

ccShader_PositionTextureColor_frag.h:

"											\n\
#ifdef GL_ES								\n\
precision lowp float;						\n\
#endif										\n\
											\n\
varying vec4 v_fragmentColor;				\n\
varying vec2 v_texCoord;					\n\
uniform sampler2D CC_Texture0;				\n\
											\n\
void main()									\n\
{											\n\

//==>> texture2D:根据采样器(CC_Texture0)和纹理坐标(v_texCoord)获取映射的颜色
//==>> 用定点颜色和映射颜色算出输出的颜色
	gl_FragColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);			\n\
}											\n\
";

kCCShaderType_PositionTextureColorAlphaTest:

ccShader_PositionTextureColor_vert.h:

同上

ccShader_PositionTextureColorAlphaTest_frag.h:

//==>>和ccShader_PositionTextureColor_frag.h类似
"															\n\
#ifdef GL_ES												\n\
precision lowp float;										\n\
#endif														\n\
															\n\
varying vec4 v_fragmentColor;								\n\
varying vec2 v_texCoord;									\n\
uniform sampler2D CC_Texture0;								\n\
uniform float CC_alpha_value;								\n\
															\n\
void main()													\n\
{															\n\
	vec4 texColor = texture2D(CC_Texture0, v_texCoord);		\n\
															\n\
	// mimic: glAlphaFunc(GL_GREATER)						\n\
	// pass if ( incoming_pixel >= CC_alpha_value ) => fail if incoming_pixel < CC_alpha_value		\n\
															\n\
//==>> 如果着色器执行了discard关键字,片元将被直接扔掉,gl_FragColor会变得毫不相干。
	if ( texColor.a <= CC_alpha_value )						\n\
		discard;											\n\
															\n\
	gl_FragColor = texColor * v_fragmentColor;				\n\
}															\n\
";

kCCShaderType_PositionColor:

ccShader_PositionColor_vert.h:

//==>>ccShader_PositionTextureColor_vert.h类似,不过没有输入输出纹理坐标
"														\n\
attribute vec4 a_position;								\n\
attribute vec4 a_color;									\n\
#ifdef GL_ES											\n\
varying lowp vec4 v_fragmentColor;						\n\
#else													\n\
varying vec4 v_fragmentColor;							\n\
#endif													\n\
														\n\
void main()												\n\
{														\n\
    gl_Position = CC_MVPMatrix * a_position;			\n\
	v_fragmentColor = a_color;							\n\
}														\n\
";

ccShader_PositionColor_frag.h:

//==>>ccShader_PositionTextureColor_frag.h类似,不过因为没有纹理坐标,
//==>>只用顶点颜色算出输出的颜色
"													\n\
#ifdef GL_ES										\n\
precision lowp float;								\n\
#endif												\n\
													\n\
varying vec4 v_fragmentColor;						\n\
													\n\
void main()											\n\
{													\n\
	gl_FragColor = v_fragmentColor;					\n\
}													\n\
";

kCCShaderType_PositionTexture:

ccShader_PositionTexture_vert.h:

//==>>ccShader_PositionTextureColor_vert.h类似,不过没有输入输出顶点颜色
"														\n\
attribute vec4 a_position;								\n\
attribute vec2 a_texCoord;								\n\
														\n\
#ifdef GL_ES											\n\
varying mediump vec2 v_texCoord;						\n\
#else													\n\
varying vec2 v_texCoord;								\n\
#endif													\n\
														\n\
void main()												\n\
{														\n\
    gl_Position = CC_MVPMatrix * a_position;			\n\
	v_texCoord = a_texCoord;							\n\
}														\n\
";

ccShader_PositionTexture_frag.h:

//==>>ccShader_PositionTextureColor_frag.h类似,不过因为没有顶点颜色,
//==>>只用映射颜色算出输出的颜色
"																		\n\
#ifdef GL_ES															\n\
precision lowp float;													\n\
#endif																	\n\
																		\n\
varying vec2 v_texCoord;												\n\
uniform sampler2D CC_Texture0;											\n\
																		\n\
void main()																\n\
{																		\n\
	gl_FragColor =  texture2D(CC_Texture0, v_texCoord);					\n\
}																		\n\
";

kCCShaderType_PositionTexture_uColor:

ccShader_PositionTexture_uColor_vert.h:

//==>>ccShader_PositionTexture_vert.h类似
"									\n\
attribute vec4 a_position;			\n\
attribute vec2 a_texCoord;			\n\
									\n\
#ifdef GL_ES						\n\
varying mediump vec2 v_texCoord;	\n\
#else								\n\
varying vec2 v_texCoord;			\n\
#endif								\n\
									\n\
void main()							\n\
{									\n\
    gl_Position = CC_MVPMatrix * a_position;		\n\
	v_texCoord = a_texCoord;		\n\
}									\n\
";

 ccShader_PositionTexture_uColor_frag.h:

//==>>ccShader_PositionTexture_frag.h类似,使用映射颜色和u_color常量来算出输出的颜色
"										\n\
#ifdef GL_ES							\n\
precision lowp float;					\n\
#endif									\n\
										\n\
uniform		vec4 u_color;				\n\
										\n\
varying vec2 v_texCoord;				\n\
										\n\
uniform sampler2D CC_Texture0;			\n\
										\n\
void main()								\n\
{										\n\
	gl_FragColor =  texture2D(CC_Texture0, v_texCoord) * u_color;	\n\
}										\n\
";

kCCShaderType_PositionTextureA8Color:

ccShader_PositionTextureA8Color_vert.h:

//==>>和ccShader_PositionTextureColor_vert.h类似,
"													\n\
attribute vec4 a_position;							\n\
attribute vec2 a_texCoord;							\n\
attribute vec4 a_color;								\n\
													\n\
#ifdef GL_ES										\n\
varying lowp vec4 v_fragmentColor;					\n\
varying mediump vec2 v_texCoord;					\n\
#else												\n\
varying vec4 v_fragmentColor;						\n\
varying vec2 v_texCoord;							\n\
#endif												\n\
													\n\
void main()											\n\
{													\n\
    gl_Position = CC_MVPMatrix * a_position;		\n\
	v_fragmentColor = a_color;						\n\
	v_texCoord = a_texCoord;						\n\
}													\n\
";

ccShader_PositionTextureA8Color_frag.h:

//==>>输出的颜色的rgb是传入的顶点的的颜色,输出的颜色的a是顶点颜色.a和映射颜色.a的计算值
"													\n\
#ifdef GL_ES										\n\
precision lowp float;								\n\
#endif												\n\
													\n\
varying vec4 v_fragmentColor;						\n\
varying vec2 v_texCoord;							\n\
uniform sampler2D CC_Texture0;						\n\
													\n\
void main()											\n\
{													\n\
	gl_FragColor = vec4( v_fragmentColor.rgb,// RGB from uniform				\n\
	v_fragmentColor.a * texture2D(CC_Texture0, v_texCoord).a	// A from texture & uniform		\n\
						);							\n\
}													\n\
";

kCCShaderType_Position_uColor:

ccShader_Position_uColor_vert.h:

//==>>输出给片段着色器的颜色是常量颜色u_color,输出给片段着色器的大小是常量大小u_pointSize,外部只传入一个position
"													\n\
attribute vec4 a_position;							\n\
uniform	vec4 u_color;								\n\
uniform float u_pointSize;							\n\
													\n\
#ifdef GL_ES										\n\
varying lowp vec4 v_fragmentColor;					\n\
#else												\n\
varying vec4 v_fragmentColor;						\n\
#endif												\n\
													\n\
void main()											\n\
{													\n\
    gl_Position = CC_MVPMatrix * a_position;		\n\
	gl_PointSize = u_pointSize;						\n\
	v_fragmentColor = u_color;						\n\
}													\n\
";

ccShader_Position_uColor_frag.h:

//==>>输出颜色只和顶点颜色有关,顶点颜色由u_color赋值。
"										\n\
#ifdef GL_ES							\n\
precision lowp float;					\n\
#endif									\n\
										\n\
varying vec4 v_fragmentColor;			\n\
										\n\
void main()								\n\
{										\n\
	gl_FragColor = v_fragmentColor;		\n\
}										\n\
";

kCCShaderType_PositionLengthTexureColor:

ccShader_PositionColorLengthTexture_vert.h:

//==>>输出给片段着色器的颜色是由输入颜色a_color计算出来的,
//==>>计算方式很简单,让a_color的rgb颜色值根据alpha值再进行一次消减
"																	\n\
#ifdef GL_ES														\n\
attribute mediump vec4 a_position;									\n\
attribute mediump vec2 a_texcoord;									\n\
attribute mediump vec4 a_color;										\n\
																	\n\
varying mediump vec4 v_color;										\n\
varying mediump vec2 v_texcoord;									\n\
																	\n\
#else																\n\
attribute vec4 a_position;											\n\
attribute vec2 a_texcoord;											\n\
attribute vec4 a_color;												\n\
																	\n\
varying vec4 v_color;												\n\
varying vec2 v_texcoord;											\n\
#endif																\n\
																	\n\
void main()			\n\
{				\n\
	v_color = vec4(a_color.rgb * a_color.a, a_color.a);				\n\
	v_texcoord = a_texcoord;										\n\
											\n\
	gl_Position = CC_MVPMatrix * a_position;						\n\
}																	\n\
";

ccShader_PositionColorLengthTexture_frag.h:

/*
(11)genType step (genType edge, genType x),genType step (float edge, genType x)
如果x < edge,返回0.0,否则返回1.0

(12)genType smoothstep (genType edge0,genType edge1,genType x),genType smoothstep (float edge0,float edge1,genType x)
如果x <= edge0,返回0.0 ;如果x >= edge1 返回1.0;如果edge0 < x < edge1,则执行0~1之间的平滑埃尔米特差值。如果edge0 >= edge1,结果是未定义的。
*/
"																															\n\
#ifdef GL_ES		\n\
// #extension GL_OES_standard_derivatives : enable	\n\																														\n\
varying mediump vec4 v_color;		\n\
varying mediump vec2 v_texcoord;	\n\
#else		\n\
varying vec4 v_color;			\n\
varying vec2 v_texcoord;        \n\
#endif		\n\																													\n\
void main()	\n\
{		\n\
// #if defined GL_OES_standard_derivatives						\n\
	// gl_FragColor = v_color*smoothstep(0.0, length(fwidth(v_texcoord)), 1.0 - length(v_texcoord)); \n\
// #else					\n\
	gl_FragColor = v_color*step(0.0, 1.0 - length(v_texcoord));		\n\
// #endif									\n\
}		\n\
";

kCCShaderType_ControlSwitch:

ccShader_PositionTextureColor_vert.h:

同上

ccShaderEx_SwitchMask_frag.h:

//==>>使用不同的采样器对同一个纹理坐标(v_texCoord)进行采样,得到两个颜色(texColor, maskColor)
//==>>然后用这两个颜色算出finalColor,最后用finalColor和顶点颜色计算出最终的输出颜色
"                                                \n\
#ifdef GL_ES                                     \n\
precision lowp float;                            \n\
#endif                                           \n\
                                                 \n\
varying vec4        v_fragmentColor;             \n\
varying vec2        v_texCoord;                  \n\
uniform sampler2D   u_texture;                   \n\
uniform sampler2D   u_mask;                      \n\
                                                 \n\
void main()                                      \n\
{                                                \n\
    vec4 texColor   = texture2D(u_texture, v_texCoord);                                      \n\
    vec4 maskColor  = texture2D(u_mask, v_texCoord);                                         \n\
    vec4 finalColor = vec4(texColor.r, texColor.g, texColor.b, maskColor.a * texColor.a);    \n\
    gl_FragColor    = v_fragmentColor * finalColor;                                          \n\
}                                                                                            \n\
";

发表评论

1 × 2 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据