0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[ARFaceTracker] ARFaceTrackerを認識してみる メモ

Posted at

ARKitでFactTrackingをしてみる

実際の実装方法
1 ARFactTrackingConfigurationをインスタンス化
2 ARsessionにセットしてrunする。

ここまで実装するとRealityKit側でARFaceAnchorに対応したエンティティを追加するようになる。

エンティティとは何か?
UnityでいうGameObject,SceneKitでいうNodeのこと。
これにコンポーネントをアタッチしていくことで機能や見た目を追加していく。ARKitではARViewを根元にして
木構造として分岐して表現される。

ARFaceAnchorを追加することでARKit側が生成してくれたエンティティを扱うためには下記のコードのようにARViewのSceneをsubscribeする必要がある。

var eventAddEntity: Cancellable?
var arView: ARView!
.
.
// エンティティが追加されたイベントをキャッチする。
func setupSubscribe() {
//このsubscribeは全てのエンティティの変化も検知する。検知した際に第二引数に設定したメソッドを呼ぶ
  eventAddEntity = arView.scene.subscribe(to: SceneEvents.DidAddEntity.self,
                                          実行したいメソッド)
}

SceneEventsとは

ARシーン上で起こるイベントの種類のこと
エンティティが追加されたときを示すDidAddEntity以外にも下記のものが存在する。

 SceneEvents.Update
// フレーム間隔ごとに1回トリガーされるイベント
    SceneEvents.AnchoredStateChanged
// アンカー状態が変更されたときにトリガーされるイベント。

Entityが追加されるたびに実行したいメソッドを定義する

// 実行したいメソッド例 DidAddEntityを引数から取ってこれるようになる
func 実行したいメソッド_ events: SceneEvents.DidAddEntity) {
// 追加されたEntityを変数に入れる。ただ、顔のトラッキングで生成されたEntityかどうかわからないので、、 HasModelという型に型キャストする
let entity = events.entity
}

HasModel型とは何か?
エンティティの見た目を定義するためのメッシュやマテリアルを提供するプロトコル

型キャストするメソッド

 // 顔のトラッキングのエンティティならHasModelにキャストして返す
    func asFaceEntity(_ entity: Entity) -> HasModel? {
        let component =  entity.components[SceneUnderstandingComponent.self] as? SceneUnderstandingComponent
        
        // シーン認識コンポーネント(物理空間をマッピングするためのコンポーネント)
        if entity.components.has(SceneUnderstandingComponent.self) && 
// シーン認識コンポーネントあるかどうか
            entity.components.has(ModelComponent.self) && 
// モデルコンポーネントあるかどうか エンティティの見た目を構成するコンポーネントかどうか
            component?.entityType == .face { 
// その中でも顔の見た目を構成するものかどうか
            
            return entity as? HasModel
        }
        return nil
    }

HasModelはMaterialを持っているので自由に表面をコーディングできる!

guard let entity = asFaceEntity(event.entity) else { return }
 entity.model?.materials = <-ここにMaterialの配列セットする
そしたらライブ映像の顔が指定したMaterialでコーディングされる
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?