jsonwang преди 3 години
родител
ревизия
dc1eff7b95
променени са 1 файла, в които са добавени 62 реда и са изтрити 0 реда
  1. 62 0
      BFFramework/Classes/PQGPUImage/akfilters/PQCornerFilter.swift

+ 62 - 0
BFFramework/Classes/PQGPUImage/akfilters/PQCornerFilter.swift

@@ -0,0 +1,62 @@
+//
+//  PQCornerFilter.swift
+//  BFFramework
+//
+//  Created by ak on 2021/12/2.
+//  功能:实现圆角
+
+import Foundation
+ /*
+ lowp vec4 sourceImageColor = texture2D(inputImageTexture, textureCoordinate);
+
+ lowp float d = distance(textureCoordinate, vec2(vignetteCenter.x, vignetteCenter.y));
+ lowp float percent = smoothstep(vignetteStart, vignetteEnd, d);
+ lowp vec4 outcolor = vec4(mix(sourceImageColor.rgb, vignetteColor, percent * 1.0), sourceImageColor.a);
+ 
+ lowp float radius = 100.0;
+ 
+ lowp float wh_ratio = 0.5;
+ lowp float circle = radius * radius;
+ lowp float rx = vignetteCenter.x * wh_ratio;
+ lowp float ry = vignetteCenter.y;
+ lowp float dis = (textureCoordinate.x * wh_ratio - rx) * (textureCoordinate.x * wh_ratio - rx) + (textureCoordinate.y  - ry) * (textureCoordinate.y - ry);
+ outcolor.a = smoothstep(circle, circle - 100.0, dis);
+//    outcolor.a = step(dis, 0.1);
+ gl_FragColor = outcolor;
+ */
+
+let cornerFragmentShader = "varying highp vec2 textureCoordinate;\n \n uniform sampler2D inputImageTexture;\n \n void main()\n {\n   lowp vec4 outcolor = texture2D(inputImageTexture, textureCoordinate);\n  outcolor.a = step(length(textureCoordinate - vec2(0.5,0.5)), 0.5);\n gl_FragColor = outcolor;\n }\n "
+
+//outcolor.a = smoothstep(circle, circle - 0.5, dis);\n
+//let cornerFragmentShader = "varying highp vec2 textureCoordinate;\n \n uniform sampler2D inputImageTexture;\n \n void main()\n {\n   lowp vec4 outcolor = texture2D(inputImageTexture, textureCoordinate);\n   lowp float radius = 100.0;\n  lowp float wh_ratio = 0.5;\n  lowp float circle = radius * radius;\n  lowp float rx = 0.5;\n  lowp float ry = 0.5;\n  lowp float dis = (textureCoordinate.x * wh_ratio - rx) * (textureCoordinate.x * wh_ratio - rx) + (textureCoordinate.y  - ry) * (textureCoordinate.y - ry);\n outcolor.a = step(dis, 0.5);\n  gl_FragColor = outcolor;\n }\n "
+ 
+open class PQCornerFilter: BasicOperation {
+ 
+    public init() {
+        super.init(fragmentShader: cornerFragmentShader, numberOfInputs: 1)
+    
+    }
+    override open func renderFrame() {
+        let inputFramebuffer: Framebuffer = inputFramebuffers[0]!
+        let inputSize = inputFramebuffer.sizeForTargetOrientation(.portrait)
+    
+        renderFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation: .portrait, size:inputSize, stencil: false)
+         
+        // 设置融合模式支持 alpha
+        glEnable(GLenum(GL_DEPTH_TEST))
+        glEnable(GLenum(GL_BLEND))
+        glBlendFunc(GLenum(GL_SRC_ALPHA), GLenum(GL_ONE_MINUS_SRC_ALPHA))
+         
+        
+        let textureProperties = InputTextureProperties(textureCoordinates:inputFramebuffer.orientation.rotationNeededForOrientation(.portrait).textureCoordinates(), texture: inputFramebuffer.texture)
+         
+        renderFramebuffer.activateFramebufferForRendering()
+        clearFramebufferWithColor(Color.transparent)
+        
+        renderQuadWithShader(shader, uniformSettings: uniformSettings, vertexBufferObject: sharedImageProcessingContext.standardImageVBO, inputTextures: [textureProperties])
+        releaseIncomingFramebuffers()
+        
+  
+    }
+ 
+}