在“统一”中,高度图在内部被缩短为Int16 (注意,只使用0-32767 )。我想发送高度图到GPU,理想情况下只使用16位。
这样做的最佳方法似乎是将高差图值编码为RG16呈现纹理(因为在“统一”中,我不能选择单一的16位整数格式),并根据需要打包/解压。
下面是我发送给GPU的高度图纹理(通过CPU端的C#脚本):
const int Size = 1024;
Color32[] colours = new Color32[Size * Size];
for (int y = 0; y < Size; y++) {
for (int x = 0; x < Size; x++) {
float height = heights[y, x]; // With Unity it's correct to flip the x/y axes
short s = (short)(heights[y, x] * short.MaxValue);
byte upper = (byte)(s >> 7); // Shift 7 and not 8 since we don't want values outside of 0-32767 anyway.
byte lower = (byte)(s & 255);
colours[y * size + x] = new Color32(upper, lower, 0, 0);
}
}
tex.SetPixels32(colours);
tex.Apply();
我遇到了一个令人困惑的问题,在解压高度图样本时,这里的精度似乎有很大的损失。我试过的方法是:
float HeightmapSample(float u, float v) {
fixed2 height = tex2Dlod(_Heightmap, float4(u, v, 0.f, 0.f)).rg;
int2 heightInt = height * 255.f;
int unpacked = (heightInt.r << 7) | heightInt.g;
return unpacked / 32767.f; // Renormalize
}
还有一种(可能)时髦的浮点法:
float HeightmapSample(float u, float v) {
fixed2 height = tex2Dlod(_Heightmap, float4(u, v, 0.f, 0.f)).rg;
return height.r + (height.g / 128.f) - (1.f / 128.f);
}
这两种方法都无法产生与实际真相相近的结果:
发布于 2018-07-22 03:41:21
经过近两天的追捕,我意识到我有一个着色器,每次加载插件时,都会错误地重新编码高度图。我高兴地没有修复那个着色器,因为我专注于上面的问题,但它变成了问题!
https://stackoverflow.com/questions/51454730
复制相似问题