- iOS 7.0+
- macOS 10.9+
- tvOS 9.0+
- watchOS 3.0+
SKEffect object renders its children into a buffer and optionally applies a Core Image filter to this rendered output. Because effect nodes conform to
SKWarpable, you can also use them to apply distortions to nodes that don't implement the protocol such as shape and video nodes. Use effect nodes to incorporate sophisticated special effects into a scene or to cache the contents of a static subtree for faster rendering performance.Each time a new frame is rendered using the effect node, the effect node follows these steps:
The effect node draws its children into a private framebuffer.
It blends the contents of its private framebuffer into its parent’s framebuffer, using one of the standard sprite blend modes.
It discards its private framebuffer. This step is optional; see the
Example: Applying a Special Effect
One possible use for effect nodes is to apply special effects to a node’s children, as shown in Figure 1. In this example, the effect node’s children are two sprites that provide lighting information. The effect node accumulates the effects of these lights, applies a blur filter to soften the resulting image, and then uses a multiply blend mode to apply this lighting to a texture.
Here’s how the scene generates this lighting effect:
The scene has two children. The first is a textured sprite that represents the ground. The second is an effect node to apply lighting.
The effect node’s children are sprite nodes rendered using an additive blend mode.
The effect node includes a filter effect to soften the lighting.
If you specify a Core Image filter, it must be a filter that takes a single input image and produces a single output image.
The effect node uses a multiplication blend mode to apply its lighting effect to the scene’s framebuffer.
Example: Warping a Label
Listing 5 shows how you can warp a label node by adding it as a child to a
SKEffect and assign the effect node a
SKWarp that pulls out the corners horizontally and stretches the center vertically.
Figure 2 shows the warped label.
Scenes Are Effect Nodes
SKScene class is a subclass of
SKEffect. This means that any scene can apply a filter to its contents. Although applying filters can be very expensive—not all filters are well designed for interactive effects—experimentation can help you find some interesting ways to use them.
Caching May Improve Performance of Static Content
An effect node normally discards its private framebuffer after rendering is complete. Rendering the content is necessary because it typically changes every frame. However, if the content is static, this is unnecessary. It might make more sense to keep the rendered framebuffer instead of discarding it. If the content of the effect node is static, set the node’s
should property to
true. Setting this property causes the following changes in behavior:
The framebuffer is not discarded at the end of rasterization. This also means that more memory is being used by the effect node, and rendering may take slightly longer.
When a new frame is rendered, the framebuffer is rendered only if the contents of the effect node’s descendants have changed.
If the effect node has a Core Image filter, changes to its properties no longer automatically update the framebuffer. You can force it to be updated by setting the
You can use effect nodes to cache static content even when you aren’t applying a filter to the rendered image. This technique can be useful when the contents of a subtree are static and expensive to render.