Scene Kit SCNMorpher不使用SCNSphere等原语

时间:2015-03-25 09:38:03

标签: ios scenekit morphing

有没有人让SCNMorpher工作?如果是这样,你究竟做了什么?

我现在要进行一个小测试程序,它应该显示一个红色圆锥体,当我点击屏幕时它应该(应该!)将其变成球形/球形。但唯一发生的事情是,第一个几何体消失(变得非常小)而第二个几何体(变形器第一个目标)永远不会出现。

我必须错过一些非常简单的事情。请问有人让我骑马吗?

import SceneKit
private let DISTANCE_FAKTOR = CGFloat(sqrt(3.0)/2.0)

class GameViewController: UIViewController {
override func viewDidLoad() {
    super.viewDidLoad()

    let (scene, view) = configScene(self.view.frame)
    self.view = view

    // Position camera to see picture full screen and light at same position
    let pov = SCNVector3(x: 0.0, y: 0.0, z: Float(max(view.frame.width, view.frame.height) * DISTANCE_FAKTOR))
    let pol = SCNVector3(x: 0.0, y: 0.0, z: Float(max(view.frame.width, view.frame.height) * DISTANCE_FAKTOR))

    scene.rootNode.addChildNode(makeShapes()) // Create and add background plane
    scene.rootNode.addChildNode(makeCamera(pov)) // Add camera to scene
    scene.rootNode.addChildNode(makeLight(pol)) // Add light to scene

    // add a tap gesture recognizer
    view.gestureRecognizers = [UITapGestureRecognizer(target: self, action: "handleTap:")]
}

func handleTap(gestureRecognize: UIGestureRecognizer) {
    NSLog("Tapped")
    if let effectNode = (view as? SCNView)?.scene?.rootNode.childNodeWithName("EFFECT", recursively: true) {
        NSLog("Animating")
        animate(effectNode)
    }
}

func makeShapes() -> SCNNode {
    // First shape is a cone
    let torus = SCNCone(topRadius: 0.0, bottomRadius: view.frame.width/2.0, height: view.frame.width/4.0)
    torus.firstMaterial = SCNMaterial()
    torus.firstMaterial?.diffuse.contents = UIColor.redColor()

    // Now an additional sphere/ball
    let sphere = SCNSphere(radius: view.frame.width/2.0)
    sphere.firstMaterial = SCNMaterial()
    sphere.firstMaterial?.diffuse.contents = UIColor.greenColor()

    // Put all in the node
    let node = SCNNode()
    node.geometry = torus
    node.morpher = SCNMorpher()
    node.morpher?.targets = [sphere]
    node.name = "EFFECT"

    // I would expect now something between a ball and a torus in red/green
    node.morpher?.setWeight(0.5, forTargetAtIndex: 0)
    return node
}

func animate(node: SCNNode) {
    SCNTransaction.begin()
    SCNTransaction.setAnimationDuration(5.0)
    node.morpher?.setWeight(1.0, forTargetAtIndex: 0) // From torus to ball

    SCNTransaction.setCompletionBlock {
        NSLog("Transaction completing")
        SCNTransaction.begin()
        SCNTransaction.setAnimationDuration(2.5)
        node.morpher?.setWeight(0.0, forTargetAtIndex: 0) // And back
        SCNTransaction.commit()
    }
    SCNTransaction.commit()
}

func configScene(frame: CGRect) -> (scene: SCNScene, view: SCNView) {
    let view = SCNView()
    view.frame = frame
    view.autoresizingMask = UIViewAutoresizing.allZeros
    view.backgroundColor = UIColor.blueColor()

    let scene = SCNScene()
    view.scene = scene

    return (scene, view)
}

func makeCamera(pov: SCNVector3) -> SCNNode {
    let camera = SCNCamera()
    camera.zFar = Double(pov.z)

    let node = SCNNode()
    node.position = pov
    node.camera = camera

    return node
}

func makeLight(pol: SCNVector3) -> SCNNode {
    let light = SCNLight()
    light.type = SCNLightTypeOmni
    light.zFar = CGFloat(pol.z)

    let node = SCNNode()
    node.position = pol
    node.light = light

    return node
}

override func shouldAutorotate() -> Bool {
    return true
}

override func prefersStatusBarHidden() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> Int {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.All.rawValue)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}
}

当我在代码中交换球和锥体时,我可以看到球,并且在点击屏幕时它会消失,但锥体永远不会出现。 如果您想运行此代码,只需在Xcode中创建一个新的GameScene项目并将此代码复制到GameViewController.swift

1 个答案:

答案 0 :(得分:6)

  

有没有人让SCNMorpher工作?

是的,我已经让SCNMorpher在自定义几何体和从文件加载的几何体之间工作。

您缺少的部分是所有变形目标需要具有相同数量的顶点,并且顶点需要具有相同的排列。这在targets属性的文档中讨论:

  

基本几何体和所有目标几何体必须在拓扑上相同 - 也就是说,它们必须包含相同数量和结构排列的顶点。

您的示例(球体和圆锥体之间的早晨)不符合此要求,预计不会起作用。

您可以尝试在两个不同的球体之间进行变换(具有相同数量的线段!),以确定它 的效果。