一般注意从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();