【问题标题】:How to change the background color of a SceneView 3D Object in SwiftUI如何在 SwiftUI 中更改 SceneView 3D 对象的背景颜色
【发布时间】:2021-08-18 09:12:24
【问题描述】:

有人知道如何更改 SceneView 对象的背景颜色吗? 我正在尝试将背景属性放置到 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()
    }
}

【问题讨论】:

  • 您可以尝试“.colorMultiply(Color.green)”而不是“.background(Color.green)”,但我不确定副作用。

标签: swift xcode swiftui scenekit


【解决方案1】:

您看到的灰色来自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)
            
        }
    }
}

结果:

【讨论】:

  • 如何让它透明?我尝试了 UIColor.clear 但它改为白色。
  • SwiftUI 中的 @SuscaBogdan SceneView 尚不支持此功能,但这可能会有所帮助:stackoverflow.com/q/67557700/14351818
猜你喜欢
  • 1970-01-01
  • 2021-02-11
  • 2020-01-01
  • 2022-08-12
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 2020-10-11
  • 1970-01-01
相关资源
最近更新 更多