简体   繁体   English

如何从 a.usdz 创建 SCNNode?

[英]How to create a SCNNode from a .usdz?

I have downloaded the.usdz models provided by Apple: https://developer.apple.com/arkit/gallery/我下载了Apple提供的.usdz模型: https://developer.apple.com/arkit/gallery/

But now, I want to create a SCNNode with one of these models, so I am doing this to obtain the node:但是现在,我想用这些模型之一创建一个 SCNNode,所以我这样做是为了获取节点:

guard let urlPath = Bundle.main.url(forResource: "retrotv", withExtension: "usdz") else {
    return
}
let mdlAsset = MDLAsset(url: urlPath)
let modelRootNode = SCNScene(mdlAsset: mdlAsset).rootNode

Then I add it to the scene and the result is this:然后我将它添加到场景中,结果是这样的:

在此处输入图像描述

Why does it have no textures?为什么它没有纹理?

I have the downloaded.usdz files into a folder in my project directory as you can see:我已将 downloaded.usdz 文件放入项目目录中的一个文件夹中,如您所见:

在此处输入图像描述

The correct way to add the .USDZ object is actually creating the scene with the file's URL:添加 .USDZ 对象的正确方法实际上是使用文件的 URL 创建场景:

 let scene = try! SCNScene(url: usdzURL, options: [.checkConsistency: true])

Or even creating via Reference Node:甚至通过参考节点创建:

 let referenceNode = SCNReferenceNode(url: usdzURL)
 referenceNode.load()

It is possible to load a usdz into an ARKit scene.可以将 usdz 加载到 ARKit 场景中。

It's important to have the these imports重要的是有这些进口

import ARKit
import SceneKit
import SceneKit.ModelIO

Load the usdz via a URL.通过 URL 加载 usdz。

guard let urlPath = Bundle.main.url(forResource: "retrotv", withExtension: "usdz") else {
    return
}
let mdlAsset = MDLAsset(url: urlPath)
// you can load the textures on an MDAsset so it's not white
mdlAsset.loadTextures()

Wrap it in a node.将其包裹在一个节点中。

let asset = mdlAsset.object(at: 0) // extract first object
let assetNode = SCNNode(mdlObject: asset)

You can now attach this node in ARKit.您现在可以在 ARKit 中附加此节点。 You would need to scale and orientate the object to where you want it in the real world, but that code depends on what you are trying to do, so I've left that out.您需要在现实世界中将对象缩放和定向到您想要的位置,但该代码取决于您要执行的操作,因此我已将其排除在外。

The correct way to access the Internal USDZ Node访问内部USDZ节点的正确方法

func usdzNodeFrom(file: String, exten: String, internal_node: String) -> SCNNode? {
    let rootNode = SCNNode()
    let scale = 1

    guard let fileUrl = Bundle.main.url(forResource: file, withExtension: exten) else {
        fatalError()
    }
    
    let scene = try! SCNScene(url: fileUrl, options: [.checkConsistency: true])
    let node = scene.rootNode.childNode(withName: internal_node, recursively: true)!
    node.name = internal_node
    let height = node.boundingBox.max.y - node.boundingBox.min.y
    node.position = SCNVector3(0, 0, 0)
    tNode.scale = SCNVector3(scale, scale, scale)
    rootNode.addChildNode(tNode)
    return rootNode
}

Further texture not displaying is different Issue, could be addressed by adding lightning to environment.不显示的进一步纹理是不同的问题,可以通过向环境添加闪电来解决。

The literal answer is that the OP forgot to load the textures.字面上的答案是 OP 忘记加载纹理。

It's easy to do,这很容易做到,

asset.loadTextures()

For 2023. It's very easy to make your.usdz a node.对于 2023 年。让 your.usdz 成为一个节点非常容易。

The secret to understanding, is that a.usdz file contains (in theory) one or more different items or characters.理解的秘诀是 a.usdz 文件包含(理论上)一个或多个不同的项目或字符。

The secret is to take the "first" (only) character in the file.秘诀是获取文件中的“第一个”(唯一)字符。

Almost always, a usdz file just has the one object. So you simply have the take the "first" (almost always only) object.几乎总是,一个 usdz 文件只有一个 object。所以你只需取“第一个”(几乎总是只有)object。

var fireDragon: SCNNode!

func loadDragonModel() {
  let p = Bundle.main.url(forResource: "Amy1", withExtension: "usdz")!
  let ass = MDLAsset(url: p)
  ass.loadTextures()
  fireDragon = SCNNode(mdlObject: ass.object(at: 0))
}

That's it.就是这样。 And then,然后,

  // .. some node of yours .. .addChildNode(fireDragon)
  // for example
  castleWall.addChildNode(fireDragon)

This is for SceneKit and also ARKit.这适用于 SceneKit 和 ARKit。

All of this applies in SceneKit and ARKit.所有这些都适用于 SceneKit 和 ARKit。 It works perfectly on tvOS, iOS, pad, phone, mac etc.它完美适用于 tvOS、iOS、pad、手机、mac 等。

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

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