A definition for a grid based deformation of nodes that conform to SKWarpable.


class SKWarpGeometryGrid : SKWarpGeometry


You create an SKWarpGeometryGrid by supplying two arrays of normalized vertex positions: the sourcePositions array defines the positions of the vertices in the un-warped geometry and the destPositions defines the final, warped destination positions for the source vertices. Both arrays are one dimensional and in “row-major” order — this means that you additionally have to supply the number of columns and rows of the geometry.

The number of columns or rows is one less than the number of horizontal or vertical vertices respectively.

The origin of the vertex positions is in the bottom left: The following code shows how you can create an array of evenly spaced vertices which could act as a default set of source positions for a warp geometry with two columns and two rows. The first item in the array refers to the position at the bottom left and the last item refers to the position at the top right.

Listing 1

Creating an array of non-deforming positions

let sourcePositions: [float2] = [
    float2(0, 1),   float2(0.5, 1),   float2(1, 1),
    float2(0, 0.5), float2(0.5, 0.5), float2(1, 0.5),
    float2(0, 0),   float2(0.5, 0),   float2(1, 0)

If you wanted to give the effect of horizontally squeezing a grid defined by the above vertices around its middle — which would cause it to stretch vertically — you could use a set of destination positions such as:

Listing 2

Creating an array of deforming positions

let destinationPositions: [float2] = [
    float2(-0.25, 1.5), float2(0.5, 1.75), float2(1.25, 1.5),
    float2(0.25, 0.5),   float2(0.5, 0.5),   float2(0.75, 0.5),
    float2(-0.25, -0.5),  float2(0.5, -0.75),  float2(1.25, -0.5)

You use these two arrays to define a warp geometry object.

Listing 3

Creating a warp geometry grid

let warpGeometryGrid = SKWarpGeometryGrid(columns: 2,
                                          rows: 2,
                                          sourcePositions: sourcePositions,
                                          destinationPositions: destinationPositions)

A geometry grid warps the geometry defined by the source positions (left illustration) into a new geometry defined by the destination positions (right illustration).

Figure 1

Warped geometry

Warped geometry

With this geometry, you have several options for applying it to a node that conforms to SKWarpable, such as an SKSpriteNode.

The geometry can be applied immediately, without animation, by setting the nodes’s warpGeometry property. You may elect to use this technique if you are calculating and applying the destination positions with each scene update, for example in response to a user’s touch.

A single warp can be applied over time to give an animated morphing effect using the action created by warp(to:duration:). To use a warp action, you first need to define an initial geometry and set it as the warpGeometry of the node you want to warp. The init(columns:rows:) initializer creates a suitable geometry that has no distortion.

In the following code listing, the stretched warp geometry created above is used to initialize a warp action with a duration specified in seconds. The node’s run method executes the action.

Listing 4

Creating and running a simple warp action

let sprite = SKSpriteNode()
let warpGeometryGridNoWarp = SKWarpGeometryGrid(columns: 2, rows: 2)
sprite.warpGeometry = warpGeometryGridNoWarp
let warpAction = SKAction.warp(to: warpGeometryGrid,duration: 0.5)!)

You can chain together multiple warp geometries to create complex morphing animations. For example, you may want to begin with the warp geometry that does not deform the node, morph to the stretched geometry at 0.5”, and finish the sequence by returning to the first geometry at 0.75”. The following code creates this action, using the animate(withWarps:times:) method.

Listing 5

Creating a composite warp action

let warpAction = SKAction.animate(withWarps:[warpGeometryGridNoWarp, 
                                  times: [0.25, 0.5, 0.75])

Objects that subclass SKNode but don't conform to SKWarpable — for example SKShapeNode, SKEmitterNode or SKVideoNode — can be warped by adding them as children of an SKEffectNode object. You can, for example, use this approach to distort vector artwork supplied as simple geometric primitives or CGPath objects and rendered with an shape node. Warping an particle emitter node that has been added as a child of an effect node allows precise control of the overall shape of a particle system.


Instance Properties

var numberOfColumns: Int

Returns the receiver's number of columns.

var numberOfRows: Int

Returns the receiver's number of rows.

var vertexCount: Int

Returns the receiver's total number of vertices.


Inherits From

See Also

Warping Nodes

class SKWarpGeometry

A definition for a deformation of nodes that conform to SKWarpable.

protocol SKWarpable

A protocol for objects that can be warped and animated by an SKWarpGeometry.