2
votes

I am new in ARKit2 and followed a couple of tutorials and official documentation.

problem

How to add 3d object as SCNNode()?

Code

let artFrame = SCNBox(width: CGFloat(w), height: CGFloat(h), length: 0.002, chamferRadius: 0.02)
artFrame.firstMaterial?.diffuse.contents = imageToDisplay
let artFrameNode = SCNNode(geometry: artFrame)
artFrameNode.physicsBody = SCNPhysicsBody(type: .static, shape: SCNPhysicsShape(geometry: artFrame, options: nil))
artFrameNode.position = SCNVector3Make(Float(x), Float(y), Float(z*3))
sceneView.scene.rootNode.addChildNode(artFrameNode)

As per the above code, I am using predefined SCNBox in SCNNode. Is there any way that I can use 3d object such as .dae, .obj instead of SCNBox and wrap with Image?

I check out the documentation and it says that you can add Mesh objects to SCNNode :

https://developer.apple.com/documentation/scenekit/scnnode/1419841-init

Edit

Whenever I am adding a new .dae and converting to .scn. Xcode is throwing follwing error:

/Users/paly/Library/Developer/Xcode/DerivedData/sample-bohiilnhthuwfkechtmhscucgccc/Build/Products/Debug-iphoneos/sample.app: resource fork, Finder information, or similar detritus not allowed
Command CodeSign failed with a nonzero exit code
1

1 Answers

3
votes

To add .dae or .obj object to the scene, you need to get childNode of the .dae object and simply add it to your scene. I recommend to convert your .dae file to .scn simply using Xcode's Editor menu.

You also need the node's name, which can be accessed in Scene Graph View. Just click on your .obj file and click the Scene Graph View button on bottom left corner of the Xcode scene editor. Here, my node's name is "objectNode":

enter image description here

Now you can add the 3D object as a SCNNode to your scene:

override func viewDidLoad() {
    super.viewDidLoad()
    ...

    let objectScene = SCNScene(named: "object.scn") // Here, add your .obj or .scn file
    let objectNode: SCNNode = objectScene.rootNode.childNode(withName: "YourObjectName", recursively: true) // Get the object name from Scene Graph View, which is "objectNode" for me.
    objectNode.position = SCNVector3(0,0,-4)

    let scene = SCNScene() // Main scene of the app
    scene.rootNode.addChildNode(objectNode)
    sceneView.scene = scene
    ...
}