【问题标题】:Scene Kit SCNMorpher not working with primitives like SCNSphere etc场景套件 SCNMorpher 不适用于 SCNSphere 等原语
【发布时间】:2015-03-25 18:55:24
【问题描述】:

有人让 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

【问题讨论】:

    标签: ios scenekit morphing


    【解决方案1】:

    有人让 SCNMorpher 工作吗?

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

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

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

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

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

    【讨论】:

    • 我想,我已经阅读了文档中所有的小字。它现在可以在球体和圆环之间工作,torus. ringSegmentCounttorus. pipeSegmentCountsphere. segmentCount 设置为相同的数字。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 2014-01-22
    • 1970-01-01
    • 2014-11-02
    • 2018-05-22
    • 2012-08-14
    • 1970-01-01
    相关资源
    最近更新 更多