当状态更改时,如何防止SceneKit场景渲染不良?

问题描述 投票:1回答:1

我的父视图有一个包含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
    }
}

谢谢!

ios swift swiftui scenekit
1个回答
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
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.