1
1

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.

【Unity】AR FoundationのARCameraBackgroundでCustom Materialを使う

Posted at

UnityのAR Foundationでカメラから取得した映像を背面に描画するためにARCameraBackgroundコンポーネントを使用します。この記事では、ARCameraBackgroundコンポーネントでCustom Materialを設定し、カメラから取得した映像にエフェクトをかける方法について解説します。

この記事の内容はURP、ARKit(iOS)のみで検証しています。使用しているUnityおよびパッケージのバージョンは以下のようになっています。

  • Unity: 2021.2.10f1
  • Universal RP: 12.1.4
  • AR Foundation: 4.2.2
  • ARKit XR Plugin: 4.2.2

まず、Custom Materialに使用するシェーダーを作成します。/Packages/ARKit XR Plugin/Assets/Shaders/ARKitBackground.shaderを雛形として使用するので、これをAssetディレクトリ以下の適当な箇所にコピーします。

コピーしたシェーダーを編集してエフェクトを加えます。ここでは、カメラ画像の色を反転させるようにしています。

ARKitBackground.shaderの194行目あたり
fragment_output o;
// o.color = videoColor;
o.color = real4(1.0 - videoColor.rgb, videoColor.a);
o.depth = depthValue;
return o;

ビルドする際にエラーとなるので、次のようにしてLWRP向けのシェーダーバリアントが作成されないようにします。

ARKitBackground.shaderの38行目
// #pragma multi_compile_local __ ARKIT_BACKGROUND_URP ARKIT_BACKGROUND_LWRP
#pragma multi_compile_local __ ARKIT_BACKGROUND_URP

このシェーダーを使用するマテリアルを作成します。ARCameraBackgroundコンポーネントのUse Custom Materialプロパティを有効化し、Custom Materialプロパティに作成したマテリアルを設定します。

ビルドして実機で確認するとカメラ映像の色が反転することが確認できます。

ARCameraBackgroundCustomMaterial.jpeg

1
1
1

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?