HelloARの中身をあさる その1

f:id:gamaspecial:20200830230227p:plain

うえから順にみてこー

ARCoreDevice

ARCoreSession

UnityシーンでARCore セッションを管理するコンポーネント

そのまますぎる

f:id:gamaspecial:20200830230402p:plain

なんだか Session Config ってのから設定値とってARの設定をうんたらかんたらするやつらしい

ARCoreSessionConfig

developers.google.com

f:id:gamaspecial:20200830232023p:plain

  • MatchCameraFramerate
    チェックつけると"デバイスのカメラフレームレートにあわせて"、Unity側のフレームレートを遅延させる

  • PlaneFindingMode

    使用する平面検索モードを選択します。

    水平だけ、垂直だけ、両方、無効 のいずれかを設定する

f:id:gamaspecial:20200830232703p:plain

  • EnableLightEstimation
    環境光推定のOn/Off
    簡単に、暗くなったらARもそれにあわせて暗くなるよ、みたいなことらしい
    これについてはこんど「Environment Light」といっしょにみてく

  • EnableCloudAnchor
    クラウドアンカーをつかうかどうか
    HelloARではfalseだった

なんでも、アプリで生成したアンカーをネットワークを介して他のユーザーと共有できるものらしい
なんかおもしろそうなので今度触ってみたい!!
inon29.hateblo.jp

  • AugmentedImageDatabase
    AugmentedImageDatabase をつかうかどうか、なんだけど
    こいつについてはしょうみよくわからなかったのでもうちょっと調査が必要、、、

  • CameraFocusMode 固定か、オートフォーカス

まぁもろもろのAR設定をするやつだな、うん

HelloARController

HelloARController.csより抜粋

タップからオブジェクト生成まで

            // If the player has not touched the screen, we are done with this update.
            Touch touch;
            if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
            {
                return;
            }

            // Should not handle input if the player is pointing on UI.
            if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
            {
                return;
            }

            // Raycast against the location the player touched to search for planes.
            TrackableHit hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                TrackableHitFlags.FeaturePointWithSurfaceNormal;

            if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
            {
                // Use hit pose and camera pose to check if hittest is from the
                // back of the plane, if it is, no need to create the anchor.
                if ((hit.Trackable is DetectedPlane) &&
                    Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position,
                        hit.Pose.rotation * Vector3.up) < 0)
                {
                    Debug.Log("Hit at back of the current DetectedPlane");
                }
                else
                {
                    if (DepthMenu != null)
                    {
                        // Show depth card window if necessary.
                        DepthMenu.ConfigureDepthBeforePlacingFirstAsset();
                    }

                    // Choose the prefab based on the Trackable that got hit.
                    GameObject prefab;
                    if (hit.Trackable is FeaturePoint)
                    {
                        prefab = GameObjectPointPrefab;
                    }
                    else if (hit.Trackable is DetectedPlane)
                    {
                        DetectedPlane detectedPlane = hit.Trackable as DetectedPlane;
                        if (detectedPlane.PlaneType == DetectedPlaneType.Vertical)
                        {
                            prefab = GameObjectVerticalPlanePrefab;
                        }
                        else
                        {
                            prefab = GameObjectHorizontalPlanePrefab;
                        }
                    }
                    else
                    {
                        prefab = GameObjectHorizontalPlanePrefab;
                    }

                    // Instantiate prefab at the hit pose.
                    var gameObject = Instantiate(prefab, hit.Pose.position, hit.Pose.rotation);

                    // Compensate for the hitPose rotation facing away from the raycast (i.e.
                    // camera).
                    gameObject.transform.Rotate(0, k_PrefabRotation, 0, Space.Self);

                    // Create an anchor to allow ARCore to track the hitpoint as understanding of
                    // the physical world evolves.
                    var anchor = hit.Trackable.CreateAnchor(hit.Pose);

                    // Make game object a child of the anchor.
                    gameObject.transform.parent = anchor.transform;
                }
            }
        }

座標取得箇所

            TrackableHit hit;
            TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon |
                TrackableHitFlags.FeaturePointWithSurfaceNormal;

            if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
            {

Frame.Raycast関数

developers.google.com

タップしたとこが平面(ARCoreで検知した)であればtrueを返しその座標をhit に代入することで座標を取得でき

取得した座標にオブジェクトを配置することで平面上に物がおかれているようにみえるってことだーね

PlaneGenerator

平面検出時に表示する格子状のやつ
↓これね
f:id:gamaspecial:20200830235814p:plain

あきたのでいったんおわり!のこりは その2でやろうそうしよう