gpt4 book ai didi

swift - 图像的立方体结构和旋转

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

使用 iOS 很长时间后,我从未有过 3D 经验,现在我需要创建 1 个可以旋转的立方体。

我正在尝试弄清楚如何从图像创建立方体,并快速旋转它。

此链接https://www.raywenderlich.com/12667/how-to-rotate-a-3d-object-using-touches-with-opengl确实做到了这一点,但速度并不快。

他的其他 swift 教程要复杂得多。

我希望有两个功能:

  1. 从图像创建 3D 立方体
  2. 将其旋转到某个 x,y,z

是否有一个简单的 Apple 3D 动画类?

最佳答案

import UIKit
import SceneKit
import QuartzCore

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let scene = SCNScene()
let scnView = self.view as! SCNView
scnView.scene = scene

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

scnView.allowsCameraControl = true //that's for you can spin it any direction with your finger if you are bored
scnView.autoenablesDefaultLighting = true //Lightting

var geometries = [
SCNBox(width: 4.0, height: 4.0, length: 4.0, chamferRadius: 0),
] //This is the cube with parameters of 4

var materials = [SCNMaterial]()
for i in 1...6 {
let material = SCNMaterial()
if i == 1 { material.diffuse.contents = UIImage (named: "qr.png") }
if i == 2 { material.diffuse.contents = UIColor(red: 0.6784, green: 0.698, blue: 0.7412, alpha: 1.0) }
if i == 3 { material.diffuse.contents = UIColor(red: 0.6784, green: 0.698, blue: 0.7412, alpha: 1.0) }
if i == 4 { material.diffuse.contents = UIColor(red: 0.6784, green: 0.698, blue: 0.7412, alpha: 1.0) }
if i == 5 { material.diffuse.contents = UIColor(red: 0.6784, green: 0.698, blue: 0.7412, alpha: 1.0) }
if i == 6 { material.diffuse.contents = UIColor(red: 0.7882, green: 0.7961, blue: 0.8863, alpha: 1.0) }
materials.append(material)
} //this is to coloring each side of cube

for i in 0..<geometries.count {
let geometry = geometries[i]
let node = SCNNode(geometry: geometry)

node.geometry?.materials = materials
node.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 1, y: 1, z: 1, duration: 5)))
scene.rootNode.addChildNode(node)
}
}

关于swift - 图像的立方体结构和旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38530588/

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