2

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
Community
  • 1
  • 1
Amit Pal
  • 10,604
  • 26
  • 80
  • 160

1 Answers1

3

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
    ...
}
M Reza
  • 18,350
  • 14
  • 66
  • 71
  • Thanks for the answer!. Can you please check the #edit section as well. I am getting a compile error. – Amit Pal Dec 19 '18 at 09:32
  • 1
    @AmitPal You're welcome, have you tried this? : https://stackoverflow.com/questions/39652867/code-sign-error-in-macos-high-sierra-xcode-resource-fork-finder-information – M Reza Dec 19 '18 at 09:34
  • Oh Yes, I was about to try it. Thanks though. BTW I was playing with the `geometry` property of `SCNNode` by using this code ` objectNode.geometry?.firstMaterial?.diffuse.contents = imageToDisplay objectNode.geometry?.firstMaterial?.diffuse.contents = imageToDisplay objectNode.geometry?.firstMaterial?.multiply.contents = imageToDisplay objectNode.geometry?.firstMaterial?.multiply.intensity = 0.5 objectNode.geometry?.firstMaterial?.lightingModel = .constant` But it's simply showing the object. How can I wrap the image around it? – Amit Pal Dec 19 '18 at 09:37
  • Try setting it using Xode's scene visual editor, see if you can change it from there! – M Reza Dec 19 '18 at 09:43
  • Ah! actually. The plan is to load an image dynamically from URL around 3d object. setting up from scene visual editor will not work I guess. – Amit Pal Dec 19 '18 at 09:46
  • That's right. Maybe this'd be helpful: https://stackoverflow.com/questions/46024466/not-able-to-add-texture-image-to-a-cube-in-arkit/51405460 – M Reza Dec 19 '18 at 09:48
  • I am doing the same thing. If I am using `SCNBox` or other predefined `SCNNod` then Image is perfectly wrapping around it but not with the `3d` objects. – Amit Pal Dec 19 '18 at 09:54