duck

Untitled

Aug 4th, 2010
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.76 KB | None | 0 0
  1. DebugX.Log("Interpolating unset height cells - " + knownHeights.Length + " known heights / "+(heightmapSize*heightmapSize)+" cells");
  2. for (int x = 0; x < heightmapSize; ++x)
  3. {
  4.     DebugX.Log(x + "/" + heightmapSize);
  5.     yield return null;
  6.     for (int z = 0; z < heightmapSize; ++z)
  7.     {
  8.         if (!(setHeights[x, z]))
  9.         {
  10.             float totalWeight = 0;
  11.             float totalHeight = 0;
  12.             float totalDistances = 0;
  13.  
  14.             //                    have per cell, a distance calc to each known cell
  15.             //[16:34] <zeroZshadow> add the distances together
  16.             //[16:35] <zeroZshadow> then the value of every known cell is divided by the total distance
  17.             //[16:35] <zeroZshadow> and multiplyd by their own distance to the cell
  18.             //[16:35] <zeroZshadow> add all the results together
  19.             //[16:35] <zeroZshadow> next cell
  20.  
  21.  
  22.             // calculate total distances from all known height cells
  23.             foreach (float[] knownHeight in knownHeights)
  24.             {
  25.                 float knownX = knownHeight[0];
  26.                 float knownY = knownHeight[1];
  27.                 float knownZ = knownHeight[2];
  28.  
  29.                 float xDiff = (x - knownX);
  30.                 float zDiff = (z - knownZ);
  31.  
  32.                 float distSq = xDiff*xDiff + zDiff*zDiff;
  33.  
  34.                 // remember dist to current unknown cell, temporarily
  35.                 knownHeight[3] = knownY * distSq;
  36.  
  37.                 totalDistances += distSq;
  38.             }
  39.  
  40.             // calculate final height based on weighting each known height by its relative distance
  41.             float finalHeight = 0;
  42.             float totalDistancesRSQ = 1.0f/totalDistances;
  43.             foreach (float[] knownHeight in knownHeights)
  44.             {
  45.                 float knownX = knownHeight[0];
  46.                 float knownY = knownHeight[1];
  47.                 float knownZ = knownHeight[2];
  48.                 float ownDist = knownHeight[3];
  49.  
  50.                 finalHeight += (knownHeight[3] * totalDistancesRSQ);
  51.             }
  52.  
  53.             heights[x, z] = finalHeight;
  54.             setHeights[x, z] = true;
  55.  
  56.  
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment