基于cocos2dx 2.x做的一些shader效果Demo

RT. 没兴趣更新了~ 
适合:
1:想初步学习shader的同学可以了解下。
2:想用cocos2dx做点效果的同学可以了解下。
请直接下载当前页面附件的rar包就可以了。

如果需要直接运行程序的可以到百度盘下载:http://pan.baidu.com/s/1jGoRCmM

1. [图片] 1.jpg    

2. [图片] 2.jpg    

3. [图片] 无标题.jpg    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//--------------------------------------------------------
// 模糊效果
static const GLchar* s_szBlurFSH =
    "                                                              
    precision mediump float;                                       
    uniform sampler2D u_Texture;                                   
    uniform vec2 u_TextureCoordOffset[25];                         
    varying vec2 v_texCoord;                                       
    varying vec4 v_fragmentColor;                                  
                                                                    
    void main(void)                                                
    {                                                              
        vec4 sample[25];                                               
                                                                        
        for (int i = 0; i < 25; i++)                                
        {                                                              
        sample[i] = texture2D(u_Texture,                               
        v_texCoord.st + u_TextureCoordOffset[i]);                      
        }                                                              
        //--------------------------------------------------------     
        //   1 3 1                                                     
        //   3 1 3   / 11                                              
        //   1 3 1                                                     
                                                                        
        //gl_FragColor = (sample[0] + (3.0*sample[1]) + sample[2] +    
        //  (3.0*sample[3]) + sample[4] + (3.0*sample[5]) +            
        //  sample[6] + (3.0*sample[7]) + sample[8]) / 11.0;           
        //--------------------------------------------------------     
        // Gaussian weighting:                                         
        // 1  4  7  4 1                                                
        // 4 16 26 16 4                                                
        // 7 26 41 26 7 / 273 (i.e. divide by total of weightings)     
        // 4 16 26 16 4                                                
        // 1  4  7  4 1                                                
                                                                        
        gl_FragColor = (                                               
        (1.0  * (sample[0] + sample[4]  + sample[20] + sample[24])) +  
        (4.0  * (sample[1] + sample[3]  + sample[5]  + sample[9] +     
            sample[15] + sample[19] + sample[21] + sample[23])) +      
        (7.0  * (sample[2] + sample[10] + sample[14] + sample[22])) +  
        (16.0 * (sample[6] + sample[8]  + sample[16] + sample[18])) +  
        (26.0 * (sample[7] + sample[11] + sample[13] + sample[17])) +  
        (41.0 * sample[12])                                            
        ) / 273.0;                                                     
    }";
//--------------------------------------------------------
// 磨砂
static const GLchar* s_szSharpenFSH =
    "                                                              
    precision mediump float;                                       
    uniform sampler2D u_Texture;                                   
    uniform vec2 u_TextureCoordOffset[25];                         
    varying vec2 v_texCoord;                                       
    varying vec4 v_fragmentColor;                                  
                                                                    
    void main(void)                                                
    {                                                              
        vec4 sample[25];                                           
                                                                    
        for (int i = 0; i < 25; i++)                            
        {                                                          
            sample[i] = texture2D(u_Texture,                       
                v_texCoord.st + u_TextureCoordOffset[i]);          
        }                                                          
        //-------------------------------------------------------- 
        //   -1 -1 -1                                              
        //   -1  9 -1                                              
        //   -1 -1 -1                                              
                                                                    
        //gl_FragColor = (sample[4] * 9.0) -                       
        //  (sample[0] + sample[1] + sample[2] +                   
        //  sample[3] + sample[5] +                                
        //  sample[6] + sample[7] + sample[8]);                    
        //-------------------------------------------------------- 
        // Sharpen weighting:                                      
        //  0 -1 -1 -1  0                                          
        // -1  2 -4  2 -1                                          
        // -1 -4 13 -4 -1                                          
        // -1  2 -4  2 -1                                          
        //  0 -1 -1 -1  0                                          
                                                                    
        //gl_FragColor = (                                         
        //(-1.0  * ( sample[1] + sample[2]  + sample[3] + sample[5] +  
        //sample[9] + sample[10] + sample[14] + sample[15] +           
        //sample[19] + sample[21] + sample[22] + sample[23]) ) +       
        //(2.0 * (sample[6] + sample[8] + sample[16] + sample[18])) +  
        //(-4.0 *(sample[7] + sample[11] + sample[13] + sample[17]))+  
        //( 13.0 * sample[12] )                                    
        //);                                                           
                                                                                        
        // 1  1  1  1  1                                                               
        // 1  1  1  1  1                                                               
        // 1  1 -14 1  1                                                               
        // 1  1  1  1  1                                                               
        // 1  1  1  1  1                                                               
                                                                                        
        gl_FragColor = -14.0 * sample[12];                                             
                                                                                        
        for (int i = 0; i < 25; i++)                                                
        {                                                                              
        if (i != 12)                                                                   
        gl_FragColor += sample[i];                                                     
        }                                                                              
        gl_FragColor /= 14.0;                                                          
    }";
//--------------------------------------------------------        
// 膨胀
static const GLchar* s_szDilateFSH =
    "                                                              
    precision mediump float;                                       
    uniform sampler2D u_Texture;                                   
    uniform vec2 u_TextureCoordOffset[25];                         
    varying vec2 v_texCoord;                                       
    varying vec4 v_fragmentColor;                                  
                                                                    
    void main(void)                                                
    {                                                              
    vec4 sample[25];                                               
    vec4 maxValue = vec4(0.0);                                                         
                                                                                        
    for (int i = 0; i < 25; i++)                                                    
    {                                                                                  
        // Sample a grid around and including our texel                                
        sample[i] = texture2D(u_Texture, v_texCoord.st + u_TextureCoordOffset[i]);     
        // Keep the maximum value                                                      
        maxValue = max(sample[i], maxValue);                                           
    }                                                                                  
                                                                                        
    gl_FragColor = maxValue;                                                           
    }";
//-------------------------------------------------------- 
// 侵蚀效果
static const GLchar* s_szErodeFSH =
    "                                                              
    precision mediump float;                                       
    uniform sampler2D u_Texture;                                   
    uniform vec2 u_TextureCoordOffset[25];                         
    varying vec2 v_texCoord;                                       
    varying vec4 v_fragmentColor;                                  
                                                                    
    void main(void)                                                
    {                                                              
    vec4 sample[25];                                                               
    vec4 minValue = vec4(1.0);                                                     
                                                                                    
    for (int i = 0; i < 25; i++)                                                
    {                                                                              
        // Sample a grid around and including our texel                            
        sample[i] = texture2D(u_Texture, v_texCoord.st + u_TextureCoordOffset[i]); 
        // Keep the minimum value                                                  
        minValue = min(sample[i], minValue);                                       
    }                                                                              
                                                                                    
    gl_FragColor = minValue;                                                       
    }";
//--------------------------------------------------------
// Laplacian描边效果
static const GLchar* s_szLaplacianEdgeDetectionFSH =
    "                                                              
    precision mediump float;                                       
    uniform sampler2D u_Texture;                                   
    uniform vec2 u_TextureCoordOffset[25];                         
    varying vec2 v_texCoord;                                       
    varying vec4 v_fragmentColor;                                  
                                                                    
    void main(void)                                                
    {                                                              
    vec4 sample[25];                                                               
                                                                                    
    for (int i = 0; i < 25; i++)                                                
    {                                                                              
        // Sample a grid around and including our texel                            
        sample[i] = texture2D(u_Texture, v_texCoord.st + u_TextureCoordOffset[i]); 
    }                                                                              
                                                                                    
    // Laplacian weighting:                                                        
    // -1 -1 -1 -1 -1                                                              
    // -1 -1 -1 -1 -1                                                              
    // -1 -1 24 -1 -1                                                              
    // -1 -1 -1 -1 -1                                                              
    // -1 -1 -1 -1 -1                                                              
                                                                                    
    gl_FragColor = 24.0 * sample[12];                                              
                                                                                    
    for (int i = 0; i < 25; i++)                                                
    {                                                                              
        if (i != 12)                                                               
            gl_FragColor -= sample[i];                                             
    }                                                                              
    }";
//--------------------------------------------------------
// Sobel边缘检测
static const GLchar* s_szSobelEdgeDetectionFSH =
    "                                                                  
    precision mediump float;                                           
    uniform sampler2D u_Texture;                                       
    uniform vec2 u_TextureCoordOffset[25];                             
    varying vec2 v_texCoord;                                           
    varying vec4 v_fragmentColor;                                      
                                                                        
    void main(void)                                                    
    {                                                                  
        vec4 sample[25];                                               
                                                                        
        for (int i = 0; i < 25; i++)                                
        {                                                              
        sample[i] = texture2D(u_Texture,                               
        v_texCoord.st + u_TextureCoordOffset[i]);                      
        }                                                              
        // Sobel x:                                                    
        // 1  2  0 -2 -1                                               
        // 4  8  0 -8 -4                                               
        // 6 12  0 -12-6    / 12                                       
        // 4  8  0 -8 -4                                               
        // 1  2  0 -2 -1                                               
        // Sobel y:                                                    
        // -1 -4 -6 -4 -1                                              
        // -2 -8 -12-8 -2                                              
        //  0  0  0  0  0   / 12                                       
        //  2  8 12  8  2                                              
        //  1  4  6  4  1                                              
                                                                        
        vec4 vertEdge = sample[0] + 4.0 * sample[1] +                  
        6.0 * sample[2] + 4.0 * sample[3] + sample[4] +                
        2.0 * sample[5] + 8.0 * sample[6] + 12.0 * sample[7] +         
        8.0 * sample[8] + 2.0 * sample[9] - 2.0 * sample[15] -         
        8.0 * sample[16] - 12.0 * sample[17] - 8.0 * sample[18] -      
        2.0 * sample[19] - sample[20] - 4.0 * sample[21] -             
        6.0 * sample[22] - 4.0 * sample[23] - sample[24];              
                                                                        
        vec4 horizEdge = - sample[0] - 2.0 * sample[1] +               
        2.0 * sample[3] + sample[4] - 4.0 * sample[5] -                
        8.0 * sample[6] + 8.0 * sample[8] + 4.0 * sample[9] -          
        6.0 * sample[10] - 12.0 * sample[11] + 12.0 * sample[13] +     
        6.0 * sample[14] - 4.0 * sample[15] - 8.0 * sample[16] +       
        8.0 * sample[18] + 4.0 * sample[19] - sample[20] -             
        2.0 * sample[21] + 2.0 * sample[23] + sample[24];              
                                                                        
        //gl_FragColor.rgb = sqrt(horizEdge.rgb) + sqrt(vertEdge.rgb); 
        gl_FragColor.rgb = sqrt((horizEdge.rgb * horizEdge.rgb) +      
            (vertEdge.rgb * vertEdge.rgb)) / 12.0f;                    
        gl_FragColor.a = 1.0;                                          
    }";
//--------------------------------------------------------
 
 

 http://www.oschina.net/code/snippet_1159242_38156

原文地址:https://www.cnblogs.com/cooka/p/4369449.html