LoginSignup
4
1

More than 5 years have passed since last update.

[Unity]ImageAnchorのサンプルが動かなかった人へ[ARKit]

Posted at

はじめに

ARKit 1.5で追加されたImageAnchor。
画像を認識してその座標を取得できる便利なもの(※詳しくはこちらの記事を参照ください)なのですが、

僕の環境のUnityARKitPluginのサンプルではどうもうまく動きませんでした。(2018.10.22現在)

Deployment Targetを12.0に設定したら動いた!!

スクリーンショット 2018-10-22 19.43.03.png

フォーラムでそう言ってた(小並感)
https://forum.unity.com/threads/arkit-2-0-beta-support.534639/#post-3522275

要するにこういうことらしい。

ARWorldTrackingConfigurationで画像認識を使うと固まる問題
https://qiita.com/k-boy/items/7ce8263392dfa7acaca4

XCodeのバージョンが上がるまで我慢するしかないね😢

ImageAnchorの実装方法

1. ARReferenceImageを作成

スクリーンショット 2018-10-22 19.06.44.png

2. パラメータを設定して・・・

Physical Sizeは1で1メートルなので0.1(10cm)を設定。

スクリーンショット 2018-10-22 19.10.55.png

3. ARReferenceImagesを作成

スクリーンショット 2018-10-22 19.06.55.png

4. 認識したい数だけReferenceImageを設定しましょう

スクリーンショット 2018-10-22 19.19.45.png

5. Unity AR Camera ManagerにReferenceImagesを設定

スクリーンショット 2018-10-22 19.21.11.png

6. Sampleのスクリプトから「Generate Image Anchor」を設定

スクリーンショット 2018-10-22 19.24.31.png

※ 僕の環境だと、認識精度が低く、すぐにオブジェクトが消滅してしまうので以下のようにコードを変更しました

    void UpdateImageAnchor(ARImageAnchor arImageAnchor)
    {
        Debug.LogFormat("image anchor updated[{0}] : tracked => {1}", arImageAnchor.identifier, arImageAnchor.isTracked);
        if (arImageAnchor.referenceImageName == referenceImage.imageName) {
            if (arImageAnchor.isTracked)
            {
                if (!imageAnchorGO.activeSelf)
                {
                    imageAnchorGO.SetActive(true);
                }
                imageAnchorGO.transform.position = UnityARMatrixOps.GetPosition(arImageAnchor.transform);
                imageAnchorGO.transform.rotation = UnityARMatrixOps.GetRotation(arImageAnchor.transform);
            }
            else if (imageAnchorGO.activeSelf)
            {
                // imageAnchorGO.SetActive(false); // <= コメントアウト
            }
        }

    }

    void RemoveImageAnchor(ARImageAnchor arImageAnchor)
    {
        Debug.LogFormat("image anchor removed[{0}] : tracked => {1}", arImageAnchor.identifier, arImageAnchor.isTracked);
        if (imageAnchorGO) {
            // GameObject.Destroy (imageAnchorGO); // <= コメントアウト
        }
    }

楽しいARライフを!

Twitterフォローしてね!!
https://twitter.com/mutun__

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1