【问题标题】:swift SceneKit object is emptyswift SceneKit 对象为空
【发布时间】:2020-05-13 18:04:42
【问题描述】:

我试图在屏幕上显示地球模型,从 here 加载模型(尝试过和其他相同的模型),但 id 没有显示在屏幕上

[let scene = SCNScene(named: "tierra-1.obj")

if scene == nil {
    print("nuuuul")
    return
}
// 2: Add camera node
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
// 3: Place camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 0)

// 4: Set camera on scene
scene?.rootNode.addChildNode(cameraNode)

// 5: Adding light to scene
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light?.type = .omni
lightNode.position = SCNVector3(x: 0, y: 0, z: 0)
scene?.rootNode.addChildNode(lightNode)

// 6: Creating and adding ambien light to scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light?.type = .ambient
ambientLightNode.light?.color = UIColor.darkGray
scene?.rootNode.addChildNode(ambientLightNode)

// If you don't want to fix manually the lights
sceneView.autoenablesDefaultLighting = true

// Allow user to manipulate camera
sceneView.allowsCameraControl = true

// Show FPS logs and timming
sceneView.showsStatistics = true

// Set background color
sceneView.backgroundColor = UIColor.white

// Allow user translate image
sceneView.cameraControlConfiguration.allowsTranslation = false

// Set scene settings
sceneView.scene = scene][2]

但加载后我的屏幕是空的 (example),如何解决?

【问题讨论】:

    标签: swift scenekit


    【解决方案1】:

    我使用自定义 obj 复制并运行了您的代码,我将其加载并在视口中。

    我使用节点加载了一个 obj,而不是尝试将 obj 作为场景本身加载。

    我注意到您的相机位于世界空间 0,0,0。这很可能在网格内部,因为材质默认启用了背面剔除,所以网格看起来是透明的。

    我将材质指定为蓝色,以便在资产非常小的情况下更容易发现。我建议在 XCode 中选择资产,您可以看到边界框大小。这将有助于更好地了解您需要将相机移动多远才能看到网格。

    import ModelIO
    import SceneKit
    import SceneKit.ModelIO
    import PlaygroundSupport
    
    let scene = SCNScene()
    let sceneView = SCNView(frame: CGRect(x:0, y:0, width:800, height:800))
    // Set scene settings
    sceneView.scene = scene
    
    // 2: Add camera node
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    // 3: Place camera
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 10)
    
    // 4: Set camera on scene
    scene.rootNode.addChildNode(cameraNode)
    
    // Material
    let blueMaterial = SCNMaterial()
    blueMaterial.diffuse.contents = UIColor.blue
    
    // Accessing the Playground resource folder
    let path = Bundle.main.path(forResource:"randomAsset", ofType: "obj")
    let asset = MDLAsset(url: URL(string: path!)!)
    let object = asset.object(at: 0)
    let objNode = SCNNode(mdlObject: object)
    objNode.geometry?.materials = [blueMaterial]
    scene.rootNode.addChildNode(objNode)
    
    // Allow user to manipulate camera
    sceneView.allowsCameraControl = true
    
    // Show FPS logs and timming
    //sceneView.showsStatistics = true
    
    // Set background color
    sceneView.backgroundColor = UIColor.gray
    
    // Allow user translate image
    sceneView.cameraControlConfiguration.allowsTranslation = true
    
    PlaygroundPage.current.setLiveView(sceneView)
    
    

    【讨论】:

    • 感谢您的回答!你能解释一下你是如何计算“z”的吗?
    • 当您有一个要加载的对象时,在 XCode 中选择该对象,您应该能够查看该对象的视图,该视图在右侧也会有一些额外的属性数据.在右栏中将有一个边界框。这是对象适合的盒子。因此,如果您将相机移动到大于边界框的距离,您应该能够看到对象。如果您的相机位于对象(几何体)内部,您将什么也看不到,因为应用于几何体的默认材质不是双面的,因此所有背面都会被剔除(看起来不可见)
    • 你也可以在 SCNNode 上调用 .boundingbox 来获取边界框的大小,然后用它来定位你的相机。
    【解决方案2】:

    我认为 SceneKit 不支持将 Obj 作为场景加载。它们目前只允许能够导出场景数据的类型,例如相机、灯光。 https://developer.apple.com/documentation/scenekit/scnscenesource

    如果你想加载 obj 文件,你必须将它导入到你的场景中。 这篇文章应该可以帮助https://forums.developer.apple.com/thread/3979

    【讨论】:

    猜你喜欢
    • 2015-12-25
    • 2016-01-13
    • 2022-06-30
    • 2019-02-14
    • 2020-12-13
    • 1970-01-01
    • 2013-10-14
    • 2015-12-27
    • 1970-01-01
    相关资源
    最近更新 更多