1

我正在使用深度传感器(Orbbec Astra Pro)并希望在 Unity 中显示红外图像。我收到的数据是一个ushort[](或任何其他类型,大于 8 位)。
所以我可以创建一个单通道16位纹理infraredTexture = new Texture2D(infraredFrame.Width, infraredFrame.Height, TextureFormat.R16, false);,但我不知道如何用红外数据填充纹理,因为LoadRawTextureData只需要一个byte[]IntPtr

所以最后我希望在 Unity 中看到 16 位灰度纹理。这甚至可能吗?先感谢您!

4

1 回答 1

1

一般注意从TextureFormat.R16

目前,这种纹理格式仅对运行时或本机代码插件有用,因为不支持这种格式的纹理导入。

注意并不是所有的显卡都支持所有的纹理格式,使用 SystemInfo.SupportsTextureFormat 来检查。

R16如果您真的想使用格式纹理,我没有直接的解决方案,除非以某种方式将数据序列化为byte[]例如使用Buffer.BlockCopy-注意:根本不知道这会起作用!

ushort[] yourData = // wherever you get this from;
byte[] byteData = new byte[sizeof(ushort) * yourData.Length];

// On memory level copy the bytes from yourData into byteData
Buffer.BlockCopy(yourData, 0, byteData, 0, byteData.Length);

infraredTexture = new Texture2D(infraredFrame.Width, infraredFrame.Height, TextureFormat.R16, false);
infraredTexture.LoadRawTextureData(byteData);
infraredTexture.Apply();

再次不知道它是否以这种方式工作。


但是,我认为您可以简单地在 Unity 中“伪造”它。如果它只是用于显示纹理,您宁愿使用“普通”RGB24纹理,只需将 16 位单通道数据映射为灰度 24 位 RGB 颜色,然后使用Texture2D.SetPixels例如应用它

ushort[] yourData = // wherever you get this from;
var pixels = new Color[yourData.Length];

for(var i = 0; i < pixels.Length; i++)
{
    // Map the 16-bit ushort value (0 to 65535) into a normal 8-bit float value (0 to 1)
    var scale = (float)yourData[i] / ushort.MaxValue;
    // Then simply use that scale value for all three channels R,G and B
    // => grey scaled pixel colors
    var pixel = new Color(scale, scale, scale);
    pixels[i] = pixel;
}

infraredTexture = new Texture2D(infraredFrame.Width, infraredFrame.Height, TextureFormat.RGB24, false);

// Then rather use SetPixels to apply all these pixel colors to your texture
infraredTexture.SetPixels(pixels);
infraredTexture.Apply();
于 2021-09-13T15:15:10.997 回答