2012-03-14 108 views
1

我想在中心切割上实现图像的平滑合并效果。我从下面的代码中取得了中心。Opengles片段着色器达到效果

varying highp vec2 textureCoordinate; 
uniform sampler2D videoFrame; 

void main(){ 
    vec4 CurrentColor = vec4(0.0); 

    if(textureCoordinate.y < 0.5){ 
     CurrentColor = texture2D(videoFrame,vec2(textureCoordinate.x,(textureCoordinate.y-0.125)));  
    } else{ 
     CurrentColor = texture2D(videoFrame,vec2(textureCoordinate.x,(textureCoordinate.y+0.125))); 
    } 

    gl_fragColor = CurrentColor;  
} 

上面的代码给出了下图的效果。

实际:

Actual

中心切:

Centre cut

所需的输出:

Desired output

我想是的不应该有锐利的切割,应该有平滑的渐变合并两半。

+0

请问您可以用photoshop来展示所需的结果吗? – datenwolf 2012-03-14 14:55:52

+0

@datenwolf这是所需的输出。 [链接](http://i.imgur.com/XRKUf.jpg) – Manish 2012-03-14 15:28:48

+0

datenwolf正在处理实际问题;我唯一的评论是,你可能会看到更好的表现,两个变化代表两个可能的采样坐标,而不是你动态修改的一个。反直觉地说,无条件地做两个样本然后选择一种颜色可能会更快(但是您需要配置文件 - 请参阅https://developer.apple.com/library/ios/documentation/3DDrawing/Conceptual/ OpenGLES_ProgrammingGuide/OpenGLES_ProgrammingGuide.pdf) – Tommy 2012-03-14 15:50:50

回答

2

你想要那里的实际模糊,或只是线性混合?因为模糊涉及模糊的内核,而混合将是这两者之间的简单插值,这取决于y坐标。

这是线性混合的代码。

varying highp vec2 textureCoordinate; 
uniform sampler2D videoFrame; 

void main(){ 
    float steepness = 20; /* controls the width of the blending zone, larger values => shaper gradient */ 
    vec4 a = texture2D(videoFrame,vec2(textureCoordinate.x,(textureCoordinate.y-0.125)));  
    vec4 b = texture2D(videoFrame,vec2(textureCoordinate.x,(textureCoordinate.y+0.125))); 

    /* EDIT: Added a clamp to the smoothstep parameter -- should not be neccessary though */ 
    vec4 final = smoothstep(a, b, clamp((y-0.5)*steepness, 0., 1.)); /* there's also mix instead of smoothstep, try both */ 

    gl_FragColor = final;  
} 

做一个实际的模糊有点复杂,因为你必须应用模糊内核。基本上它涉及两个嵌套循环,遍历相邻纹理元素并根据某种分布进行总结(最灵活的方式是通过额外的纹理提供该分布,这也允许增加一些散景)。

+0

嗨@datenwolf,上面并没有帮助我达到预期的效果。它做的是做整个像素的平滑步骤,但根据我想要的只是在中心。 – Manish 2012-03-15 09:47:43

+0

为了进一步说明我的需要,我想要做的是剪切图像的中心部分(比如说20像素),然后用平滑的渐变合并将其他2个半部分合并。希望这有助于你理解我的需求并进一步帮助我。 – Manish 2012-03-15 10:15:30

+0

@Manish:这就是参数陡度。根据需要增加该参数,值越大,插值区域越小。更好地将它作为参数进行调整。 – datenwolf 2012-03-15 10:24:19