Contents

ARView.PostProcessContext

An object the framework uses to pass data to a postprocess callback.

Declaration

struct PostProcessContext

Mentioned in

Overview

In iOS 15 and later, as well as macOS 12 and later, you can register a callback function to apply postprocessing effects to RealityKit’s rendered scene. Once every frame, RealityKit calls that function before it displays the scene. You can use this callback to apply postprocess effects using any APIs that can modify an image texture. However, because RealityKit calls this method every frame, you should only use APIs that execute on the GPU, such as Metal compute functions, Metal Performance Shaders, or Core Image. You can also render additional content, such as a rendered SpriteKit scene, on top of the frame buffer.

A postprocess callback takes a single PostProcessContext parameter, which contains data the callback function needs to modify the rendered scene, including the frame buffer, depth map, and a property for writing the modified image. If you’ve registered a postprocess callback, that function needs to encode to the output texture or the frame is never displayed.

A postprocess function looks like this:

    func myPostProcessCallback(context: ARView.PostProcessContext) {
        // Handle postprocessing here.
    }

To register a function as the postprocess render callback, assign it to the postProcess property of the renderCallbacks property of the scene’s ARView:

arView.renderCallbacks.postProcess = myPostProcessCallback

To stop postprocessing, set the postProcess render callback to nil.

arView.renderCallbacks.postProcess = nil

If your app turns postprocessing on and off frequently, another option for disabling postprocess effects is to keep your callback registered, but use an MTLBlitCommandEncoder to encode the unmodified framebuffer directly to the output texture whenever postprocess effects aren’t active.

 func myPostProcessCallback(context: ARView.PostProcessContext) {
     if (usePostProcessEffects) {
         handlePostProcessing(context: ARView.PostProcessContext)
         return
     }


     // If postprocess effects are disabled, copy sourceColorTexture
     // directly to targetColorTexture.
     let blitEncoder = context.commandBuffer.makeBlitCommandEncoder()
     blitEncoder?.copy(from: context.sourceColorTexture, to: context.targetColorTexture)
     blitEncoder?.endEncoding()
 }

Topics

Initializers

Instance Properties

See Also

Postprocessing