基于正方形内距离的递减回报
本文关键字:回报 距离 正方形 | 更新日期: 2023-09-27 18:29:15
简而言之,我正在将地形(二维高度图)从生成的值平滑回其原始值。
有一个6个单位的平坦区域,只使用生成的值,然后是一个3个单位的平滑区域,从生成的区域移回原始区域(总共9个)
在平坦区域中,所有的x、z值都被分配了一个y值,例如4,那么3个平滑单元应该移回零,意味着3,然后是2,然后是1,如果0是我们的原始数,这将使下一步(方程之外)回到0。
经过反复试验,我似乎需要分别计算角和边,因为角的最大距离是对角线,即4.24,而边的最大距离只有3。我已经尝试了很多冗长的方法来达到我现在的水平,但仍然不起作用。不用说,尽管我仍然会这么说,但我不是一个数学奇才。
有人能比我更好地理解这件事吗?
Vector3 pos = holeCenter - terrain.transform.position;
//flatten area
int posX = Mathf.FloorToInt(pos.x * (td.heightmapResolution / td.size.x));
int posZ = Mathf.FloorToInt(pos.z * (td.heightmapResolution / td.size.z));
float[,] heightMap = td.GetHeights(0, 0, td.heightmapResolution, td.heightmapResolution);
float height = heightMap[posZ, posX];
int modZone = 9;
int flatZone = 6;
for (int x = posX - modZone; x <= posX + modZone; x++) {
for (int z = posZ - modZone; z <= posZ + modZone; z++) {
//if within 6 of cave ent (flat zone)
if (x >= posX - flatZone && x <= posX + flatZone && z >= posZ - flatZone && z <= posZ + flatZone) {
heightMap[z, x] = height + Random.Range(-0.00015f, 0.00015f);
}
//otherwise apply gently to the three rings around the flat area
else {
//if on a corner
bool corner = false;
if (x < posX - flatZone) {
if (z > posZ + flatZone) {
corner = true;
}
else if (z < posZ - flatZone) {
corner = true;
}
}
else if (x > posX + flatZone) {
if (z > posZ + flatZone) {
corner = true;
}
else if (z < posZ - flatZone) {
corner = true;
}
}
if (corner) {
//apply the new height to the old height decreasingly based on distance
float dist = Mathf.Sqrt(Mathf.Pow(Mathf.Abs(posX - x) - flatZone, 2f) +
Mathf.Pow(Mathf.Abs(posZ - z) - flatZone, 2f));
float maxDist = Mathf.Sqrt(Mathf.Pow(modZone - flatZone, 2f) * 2);
float multiplier = dist / maxDist;
heightMap[z, x] = (heightMap[z, x] * multiplier) + (height * (1 - multiplier)) + Random.Range(-0.00015f, 0.00015f);
}
else { //for an edge, only one value is in the modZone, find which, then apply
if (x < posX - flatZone || x > posX + flatZone) {
float multiplier = (Mathf.Abs(x - posX) - flatZone) / 4f;
heightMap[x, z] = (heightMap[z, x] * multiplier) + (height * (1 - multiplier)) + Random.Range(-0.00015f, 0.00015f);
}
else {
float multiplier = (Mathf.Abs(z - posZ) - flatZone) / 4f;
heightMap[x, z] = (heightMap[z, x] * multiplier) + (height * (1 - multiplier)) + Random.Range(-0.00015f, 0.00015f);
}
}
}
}
}
td.SetHeights(0, 0, heightMap);
我回来后让它工作了。
int modZone = 11;
int flatZone = 7;
for (int x = posX - modZone; x <= posX + modZone; x++) {
for (int z = posZ - modZone; z <= posZ + modZone; z++) {
float dist = Mathf.Sqrt(Mathf.Pow(Mathf.Abs(x - posX), 2f) + Mathf.Pow(Mathf.Abs(z - posZ), 2f));
if (dist <= flatZone) { heightMap[z, x] = height + Random.Range(-0.00015f, 0.00015f); }
else {
float multiplier = Mathf.Clamp01((modZone - dist) / (modZone - flatZone));
heightMap[z, x] = (heightMap[z, x] * (1 - multiplier)) + ((height + Random.Range(0, 0.0003f)) * multiplier);
}
}
}
我放弃了平方法,但它效果很好,圆法效果很好。也可能在更多变的情况下工作得更好。