6

I need some help with placing a 3D model with the new apple ARKit. Is it possible to place an object of type OBJ? I'm trying to place a 3d model of a skull.

//Load the OBJ file
let bundle = Bundle.main
guard let url = bundle.url(forResource: "Cranial", withExtension: "obj") else {
    fatalError("Failed to find model file")
}

let asset = MDLAsset(url:url)
guard let object = asset.object(at: 0) as? MDLMesh else {
    fatalError("Failed to get mesh from asset")
}

let scene = SCNScene()
let nodeCranial = SCNNode.init(mdlObject: object)
nodeCranial.simdPosition = float3(0, 0, 0.5)
sceneView.scene.rootNode.addChildNode(nodeCranial)

I don't know why nothing shows up in front of the camera. It works if I use the following line instead of the nodeCranial from above:

let cubeNode = SCNNode(geometry: SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0))

Why doesn't it work for OBJ file too? The code above is in the func viewDidLoad().

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
CoderOfTheForce
  • 359
  • 7
  • 22
  • 2
    How big is your head? 3D assets are often built to arbitrary scale, but in ARKit one unit of scene space maps to one real-world meter. If your OBJ mesh is, say, several tens of units wide, it might not appear the way you want in AR. You can check its size with the `boundingBox` property at run time or by loading the OBJ file in the scene editor in Xcode. – rickster Jul 17 '17 at 14:44

1 Answers1

9
    let tempScene = SCNScene(named: "art.scnassets/cat/cat.obj")!
    modelNode = tempScene.rootNode
    modelNode.simdPosition = float3(0, 0, 0.5)
    sceneView.scene.rootNode.addChildNode(modelNode)

you can load .obj, .scn or .dae files like this way.

Mayuri R Talaviya
  • 613
  • 1
  • 9
  • 14
  • .scn is working all fine with it but adding objc or other is not working. app is not showing anything neither is throwing any error. – Jamil Jul 12 '20 at 11:51