有人让SCNMorpher工作吗?如果是这样,您到底做了什么?

我现在使用的是一个小型测试程序,该程序应该显示一个红色的圆锥体,当我点击屏幕时,它应该(应该!)将其变形为球形/球形。但是,唯一发生的是,第一个几何图形消失了(变得很小),而第二个几何图形(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

最佳答案

有人让SCNMorpher工作吗?

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

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

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

您的示例(在球体与圆锥体之间进行早晨运动)不满足此要求,因此无法正常工作。

您可以尝试在两个不同的球体之间进行变形(具有相同的段数!),以查看它确实起作用。

关于ios - Scene Kit SCNMorpher无法与SCNSphere等原语一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29252017/

10-10 18:39