Skip to content

Instantly share code, notes, and snippets.

Created November 27, 2020 10:21
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andijakl/21057f3f71d31e8d5bc5a169a938556b to your computer and use it in GitHub Desktop.
Save andijakl/21057f3f71d31e8d5bc5a169a938556b to your computer and use it in GitHub Desktop.
Convert the XRCpuImage to a Texture2D for AR Foundation.
// Source:
private static void UpdateRawImage(RawImage rawImage, XRCpuImage cpuImage)
// Get the texture associated with the UI.RawImage that we wish to display on screen.
var texture = rawImage.texture as Texture2D;
// If the texture hasn't yet been created, or if its dimensions have changed, (re)create the texture.
// Note: Although texture dimensions do not normally change frame-to-frame, they can change in response to
// a change in the camera resolution (for camera images) or changes to the quality of the human depth
// and human stencil buffers.
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;
// For display, we need to mirror about the vertical access.
var conversionParams = new XRCpuImage.ConversionParams(cpuImage, cpuImage.format.AsTextureFormat(), XRCpuImage.Transformation.MirrorY);
//Debug.Log("Texture format: " + cpuImage.format.AsTextureFormat()); -> RFloat
// Get the Texture2D's underlying pixel buffer.
var rawTextureData = texture.GetRawTextureData<byte>();
// Make sure the destination buffer is large enough to hold the converted data (they should be the same size)
Debug.Assert(rawTextureData.Length == cpuImage.GetConvertedDataSize(conversionParams.outputDimensions, conversionParams.outputFormat),
"The Texture2D is not the same size as the converted data.");
// Perform the conversion.
cpuImage.Convert(conversionParams, rawTextureData);
// "Apply" the new pixel data to the Texture2D.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment