Swift如何用核心图形绘制3D对象?

How to draw 3D object with core graphics in Swift?

如何在Swift中绘制带有核心图形的3D对象(最好是矩形)?

是否可以或我必须使用不同的库?

可以用 UIKit 实现吗?

借用这个答案:

你问题的关键部分是:

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

这会使用 SceneKit 和 UIKit 绘制一个矩形框。它被设置为在您的项目中的自定义 UIViewController 中使用,但它可以很容易地适应其他用途。

示例代码:

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.
}

这个问题类似于是否可以在 sheet 纸上绘制 3D 对象的问题,该纸介于 2D 之间。第三维效果是通过绘制额外的线作为投影来实现的。第三维度也可以通过运动来感知,因此,Core Animation 可能是 Core Graphics but it requires a lot of calculation, as result, it is quite complicated (using Core Animation) 的伴侣。

实际上,SceneKit 或 Metal 是使用 Swift 绘制 3D 模型的选项。