0

I'm new to 3D.

I have a straight stick object which is a SCNBox, and at some stage I want to bend it (with animation). Is there any way to achieve this? Or do I have to use multiple objects to simulate a SCNBox in the first place, and then move and rotate those objects to simulate bending?

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
agou
  • 728
  • 1
  • 10
  • 24

2 Answers2

4

SCNBox has a heightSegmentCount property that you can use to produce more vertices.

Using a shader modifier you'll be able to bend the cube. That's how the grass blades are animated in Fox: Building a SceneKit Game with the Xcode Scene Editor. This can be done right in the SceneKit editor see (Enhancements to SceneKit from WWDC 2015) or programmatically.

edit:

Here is the modifier for the SCNShaderModifierEntryPointGeometry entry point:

float offset = _geometry.color.x * (sin(1.2 * u_time + (_geometry.position.x + _geometry.position.z) * 4.0) + 0.5) * 0.02;
_geometry.position.x += offset;
_geometry.color.xyz = vec3(1.0);

Bending weights are baked in the vertex color (SCNGeometrySourceSemanticColor, but you could also use texture coordinates) which is later reset to 1.0 so that they doesn't affect the final color of the geometry.

mnuages
  • 13,049
  • 2
  • 23
  • 40
  • Is this a geometry shader modifier? – Confused Jan 10 '16 at 14:52
  • There should be a moral law against using the word "shader" without the contextualisation of its type and explicit clarification of that which it's operating upon ;) – Confused Jan 12 '16 at 14:43
1

You have multiple options, depending on what effect you're after. I don't think you'll be able to do it with just a single SCNBox, though.

You can design your object in a 3D tool like Blender, Maya, or Cheetah3D. Create an animation within that tool, export the model to Collada/DAE format, and display it in SceneKit. You can then play the animation by name.

You might also be able to use the morphing API. That's slide 34 of the WWDC 2014 SceneKit sample code. The live session is Session 610.

Inverse Kinematics could work for you if you want the end of your stick to touch a particular point. That would involve building IK into your Collada file, or constructing the object programmatically using physics joints.

None of these approaches are beginner-level techniques.

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42