SceneKit – SwiftUI 实现似乎没有更新任何内容

发布于 2025-01-10 00:25:58 字数 3137 浏览 0 评论 0原文

Swift 5.x iOS 15

在此处输入图像描述

尝试了解 SceneKit,但我陷入困境。我创建了一个场景并使用 SwiftUI 显示它。但是,当我尝试使用该类运行函数时,场景已定义,但它什么也不做。当我点击“推”标签时,我希望球在空中跳跃。什么也没发生。

import SwiftUI
import SceneKit

struct ContentView: View {    
    var body: some View {
        let scene = GameScene()
        Text("push")
            .onTapGesture {
                let foo = SharedView.shared
                print("push ",foo.xCord,foo.yCord,foo.zCord)
                scene.printMe()
            }
    }
}

class SharedView: ObservableObject {

    @Published var xCord:Float = 0
    @Published var yCord:Float = 0
    @Published var zCord:Float = 0

    @Published var xCord2:Float = 0
    @Published var yCord2:Float = 1.8
    @Published var zCord2:Float = 0

    static var shared = SharedView()
}

...

class SceneDelegate: NSObject, SCNSceneRendererDelegate {
    let foo = SharedView.shared

    func renderer(_ renderer: SCNSceneRenderer, 
           updateAtTime time: TimeInterval) {
        print("running!!")
    }

    static var shared = SceneDelegate()
}

struct GameScene: View {
//  @ObservedObject var foo = SharedView.shared
    @StateObject var foo = SharedView.shared

    @State var scene = SCNScene(named: "MyScene.scn")
    @State var cameraNode = SCNNode()
    @State var ball = SCNNode()
    @State var box = SCNNode()

var body: some View {       
    scene?.isPaused = false
    
    self.scene?.rootNode.enumerateChildNodes({ (node, _ ) in
        if node.name == "ball" {
            Task {
                ball = node
                ball.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(node: ball, options: [:]))
                ball.physicsBody?.isAffectedByGravity = true
                ball.physicsBody?.restitution = 1.2
            }
        }
        if node.name == "box" {
            Task {
                box = node
                box.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(node: box, options: [:]))
                box.physicsBody?.isAffectedByGravity = false
                box.physicsBody?.restitution = 1
            }
        }
    })
    
    cameraNode.camera = SCNCamera()
            
    cameraNode.position.x = foo.xCord
    cameraNode.position.y = foo.yCord
    cameraNode.worldPosition.z = foo.zCord
    cameraNode.eulerAngles = SCNVector3(foo.xCord2,foo.yCord2,foo.zCord2)
    cameraNode.camera = SCNCamera()
    
    let sceneView = SceneView(
        scene: scene,
        pointOfView: cameraNode,
        options: [.autoenablesDefaultLighting,.allowsCameraControl], delegate: SceneDelegate.shared)

    let dummyNode = scene!.rootNode.childNode(withName: "DummyNode", recursively: false)
    dummyNode?.position = SCNVector3(0, 0, 0)
    
    return sceneView
        .frame(width: 1024, height: 256, alignment: .center)  
}

func printMe() {
    print(cameraNode.eulerAngles, cameraNode.position)
    ball.physicsBody?.applyForce(SCNVector3(0,10,0), asImpulse: true)
}
}

Swift 5.x iOS 15

enter image description here

Trying to understand SceneKit, but I am stuck. I created a scene and display it with SwiftUI. But when I try and run a function with that class the scene is defined it does nothing. When I tap on the label "push" I want the ball to jump in the air. Nothing happens.

import SwiftUI
import SceneKit

struct ContentView: View {    
    var body: some View {
        let scene = GameScene()
        Text("push")
            .onTapGesture {
                let foo = SharedView.shared
                print("push ",foo.xCord,foo.yCord,foo.zCord)
                scene.printMe()
            }
    }
}

class SharedView: ObservableObject {

    @Published var xCord:Float = 0
    @Published var yCord:Float = 0
    @Published var zCord:Float = 0

    @Published var xCord2:Float = 0
    @Published var yCord2:Float = 1.8
    @Published var zCord2:Float = 0

    static var shared = SharedView()
}

...

class SceneDelegate: NSObject, SCNSceneRendererDelegate {
    let foo = SharedView.shared

    func renderer(_ renderer: SCNSceneRenderer, 
           updateAtTime time: TimeInterval) {
        print("running!!")
    }

    static var shared = SceneDelegate()
}

struct GameScene: View {
//  @ObservedObject var foo = SharedView.shared
    @StateObject var foo = SharedView.shared

    @State var scene = SCNScene(named: "MyScene.scn")
    @State var cameraNode = SCNNode()
    @State var ball = SCNNode()
    @State var box = SCNNode()

var body: some View {       
    scene?.isPaused = false
    
    self.scene?.rootNode.enumerateChildNodes({ (node, _ ) in
        if node.name == "ball" {
            Task {
                ball = node
                ball.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(node: ball, options: [:]))
                ball.physicsBody?.isAffectedByGravity = true
                ball.physicsBody?.restitution = 1.2
            }
        }
        if node.name == "box" {
            Task {
                box = node
                box.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(node: box, options: [:]))
                box.physicsBody?.isAffectedByGravity = false
                box.physicsBody?.restitution = 1
            }
        }
    })
    
    cameraNode.camera = SCNCamera()
            
    cameraNode.position.x = foo.xCord
    cameraNode.position.y = foo.yCord
    cameraNode.worldPosition.z = foo.zCord
    cameraNode.eulerAngles = SCNVector3(foo.xCord2,foo.yCord2,foo.zCord2)
    cameraNode.camera = SCNCamera()
    
    let sceneView = SceneView(
        scene: scene,
        pointOfView: cameraNode,
        options: [.autoenablesDefaultLighting,.allowsCameraControl], delegate: SceneDelegate.shared)

    let dummyNode = scene!.rootNode.childNode(withName: "DummyNode", recursively: false)
    dummyNode?.position = SCNVector3(0, 0, 0)
    
    return sceneView
        .frame(width: 1024, height: 256, alignment: .center)  
}

func printMe() {
    print(cameraNode.eulerAngles, cameraNode.position)
    ball.physicsBody?.applyForce(SCNVector3(0,10,0), asImpulse: true)
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文