Search code examples
iosscenekitmorphing

morphing a cube into sphere


This is an image from apple's documentation. They show a transform from a cube to sphere and also to some random geometry. enter image description here

Only a few lines lower they state:

A morpher and its target geometries may be loaded from a scene file or created programmatically. The base geometry and all target geometries must be topologically identical—that is, they must contain the same number and structural arrangement of vertices.

Could someone explain this paragraph because apparently I don't understand it. Since a sphere will never have the same structural arrangement of vertices as cube(at least I think so), it is impossible to make transformation. But hey, we all see it in the picture. I also tried do to the transformation and I don't get the expected results. So how do you go from sphere to cube or vice versa?


Solution

  • "Topologically identical" means that the relationships between vertices in a mesh must be preserved, but their locations in space can change. Here's an example of that in 2D:

    rect mesh enter image description here

    These two meshes have the same eight vertices, connected to each other in the same ways, but their positions (and thus the shape they form) differ.

    To do the same in 3D with SceneKit, you need custom vertex data — the primitive shapes that SceneKit can generate for you (like SCNSphere, SCNBox, and whatnot) all have different topologies, so they can't be used as morpher targets.

    If you want to morph a box into a sphere, you'll need to generate your own box and sphere with identical topology. The "some random shape" in Apple's illustration is a hint at how you might do that — it appears to be one of the variants of a superellipsoid. If you use the equations in that Wikipedia page you can generate a set of points that can be either on a sphere or on a cube depending on other parameters. Vary those parameters to generate a couple of meshes, create SCNGeometry from those meshes, and you've got valid SCNMorpher targets.

    You can see a simpler example of morphing in Apple's SceneKit WWDC 2014 Slides sample app.