ビルドの構成
アプリでアニメーションを有効にするようにビルドを構成するには、build.gradle
ファイルを変更し、プロジェクトにアニメーションをインポートします。
app's
build.gradle
を更新して、シーン アニメーションの依存関係を追加して、インポートしたアニメーション モデルのレンダリングをサポートするようにします。dependencies { … // Support for animated model renderables. implementation "com.google.ar.sceneform:animation:1.15.0" }
*.fbx
アニメーション ファイルをインポートしてプレビューし、インポートしたモデルを含む*.sfb
ファイルを取得します。
ランタイムでのアニメーションの使用
ランタイム オペレーションを使用して、次のことを行います。
レンダリング可能なアニメーションの作成
ランタイムでは、ModelRenderable.Builder
を使用して *.sfb
を読み込み、シーン内のノードにアタッチします。これは、他の ModelRenderable
の場合と同様です。
// Create the ModelRenderable.
ModelRenderable.builder()
.setSource(this, R.raw.andy)
.build()
.thenAccept(renderable -> andyRenderable = renderable)
.exceptionally(
throwable -> {
Log.e(TAG, "Unable to load Renderable.", throwable);
return null;
});
// Attach the ModelRenderable to the node in the scene.
Node andyNode = new Node();
andyNode.setParent(arFragment.getArSceneView().getScene());
andyNode.setRenderable(andyRenderable);
アニメーション データへのアクセス
// Get the animation data called "andy_dance" from the `andyRenderable`.
AnimationData danceData = andyRenderable.getAnimationData("andy_dance");
異なるメタデータに基づくアニメーションへのアクセス
// Get the animation name.
danceData.getName();
アニメーション データのインスタンスを取得するには、ModelRenderable.getAnimationData()
メソッドを使用します。
// Access animations by index.
numAnimations = andyRenderable.getAnimationDataCount();
danceData = andyRenderable.getAnimationData(0);
再生を操作する
再生を制御する ModelAnimator
を作成します。
ModelAnimator andyAnimator = new ModelAnimator(danceData, andyRenderable);
start()
を使用して、アニメーションを再生します。最後に自動的に停止します。
andyAnimator.start();
アニメーションをループさせるには、setRepeatCount()
を使用します。
andyAnimator.setRepeatCount(<number of repeats>)
(省略可)プロパティ アニメーション オペレーションを追加する
ModelAnimator
は Android の Animator
クラスを拡張し、ループ、イベントへの対応、非線形インターポレータなど、より高度な操作を可能にします。
SkeletonNode
を使用してモデルを特定し、ボーンにアタッチします。
ボーンを含むレンダリング可能なアイテムを扱う場合は、SkeletonNode
クラスを使用して、ノードにボーンをアタッチすることで、スケルトン内の個々のボーンにアクセスします。これにより、オブジェクトをボーンに「アタッチ」したり、オブジェクトをその位置を制御したりできます。
アニメーションの再生中に、接続されたノードの位置、スケール、向きがフレームごとに SkeletonNode
によって更新されます。アタッチされたノードの位置、縮尺、回転を設定すると、ボーンがアニメーションで次に更新されるまでボーンがオーバーライドされます。
アニメーション サンプルでは、アンディの「頭」のために帽子のモデルを含む Node
をアタッチしています。アンディがアニメーション表示されたとき、帽子は頭に残ります。
骨に関する情報へのアクセス
ModelRenderable
でボーンに関する情報にアクセスするには、getBoneCount()
、getBoneName()
、または getBoneParent()
メソッドを使用します。
// Get the number of bones in the model’s skeleton.
andyRenderable.getBoneCount();
// Get the names of the bones in the model’s skeleton.
andyRenderable.getBoneName();
// Get the hierarchy of the bones in the model’s skeleton.
andyRenderable.getBoneParent();
SkeletonNode
の操作
SkeletonNode
クラスは、ノードを特定のボーンにアタッチするためにモデルのスケルトンを公開します。
SkeletonNode
を使用するには、新しいインスタンス化を作成し、スケルトンを持つモデルを含むレンダリング可能をレンダリング可能に設定します。
andyWithSkeleton = new SkeletonNode();
andyWithSkeleton.setRenderable(andyRenderable);
andyWithSkeleton.setParent(scene);
レンダリング可能なものを特定のボーンにアタッチするには、まず新しいノードを作成してそのボーンにアタッチします。レンダリング可能なノードを含むノードを最初のノードの子として追加します。ボーンのスケーリングと回転がノードの相対変換の設定に使用されないようにするには、2 つ目のノードのスケールと位置をリセットします。
hatNode = new Node();
Node boneNode = new Node();
boneNode.setParent(andy);
andy.setBoneAttachment(HAT_BONE_NAME, boneNode);
hatNode.setRenderable(hatRenderable);
hatNode.setParent(boneNode);
hatNode.setWorldScale(Vector3.one());
hatNode.setWorldRotation(Quaternion.identity());
Vector3 pos = hatNode.getWorldPosition();