duck

duck

Aug 4th, 2010
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 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] = 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.             foreach (float[] knownHeight in knownHeights)
  43.             {
  44.                 float knownX = knownHeight[0];
  45.                 float knownY = knownHeight[1];
  46.                 float knownZ = knownHeight[2];
  47.                 float ownDist = knownHeight[3];
  48.  
  49.                 finalHeight += ((knownY / totalDistances) * ownDist);
  50.             }
  51.  
  52.             heights[x, z] = finalHeight;
  53.             setHeights[x, z] = true;
  54.  
  55.  
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment