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

fel*_*ode 4 xcode scenekit swift swiftui

有谁知道如何更改 SceneView 对象的背景颜色?我尝试将背景属性放置到 SceneView 中,但它不会改变它,仍然具有默认背景。在这种情况下,我希望背景是绿色的,但它保持灰色/白色

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()
    }
}
Run Code Online (Sandbox Code Playgroud)

ahe*_*eze 12

您看到的灰色来自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)
            
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果:

SceneView 中的绿色背景

  • 我怎样才能使它透明?我尝试了 UIColor.clear 但它使它变成白色。 (3认同)