我为获取深度数据所做的是创建自己的继承自 RawImage 的类。我使用我的自定义类作为深度渲染流的目标,并从我的类中的纹理组件中获取图像。
绑定到自定义类
在我的情况下,我想将 16 位深度数据转换为 8 位 pr 通道 rgb pgn,以便我可以将其导出为灰度图像。这是我解析图像数据的方式:
byte[] input = texture.GetRawTextureData();
//create array of pixels from texture
//remember to convert to texture2D first
Color[] pixels = new Color[width*height];
//converts R16 bytes to PNG32
for(int i = 0; i < input.Length; i+=2)
{
//combine bytes into 16bit number
UInt16 num = System.BitConverter.ToUInt16(input, i);
//turn into float with range 0->1
float greyValue = (float)num / 2048.0f;
alpha = 1.0f;
//makes pixels outside measuring range invisible
if (num >= 2048 || num <= 0)
alpha = 0.0f;
Color grey = new Color(greyValue, greyValue, greyValue, alpha);
//set grey value of pixel based on float
pixels.SetValue(grey, i/2);
}
要获取像素,您可以简单地访问新的像素数组。