usdz 对象在加载 SCNReferenceNode 时没有移动
usdz object is not moving while loaded with SCNReferenceNode
我正在按照 Apple 文档和示例项目使用 .SCN 文件和虚拟对象(SCNReferenceNode 的子class)加载 3d 对象 class 但突然我需要从 .scn 更改模型到 usdz。现在我的 usdz 对象加载成功但它不在表面上(在空中的中途)并且我不能像(点击,平移,旋转)那样与它交互......是否有任何其他方式与 usdz 对象进行交互并且我怎样才能把它放在表面上,就像我以前用 .scn 文件做的那样
获取模型URL(从服务器下载)
static let loadDownloadedModel : VirtualObject = {
let downloadedScenePath = getDocumentsDirectory().appendingPathComponent("\(Api.Params.inputModelName).usdz")
return VirtualObject(url: downloadedScenePath)!
}()
正在从 URL
加载
func loadVirtualObject(_ object: VirtualObject, loadedHandler: @escaping (VirtualObject) -> Void) {
isLoading = true
loadedObjects.append(object)
// Load the content asynchronously.
DispatchQueue.global(qos: .userInitiated).async {
object.reset()
object.load()
self.isLoading = false
loadedHandler(object)
}
}
场景中放置
func placeObjectOnFocusSquare() {
virtualObjectLoader.loadVirtualObject(VirtualObject.loadDownloadedModel) { (loadedObject) in
DispatchQueue.main.async {
self.placeVirtualObject(loadedObject)
self.setupBottomButtons(isSelected: true)
}
}
}
func placeVirtualObject(_ virtualObject: VirtualObject) {
guard let cameraTransform = session.currentFrame?.camera.transform,
let focusSquarePosition = focusSquare.lastPosition else {
statusViewController.showMessage("CANNOT PLACE OBJECT\nTry moving left or right.")
return
}
Api.Params.selectedModel = virtualObject
virtualObject.name = String(Api.Params.inputPreviewId)
virtualObject.scale = SCNVector3(Api.Params.modelXAxis, Api.Params.modelYAxis, Api.Params.modelZAxis)
virtualObject.setPosition(focusSquarePosition, relativeTo: cameraTransform, smoothMovement: false)
updateQueue.async {
self.sceneView.scene.rootNode.addChildNode(virtualObject)
}
}
.usdz 场景视图中的对象
经过多次尝试,终于发现是模型动态缩放导致的问题,参考这篇
我将所有轴(x、y 和 z)的对象缩放为 0.01
virtualObject.scale = SCNVector3Make(0.01, 0.01, 0.01)
我正在按照 Apple 文档和示例项目使用 .SCN 文件和虚拟对象(SCNReferenceNode 的子class)加载 3d 对象 class 但突然我需要从 .scn 更改模型到 usdz。现在我的 usdz 对象加载成功但它不在表面上(在空中的中途)并且我不能像(点击,平移,旋转)那样与它交互......是否有任何其他方式与 usdz 对象进行交互并且我怎样才能把它放在表面上,就像我以前用 .scn 文件做的那样
获取模型URL(从服务器下载)
static let loadDownloadedModel : VirtualObject = {
let downloadedScenePath = getDocumentsDirectory().appendingPathComponent("\(Api.Params.inputModelName).usdz")
return VirtualObject(url: downloadedScenePath)!
}()
正在从 URL
加载 func loadVirtualObject(_ object: VirtualObject, loadedHandler: @escaping (VirtualObject) -> Void) {
isLoading = true
loadedObjects.append(object)
// Load the content asynchronously.
DispatchQueue.global(qos: .userInitiated).async {
object.reset()
object.load()
self.isLoading = false
loadedHandler(object)
}
}
场景中放置
func placeObjectOnFocusSquare() {
virtualObjectLoader.loadVirtualObject(VirtualObject.loadDownloadedModel) { (loadedObject) in
DispatchQueue.main.async {
self.placeVirtualObject(loadedObject)
self.setupBottomButtons(isSelected: true)
}
}
}
func placeVirtualObject(_ virtualObject: VirtualObject) {
guard let cameraTransform = session.currentFrame?.camera.transform,
let focusSquarePosition = focusSquare.lastPosition else {
statusViewController.showMessage("CANNOT PLACE OBJECT\nTry moving left or right.")
return
}
Api.Params.selectedModel = virtualObject
virtualObject.name = String(Api.Params.inputPreviewId)
virtualObject.scale = SCNVector3(Api.Params.modelXAxis, Api.Params.modelYAxis, Api.Params.modelZAxis)
virtualObject.setPosition(focusSquarePosition, relativeTo: cameraTransform, smoothMovement: false)
updateQueue.async {
self.sceneView.scene.rootNode.addChildNode(virtualObject)
}
}
.usdz 场景视图中的对象
经过多次尝试,终于发现是模型动态缩放导致的问题,参考这篇
我将所有轴(x、y 和 z)的对象缩放为 0.01
virtualObject.scale = SCNVector3Make(0.01, 0.01, 0.01)