你的位置:首页 > 操作系统

[操作系统]iOS滤镜实现之Nashville【instagram】


Nashville是Instagram众多滤镜中最惊艳的一款,独特的奶昔色调赋予照片童话般的唯美感觉。
适用范围:营造浪漫唯美的感觉。
的确如此啊
有2张输入图像
顶点着色
有2组坐标
NSString *const kGPUImageTwoInputTextureVertexShaderString = SHADER_STRING
( attribute vec4 position;
//输入的坐标 attribute vec4 inputTextureCoordinate; attribute vec4 inputTextureCoordinate2; //输出的坐标,与片段着色器 varying vec2 textureCoordinate; varying vec2 textureCoordinate2; void main() {
//告诉片段着色器,我在处理哪些像素 gl_Position = position; textureCoordinate = inputTextureCoordinate.xy; textureCoordinate2 = inputTextureCoordinate2.xy; });

片段着色
NSString *const kFWNashvilleShaderString = SHADER_STRING( precision lowp float; varying highp vec2 textureCoordinate; uniform sampler2D inputImageTexture; uniform sampler2D inputImageTexture2; void main() {   vec3 texel = texture2D(inputImageTexture, textureCoordinate).rgb;   texel = vec3(         texture2D(inputImageTexture2, vec2(texel.r, .16666)).r,         texture2D(inputImageTexture2, vec2(texel.g, .5)).g,         texture2D(inputImageTexture2, vec2(texel.b, .83333)).b);   gl_FragColor = vec4(texel, 1.0); } );

 

 初始化
- (id)init{  if (!(self = [super init]))  {    return nil;  }    UIImage *image = [UIImage imageNamed:@"nashvilleMap.png"];    imageSource = [[GPUImagePicture alloc] initWithImage:image];  FWFilter1 *filter = [[FWFilter1 alloc] init];    [self addFilter:filter];  [imageSource addTarget:filter atTextureLocation:1];  [imageSource processImage];    self.initialFilters = [NSArray arrayWithObjects:filter, nil];  self.terminalFilter = filter;    return self;}

//// FWNashvilleFilter.h// FWMeituApp//// Created by hzkmn on 16/1/8.// Copyright © 2016年 ForrestWoo co,.ltd. All rights reserved.//#import "GPUImageTwoInputFilter.h"@interface FWFilter1 : GPUImageTwoInputFilter@end@interface FWNashvilleFilter : GPUImageFilterGroup{  GPUImagePicture *imageSource ;}@end

 

//// FWNashvilleFilter.m// FWMeituApp//// Created by hzkmn on 16/1/8.// Copyright © 2016年 ForrestWoo co,.ltd. All rights reserved.//#import "FWNashvilleFilter.h"NSString *const kFWNashvilleShaderString = SHADER_STRING( precision lowp float; varying highp vec2 textureCoordinate; uniform sampler2D inputImageTexture; uniform sampler2D inputImageTexture2; void main() {   vec3 texel = texture2D(inputImageTexture, textureCoordinate).rgb;   texel = vec3(         texture2D(inputImageTexture2, vec2(texel.r, .16666)).r,         texture2D(inputImageTexture2, vec2(texel.g, .5)).g,         texture2D(inputImageTexture2, vec2(texel.b, .83333)).b);   gl_FragColor = vec4(texel, 1.0); } );@implementation FWFilter1- (id)init;{  if (!(self = [super initWithFragmentShaderFromString:kFWNashvilleShaderString]))  {    return nil;  }    return self;}@end@implementation FWNashvilleFilter- (id)init{  if (!(self = [super init]))  {    return nil;  }    UIImage *image = [UIImage imageNamed:@"nashvilleMap.png"];    imageSource = [[GPUImagePicture alloc] initWithImage:image];  FWFilter1 *filter = [[FWFilter1 alloc] init];    [self addFilter:filter];  [imageSource addTarget:filter atTextureLocation:1];  [imageSource processImage];    self.initialFilters = [NSArray arrayWithObjects:filter, nil];  self.terminalFilter = filter;    return self;}@end

 

原图

效果图

完整项目代码查看我在github

 

 我们再来看几组效果