gpt4 book ai didi

swift - 如何在Swift中使用核心图形绘制3D对象?

转载 作者:行者123 更新时间:2023-11-30 13:31:51 24 4
gpt4 key购买 nike

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

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

用 UIKit 可以吗?

最佳答案

借用这个答案:https://stackoverflow.com/a/24127282/887210

您问题的关键部分是:

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

关于swift - 如何在Swift中使用核心图形绘制3D对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36487406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com