I am working on an ARKit app using Swift in Xcode trying to load models from a remote server but am having an issue getting the model and the textures / materials to load together and have the textures / materials display correctly.
I have gone through a few links and tutorials and get the model to load but the materials do not show. I have created models in the scene editor, or downloaded them and converted to .scn files, locate them in Finder and then uploaded them to a web-server. Just the .scn file and material (images).
//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!)
}
I would like the model to load correctly as it would locally, showing the model with the applied textures but instead I get the model with no textures, just colors or white where the material is expected.
I receive the following error which looks like it is trying to load it locally?
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]>
Any help is appreciated and if there is a better way to do this please let me know. Thank you!