【问题标题】:XNA - HeightData to heightMap imgXNA - 高度数据到高度图 img
【发布时间】:2012-05-12 06:57:01
【问题描述】:

您好,我在内存中有 heightData,有时(当我编辑它时)我想将它保存为 jpg。这是我的代码:

float multi = 0.2f;
float[,] heightData = quadTree.HeightData;
Color[] heightMapColors = new Color[heightData.Length];

for (int x = 0; x < heightData.GetLength(0); x++)
{
    for (int y = 0; y < heightData.GetLength(1); y++)
    {
         byte colorData = (byte)(heightData[x, y] / multi);

         heightMapColors[x + y * heightData.GetLength(0)].R = colorData;
         heightMapColors[x + y * heightData.GetLength(0)].G = colorData;
         heightMapColors[x + y * heightData.GetLength(0)].B = colorData;
    }
}

Texture2D heightMap = new Texture2D(device, heightData.GetLength(0), heightData.GetLength(1), false, SurfaceFormat.Color);
heightMap.SetData<Color>(heightMapColors);

using (System.IO.Stream stream = System.IO.File.OpenWrite(@"D:\test.jpg"))
{
     heightMap.SaveAsJpeg(stream, heightData.GetLength(0), heightData.GetLength(1));
}

我 100% 确定我在 heightMapColors 中有数据,但保存的 jpg 只有黑色。 :/ 这是一个好方法还是有什么问题?

【问题讨论】:

    标签: c# colors xna texture2d


    【解决方案1】:

    Alpha 不应该为零

         heightMapColors[x + y * heightData.GetLength(0)].R = colorData;
         heightMapColors[x + y * heightData.GetLength(0)].G = colorData;
         heightMapColors[x + y * heightData.GetLength(0)].B = colorData;
         heightMapColors[x + y * heightData.GetLength(0)].A = 255;
    

    【讨论】:

    • 这可能不是问题,他似乎将其保存为 JPEG,它(大部分)不关心 alpha。
    • 你测试了吗?因为我几乎可以肯定是问题所在
    • 啊,他似乎将数据放入纹理中,这可能会预乘 alpha - 这就是为什么重要?好吧,无论如何,这是我能想到的唯一解释。很高兴你明白了!
    • 反正 jpeg 也支持 alpha 通道,我知道很少见,但暴雪在 warcraft3 纹理中使用它们...
    • 因此“大部分”:) 我自己在罕见的场合使用过它们。
    【解决方案2】:

    JPG 可能不是存储高度图的好格式,因为它是一种有损格式。您应该将其放入 BMP pr PNG 中。也就是说,你的“身高”范围是多少?看起来您的身高是一个浮点数,这意味着它可能不在正确的范围内,无法显示甚至转换为离散值。

    如果您允许的高度范围是 Xf 到 Yf,则将其转换为 0 - 255 范围using

    byteValue = (byte)(((OldValue - OldMin) * (255 - 0)) / (OldMax - OldMin)) + 0

    然后试一试。

    【讨论】:

      猜你喜欢
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-22
      • 2015-06-22
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      • 2020-03-23
      相关资源
      最近更新 更多