本篇文章主要介绍了"GPUImage滤镜中的shader代码分析,及自定义滤镜",主要涉及到滤镜方面的内容,对于IOS开发感兴趣的同学可以参考一下:
http://m.blog.csdn.net/blog/vegerjiangsir_11109/27172143#GPUImage由于使用GPU,顾其在滤镜染色...
http://m.blog.csdn.net/blog/vegerjiangsir_11109/27172143#
GPUImage由于使用GPU,顾其在滤镜染色的时候真正使用的是Open GL的shader语言。下面我们就GPUImagePinchDistortionFilter分析这些shader语言。
GPUImagePinchDistortionFilter是一个实现收缩失真,凹面镜效果的滤镜,头文件代码如下:
#import "GPUImageFilter.h"
/** Creates a pinch distortion of the image
*/
@interface GPUImagePinchDistortionFilter : GPUImageFilter
{
GLint aspectRatioUniform, radiusUniform, centerUniform, scaleUniform;
}
/** The center about which to apply the distortion, with a default of (0.5, 0.5)
*/
@property(readwrite, nonatomic) CGPoint center;
/** The radius of the distortion, ranging from 0.0 to 2.0, with a default of 1.0
*/
@property(readwrite, nonatomic) CGFloat radius;
/** The amount of distortion to apply, from -2.0 to 2.0, with a default of 0.5
*/
@property(readwrite, nonatomic) CGFloat scale;
@end
我们来分析一下。
首先,这个类必须继承于GPUImageFilter(这个必须,要不然就没法用)。
其次,这个类有三个参数:center表示效果所在圆的中心点,radius表示所在圆的半径(注意范围是-2.0到2.0),scale表示缩放倍数(当scale小于0的时候,中间缩小,周围放大;当scale大于0的时候,中间放大,周围缩小)。
最后,我们需要关注一个单位(这个很重要,要不然后面的shader代码就很迷茫了)。细心的朋友肯定会发现圆半径的范围是-2.0到2.0。很显然,这不是像素单位,要不然这了滤镜效果范围也太小了,和没有效果一样)。事实上,在shader语言中,半径是真实半径除以图片矩形的宽(横着图片的为高),记realRadio/targetWidth(横着的图片为realRadio/targetHeight);
源文件中,我们重点分析shader语句kGPUImagePinchDistortionFragmentShaderString。在这里,我直接加注释。