首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XNA - HeightData到heightMap img

XNA - HeightData到heightMap img
EN

Stack Overflow用户
提问于 2012-05-03 02:51:02
回答 2查看 389关注 0票数 0

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

代码语言:javascript
复制
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格式只有黑色。:/这是一个好的方法,怎么做,还是有什么问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-03 04:09:46

Alpha不应为零

代码语言:javascript
复制
     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;
票数 2
EN

Stack Overflow用户

发布于 2012-05-03 04:09:25

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

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

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

然后试一试。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10419885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档