菜鸟教程小白 发表于 2022-12-11 20:12:55

ios - 使用 Scenekit 场景时间来擦洗动画 iOS


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

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

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

<p>这是我的 GameViewController 中的 viewDidLoad 函数:</p>

<pre><code>override func viewDidLoad() {
    super.viewDidLoad()

    // create a new scene
    let scene = SCNScene(named: &#34;art.scnassets/ship.scn&#34;)!

    // 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: &#34;ship&#34;, recursively: true)!

    // define the animation
    //ship.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)))
    let positionAnimation = CAKeyframeAnimation(keyPath: &#34;position.y&#34;)
    positionAnimation.values =
    positionAnimation.keyTimes =
    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: &#34;position.y&#34;)

    // 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
}
</code></pre>

<p>感谢任何帮助! :)</p>

<p>引用资料:</p>

<p>场景时间:</p>

<p> <a href="https://developer.apple.com/documentation/scenekit/scnscenerenderer/1522680-scenetime" rel="noreferrer noopener nofollow">https://developer.apple.com/documentation/scenekit/scnscenerenderer/1522680-scenetime</a> </p>

<p>正在播放:</p>

<p> <a href="https://developer.apple.com/documentation/scenekit/scnscenerenderer/1523401-isplaying" rel="noreferrer noopener nofollow">https://developer.apple.com/documentation/scenekit/scnscenerenderer/1523401-isplaying</a> </p>

<p>相关问题:</p>

<p> <a href="https://stackoverflow.com/questions/35390959/scenekit-scnscenerendererdelegate-renderer-function-not-called" rel="noreferrer noopener nofollow">SceneKit SCNSceneRendererDelegate - renderer function not called</a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我无法让它以一种优雅的方式工作,但我通过添加这个 Timer 调用来修复它:</p>

<pre><code>Timer.scheduledTimer(timeInterval: timeIncrement, target: self,   selector: (#selector(updateTimer)), userInfo: nil, repeats: true)
</code></pre>

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

<pre><code>// helper function updateTimer
@objc func updateTimer() {
    let scnView = self.view.subviews as! SCNView
    scnView.sceneTime += Double(timeIncrement)
}
</code></pre>

<p>我确信有更好的解决方案,但这可行。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用 Scenekit 场景时间来擦洗动画 iOS,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/50650615/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/50650615/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用 Scenekit 场景时间来擦洗动画 iOS