How to create a SCNNode from a .usdz?
The correct way to add the .USDZ object is actually creating the scene with the file's URL:
let scene = try! SCNScene(url: usdzURL, options: [.checkConsistency: true])
Or even creating via Reference Node:
let referenceNode = SCNReferenceNode(url: usdzURL)
referenceNode.load()
Load 3d usdz model from network in Scenekit view does not work?
I loaded the model this way and it worked for me:
import SceneKit.ModelIO
func loadModel(){
//1. Get The Path Of The Downloaded File
let downloadedScenePath = getDocumentsDirectory().appendingPathComponent("nike.usdz")
self.scnView.autoenablesDefaultLighting=true
self.scnView.showsStatistics=true
self.scnView.backgroundColor = UIColor.blue
let asset = MDLAsset(url: downloadedScenePath)
asset.loadTextures()
let scene = SCNScene(mdlAsset: asset)
self.scnView.scene=scene
self.scnView.allowsCameraControl=true
}
.usdz model has no texture when loaded into scene
Your scene has no light, that's why the object is showing dark. Just add a directional light to your scene:
let spotLight = SCNNode()
spotLight.light = SCNLight()
spotLight.light?.type = .directional
sceneView.scene.rootNode.addChildNode(spotLight)
Node name was overwritten by USDZ model
The name of your USDZ model isn't overridden. It's the peculiarities of SceneKit hit-testing and scene hierarchy. When you perform a hit-test search, SceneKit looks for SCNGeometry
objects (not a main node) along the ray you specify. So, all you need to do, once the hit-test is completed, is to find the corresponding parent nodes.
Try this code:
import SceneKit.ModelIO
class GameViewController: UIViewController {
var sceneView: SCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView = (self.view as! SCNView)
let scene = SCNScene(named: "art.scnassets/ship.scn")!
sceneView.scene = SCNScene()
sceneView.backgroundColor = .black
let recog = UITapGestureRecognizer(target: self, action: #selector(tap))
sceneView.addGestureRecognizer(recog)
// ASSET
let mdlAsset = MDLAsset(scnScene: scene)
let asset = mdlAsset.object(at: 0)
let node = SCNNode(mdlObject: asset.children[0])
node.name = "Main-Node-Name" // former "ship"
node.childNodes[0].name = "SubNode-Name" // former "shipMesh"
node.childNodes[0].childNodes[0].name = "Geo-Name" // former "Scrap_MeshShape"
sceneView.scene?.rootNode.addChildNode(node)
}
}
And your hit-testing method:
extension GameViewController {
@objc func tap(recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: sceneView)
let results = sceneView.hitTest(location)
guard recognizer.state == .ended else { return }
if results.count > 0 {
let result = results[0] as SCNHitTestResult
let node = result.node
print(node.name!) // Geo-Name
print(node.parent!.name!) // SubNode-Name
print(node.parent!.parent!.name!) // Main-Node-Name
}
}
}
usdz object is not moving while loaded with SCNReferenceNode
After so many tries , finally i found out that dynamic scaling of the model causing problem , Reference to this
iOS ARKit: Large size object always appears to move with the change in the position of the device camera
I scaled the object to 0.01 for all the axis (x,y and z)
virtualObject.scale = SCNVector3Make(0.01, 0.01, 0.01)
USDZ export from SceneKit results in dull models
The default lighting model is SCNLightingModelBlinn
which means the material used for the plane
geometry is not physically based. USDZ only supports physically based materials and upon export SceneKit will try to build a PBR material from the Blinn material, and will infer a roughness
on your behalf.
The fix consists in setting lightingModelName
to SCNLightingModelPhysicallyBased
.
Related Topics
Using Delegates to Transfer Data from One Tableview to Another
iOS 11 CPU Throttling and Idle Timer
How to Prevent Duplicates in Realmswift List
How to Dismiss a Modal Segue Then Perform Push Segue to New View Controller
Check the Position of the Xcuielement on Screen While Testing iOS Application Using Xctest
Case Insensitive Matching Search in String Array Swift 3
How to Open Application Using Url
How to Get a Low Res Image, or Thumbnail from the Alassetrepresentation in Swift
Upgrading to Swfit 3: Cannot Override 'Init' Which Has Been Marked Unavailable
Uitabbar Change Background Color of One UItabbaritem on iOS7
Whats the Correct Way, Using "Init" or "Didmove"
How to Use Pod in Notification Service Extension
Swift Uiview Opacity Programmatically
Is History API Broken on iOS? (Location Bar Doesn't Update on Pushstate)