【问题标题】:A cube structure from image, and rotation来自图像和旋转的立方体结构
【发布时间】:2017-07-31 20:32:31
【问题描述】:

在 iOS 上玩了很久,一直没有 3D 体验,现在我需要创建 1 个可以旋转的立方体。

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

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

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

我希望有 2 个功能:

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

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

【问题讨论】:

  • “来自图像”?如...
  • @tktsubota 作为它的一方。与链接中的完全一样。
  • 你试过 SceneKit 吗?
  • @Curnelious 您是否从 Image 创建了 3d 立方体?我也想做,请帮忙!

标签: swift animation 3d


【解决方案1】:
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)
    }
 }

【讨论】:

  • 请描述并详细说明您的答案有效的原因。
猜你喜欢
  • 2014-12-08
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 2018-01-01
相关资源
最近更新 更多