Search code examples
iosswiftunity-game-engineaugmented-realityarkit

Detecting a real world object using ARKit with iOS


I am currently playing a bit with ARKit. My goal is to detect a shelf and draw stuff onto it.

I did already find the ARReferenceImage and that basically works for a very, very simple prototype, but the image needs to be quite complex it seems? Xcode always complains if I try to use something a lot simpler (like a QR-Code like image). With that marker I would know the position of an edge and then I'd know the physical size of my shelf and know how to place stuff into it. So that would be ok, but I think small and simple markers will not work, right?

But ideally I would not need a marker at all. I know that I can detect e.g. planes, but I want to detect the shelf itself. But as my shelf is open, it's not really a plane. Are there other possibilities to find an object using ARKit?

I know that my question is very vague, but maybe somebody could point me in the right direction. Or tell me if that's even possible with ARKit or if I need other tools? Like Unity?


Solution

  • There are several different possibilities for positioning content in augmented reality. They are called content anchors, and they are all subclasses of the ARAnchor class.

    Image anchor

    Using an image anchor, you would stick your reference image on a pre-determined spot on the shelf and position your 3D content relative to it.

    the image needs to be quite complex it seems? Xcode always complains if I try to use something a lot simpler (like a QR-Code like image)

    That's correct. The image needs to have enough visual detail for ARKit to track it. Something like a simple black and white checkerboard pattern doesn't work very well. A complex image does.

    Object anchor

    Using object anchors, you scan the shape of a 3D object ahead of time and bundle this data file with your app. When a user uses the app, ARKit will try to recognise this object and if it does, you can position your 3D content relative to it. Apple has some sample code for this if you want to try it out quickly.

    Manually creating an anchor

    Another option would be to enable ARKit plane detection, and have the user tap a point on the horizontal shelf. Then you perform a raycast to get the 3D coordinate of this point. You can create an ARAnchor object using this coordinate, and add it to the ARSession. Then you can again position your content relative to the anchor.

    You could also implement a drag gesture to let the user fine-tune the position along the shelf's plane.

    Conclusion

    Which one of these placement options is best for you depends on the use case of your app. I hope this answer was useful :)

    References

    There are a lot of informative WWDC videos about ARKit. You could start off by watching this one: https://developer.apple.com/videos/play/wwdc2018/610