Scene Semantic APIとは?
ARCoreの拡張機能の1つです。カメラ画像からセグメンテーションを行い、pixelがどのセグメントに割り当てられているかを計算し、Textureとして取得可能です。
引用元:Understand the user's environment with the Scene Semantics API
デモ
先にデモをお見せします。空の領域にのみオブジェクトが描画されるデモです。オブジェクトが建物に遮蔽されているように見えるので、疑似的なオクルージョン表現が可能になります。
バージョン情報
| ツール、ライブラリ | バージョン | 
|---|---|
| Unity | 2022.3.24f1 | 
| ARCore Extensions | 1.46.0 | 
オクルージョンのロジック
セグメントした任意の領域にのみ描画することで、オクルージョンされているような見え方を実現します。今回は以下フローで実装しました。
- 
MainCameraのnear clipギリギリの位置にQuadを置く 
- 
QuadをMainCameraの描画範囲にぴったり重ねる 
- 
SceneSemanticAPIで取得したTexutureを加工する 
- 
Shader側にて、3で加工したTextureを受け取り、オクルージョン処理を行う 
1. MainCameraのnear clipギリギリの位置にQuadを置く
この工程では遮蔽物としてQuadを配置しています。以後の過程で、このQuadを用いて任意のセグメント以外の描画を遮断するイメージです。
near clipが0.1なのに対して、Quadの位置を0.1005にしておきます。

2. QuadをMainCameraの描画範囲にぴったり重ねる
Quadが正しいピクセルを遮断できるように、カメラの描画範囲とQuadがぴったりと重なるように計算します。

3. SceneSemanticAPIで取得したTexutureを加工する
セグメント結果を利用しやすいように加工します。Shader側でR成分の値を元にオクルージョンの適用範囲を設定するので、任意のセグメントを赤色に塗りつぶしたTextureを動的に生成します。以下は空(sky)のセグメントを赤色に塗りつぶした想定のシミュレーションです。
4. Shader側にて、3で加工したTextureを受け取り、オクルージョン処理を行う
任意のセグメントのみ赤色に塗りつぶされたTextureが渡ってくるので、R成分を調べて塗り分けます。以下画像のように、赤色だった部分にはオブジェクトが描画され、それ以外の部分にはオブジェクトが描画されない(skyboxのみ映る)、という状態を作り出せました。
上記はUnityEditor上でのシミュレーションですが、実機の場合、オブジェクトが描画されない部分にはカメラ映像が映し出されることになります。
サンプルコード
以下サンプルコード全文です。下準備としてARCoreを利用可能なプロジェクトを作って色々と下準備が必要ですが、そこは省略します。
using System.Collections;
using Google.XR.ARCoreExtensions;
using UnityEngine;
public class CustomSceneSemanticManager : MonoBehaviour
{
    [SerializeField] private ARSemanticManager _semanticManager;
    [SerializeField] private GameObject _semanticQuad;
    [SerializeField] private Sprite _debugImage;
    [SerializeField] private Material _semanticMaterial;
    [SerializeField] private float _checkForSupportFrequency = 0.1f;
    [SerializeField] private int _maxAttemptsCheckForSupport = 5;
    [SerializeField] private SemanticLabel[] _labels;
    private Texture2D _semanticTexture;
    private Texture2D _outputTexture;
    private bool _isSemanticModeSupported;
    private bool _isQuadSizeInitialized;
    private static readonly int TextureId = Shader.PropertyToID("_SemanticTex");
    private void Start()
    {
        if (Application.isEditor)
        {
            ResizeQuadToScreen(_debugImage.texture.width, _debugImage.texture.height);
            _semanticMaterial.SetTexture(TextureId, _debugImage.texture);
            return;
        }
        // Semantic APIがサポートされているかチェック開始。
        StartCoroutine(CheckForSemanticFeatureSupport());
    }
    private void Update()
    {
        if (!_isSemanticModeSupported) return;
        _semanticMaterial.SetTexture(TextureId, RequestSemanticTexture(ref _outputTexture) ? _outputTexture : null);
        if (!_semanticMaterial.GetTexture(TextureId)) return;
        // 画面サイズにぴったり合うようにQuadのサイズを調整する。
        // 何度も行う必要はないので、SemanticTextureが取得できたら1回だけ行う。
        if (_isQuadSizeInitialized) return;
        _isQuadSizeInitialized = true;
        ResizeQuadToScreen(_semanticTexture.width, _semanticTexture.height);
    }
    /// <summary>
    /// Semantic APIがサポートされているか確認する。
    /// </summary>
    private IEnumerator CheckForSemanticFeatureSupport()
    {
        var checkForSupportAttempts = 0;
        while (true)
        {
            if (_semanticManager.IsSemanticModeSupported(SemanticMode.Enabled) == FeatureSupported.Supported)
            {
                _isSemanticModeSupported = true;
                break;
            }
           
            checkForSupportAttempts++;
            if (checkForSupportAttempts >= _maxAttemptsCheckForSupport) break;
            yield return new WaitForSeconds(_checkForSupportFrequency);
        }
    }
    /// <summary>
    /// 画面サイズにぴったり合うようにQuadのサイズを調整する。
    /// Note:ScreenSpaceのCanvasを使えばぴったり合せられるのでは?と思ってやってみたが、なぜかずれるのでこのやり方を採用している。
    /// </summary>
    private void ResizeQuadToScreen(int imgWidth, int imgHeight)
    {
        var imageAspectRatio = imgHeight / (float)imgWidth;
        var cameraFovRad = Camera.main.fieldOfView * Mathf.Deg2Rad;
        var quadHeightAtDistance = 2.0f * _semanticQuad.transform.localPosition.z * Mathf.Tan(cameraFovRad / 2.0f);
        var quadWidthAtDistance = quadHeightAtDistance * imageAspectRatio;
        _semanticQuad.transform.localScale = new Vector3(quadWidthAtDistance, quadHeightAtDistance, 1);
    }
    private bool RequestSemanticTexture(ref Texture2D result)
    {
        if (!_semanticManager.TryGetSemanticTexture(ref _semanticTexture))
        {
            return false;
        }
        ConvertR8ToRGBA32Flipped(_semanticTexture,ref result);
        return true;
    }
    private void ConvertR8ToRGBA32Flipped(Texture2D semanticInputTexture, ref Texture2D outputTexture)
    {
        var width = semanticInputTexture.width;
        var height = semanticInputTexture.height;
        outputTexture = new Texture2D(height, width, TextureFormat.RGBA32, false);
        var rawSemanticTextureData = semanticInputTexture.GetRawTextureData<byte>();
        var pixels = new Color[rawSemanticTextureData.Length];
        for (var i = 0; i < height; i++)
        {
            for (var j = 0; j < width; j++)
            {
                var label = (SemanticLabel)rawSemanticTextureData[i * width + j];
                var color = GetColor(label);
                var index = outputTexture.width * outputTexture.height - (j * outputTexture.width + i + 1);
                pixels[index] = color;
            }
        }
        outputTexture.SetPixels(pixels);
        outputTexture.Apply();
    }
    private Color GetColor(SemanticLabel semanticLabel)
    {
        var notLabeledColor = new Color(0, 0, 0, 0);
        foreach (var label in _labels)
        {
            return label == semanticLabel ? Color.red : notLabeledColor;
        }
        return notLabeledColor;
    }
}
以下は、_SemanticTexを受け取って利用するShaderです。
Shader "Custom/SceneSemantic"
{
    Properties
    {
        _SemanticTex("Semantic Texture", 2D) = "white" {}
    }
    SubShader
    {
        Pass
        {
            Tags
            {
                "Queue"="Geometry"
            }
            Blend One OneMinusSrcAlpha
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
            sampler2D _SemanticTex;
            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }
            float4 frag(v2f i) : SV_Target
            {
                float4 semanticCol = tex2D(_SemanticTex, i.uv);
                float interpolation = saturate(semanticCol.r);
                if (interpolation > 0.5)
                {
                    discard;
                }
                return 0;
            }
            ENDCG
        }
    }
}
課題
横持ちにするとおかしくなります。issueも立てましたが、特に対応の見込みは無いとのことです。

対策書きました。→ 【Unity】モバイル端末の横持検知をジャイロで行う






