Skip to content

Instantly share code, notes, and snippets.

@Lance5057
Last active December 3, 2018 06:57
Show Gist options
  • Save Lance5057/c7886b2702c38ddf955ec939e700ebcc to your computer and use it in GitHub Desktop.
Save Lance5057/c7886b2702c38ddf955ec939e700ebcc to your computer and use it in GitHub Desktop.
if (smoothRadius > 0)
{
for (int i = 0; i < smoothRepeat; i++)
{
for (int x = 0; x < hmWidth; x++)
for (int z = 0; z < hmHeight; z++)
{
Ray ray = new Ray(new Vector3(x + terrain.transform.position.x, 1000, z + terrain.transform.position.z), Vector3.down);
RaycastHit[] hit = Physics.RaycastAll(ray);
bool cont = true;
RaycastHit h = new RaycastHit();
foreach (RaycastHit hi in hit)
{
if (hi.collider.gameObject == parent.gameObject)
cont = false;
if (hi.collider.gameObject == terrain.gameObject)
h = hi;
}
if (cont)
{
Vector3 hitPoint = (h.point - terrain.transform.position);
int hitX = (int)(((h.point.x - terrain.transform.position.x) / terrain.terrainData.size.x) * hmWidth);
int hitZ = (int)(((h.point.z - terrain.transform.position.z) / terrain.terrainData.size.z) * hmHeight);
float avg = 0;
int count = 0;
avg += smooth(heights, hitX, hitZ, avg, 0, ref count);
//avg += smooth(heights, hitX - 1, hitZ, avg, 0, ref count);
//avg += smooth(heights, hitX, hitZ + 1, avg, 0, ref count);
//avg += smooth(heights, hitX, hitZ - 1, avg, 0, ref count);
heights[hitX, hitZ] = avg / count;
}
}
}
}
float smooth(float[,] heights, int x, int y, float avg, int radius, ref int count)
{
radius++;
count++;
if (radius > smoothRadius)
{
return avg;
}
if(x > 0 && x < heights.GetLength(0))
{
if (y > 0 && y < heights.GetLength(1))
{
avg += heights[x,y];
avg += smooth(heights, x + 1, y, avg, radius, ref count);
avg += smooth(heights, x - 1, y, avg, radius, ref count);
avg += smooth(heights, x, y + 1, avg, radius, ref count);
avg += smooth(heights, x, y - 1, avg, radius, ref count);
}
}
return avg;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment