美文网首页
RealityKit Postprocess Filter

RealityKit Postprocess Filter

作者: Xtuphe | 来源:发表于2022-11-16 10:48 被阅读0次
    滤镜效果

    引入CoreImage

    import CoreImage
    import CoreImage.CIFilterBuiltins
    

    确保输出材质采用设备支持的像素格式

    extension RealityKit.ARView.PostProcessContext {
        /// Returns the output texture, ensuring that the pixel format is appropriate for the current device's GPU.
        var compatibleTargetTexture: MTLTexture! {
            if self.device.supportsFamily(.apple2) {
                return targetColorTexture
            } else {
                return targetColorTexture.makeTextureView(pixelFormat: .bgra8Unorm)!
            }
        }
    }
    

    声明一个CIContext

    var ciContext: CIContext!
    

    注册回调函数

        func setupPostprocess() {
            arView.renderCallbacks.prepareWithDevice = setupCoreImage(device:)
            arView.renderCallbacks.postProcess = postProcessWithCoreImage(context:)
        }
    

    RealityKit加载完成在渲染第一帧前会调用prepareWithDevice一次, 可在此做耗时操作

        func setupCoreImage(device: MTLDevice) {
            // Create a CIContext and store it in a property.
            ciContext = CIContext(mtlDevice: device)
    
            // Do other expensive tasks, like loading images, here.
        }
    

    展示到屏幕之前每帧都会调用, 拒绝耗时操作

        func postProcessWithCoreImage(context: ARView.PostProcessContext) {
    
            // Create and configure the Core Image filter.
            let filter = CIFilter.gammaAdjust()
            filter.power = 4
    
            // Convert the frame buffer from a Metal texture to a CIImage, and
            // set the CIImage as the filter's input image.
            guard let input = CIImage(mtlTexture: context.sourceColorTexture) else {
                fatalError("Unable to create a CIImage from sourceColorTexture.")
            }
            filter.setValue(input, forKey: kCIInputImageKey)
    
            // Get a reference to the filter's output image.
            guard let output = filter.outputImage else {
                fatalError("Error applying filter.")
            }
    
            // Create a render destination and render the filter to the context's command buffer.
            let destination = CIRenderDestination(mtlTexture: context.compatibleTargetTexture,
                                                  commandBuffer: context.commandBuffer)
            destination.isFlipped = false
            _ = try? self.ciContext.startTask(toRender: output, to: destination)
        }
    

    苹果提供了十分多的filter

    @available(iOS 13.0, *)
    extension CIFilter {
    
        
        // CICategoryGradient
        open class func gaussianGradient() -> CIFilter & CIGaussianGradient
    
        open class func hueSaturationValueGradient() -> CIFilter & CIHueSaturationValueGradient
    
        open class func linearGradient() -> CIFilter & CILinearGradient
    
        open class func radialGradient() -> CIFilter & CIRadialGradient
    
        open class func smoothLinearGradient() -> CIFilter & CISmoothLinearGradient
    
        
        // CICategorySharpen
        open class func sharpenLuminance() -> CIFilter & CISharpenLuminance
    
        open class func unsharpMask() -> CIFilter & CIUnsharpMask
    
        
        // CICategoryHalftoneEffect
        open class func circularScreen() -> CIFilter & CICircularScreen
    
        open class func cmykHalftone() -> CIFilter & CICMYKHalftone
    
        open class func dotScreen() -> CIFilter & CIDotScreen
    
        open class func hatchedScreen() -> CIFilter & CIHatchedScreen
    
        open class func lineScreen() -> CIFilter & CILineScreen
    
        
        // CICategoryGeometryAdjustment
        open class func bicubicScaleTransform() -> CIFilter & CIBicubicScaleTransform
    
        open class func edgePreserveUpsample() -> CIFilter & CIEdgePreserveUpsample
    
        open class func keystoneCorrectionCombined() -> CIFilter & CIKeystoneCorrectionCombined
    
        open class func keystoneCorrectionHorizontal() -> CIFilter & CIKeystoneCorrectionHorizontal
    
        open class func keystoneCorrectionVertical() -> CIFilter & CIKeystoneCorrectionVertical
    
        open class func lanczosScaleTransform() -> CIFilter & CILanczosScaleTransform
    
        open class func perspectiveCorrection() -> CIFilter & CIPerspectiveCorrection
    
        open class func perspectiveRotate() -> CIFilter & CIPerspectiveRotate
    
        open class func perspectiveTransform() -> CIFilter & CIPerspectiveTransform
    
        open class func perspectiveTransformWithExtent() -> CIFilter & CIPerspectiveTransformWithExtent
    
        open class func straighten() -> CIFilter & CIStraighten
    
        
        // CICategoryTransition
        open class func accordionFoldTransition() -> CIFilter & CIAccordionFoldTransition
    
        open class func barsSwipeTransition() -> CIFilter & CIBarsSwipeTransition
    
        open class func copyMachineTransition() -> CIFilter & CICopyMachineTransition
    
        open class func disintegrateWithMaskTransition() -> CIFilter & CIDisintegrateWithMaskTransition
    
        open class func dissolveTransition() -> CIFilter & CIDissolveTransition
    
        open class func flashTransition() -> CIFilter & CIFlashTransition
    
        open class func modTransition() -> CIFilter & CIModTransition
    
        open class func pageCurlTransition() -> CIFilter & CIPageCurlTransition
    
        open class func pageCurlWithShadowTransition() -> CIFilter & CIPageCurlWithShadowTransition
    
        open class func rippleTransition() -> CIFilter & CIRippleTransition
    
        open class func swipeTransition() -> CIFilter & CISwipeTransition
    
        
        // CICategoryCompositeOperation
        open class func additionCompositing() -> CIFilter & CICompositeOperation
    
        open class func colorBlendMode() -> CIFilter & CICompositeOperation
    
        open class func colorBurnBlendMode() -> CIFilter & CICompositeOperation
    
        open class func colorDodgeBlendMode() -> CIFilter & CICompositeOperation
    
        open class func darkenBlendMode() -> CIFilter & CICompositeOperation
    
        open class func differenceBlendMode() -> CIFilter & CICompositeOperation
    
        open class func divideBlendMode() -> CIFilter & CICompositeOperation
    
        open class func exclusionBlendMode() -> CIFilter & CICompositeOperation
    
        open class func hardLightBlendMode() -> CIFilter & CICompositeOperation
    
        open class func hueBlendMode() -> CIFilter & CICompositeOperation
    
        open class func lightenBlendMode() -> CIFilter & CICompositeOperation
    
        open class func linearBurnBlendMode() -> CIFilter & CICompositeOperation
    
        open class func linearDodgeBlendMode() -> CIFilter & CICompositeOperation
    
        @available(iOS 15.0, *)
        open class func linearLightBlendMode() -> CIFilter & CICompositeOperation
    
        open class func luminosityBlendMode() -> CIFilter & CICompositeOperation
    
        open class func maximumCompositing() -> CIFilter & CICompositeOperation
    
        open class func minimumCompositing() -> CIFilter & CICompositeOperation
    
        open class func multiplyBlendMode() -> CIFilter & CICompositeOperation
    
        open class func multiplyCompositing() -> CIFilter & CICompositeOperation
    
        open class func overlayBlendMode() -> CIFilter & CICompositeOperation
    
        open class func pinLightBlendMode() -> CIFilter & CICompositeOperation
    
        open class func saturationBlendMode() -> CIFilter & CICompositeOperation
    
        open class func screenBlendMode() -> CIFilter & CICompositeOperation
    
        open class func softLightBlendMode() -> CIFilter & CICompositeOperation
    
        open class func sourceAtopCompositing() -> CIFilter & CICompositeOperation
    
        open class func sourceInCompositing() -> CIFilter & CICompositeOperation
    
        open class func sourceOutCompositing() -> CIFilter & CICompositeOperation
    
        open class func sourceOverCompositing() -> CIFilter & CICompositeOperation
    
        open class func subtractBlendMode() -> CIFilter & CICompositeOperation
    
        @available(iOS 15.0, *)
        open class func vividLightBlendMode() -> CIFilter & CICompositeOperation
    
        
        // CICategoryColorAdjustment
        @available(iOS 14.0, *)
        open class func colorAbsoluteDifference() -> CIFilter & CIColorAbsoluteDifference
    
        open class func colorClamp() -> CIFilter & CIColorClamp
    
        open class func colorControls() -> CIFilter & CIColorControls
    
        open class func colorMatrix() -> CIFilter & CIColorMatrix
    
        open class func colorPolynomial() -> CIFilter & CIColorPolynomial
    
        @available(iOS 14.0, *)
        open class func colorThreshold() -> CIFilter & CIColorThreshold
    
        @available(iOS 14.0, *)
        open class func colorThresholdOtsu() -> CIFilter & CIColorThresholdOtsu
    
        open class func depthToDisparity() -> CIFilter & CIDepthToDisparity
    
        open class func disparityToDepth() -> CIFilter & CIDisparityToDepth
    
        open class func exposureAdjust() -> CIFilter & CIExposureAdjust
    
        open class func gammaAdjust() -> CIFilter & CIGammaAdjust
    
        open class func hueAdjust() -> CIFilter & CIHueAdjust
    
        open class func linearToSRGBToneCurve() -> CIFilter & CILinearToSRGBToneCurve
    
        open class func sRGBToneCurveToLinear() -> CIFilter & CISRGBToneCurveToLinear
    
        open class func temperatureAndTint() -> CIFilter & CITemperatureAndTint
    
        open class func toneCurve() -> CIFilter & CIToneCurve
    
        open class func vibrance() -> CIFilter & CIVibrance
    
        open class func whitePointAdjust() -> CIFilter & CIWhitePointAdjust
    
        
        // CICategoryColorEffect
        open class func colorCrossPolynomial() -> CIFilter & CIColorCrossPolynomial
    
        open class func colorCube() -> CIFilter & CIColorCube
    
        open class func colorCubesMixedWithMask() -> CIFilter & CIColorCubesMixedWithMask
    
        open class func colorCubeWithColorSpace() -> CIFilter & CIColorCubeWithColorSpace
    
        open class func colorCurves() -> CIFilter & CIColorCurves
    
        open class func colorInvert() -> CIFilter & CIColorInvert
    
        open class func colorMap() -> CIFilter & CIColorMap
    
        open class func colorMonochrome() -> CIFilter & CIColorMonochrome
    
        open class func colorPosterize() -> CIFilter & CIColorPosterize
    
        @available(iOS 16.0, *)
        open class func convertLabToRGB() -> CIFilter & CIConvertLab
    
        @available(iOS 16.0, *)
        open class func convertRGBtoLab() -> CIFilter & CIConvertLab
    
        open class func dither() -> CIFilter & CIDither
    
        open class func documentEnhancer() -> CIFilter & CIDocumentEnhancer
    
        open class func falseColor() -> CIFilter & CIFalseColor
    
        open class func labDeltaE() -> CIFilter & CILabDeltaE
    
        open class func maskToAlpha() -> CIFilter & CIMaskToAlpha
    
        open class func maximumComponent() -> CIFilter & CIMaximumComponent
    
        open class func minimumComponent() -> CIFilter & CIMinimumComponent
    
        open class func paletteCentroid() -> CIFilter & CIPaletteCentroid
    
        open class func palettize() -> CIFilter & CIPalettize
    
        open class func photoEffectChrome() -> CIFilter & CIPhotoEffect
    
        open class func photoEffectFade() -> CIFilter & CIPhotoEffect
    
        open class func photoEffectInstant() -> CIFilter & CIPhotoEffect
    
        open class func photoEffectMono() -> CIFilter & CIPhotoEffect
    
        open class func photoEffectNoir() -> CIFilter & CIPhotoEffect
    
        open class func photoEffectProcess() -> CIFilter & CIPhotoEffect
    
        open class func photoEffectTonal() -> CIFilter & CIPhotoEffect
    
        open class func photoEffectTransfer() -> CIFilter & CIPhotoEffect
    
        open class func sepiaTone() -> CIFilter & CISepiaTone
    
        open class func thermal() -> CIFilter & CIThermal
    
        open class func vignette() -> CIFilter & CIVignette
    
        open class func vignetteEffect() -> CIFilter & CIVignetteEffect
    
        open class func xRay() -> CIFilter & CIXRay
    
        
        // CICategoryDistortionEffect
        @available(iOS 14.0, *)
        open class func bumpDistortion() -> CIFilter & CIBumpDistortion
    
        @available(iOS 14.0, *)
        open class func bumpDistortionLinear() -> CIFilter & CIBumpDistortionLinear
    
        @available(iOS 14.0, *)
        open class func circleSplashDistortion() -> CIFilter & CICircleSplashDistortion
    
        @available(iOS 14.0, *)
        open class func circularWrap() -> CIFilter & CICircularWrap
    
        @available(iOS 14.0, *)
        open class func displacementDistortion() -> CIFilter & CIDisplacementDistortion
    
        @available(iOS 14.0, *)
        open class func droste() -> CIFilter & CIDroste
    
        @available(iOS 14.0, *)
        open class func glassDistortion() -> CIFilter & CIGlassDistortion
    
        @available(iOS 14.0, *)
        open class func glassLozenge() -> CIFilter & CIGlassLozenge
    
        @available(iOS 14.0, *)
        open class func holeDistortion() -> CIFilter & CIHoleDistortion
    
        @available(iOS 14.0, *)
        open class func lightTunnel() -> CIFilter & CILightTunnel
    
        @available(iOS 14.0, *)
        open class func ninePartStretched() -> CIFilter & CINinePartStretched
    
        @available(iOS 14.0, *)
        open class func ninePartTiled() -> CIFilter & CINinePartTiled
    
        @available(iOS 14.0, *)
        open class func pinchDistortion() -> CIFilter & CIPinchDistortion
    
        @available(iOS 14.0, *)
        open class func stretchCrop() -> CIFilter & CIStretchCrop
    
        @available(iOS 14.0, *)
        open class func torusLensDistortion() -> CIFilter & CITorusLensDistortion
    
        @available(iOS 14.0, *)
        open class func twirlDistortion() -> CIFilter & CITwirlDistortion
    
        @available(iOS 14.0, *)
        open class func vortexDistortion() -> CIFilter & CIVortexDistortion
    
        
        // CICategoryTileEffect
        open class func affineClamp() -> CIFilter & CIAffineClamp
    
        open class func affineTile() -> CIFilter & CIAffineTile
    
        open class func eightfoldReflectedTile() -> CIFilter & CIEightfoldReflectedTile
    
        open class func fourfoldReflectedTile() -> CIFilter & CIFourfoldReflectedTile
    
        open class func fourfoldRotatedTile() -> CIFilter & CIFourfoldRotatedTile
    
        open class func fourfoldTranslatedTile() -> CIFilter & CIFourfoldTranslatedTile
    
        open class func glideReflectedTile() -> CIFilter & CIGlideReflectedTile
    
        open class func kaleidoscope() -> CIFilter & CIKaleidoscope
    
        open class func opTile() -> CIFilter & CIOpTile
    
        open class func parallelogramTile() -> CIFilter & CIParallelogramTile
    
        open class func perspectiveTile() -> CIFilter & CIPerspectiveTile
    
        open class func sixfoldReflectedTile() -> CIFilter & CISixfoldReflectedTile
    
        open class func sixfoldRotatedTile() -> CIFilter & CISixfoldRotatedTile
    
        open class func triangleKaleidoscope() -> CIFilter & CITriangleKaleidoscope
    
        open class func triangleTile() -> CIFilter & CITriangleTile
    
        open class func twelvefoldReflectedTile() -> CIFilter & CITwelvefoldReflectedTile
    
        
        // CICategoryGenerator
        open class func attributedTextImageGenerator() -> CIFilter & CIAttributedTextImageGenerator
    
        open class func aztecCodeGenerator() -> CIFilter & CIAztecCodeGenerator
    
        open class func barcodeGenerator() -> CIFilter & CIBarcodeGenerator
    
        open class func checkerboardGenerator() -> CIFilter & CICheckerboardGenerator
    
        open class func code128BarcodeGenerator() -> CIFilter & CICode128BarcodeGenerator
    
        open class func lenticularHaloGenerator() -> CIFilter & CILenticularHaloGenerator
    
        open class func meshGenerator() -> CIFilter & CIMeshGenerator
    
        open class func pdf417BarcodeGenerator() -> CIFilter & CIPDF417BarcodeGenerator
    
        open class func qrCodeGenerator() -> CIFilter & CIQRCodeGenerator
    
        open class func randomGenerator() -> CIFilter & CIRandomGenerator
    
        open class func roundedRectangleGenerator() -> CIFilter & CIRoundedRectangleGenerator
    
        open class func starShineGenerator() -> CIFilter & CIStarShineGenerator
    
        open class func stripesGenerator() -> CIFilter & CIStripesGenerator
    
        open class func sunbeamsGenerator() -> CIFilter & CISunbeamsGenerator
    
        open class func textImageGenerator() -> CIFilter & CITextImageGenerator
    
        
        // CICategoryStylize
        open class func blendWithAlphaMask() -> CIFilter & CIBlendWithMask
    
        open class func blendWithBlueMask() -> CIFilter & CIBlendWithMask
    
        open class func blendWithMask() -> CIFilter & CIBlendWithMask
    
        open class func blendWithRedMask() -> CIFilter & CIBlendWithMask
    
        open class func bloom() -> CIFilter & CIBloom
    
        open class func comicEffect() -> CIFilter & CIComicEffect
    
        open class func convolution3X3() -> CIFilter & CIConvolution
    
        open class func convolution5X5() -> CIFilter & CIConvolution
    
        open class func convolution7X7() -> CIFilter & CIConvolution
    
        open class func convolution9Horizontal() -> CIFilter & CIConvolution
    
        open class func convolution9Vertical() -> CIFilter & CIConvolution
    
        @available(iOS 15.0, *)
        open class func convolutionRGB3X3() -> CIFilter & CIConvolution
    
        @available(iOS 15.0, *)
        open class func convolutionRGB5X5() -> CIFilter & CIConvolution
    
        @available(iOS 15.0, *)
        open class func convolutionRGB7X7() -> CIFilter & CIConvolution
    
        @available(iOS 15.0, *)
        open class func convolutionRGB9Horizontal() -> CIFilter & CIConvolution
    
        @available(iOS 15.0, *)
        open class func convolutionRGB9Vertical() -> CIFilter & CIConvolution
    
        open class func coreMLModel() -> CIFilter & CICoreMLModel
    
        open class func crystallize() -> CIFilter & CICrystallize
    
        open class func depthOfField() -> CIFilter & CIDepthOfField
    
        open class func edges() -> CIFilter & CIEdges
    
        open class func edgeWork() -> CIFilter & CIEdgeWork
    
        open class func gaborGradients() -> CIFilter & CIGaborGradients
    
        open class func gloom() -> CIFilter & CIGloom
    
        open class func heightFieldFromMask() -> CIFilter & CIHeightFieldFromMask
    
        open class func hexagonalPixellate() -> CIFilter & CIHexagonalPixellate
    
        open class func highlightShadowAdjust() -> CIFilter & CIHighlightShadowAdjust
    
        open class func lineOverlay() -> CIFilter & CILineOverlay
    
        open class func mix() -> CIFilter & CIMix
    
        @available(iOS 15.0, *)
        open class func personSegmentation() -> CIFilter & CIPersonSegmentation
    
        open class func pixellate() -> CIFilter & CIPixellate
    
        open class func pointillize() -> CIFilter & CIPointillize
    
        open class func saliencyMap() -> CIFilter & CISaliencyMap
    
        open class func shadedMaterial() -> CIFilter & CIShadedMaterial
    
        open class func spotColor() -> CIFilter & CISpotColor
    
        open class func spotLight() -> CIFilter & CISpotLight
    
        
        // CICategoryBlur
        open class func bokehBlur() -> CIFilter & CIBokehBlur
    
        open class func boxBlur() -> CIFilter & CIBoxBlur
    
        open class func discBlur() -> CIFilter & CIDiscBlur
    
        open class func gaussianBlur() -> CIFilter & CIGaussianBlur
    
        open class func maskedVariableBlur() -> CIFilter & CIMaskedVariableBlur
    
        open class func median() -> CIFilter & CIMedian
    
        open class func morphologyGradient() -> CIFilter & CIMorphologyGradient
    
        open class func morphologyMaximum() -> CIFilter & CIMorphologyMaximum
    
        open class func morphologyMinimum() -> CIFilter & CIMorphologyMinimum
    
        open class func morphologyRectangleMaximum() -> CIFilter & CIMorphologyRectangleMaximum
    
        open class func morphologyRectangleMinimum() -> CIFilter & CIMorphologyRectangleMinimum
    
        open class func motionBlur() -> CIFilter & CIMotionBlur
    
        open class func noiseReduction() -> CIFilter & CINoiseReduction
    
        open class func zoomBlur() -> CIFilter & CIZoomBlur
    
        
        // CICategoryReduction
        @available(iOS 14.0, *)
        open class func areaAverage() -> CIFilter & CIAreaAverage
    
        @available(iOS 14.0, *)
        open class func areaHistogram() -> CIFilter & CIAreaHistogram
    
        @available(iOS 16.0, *)
        open class func areaLogarithmicHistogram() -> CIFilter & CIAreaLogarithmicHistogram
    
        @available(iOS 14.0, *)
        open class func areaMaximum() -> CIFilter & CIAreaMaximum
    
        @available(iOS 14.0, *)
        open class func areaMaximumAlpha() -> CIFilter & CIAreaMaximumAlpha
    
        @available(iOS 14.0, *)
        open class func areaMinimum() -> CIFilter & CIAreaMinimum
    
        @available(iOS 14.0, *)
        open class func areaMinimumAlpha() -> CIFilter & CIAreaMinimumAlpha
    
        @available(iOS 14.0, *)
        open class func areaMinMax() -> CIFilter & CIAreaMinMax
    
        @available(iOS 14.0, *)
        open class func areaMinMaxRed() -> CIFilter & CIAreaMinMaxRed
    
        @available(iOS 14.0, *)
        open class func columnAverage() -> CIFilter & CIColumnAverage
    
        @available(iOS 14.0, *)
        open class func histogramDisplay() -> CIFilter & CIHistogramDisplay
    
        @available(iOS 14.0, *)
        open class func kMeans() -> CIFilter & CIKMeans
    
        @available(iOS 14.0, *)
        open class func rowAverage() -> CIFilter & CIRowAverage
    }
    

    相关文章

      网友评论

          本文标题:RealityKit Postprocess Filter

          本文链接:https://www.haomeiwen.com/subject/otbsxdtx.html