根据@rickster 的建议,我查看了 ARKit 2.0 Unity 插件的实现,并设法在我的 Unity 场景中实际使用了AREnvironmentProbeAnchor.environmentTexture。
MTLTexture 有一个名为textureType 的属性,它是一个枚举值,对于AREnvironmentProbeAnchor.environmentTexture 返回的纹理是.typeCubeArray。这在 MTLTexture documentation page 上有详细解释。
具有texturetype 类型为.typeCubeArray 的MTLTexture 意味着当您将指向此MTLTexture 的指针传递给Unity 端时,您可以使用它来创建Cubemap,然后您可以使用它当您的反射探测环境纹理时。以下是 Unity 方面的大致工作方式:
// You can pass the pointer to your MTLTexture as an IntPtr to the Unity side
[DllImport("__Internal")]
public static extern IntPtr GetEnvironmentTexture();
void AddNewProbe()
{
var texturePtr = GetEnvironmentTexture();
if (texturePtr == IntPtr.Zero)
{
continue;
}
var cubemap = Cubemap.CreateExternalTexture(0, TextureFormat.R8, false, texturePtr);
var probeComponent = AddComponent<ReflectionProbe>();
probeComponent.customBakedTexture = cubemap;
}
在 iOS 端,您只需要使用您在 Unity 端声明的名称的方法,它返回指向您的 MTLTexture 的指针。它的返回类型可以是void* 或id<MTLTexture>,它们都可以正常工作。这个方法应该放在你的 Unity 桥上,以便它对 Unity 端可见。
extern "C" void* GetEnvironmentTexture() {
AREnvironmentProbeAnchor* anchor = [self updatedEnvironmentProbeAnchor];
return (__bridge_retained void*) [anchor environmentTexture];
}
您可以(坦率地说,应该)修改和改进它以满足您的需求。