【发布时间】:2019-09-26 02:51:57
【问题描述】:
我正在开发一个在 Xcode 中使用 Swift 的 ARKit 应用程序,试图从远程服务器加载模型,但在将模型和纹理/材质加载在一起并正确显示纹理/材质时遇到问题。
我浏览了一些链接和教程并加载了模型,但没有显示材料。我在场景编辑器中创建了模型,或者下载了它们并转换为 .scn 文件,在 Finder 中找到它们,然后将它们上传到网络服务器。只是 .scn 文件和材料(图片)。
//Tap Gesture
@objc func handleTap(_ gesture: UITapGestureRecognizer) {
//hittest
let results = self.sceneView.hitTest(gesture.location(in: gesture.view), types: ARHitTestResult.ResultType.featurePoint)
//return first tap
guard let result: ARHitTestResult = results.first else {
return
}
//Set URL of location of model
let myURL = NSURL(string: "https://www.website.com/scnfiles/iPhoneX.scn")
//Try getting this url or return
guard let scene = try? SCNScene(url: myURL! as URL, options: nil) else {return}
//Set the node to be the model
let node = scene.rootNode.childNode(withName: "SketchUp", recursively: true)
//Set scale
node?.scale = SCNVector3(0.025,0.025,0.025)
//The material image is located in the same directory
node?.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "https://website/scnfiles/iPhoneX_Screen.jpg")
//set the position of the model
let position = SCNVector3Make(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)
node?.position = position
//Add to scene
self.sceneView.scene.rootNode.addChildNode(node!)
}
我希望模型能够像在本地一样正确加载,显示具有应用纹理的模型,但我得到的模型没有纹理,只有颜色或白色的预期材料。
我收到以下错误,看起来好像是在尝试在本地加载它?
ARKitModels[10386:3406637] [SceneKit] Error: Failed to load : <C3DImage 0x281e45180 src:file:///var/containers/Bundle/Application/233AE78F-748F-420B-96AD-30F591ADF80C/ARKitModels.app/material/iPhoneX_Screen.jpg [0.000000x0.000000]>
感谢您的帮助,如果有更好的方法,请告诉我。谢谢!
【问题讨论】: