2017-07-02 99 views
0

這可能是一個愚蠢的問題,但我堅持這一段時間,所以要問它無論如何。使用GPUImage實現自定義過濾器Swift庫

我想實現一個寵物項目Hudson/Nashville過濾器。我GOOGLE了一下,結帳了幾個開源項目,發現一些Objective-C(我不明白)基於項目。他們確實使用GPUImage2實施了過濾器,但我不確定他們的方法。

我有重疊和其他圖像,他們已經使用和GLSL文件。

所以我的問題是我怎麼去使用這個圖像和着色器文件來實現自定義過濾器?

注:我嘗試使用LookupFilter方法的建議,但結果不太好。如果你能給我看一些代碼,這將是非常有用的。由於

更新:

我試圖理解這一點。給定一個自定義着色器類似下面,我怎麼通過對輸入圖像進行uniform inputImageTexture2inputImageTexture3 & inputImageTexture4。我是否通過子類化將它作爲PictureInput轉換爲BasicOperation?如果是這樣,怎麼樣?我錯過了什麼?由於缺乏適當的文檔,我無法完成代碼。我已經閱讀了着色器及其不同的組件,但仍然無法找到在GPUImage2上使用自定義過濾器的方法。請幫忙。

precision highp float; 

varying highp vec2 textureCoordinate; 

uniform sampler2D inputImageTexture; 
uniform sampler2D inputImageTexture2; //blowout; 
uniform sampler2D inputImageTexture3; //overlay; 
uniform sampler2D inputImageTexture4; //map 

uniform float strength; 

void main() 
{ 
    vec4 originColor = texture2D(inputImageTexture, textureCoordinate); 

    vec4 texel = texture2D(inputImageTexture, textureCoordinate); 

    vec3 bbTexel = texture2D(inputImageTexture2, textureCoordinate).rgb; 

    texel.r = texture2D(inputImageTexture3, vec2(bbTexel.r, texel.r)).r; 
    texel.g = texture2D(inputImageTexture3, vec2(bbTexel.g, texel.g)).g; 
    texel.b = texture2D(inputImageTexture3, vec2(bbTexel.b, texel.b)).b; 

    vec4 mapped; 
    mapped.r = texture2D(inputImageTexture4, vec2(texel.r, .16666)).r; 
    mapped.g = texture2D(inputImageTexture4, vec2(texel.g, .5)).g; 
    mapped.b = texture2D(inputImageTexture4, vec2(texel.b, .83333)).b; 
    mapped.a = 1.0; 

    mapped.rgb = mix(originColor.rgb, mapped.rgb, strength); 

    gl_FragColor = mapped; 
} 

回答

3

的GPUImage慣例是第一輸入紋理着色器被稱爲inputTextureCoordinate,第二inputTextureCoordinate2,等等。在原始的GPUImage的Objective-C版本中,您可以手動對與着色器中輸入紋理數量匹配的過濾器類型進行子類化。

在Swift GPUImage 2中,我已經這樣做了,只需要使用BasicOperation類或子類,它會自動將紋理附加到着色器所需的輸入數量。您可以通過初始化BasicOperation和設置的輸入數量做到這一點:

let myOperation = BasicOperation(fragmentShaderFile:myFragmentShader, numberOfInputs:4) 

以上套numberOfInputs至4,符合上述着色器。通過將vertexShaderFile參數保留爲nil(缺省值),BasicOperation將選擇具有四個紋理輸入的合適的簡單頂點着色器。

所有你需要做的是設置你的投入像你這樣過濾任何其他,通過增加新的BasicOperation作爲圖像源的目標。您附加輸入的順序很重要,因爲這將從着色器中的第一個紋理開始,然後向下進行。

在大多數情況下,BasicOperation非常靈活原樣,這樣你就不需要子類。至多,您可能需要提供自定義頂點着色器,但上面的片段着色器代碼不需要。

相關問題