Scene Kit SCNMorpher 不适用于 SCNSphere 等原语

Scene Kit SCNMorpher not working with primitives like SCNSphere etc

有人让 SCNMorpher 工作吗?如果有,你具体做了什么?

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

我肯定漏掉了一些非常简单的东西。有谁能帮我上马吗?

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

Has anybody got the SCNMorpher to work?

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

您缺少的部分是所有变形目标需要具有相同数量的顶点并且顶点需要具有相同的排列。 targets 属性:

的文档中对此进行了讨论

The base geometry and all target geometries must be topologically identical — that is, they must contain the same number and structural arrangement of vertices.

您的示例(介于球体和圆锥体之间的早晨)不满足此要求,预计无法运行。

您可以尝试在两个不同的球体(具有相同的分段数!)之间变形,看看它是否 有效。