【cocos2d-x 2.x 学习与应用总结】12: cocos2d-x预定义shader源码

前言

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

下面以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博客, 欢迎访问

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值