Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DebugX.Log("Interpolating unset height cells - " + knownHeights.Length + " known heights / "+(heightmapSize*heightmapSize)+" cells");
- for (int x = 0; x < heightmapSize; ++x)
- {
- DebugX.Log(x + "/" + heightmapSize);
- yield return null;
- for (int z = 0; z < heightmapSize; ++z)
- {
- if (!(setHeights[x, z]))
- {
- float totalWeight = 0;
- float totalHeight = 0;
- float totalDistances = 0;
- // have per cell, a distance calc to each known cell
- //[16:34] <zeroZshadow> add the distances together
- //[16:35] <zeroZshadow> then the value of every known cell is divided by the total distance
- //[16:35] <zeroZshadow> and multiplyd by their own distance to the cell
- //[16:35] <zeroZshadow> add all the results together
- //[16:35] <zeroZshadow> next cell
- // calculate total distances from all known height cells
- foreach (float[] knownHeight in knownHeights)
- {
- float knownX = knownHeight[0];
- float knownY = knownHeight[1];
- float knownZ = knownHeight[2];
- float xDiff = (x - knownX);
- float zDiff = (z - knownZ);
- float distSq = xDiff*xDiff + zDiff*zDiff;
- // remember dist to current unknown cell, temporarily
- knownHeight[3] = knownY * distSq;
- totalDistances += distSq;
- }
- // calculate final height based on weighting each known height by its relative distance
- float finalHeight = 0;
- float totalDistancesRSQ = 1.0f/totalDistances;
- foreach (float[] knownHeight in knownHeights)
- {
- float knownX = knownHeight[0];
- float knownY = knownHeight[1];
- float knownZ = knownHeight[2];
- float ownDist = knownHeight[3];
- finalHeight += (knownHeight[3] * totalDistancesRSQ);
- }
- heights[x, z] = finalHeight;
- setHeights[x, z] = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment