0

How can I draw a 3D object (preferably a rectangle) with core graphics in Swift?

Is it possible or do I have to use a different library?

Is it possible with UIKit?

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
Tob
  • 985
  • 1
  • 10
  • 26

2 Answers2

1

Borrowing from this answer: https://stackoverflow.com/a/24127282/887210

The key part for your question is:

SCNBox(width: 1, height: 4, length: 9, chamferRadius: 0)

This draws a rectangular box with SceneKit and UIKit. It's set up to be used in a custom UIViewController in your project but it can easily be adapted to other uses.

The example code:

override func loadView() {
  // create a scene view with an empty scene
  let sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
  let scene = SCNScene()
  sceneView.scene = scene

  // default lighting
  sceneView.autoenablesDefaultLighting = true

  // a camera
  let cameraNode = SCNNode()
  cameraNode.camera = SCNCamera()
  cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
  scene.rootNode.addChildNode(cameraNode)

  // a geometry object
  let box = SCNBox(width: 1, height: 4, length: 9, chamferRadius: 0)
  let boxNode = SCNNode(geometry: box)
  scene.rootNode.addChildNode(boxNode)

  // configure the geometry object
  box.firstMaterial?.diffuse.contents  = UIColor.red
  box.firstMaterial?.specular.contents = UIColor.white

  // set a rotation axis (no angle) to be able to
  // use a nicer keypath below and avoid needing
  // to wrap it in an NSValue
  boxNode.rotation = SCNVector4(x: 1, y: 1, z: 0.0, w: 0.0)

  // animate the rotation of the torus
  let spin = CABasicAnimation(keyPath: "rotation.w") // only animate the angle
    spin.toValue = 2.0*Double.pi
  spin.duration = 10
  spin.repeatCount = HUGE // for infinity
  boxNode.addAnimation(spin, forKey: "spin around")

  view = sceneView // Set the view property to the sceneView created here.
}
nickdnk
  • 4,010
  • 4
  • 24
  • 43
  • So how would I incorporate this to a project that is not a game. Would I add the sceneView to my viewcontroller – Tob Apr 07 '16 at 22:24
  • I've updated the example so that you can see how to embed it in a UIViewController subclass. You basically override the `loadView()` method and set the `view` property to the `sceneView` variable. –  Apr 08 '16 at 12:06
  • One last quick question, how would a move this to a smaller view within my view controller or say to a button – Tob Apr 08 '16 at 20:56
  • The size of the view shouldn't matter, the drawing should scale appropriately. Just put this in a subclass of UIViewController and set the class of the view controller to be that subclass instead of UIViewController. –  Apr 10 '16 at 02:15
  • This answer does not correspond to the question. – Darius Miliauskas Jul 24 '18 at 15:54
0

This question is similar to the question whether it is possible to draw a 3D object on a sheet of paper which is, between, 2D. The third dimension effect is achieved drawing additional lines as the projections. The third dimension could also be perceived through the motion, so, Core Animation is a possible companion to Core Graphics but it requires a lot of calculation, as result, it is quite complicated (using Core Animation).

Actually, SceneKit or Metal are the options to draw 3D models using Swift.

Darius Miliauskas
  • 3,391
  • 4
  • 35
  • 53