Exploring Morphing Techniques in SceneKit for iOS Development
Written on
Chapter 1: Introduction to Morphing in SceneKit
In this discussion, we will delve into the transformation of object geometries within SceneKit, Apple's powerful 3D graphics framework.
Reflecting on the impact of visionary leaders like Steve Jobs, it's fascinating to consider how he revolutionized both the computer and film industries. The advent of Pixar, responsible for the groundbreaking Toy Story in 1995, showcased the potential of 3D animation. At that time, it's likely they utilized early Apple systems and frameworks like Core Image and SceneKit, which include morphers for blending shapes.
Join me as we explore the foundational techniques for morphing geometries in SceneKit, a process that may not be as straightforward as Apple's documentation implies.
Section 1.1: Basic Morphing Techniques
To illustrate basic morphing, consider the following animated GIF example.
Here, we see a blue cube that expands, turns red, and then shrinks back to blue. This effect is achieved through a simple code snippet:
// Your code snippet here
Initially, I created a cube with the cubeGeometry layout, which I then modified to cubeGeometry2. The original node is replaced with the new one upon completion of the morphing process. This transition is activated via a subscription setup I described in a previous article.
Subsection 1.1.1: Custom Morphing Techniques
Next, we explore custom geometries morphing into one another.
This morphing process requires more extensive code due to the need for extensions to create custom geometries. Here’s an example of the necessary code:
// Your custom shape creation code here
Section 1.2: Combining Morphing Techniques
In this section, we will see an example where the geometry of the number one transforms into multiple cubes, creating an explosive effect.
I attempted to use a circle for this effect, but it did not yield satisfactory results. You can experiment with different shapes as long as the vertex counts align. However, direct access to the vertices of a TextNode is not available, making it impossible to morph directly between numbers. The morphing command will alert you to the required vertex count if attempted.
Different numbers have varying vertex counts: for instance, one has 230, two has 322, and so forth, which may also change depending on the font used.
let rect3 = CGRect(x: 0, y: 0, width: 2, height: 2)
let roundedRect3 = UIBezierPath(roundedRect: rect3, cornerRadius: 2)
neuText.chamferRadius = 0.5
neuText.flatness = 0.2
neuText.chamferProfile = roundedRect3
neuText.chamferRadius = 0.5
This code snippet details the setup for the TextNode.
Morphing Magic
I continued experimenting and discovered that it is possible to access the vertices of a TextNode by creating a second SCNNode from its geometry. The new node may not be perfect, but it allows for direct morphing between numbers.
The method involves identifying the number with the highest vertex count (number 8 in this case) and padding the others to ensure uniformity in vertex count, allowing the use of the previously established morphing code.
var vertex = cubeNode.geometry?.vertices(multiple: 3.0)
var cvertex = vertex!
for i in 0...7 {
vertex!.append(contentsOf: cvertex)
}
for i in 0...13 {
vertex!.append(SCNVector3(x: 1, y: 1, z: 0))
}
This snippet shows how to extract vertices initially.
Thanks for joining me on this journey through morphing in SceneKit!
Chapter 2: Advanced Morphing Techniques
Explore the fascinating world of geometry nodes with this video, "GeoNoding Adventure | Morphing meshes with Geometry Nodes," where you'll learn about mesh transformations and creative uses of geometry nodes.
In the next video, "Blender Geometry Nodes Morph Shape Sequence Build," dive deeper into creating stunning morphing sequences with geometry nodes in Blender.