ios – 前两个片段着色器输出是不同的

ios – 前两个片段着色器输出是不同的,第1张

概述我目前正试图让这个散景着色器与GPU Image: http://blenderartists.org/forum/showthread.php?237488-GLSL-depth-of-field-with-bokeh-v2-4-(update)一起使用 这就是我现在所拥有的: precision mediump float;varying highp vec2 textureCoordin 我目前正试图让这个散景着色器与GPU Image: http://blenderartists.org/forum/showthread.php?237488-GLSL-depth-of-field-with-bokeh-v2-4-(update)一起使用

这就是我现在所拥有的:

precision mediump float;varying highp vec2 textureCoordinate;varying highp vec2 textureCoordinate2;uniform sampler2D inputimageTexture;uniform sampler2D inputimageTexture2;uniform float inputimageTextureWIDth;uniform float inputimageTextureHeight;#define PI 3.14159265float wIDth = inputimageTextureWIDth; //texture wIDthfloat height = inputimageTextureHeight; //texture heightvec2 texel = vec2(1.0/wIDth,1.0/height);//uniform variables from external scriptuniform float focalDepth;  //focal distance value in meters,but you may use autofocus option belowuniform float focalLength; //focal length in mmuniform float fstop; //f-stop valuebool showFocus = false; //show deBUG focus point and focal range (red = focal point,green = focal range)float znear = 0.1; //camera clipPing startfloat zfar = 5.0; //camera clipPing end//------------------------------------------//user variablesint samples = 3; //samples on the first ringint rings = 3; //ring countbool manualdof = false; //manual dof calculationfloat ndofstart = 1.0; //near dof blur startfloat ndofdist = 2.0; //near dof blur falloff distancefloat fdofstart = 1.0; //far dof blur startfloat fdofdist = 3.0; //far dof blur falloff distancefloat CoC = 0.03;//circle of confusion size in mm (35mm film = 0.03mm)bool vignetting = false; //use optical lens vignetting?float vignout = 1.3; //vignetting outer borderfloat vignin = 0.0; //vignetting inner borderfloat vignfade = 22.0; //f-stops till vignete fadesbool autofocus = false; //use autofocus in shader? disable if you use external focalDepth valuevec2 focus = vec2(0.5,0.5); // autofocus point on screen (0.0,0.0 - left lower corner,1.0,1.0 - upper right)float maxblur = 1.0; //clamp value of max blur (0.0 = no blur,1.0 default)float threshold = 0.5; //highlight threshold;float gain = 2.0; //highlight gain;float bias = 0.5; //bokeh edge biasfloat fringe = 0.7; //bokeh @R_756_4048@ aberration/fringingbool noise = false; //use noise instead of pattern for sample ditheringfloat namount = 0.0001; //dither amountbool depthblur = false; //blur the depth buffer?float dbsize = 1.25; //depthblursize/* next part is experimental not looking good with small sample and ring count looks okay starting from samples = 4,rings = 4 */bool pentagon = false; //use pentagon as bokeh shape?float feather = 0.4; //pentagon shape feather//------------------------------------------float penta(vec2 coords) //pentagonal shape{    float scale = float(rings) - 1.3;    vec4  HS0 = vec4( 1.0,0.0,1.0);    vec4  HS1 = vec4( 0.309016994,0.951056516,1.0);    vec4  HS2 = vec4(-0.809016994,0.587785252,1.0);    vec4  HS3 = vec4(-0.809016994,-0.587785252,1.0);    vec4  HS4 = vec4( 0.309016994,-0.951056516,1.0);    vec4  HS5 = vec4( 0.0,1.0);    vec4  one = vec4( 1.0 );    vec4 P = vec4((coords),vec2(scale,scale));    vec4 dist = vec4(0.0);    float inorout = -4.0;    dist.x = dot( P,HS0 );    dist.y = dot( P,HS1 );    dist.z = dot( P,HS2 );    dist.w = dot( P,HS3 );    dist = smoothstep( -feather,feather,dist );    inorout += dot( dist,one );    dist.x = dot( P,HS4 );    dist.y = HS5.w - abs( P.z );    dist = smoothstep( -feather,dist );    inorout += dist.x;    return clamp( inorout,1.0 );}float bdepth(vec2 coords) //blurring depth{    float d = 0.0;    float kernel[9];    vec2 offset[9];    vec2 wh = vec2(texel.x,texel.y) * dbsize;    offset[0] = vec2(-wh.x,-wh.y);    offset[1] = vec2( 0.0,-wh.y);    offset[2] = vec2( wh.x -wh.y);    offset[3] = vec2(-wh.x,0.0);    offset[4] = vec2( 0.0,0.0);    offset[5] = vec2( wh.x,0.0);    offset[6] = vec2(-wh.x,wh.y);    offset[7] = vec2( 0.0,wh.y);    offset[8] = vec2( wh.x,wh.y);    kernel[0] = 1.0/16.0;   kernel[1] = 2.0/16.0;   kernel[2] = 1.0/16.0;    kernel[3] = 2.0/16.0;   kernel[4] = 4.0/16.0;   kernel[5] = 2.0/16.0;    kernel[6] = 1.0/16.0;   kernel[7] = 2.0/16.0;   kernel[8] = 1.0/16.0;    for( int i=0; i<9; i++ )    {        float tmp = texture2D(inputimageTexture2,coords + offset[i]).r;        d += tmp * kernel[i];    }    return d;}vec3 color(vec2 coords,float blur) //processing the sample{    vec3 col = vec3(0.0);    col.r = texture2D(inputimageTexture,coords + vec2(0.0,1.0)*texel*fringe*blur).r;    col.g = texture2D(inputimageTexture,coords + vec2(-0.866,-0.5)*texel*fringe*blur).g;    col.b = texture2D(inputimageTexture,coords + vec2(0.866,-0.5)*texel*fringe*blur).b;    vec3 lumcoeff = vec3(0.299,0.587,0.114);    float lum = dot(col.rgb,lumcoeff);    float thresh = max((lum-threshold)*gain,0.0);    return col+mix(vec3(0.0),col,thresh*blur);}vec2 rand(vec2 coord) //generating noise/pattern texture for dithering{    float noiseX = ((fract(1.0-coord.s*(wIDth/2.0))*0.25)+(fract(coord.t*(height/2.0))*0.75))*2.0-1.0;    float noiseY = ((fract(1.0-coord.s*(wIDth/2.0))*0.75)+(fract(coord.t*(height/2.0))*0.25))*2.0-1.0;    if (noise)    {        noiseX = clamp(fract(sin(dot(coord,vec2(12.9898,78.233))) * 43758.5453),1.0)*2.0-1.0;        noiseY = clamp(fract(sin(dot(coord,78.233)*2.0)) * 43758.5453),1.0)*2.0-1.0;    }    return vec2(noiseX,noiseY);}vec3 deBUGFocus(vec3 col,float blur,float depth){    float edge = 0.002*depth; //distance based edge smoothing    float m = clamp(smoothstep(0.0,edge,blur),1.0);    float e = clamp(smoothstep(1.0-edge,1.0);    col = mix(col,vec3(1.0,0.0),(1.0-m)*0.6);    col = mix(col,vec3(0.0,1.0),((1.0-e)-(1.0-m))*0.2);    return col;}float linearize(float depth){    return -zfar * znear / (depth * (zfar - znear) - zfar);}float vignette(){    float dist = distance(textureCoordinate.xy,vec2(0.5,0.5));    dist = smoothstep(vignout+(fstop/vignfade),vignin+(fstop/vignfade),dist);    return clamp(dist,1.0);}voID main(){    //scene depth calculation    float depth = linearize(texture2D(inputimageTexture2,textureCoordinate2.xy).x);    if (depthblur)    {        depth = linearize(bdepth(textureCoordinate2.xy));    }    //focal plane calculation    float fDepth = focalDepth;    if (autofocus)    {        fDepth = linearize(texture2D(inputimageTexture2,focus).x);    }    //dof blur factor calculation    float blur = 0.0;    if (manualdof)    {        float a = depth-fDepth; //focal plane        float b = (a-fdofstart)/fdofdist; //far DoF        float c = (-a-ndofstart)/ndofdist; //near Dof        blur = (a>0.0)?b:c;    }    else    {        float f = focalLength; //focal length in mm        float d = fDepth*1000.0; //focal plane in mm        float o = depth*1000.0; //depth in mm        float a = (o*f)/(o-f);        float b = (d*f)/(d-f);        float c = (d-f)/(d*fstop*CoC);        blur = abs(a-b)*c;    }    blur = clamp(blur,1.0);    // calculation of pattern for ditering    vec2 noise = rand(textureCoordinate.xy)*namount*blur;    // getting blur x and y step factor    float w = (1.0/wIDth)*blur*maxblur+noise.x;    float h = (1.0/height)*blur*maxblur+noise.y;    // calculation of final color    vec3 col = vec3(0.0);    if(blur < 0.05) //some optimization thingy    {        col = texture2D(inputimageTexture,textureCoordinate.xy).rgb;    }    else    {        col = texture2D(inputimageTexture,textureCoordinate.xy).rgb;        float s = 1.0;        int ringsamples;        for (int i = 1; i <= rings; i += 1)        {            ringsamples = i * samples;            for (int j = 0 ; j < ringsamples ; j += 1)            {                float step = PI*2.0 / float(ringsamples);                float pw = (cos(float(j)*step)*float(i));                float ph = (sin(float(j)*step)*float(i));                float p = 1.0;                if (pentagon)                {                    p = penta(vec2(pw,ph));                }                col += color(textureCoordinate.xy + vec2(pw*w,ph*h),blur)*mix(1.0,(float(i))/(float(rings)),bias)*p;                s += 1.0*mix(1.0,bias)*p;            }        }        col /= s; //divIDe by sample count    }    if (showFocus)    {        col = deBUGFocus(col,blur,depth);    }    if (vignetting)    {        col *= vignette();    }    gl_Fragcolor.rgb = col;    gl_Fragcolor.a = 1.0;}

这是我的散景过滤器,GPUImageTwoinputFilter的子类:

@implementation GPUImagebokehFilter- (ID)init;{    Nsstring *fragmentShaderPathname = [[NSBundle mainBundle] pathForResource:@"bokehShader" ofType:@"fsh"];    Nsstring *fragmentShaderString = [Nsstring stringWithContentsOffile:fragmentShaderPathname enCoding:NSUTF8StringEnCoding error:nil];    if (!(self = [super initWithFragmentShaderFromString:fragmentShaderString]))    {        return nil;    }    focalDepthUniform = [filterProgram uniformIndex:@"focalDepth"];    focalLengthUniform = [filterProgram uniformIndex:@"focalLength"];    fStopUniform = [filterProgram uniformIndex:@"fstop"];    [self setFocalDepth:1.0];    [self setFocalLength:35.0];    [self setFStop:2.2];    return self;}#pragma mark -#pragma mark Accessors- (voID)setFocalDepth:(float)focalDepth {    _focalDepth = focalDepth;    [self setfloat:_focalDepth forUniform:focalDepthUniform program:filterProgram];}- (voID)setFocalLength:(float)focalLength {    _focalLength = focalLength;    [self setfloat:_focalLength forUniform:focalLengthUniform program:filterProgram];}- (voID)setFStop:(CGfloat)fStop {    _fStop = fStop;    [self setfloat:_fStop forUniform:fStopUniform program:filterProgram];}@end

最后,这是我使用所述过滤器的方式:

@implementation VIEwController {    GPUImagebokehFilter *bokehFilter;    GPUImagePicture *bokehMap;    UIImage *inputimage;}- (voID)vIEwDIDLoad{    [super vIEwDIDLoad];    inputimage = [UIImage imagenamed:@"stones"];    bokehMap = [[GPUImagePicture alloc] initWithImage:[UIImage imagenamed:@"bokehmask"]];    _backgroundImage.image = inputimage;    bokehFilter = [[GPUImagebokehFilter alloc] init];    [self processImage];}- (IBAction)datainputUpdated:(ID)sender {    [self processImage];}- (voID *)processImage {    dispatch_async(dispatch_get_global_queue( disPATCH_QUEUE_PRIORITY_DEFAulT,0),^{        GPUImagePicture *gpuPicture = [[GPUImagePicture alloc] initWithImage:inputimage];        [gpuPicture addTarget:bokehFilter];        [gpuPicture processImage];        [bokehMap addTarget:bokehFilter];        [bokehMap processImage];        [bokehFilter useNextFrameForImageCapture];        [bokehFilter setfloat:inputimage.size.wIDth forUniformname:@"inputimageTextureWIDth"];        [bokehFilter setfloat:inputimage.size.height forUniformname:@"inputimageTextureHeight"];        UIImage *blurredImage = [bokehFilter imageFromCurrentFramebuffer];        dispatch_async(dispatch_get_main_queue(),^{            [self displayNewImage:blurredImage];        });    });}- (voID)displayNewImage:(UIImage*)newImage {    [UIVIEw TransitionWithVIEw:_backgroundImage                      duration:.6f                       options:UIVIEwAnimationoptionTransitionCrossdissolve                    animations:^{                        _backgroundImage.image = newImage;                    } completion:nil];}...

第一个图像是我试图模糊的图像,第二个图像是一个随机渐变来测试着色器的深度图:

当我在iPhone上启动应用程序时,我得到了这个:

移动滑块(触发datainputChanged方法)后,我得到:

虽然这看起来比第一张图片要好得多,但我仍有一些问题:

>有一条对角线的嘈杂线(在我放在图片上的红线内)似乎没有模糊.
>图像的左上角是模糊的,即使它不应该是模糊的.

为什么我会得到这种奇怪的行为?着色器输出不应该每次都相同吗?
另外,我如何让它尊重深度图?我的GLSL着色器知识非常有限,所以请耐心等待.

解决方法 对角线伪影似乎是由您的测试渐变引起的.您可以看到它出现在与渐变变为完全白色的位置大致相同的位置.尝试展开渐变,使其在图像的角落处仅达到1.0或0.0. 总结

以上是内存溢出为你收集整理的ios – 前两个片段着色器输出是不同的全部内容,希望文章能够帮你解决ios – 前两个片段着色器输出是不同的所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1101651.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存