经过iOS很长一段时间后,我从未有过3D体验,现在我需要创建一个可以旋转的立方体。
我试图弄清楚如何从图像创建一个立方体,并在swift中旋转它。
此链接https://www.raywenderlich.com/12667/how-to-rotate-a-3d-object-using-touches-with-opengl已完全相同,但不是很快。
他对swift的其他教程要复杂得多。
我希望有两个功能:
是否有一个简单的Apple 3D动画类?
答案 0 :(得分:0)
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)
}
}