【问题标题】:How can I keep a SceneKit scene from rerendering poorly when State changes?当状态更改时,如何防止 SceneKit 场景重新渲染不佳?
【发布时间】:2020-09-07 12:50:15
【问题描述】:

我的父视图有一个包含 SceneKit 场景的子视图,但是当父视图中发生任何状态更改时,它会重置 SceneKit 动画,更改模型的纹理,并使模型更大。

有没有办法让 SceneKit 场景不受状态变化的影响?

Image of the model before and after the state changes

这是父视图的代码:

struct ContentView: View {
    @State private var color: Color = .sBlue

    var body: some View {
        VStack {
            Button(action: { self.color = .sOrange }) {
               self.color
            }
            .frame(height: 240)

            ModelView()
        }
    }
}

这里是 SceneKit 视图的代码:

struct ModelView: UIViewRepresentable {
    let model = SCNScene(named: "art.scnassets/3D Models/yBotIdle.scn")!

    func makeUIView(context: Context) -> SCNView {
        model.rootNode.childNode(withName: "yBot", recursively: true)?
            .scale = SCNVector3(x: 0.03, y: 0.03, z: 0.03)

        let cameraNode = SCNNode()
        let camera = SCNCamera()
        camera.focalLength = 120
        cameraNode.camera = camera
        cameraNode.position = SCNVector3(x: 0, y: 2.8, z: 35)
        model.rootNode.addChildNode(cameraNode)

        let directionalLightNode = SCNNode()
        directionalLightNode.light = SCNLight()
        directionalLightNode.light?.type = SCNLight.LightType.directional
        directionalLightNode.light?.intensity = 1500
        directionalLightNode.position = SCNVector3(x: 0, y: 6, z: 10)
        directionalLightNode.eulerAngles = SCNVector3(x: -0.4, y: 0, z: 0)
        model.rootNode.addChildNode(directionalLightNode)

        let modelView = SCNView()
        modelView.antialiasingMode = SCNAntialiasingMode.multisampling4X
        modelView.backgroundColor = UIColor(ciColor: .clear)

        return modelView
    }

    func updateUIView(_ modelView: SCNView, context: Context) {
        modelView.scene = model
    }
}

谢谢!

【问题讨论】:

  • 我们需要看看你写了什么才能帮助你。请点击编辑按钮并添加您的格式化代码。如果你不这样做,你可能会被否决。
  • @Mozahler 感谢您的提示,我是菜鸟。我现在已经包含了代码。
  • 太棒了!我赞成帮助您获得一些知名度。 ????

标签: ios swift swiftui scenekit


【解决方案1】:

想通了!创建 SceneKit 视图的实例以在父视图中使用而不是直接使用 SceneKit 视图可以解决这些问题。我不知道为什么会这样,如果有人能解释我很想听听。

struct ContentView: View {
    @State private var color: Color = .sBlue
    let modelView = ModelView()

    var body: some View {
        VStack {
            Button(action: { self.color = .sOrange }) {
                self.color
            }
            .frame(height: 240)

            modelView
        }
    }
}

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 2020-10-20
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2018-01-03
    相关资源
    最近更新 更多