浏览代码

Update 'TestCameraImage' sample to support human depth and stencil

/4.0
Tim Mowrer 4 年前
当前提交
6bc2717d
共有 2 个文件被更改,包括 299 次插入37 次删除
  1. 184
      Assets/Scenes/CameraImage.unity
  2. 152
      Assets/Scripts/TestCameraImage.cs

184
Assets/Scenes/CameraImage.unity


m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.44657874, g: 0.49641275, b: 0.5748172, a: 1}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.49641287, b: 0.5748173, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:

m_Script: {fileID: 11500000, guid: fa850fbd5b8aded44846f96e35f1a9f5, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &456510980
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 456510981}
- component: {fileID: 456510983}
- component: {fileID: 456510982}
m_Layer: 5
m_Name: RawHumanStencilImage
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &456510981
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 456510980}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 485666366}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 0, y: -951}
m_SizeDelta: {x: 640, y: 480}
m_Pivot: {x: 0, y: 1}
--- !u!114 &456510982
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 456510980}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1344c3c82d62a2a41a3576d8abb8e3ea, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Texture: {fileID: 0}
m_UVRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
--- !u!222 &456510983
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 456510980}
m_CullTransparentMesh: 0
--- !u!1 &485666362
GameObject:
m_ObjectHideFlags: 0

m_LocalScale: {x: 0, y: 0, z: 0}
m_Children:
- {fileID: 1474869892}
- {fileID: 1218579280}
- {fileID: 456510981}
- {fileID: 901906344}
- {fileID: 642201223}
- {fileID: 1756864493}

- {fileID: 1791068495}
- {fileID: 773643825}
m_Father: {fileID: 485666366}
m_RootOrder: 2
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 485666366}
m_RootOrder: 1
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 1}
m_AnchorMax: {x: 1, y: 1}

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

- target: {fileID: 2304810881573073946, guid: 4ee22c1f54b174122b6a766fe09e0de8,
type: 3}
propertyPath: m_RootOrder
value: 3
value: 5
objectReference: {fileID: 0}
- target: {fileID: 2304810881573073946, guid: 4ee22c1f54b174122b6a766fe09e0de8,
type: 3}

m_Name:
m_EditorClassIdentifier:
m_CameraManager: {fileID: 282840816}
m_RawImage: {fileID: 1474869893}
m_RawCameraImage: {fileID: 1474869893}
m_OcclusionManager: {fileID: 1335839536}
m_RawHumanDepthImage: {fileID: 1218579281}
m_RawHumanStencilImage: {fileID: 456510982}
m_ImageInfo: {fileID: 901906345}
--- !u!4 &1141330383
Transform:

m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1218579279
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1218579280}
- component: {fileID: 1218579282}
- component: {fileID: 1218579281}
m_Layer: 5
m_Name: RawHumanDepthImage
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &1218579280
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1218579279}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 485666366}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 0, y: 1}
m_AnchoredPosition: {x: 0, y: -475}
m_SizeDelta: {x: 640, y: 480}
m_Pivot: {x: 0, y: 1}
--- !u!114 &1218579281
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1218579279}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1344c3c82d62a2a41a3576d8abb8e3ea, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Texture: {fileID: 0}
m_UVRect:
serializedVersion: 2
x: 0
y: 0
width: 1
height: 1
--- !u!222 &1218579282
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1218579279}
m_CullTransparentMesh: 0
--- !u!1 &1248549346
GameObject:
m_ObjectHideFlags: 0

m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_Material: {fileID: 0}
m_Color: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_Component:
- component: {fileID: 1335839535}
- component: {fileID: 1335839534}
- component: {fileID: 1335839536}
m_Layer: 0
m_Name: AR Session Origin
m_TagString: Untagged

m_Father: {fileID: 0}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1335839536
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1335839533}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: b15f82cc229284894964d2d30806969d, type: 3}
m_Name:
m_EditorClassIdentifier:
m_HumanSegmentationStencilMode: 1
m_HumanSegmentationDepthMode: 1
--- !u!1 &1449726330
GameObject:
m_ObjectHideFlags: 0

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

- component: {fileID: 1474869894}
- component: {fileID: 1474869893}
m_Layer: 5
m_Name: RawImage
m_Name: RawCameraImage
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []

152
Assets/Scripts/TestCameraImage.cs


[Tooltip("The ARCameraManager which will produce frame events.")]
ARCameraManager m_CameraManager;
/// <summary>
/// <summary>
/// Get or set the <c>ARCameraManager</c>.
/// </summary>
public ARCameraManager cameraManager

}
[SerializeField]
RawImage m_RawImage;
RawImage m_RawCameraImage;
public RawImage rawImage
public RawImage rawCameraImage
get { return m_RawImage; }
set { m_RawImage = value; }
get { return m_RawCameraImage; }
set { m_RawCameraImage = value; }
[SerializeField]
[Tooltip("The AROcclusionManager which will produce human depth and stencil textures.")]
AROcclusionManager m_OcclusionManager;
public AROcclusionManager occlusionManager
{
get => m_OcclusionManager;
set => m_OcclusionManager = value;
}
[SerializeField]
RawImage m_RawHumanDepthImage;
/// <summary>
/// The UI RawImage used to display the image on screen.
/// </summary>
public RawImage rawHumanDepthImage
{
get { return m_RawHumanDepthImage; }
set { m_RawHumanDepthImage = value; }
}
[SerializeField]
RawImage m_RawHumanStencilImage;
/// <summary>
/// The UI RawImage used to display the image on screen.
/// </summary>
public RawImage rawHumanStencilImage
{
get { return m_RawHumanStencilImage; }
set { m_RawHumanStencilImage = value; }
}
[SerializeField]
Text m_ImageInfo;

}
}
unsafe void OnCameraFrameReceived(ARCameraFrameEventArgs eventArgs)
unsafe void UpdateCameraImage()
XRCameraImage image;
if (!cameraManager.TryGetLatestImage(out image))
if (!cameraManager.TryGetLatestImage(out XRCpuImage image))
{
return;
}

"Image info:\n\twidth: {0}\n\theight: {1}\n\tplaneCount: {2}\n\ttimestamp: {3}\n\tformat: {4}",
image.width, image.height, image.planeCount, image.timestamp, image.format);
// Once we have a valid XRCameraImage, we can access the individual image "planes"
// (the separate channels in the image). XRCameraImage.GetPlane provides
// Once we have a valid XRCpuImage, we can access the individual image "planes"
// (the separate channels in the image). XRCpuImage.GetPlane provides
// See XRCameraImage.FormatSupported for a complete list of supported formats.
// See XRCpuImage.FormatSupported for a complete list of supported formats.
if (m_Texture == null || m_Texture.width != image.width || m_Texture.height != image.height)
if (m_CameraTexture == null || m_CameraTexture.width != image.width || m_CameraTexture.height != image.height)
m_Texture = new Texture2D(image.width, image.height, format, false);
m_CameraTexture = new Texture2D(image.width, image.height, format, false);
var conversionParams = new XRCameraImageConversionParams(image, format, CameraImageTransformation.MirrorY);
var conversionParams = new XRCpuImage.ConversionParams(image, format, XRCpuImage.Transformation.MirrorY);
var rawTextureData = m_Texture.GetRawTextureData<byte>();
var rawTextureData = m_CameraTexture.GetRawTextureData<byte>();
try
{
image.Convert(conversionParams, new IntPtr(rawTextureData.GetUnsafePtr()), rawTextureData.Length);

// We must dispose of the XRCameraImage after we're finished
// We must dispose of the XRCpuImage after we're finished
m_Texture.Apply();
m_CameraTexture.Apply();
m_RawImage.texture = m_Texture;
m_RawCameraImage.texture = m_CameraTexture;
Texture2D m_Texture;
void UpdateHumanDepthImage()
{
if (occlusionManager == null)
return;
// Attempt to get the latest human depth image. If this method succeeds,
// it acquires a native resource that must be disposed (see below).
if (!occlusionManager.TryAcquireHumanDepthCpuImage(out XRCpuImage image))
return;
using (image)
{
if (m_HumanDepthTexture == null || m_HumanDepthTexture.width != image.width || m_HumanDepthTexture.height != image.height)
{
m_HumanDepthTexture = new Texture2D(image.width, image.height, TextureFormat.R8, false);
}
var rawData = m_HumanDepthTexture.GetRawTextureData<byte>();
rawData.CopyFrom(image.GetPlane(0).data);
m_HumanDepthTexture.Apply();
m_RawHumanDepthImage.texture = m_HumanDepthTexture;
}
}
void UpdateRawImage(RawImage rawImage, XRCpuImage cpuImage)
{
var texture = rawImage.texture as Texture2D;
if (texture == null || texture.width != cpuImage.width || texture.height != cpuImage.height)
{
texture = new Texture2D(cpuImage.width, cpuImage.height, cpuImage.format.AsTextureFormat(), false);
rawImage.texture = texture;
Debug.Log($"Texture2D has {texture.GetRawTextureData<byte>().Length} bytes.");
}
Debug.Log($"Plane 0 has {cpuImage.GetPlane(0).data.Length} bytes");
texture.GetRawTextureData<byte>().CopyFrom(cpuImage.GetPlane(0).data);
texture.Apply();
}
void UpdateHumanStencilImage()
{
if (occlusionManager == null)
return;
if (m_RawHumanStencilImage == null)
return;
// Attempt to get the latest human stencil image. If this method succeeds,
// it acquires a native resource that must be disposed (see below).
if (!occlusionManager.TryAcquireHumanStencilCpuImage(out XRCpuImage image))
return;
using (image)
{
var texture = m_RawHumanStencilImage.texture as Texture2D;
if (texture == null || texture.width != image.width || texture.height != image.height)
{
texture = new Texture2D(image.width, image.height, TextureFormat.RFloat, false);
m_RawHumanStencilImage.texture = texture;
Debug.Log($"Texture2D has {texture.GetRawTextureData<byte>().Length} bytes.");
}
Debug.Log($"Plane 0 has {image.GetPlane(0).data.Length} bytes");
texture.GetRawTextureData<byte>().CopyFrom(image.GetPlane(0).data);
texture.Apply();
}
}
void OnCameraFrameReceived(ARCameraFrameEventArgs eventArgs)
{
UpdateCameraImage();
UpdateHumanDepthImage();
UpdateHumanStencilImage();
}
Texture2D m_CameraTexture;
Texture2D m_HumanDepthTexture;
}
正在加载...
取消
保存