简体   繁体   English

SceneKit – 在 SwiftUI 中渲染时,相机看不到 model

[英]SceneKit – Camera does not see a model when rendering in SwiftUI

I have a USDZ model that when imported in to SceneKit scene graph shows fine, if I set the preview to show from the cameras perspective it is in frame.我有一个 USDZ model 导入到 SceneKit 场景图时显示正常,如果我将预览设置为从相机角度显示它在帧中。 However when I run it via SwiftUI preview, on simulator or on device it does not render.但是,当我在模拟器或设备上通过 SwiftUI 预览运行它时,它不会呈现。 I've turned on camera controls on the simulator to look over the scene and it's simply not rendering rather than out of frame.我已经打开了模拟器上的相机控制来查看场景,它根本没有渲染而不是超出框架。

在此处输入图像描述

在此处输入图像描述

import SceneKit

struct HomeScreen: View {
    private var userDefaults: UserDefaults?
    var scene = SCNScene(named: "MyScene.scn")
    var cameraNode: SCNNode? {
        scene?.rootNode.childNode(withName: "camera", recursively: false)
    }
    var body: some View {
        VStack {
            
            SceneView(
                scene: scene,
                pointOfView: cameraNode,
                options: [.allowsCameraControl]
            )
        }
    }
}

struct SwiftUIView_Previews: PreviewProvider {
    static var previews: some View {
        HomeScreen()
    }
}

SCN scene SCN场景

Loading a .scn model using SwiftUI's SceneView is as simple as that.使用 SwiftUI 的 SceneView 加载.scn model 就这么简单。 Model is centered in frustum. Model 以平截头体为中心。

If you don't use .autoenablesDefaultLighting option, the background will not be visible.如果您不使用.autoenablesDefaultLighting选项,则背景将不可见。

import SwiftUI
import SceneKit

struct ContentView: View {
    
    var scene = SCNScene(named: "art.scnassets/ship.scn")

    var options: SceneView.Options = [.autoenablesDefaultLighting,
                                      .allowsCameraControl ]   
    var body: some View {
        ZStack {
            SceneView(scene: scene, options: options)
                .ignoresSafeArea()
        }
    }
}

在此处输入图像描述


USDZ model USDZ model

.usdz models are centered in SceneKit's camera frustum as well. .usdz模型也以 SceneKit 的相机视锥为中心。 However, in SceneKit + SwiftUI or in SceneKit + UIKit , you are obliged to turn a lighting on for .usdz models.但是,在SceneKit + SwiftUISceneKit + UIKit中,您必须为.usdz模型打开照明。

var scene = SCNScene(named: "gramophone.usdz")

var options: SceneView.Options = [.autoenablesDefaultLighting,
                                  .allowsCameraControl ]

var body: some View {
    ZStack {
        SceneView(scene: scene, options: options).ignoresSafeArea()
        let _ = scene?.background.contents = UIColor.black

    }
}

If your model is rendered out of a center of a screen, there might be another invisible node in that framing.如果您的 model 呈现在屏幕中心之外,则该框架中可能存在另一个不可见节点。 Or model's pivot point is offset.或者型号的 pivot 点偏移。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM