xcode 如何在SwiftUI中更改SceneView 3D对象的背景色

pw9qyyiw  于 2023-08-07  发布在  Swift
关注(0)|答案(1)|浏览(112)

有谁知道如何更改SceneView对象的背景颜色吗?我尝试将background属性放置到SceneView中,但它不会改变它,仍然使用默认背景。In this case I wanted to the background be green, but it's keeps gray/white

import SwiftUI
import SceneKit

struct ContentView: View {
    var body: some View {
    
        ZStack{
            Color.red
            SceneView(
                scene: SCNScene(named: "Earth.scn"),
                options: [.autoenablesDefaultLighting,.allowsCameraControl]
            )
            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height/2, alignment: .center)
            .background(Color.green)
            .border(Color.blue, width: 3)
        
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

字符串

8aqjt8rx

8aqjt8rx1#

您看到的灰色来自SCNScenebackground属性。您需要将其设置为UIColor.green

struct ContentView: View {
    var body: some View {
        
        ZStack{
            Color.red
            SceneView(
                scene: {
                    let scene = SCNScene(named: "Earth.scn")!
                    scene.background.contents = UIColor.green /// here!
                    return scene
                }(),
                options: [.autoenablesDefaultLighting, .allowsCameraControl]
            )
            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height/2, alignment: .center)
            .border(Color.blue, width: 3)
            
        }
    }
}

字符串
测试结果:


的数据

相关问题