如何让SceneView的背景透明?

Aru*_*yan 9 scenekit swiftui

我想打开一个 3D 模型并使其背景透明,以便我可以看到 SceneView 后面的 UI。我试过这段代码,但 sceneView 变成白色,不透明。


struct ModelView: View {
    var body: some View {
        ZStack {
            Text("Behind Text Behind Text Behind Text")
            SceneView(
                scene: { () -> SCNScene in
                    let scene = SCNScene()
                    scene.background.contents = UIColor.clear
                    return scene
                }(),
                pointOfView: { () -> SCNNode in
                    let cameraNode = SCNNode()
                    cameraNode.camera = SCNCamera()
                    cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
                    return cameraNode
                }(),
                options: [
                    .allowsCameraControl,
                    .temporalAntialiasingEnabled,
                ]
            )
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用 XCode 12.5 和 iPhone 8。

编辑1:

感谢下面的评论,我决定尝试新的方法,但它们仍然不起作用。

方法#1

首先,我尝试通过 UIViewRepresentable 使用 SCNView 创建 MySceneView:

struct MySceneView: UIViewRepresentable {
    typealias UIViewType = SCNView
    typealias Context = UIViewRepresentableContext<MySceneView>

    func updateUIView(_ uiView: UIViewType, context: Context) {}
    func makeUIView(context: Context) -> UIViewType {
        let view = SCNView()
        view.allowsCameraControl = true
        view.isTemporalAntialiasingEnabled = true
        view.autoenablesDefaultLighting = true
        view.scene = MySceneView.scene
        return view
    }
    
    static let scene: SCNScene = {
        let scene = SCNScene(named: "art.scnassets/man.obj")!
        scene.background.contents = UIColor.clear
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
        scene.rootNode.addChildNode(cameraNode)
        return scene
    }()
}
Run Code Online (Sandbox Code Playgroud)

方法#2 我尝试使用 SpriteView,代码如下:

        ZStack {
            Text("Behind Text Behind Text Behind Text")
            SpriteView(scene: { () -> SKScene in
                let scene = SKScene()
                scene.backgroundColor = UIColor.clear
                let model = SK3DNode(viewportSize: .init(width: 200, height: 200))
                model.scnScene = MySceneView.scene
                scene.addChild(model)
                return scene
            }(), options: [.allowsTransparency])
}
Run Code Online (Sandbox Code Playgroud)

K K*_*K B 5

我无法在这里找到完整的工作片段,但感谢 Arutyun 的回答,我成功地编译了一个工作解决方案,而不需要SpriteKit.

import SceneKit
import SwiftUI

struct MySceneView: UIViewRepresentable {
    typealias UIViewType = SCNView
    typealias Context = UIViewRepresentableContext<MySceneView>

    func updateUIView(_ uiView: UIViewType, context: Context) {}
    func makeUIView(context: Context) -> UIViewType {
        let view = SCNView()
        view.backgroundColor = UIColor.clear // this is key!
        view.allowsCameraControl = true
        view.autoenablesDefaultLighting = true
        // load the object here, could load a .scn file too
        view.scene = SCNScene(named: "model.obj")!
        return view
    }
}
Run Code Online (Sandbox Code Playgroud)

并像常规视图一样使用它:

import SwiftUI

struct MySceneView: View {
    var body: some View { 
        ZStack{
            // => background views here
            MySceneView()
                .frame( // set frame as required
                    maxWidth: .infinity,
                    maxHeight: .infinity,
                    alignment: .center
                )
        }
    }
}

struct MySceneView_Previews: PreviewProvider {
    static var previews: some View {
        MySceneView()
    }
}
Run Code Online (Sandbox Code Playgroud)


Aru*_*yan 2

更新:

一个更简单的解决方案是使用 UIViewRepresentable,创建 SCNView 并将 backgroundColor 设置为清除

老的:

谢谢 George_E,你的 SpriteKit 想法完美地实现了。这是代码:

SpriteView(scene: { () -> SKScene in
    let scene = SKScene()
    scene.backgroundColor = UIColor.clear
    let model = SK3DNode(viewportSize: .init(width: 200, height: 200))
    model.scnScene = {
        let scene = SCNScene(named: "art.scnassets/man.obj")!
        scene.background.contents = UIColor.clear
        let cameraNode = SCNNode()
        cameraNode.camera = SCNCamera()
        cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
        scene.rootNode.addChildNode(cameraNode)
        return scene
    }()
    scene.addChild(model)
    return scene
}(), options: [.allowsTransparency])
Run Code Online (Sandbox Code Playgroud)