【问题标题】:Using Scenekit sceneTime to scrub through animations iOS使用 Scenekit 场景时间在 iOS 中擦洗动画
【发布时间】:2018-11-12 00:29:04
【问题描述】:

我正在尝试修改 Xcode 的默认游戏设置,以便我可以:将动画编程到几何体中,浏览该动画,并让用户自动播放动画。

我设法通过根据擦洗器的值设置视图的场景时间来使动画擦洗工作。但是,当我将 SCNSceneRenderer 上的 isPlaying 布尔值设置为 true 时,它​​会将每一帧的时间重置为 0,我无法让它离开第一帧。

从文档中,我假设这意味着它不会检测到我的动画,并认为所有动画的持续时间都是 0。

这是我的 GameViewController 中的 viewDidLoad 函数:

override func viewDidLoad() {
    super.viewDidLoad()

    // create a new scene
    let scene = SCNScene(named: "art.scnassets/ship.scn")!

    // create and add a camera to the scene
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    scene.rootNode.addChildNode(cameraNode)

    // place the camera
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

    // create and add a light to the scene
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = .omni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    scene.rootNode.addChildNode(lightNode)

    // create and add an ambient light to the scene
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = .ambient
    ambientLightNode.light!.color = UIColor.darkGray
    scene.rootNode.addChildNode(ambientLightNode)

    // retrieve the ship node
    let ship = scene.rootNode.childNode(withName: "ship", recursively: true)!

    // define the animation
    //ship.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)))
    let positionAnimation = CAKeyframeAnimation(keyPath: "position.y")
    positionAnimation.values = [0, 2, -2, 0]
    positionAnimation.keyTimes = [0, 1, 3, 4]
    positionAnimation.duration = 5
    positionAnimation.usesSceneTimeBase = true

    // retrieve the SCNView
    let scnView = self.view as! SCNView
    scnView.delegate = self

    // add the animation
    ship.addAnimation(positionAnimation, forKey: "position.y")

    // set the scene to the view
    scnView.scene = scene

    // allows the user to manipulate the camera
    scnView.allowsCameraControl = true

    // show statistics such as fps and timing information
    scnView.showsStatistics = true

    // configure the view
    scnView.backgroundColor = UIColor.black

    // add a tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(_:)))
    scnView.addGestureRecognizer(tapGesture)

    // play the scene
    scnView.isPlaying = true
    //scnView.loops = true
}

感谢任何帮助! :)

参考资料:

场景时间:

https://developer.apple.com/documentation/scenekit/scnscenerenderer/1522680-scenetime

正在播放:

https://developer.apple.com/documentation/scenekit/scnscenerenderer/1523401-isplaying

相关问题:

SceneKit SCNSceneRendererDelegate - renderer function not called

【问题讨论】:

    标签: ios swift scenekit caanimation scnscene


    【解决方案1】:

    我无法让它以一种优雅的方式工作,但我通过添加这个 Timer 调用来修复它:

    Timer.scheduledTimer(timeInterval: timeIncrement, target: self,   selector: (#selector(updateTimer)), userInfo: nil, repeats: true)
    

    timeIncrement 是一个 Double 设置为 0.01,updateTimer 是如下函数:

    // helper function updateTimer
    @objc func updateTimer() {
        let scnView = self.view.subviews[0] as! SCNView
        scnView.sceneTime += Double(timeIncrement)
    }
    

    我确信有更好的解决方案,但这可行。

    【讨论】:

      【解决方案2】:

      在每帧运行动作和动画后,sceneTime 会自动设置为 0.0。

      在 SceneKit 运行动作和动画之前,可以使用 renderer(_:updateAtTime:) 委托方法将场景时间设置为所需的值。

      使 GameViewController 符合 SCNSceneRendererDelegate:

      class GameViewController: UIViewController, SCNSceneRendererDelegate {
          // ...
      }
      

      确保将 scnView.delegate = self 保留在 viewDidLoad() 中。

      现在在 GameViewController 类中实现 renderer(_:updateAtTime:):

      // need to remember scene start time in order to calculate current scene time
      var sceneStartTime: TimeInterval? = nil
      
      func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
          // if startTime is nil assign time to it
          sceneStartTime = sceneStartTime ?? time
      
          // make scene time equal to the current time
          let scnView = self.view as! SCNView
          scnView.sceneTime = time - sceneStartTime!
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-29
        • 2015-01-04
        • 1970-01-01
        • 2012-11-29
        • 2016-06-07
        • 2019-06-20
        • 2015-01-27
        • 2014-10-30
        相关资源
        最近更新 更多