Contents

PostProcessEffectContext

An object RealityKit passes data to a post process effect method.

Declaration

struct PostProcessEffectContext<CommandBuffer>

Overview

You can register a method that applies postprocessing effects to a scene RealityKit renders with a RealityView. Once every frame, RealityKit calls that function before displaying the scene. You can use this to apply postprocess effects using any APIs that can modify an image texture. However, because RealityKit calls this method every frame, prioritize using APIs that execute on the GPU, such as Metal compute functions, Metal Performance Shaders, or Core Image. You can also render additional content on top of the frame buffer, such as a SpriteKit scene.

A postprocess method takes a single PostProcessEffectContext parameter, which contains data the callback method needs to modify the rendered scene, including the frame buffer, depth map, and a property for writing the image you modify. The postprocess method in your PostProcessEffect type needs to encode to the output texture, or the frame is never displayed.

A postprocess method looks like this:

func postProcess(context: borrowing PostProcessEffectContext<MTLCommandBuffer>) {
    // Handle postprocessing here.
}

To register a method as the postprocess render callback, add an implementation for postProcess(context:) in your type that conforms to PostProcessEffect:

class LaplacianPostProcess: PostProcessEffect {
    // Post process callback.
    func postProcess(context: borrowing PostProcessEffectContext<MTLCommandBuffer>) {
        let filter = MPSImageLaplacian()
        filter.encode(commandBuffer: context.commandBuffer,
                    sourceTexture: context.sourceColorTexture,
                    destinationTexture: context.compatibleTargetTexture)
    }
}

To stop postprocessing, you can set the customPostProcessing property to none.

content.renderingEffects.customPostProcessing = .none

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

var isEnabled: Binding<Bool>

func postProcess(context: borrowing PostProcessEffectContext<MTLCommandBuffer>) {
    if isEnabled.wrappedValue {
        let filter = MPSImageLaplacian()
        filter.encode(commandBuffer: context.commandBuffer,
                    sourceTexture: context.sourceColorTexture,
                    destinationTexture: context.compatibleTargetTexture)
    } else {
        // Remove processing effects.
        let blitEncoder = context.commandBuffer.makeBlitCommandEncoder()
        blitEncoder?.copy(from: context.sourceColorTexture, to: context.targetColorTexture)
        blitEncoder?.endEncoding()
    }
}

Topics

Instance Properties

See Also

Visual environment adjustments