public void Execute() { // Convert from rect in tilemap space to interior corners in 0-1 range float xMin = locationRect.xMin / MapsFile.WorldMapTileDim; float xMax = locationRect.xMax / MapsFile.WorldMapTileDim; float yMin = locationRect.yMin / MapsFile.WorldMapTileDim; float yMax = locationRect.yMax / MapsFile.WorldMapTileDim; // Scale values for converting blend space into 0-1 range float leftScale = 1 / xMin; float rightScale = 1 / (1 - xMax); float topScale = 1 / yMin; float bottomScale = 1 / (1 - yMax); // Flatten location area and blend with surrounding heights float strength = 0; float targetHeight = avgMaxHeight[avgHeightIdx]; for (int y = 0; y < hDim; y++) { float v = (float)y / (float)(hDim - 1); bool insideY = (v >= yMin && v <= yMax); for (int x = 0; x < hDim; x++) { float u = (float)x / (float)(hDim - 1); bool insideX = (u >= xMin && u <= xMax); if (insideX || insideY) { if (insideY && u <= xMin) { strength = u * leftScale; } else if (insideY && u >= xMax) { strength = (1 - u) * rightScale; } else if (insideX && v <= yMin) { strength = v * topScale; } else if (insideX && v >= yMax) { strength = (1 - v) * bottomScale; } } else { float xs = 0, ys = 0; if (u <= xMin) { xs = u * leftScale; } else if (u >= xMax) { xs = (1 - u) * rightScale; } if (v <= yMin) { ys = v * topScale; } else if (v >= yMax) { ys = (1 - v) * bottomScale; } strength = TerrainHelper.BilinearInterpolator(0, 0, 0, 1, xs, ys); } int idx = JobA.Idx(y, x, hDim); float height = heightmapData[idx]; if (insideX && insideY) { height = targetHeight; } else { height = Mathf.Lerp(height, targetHeight, strength); } heightmapData[idx] = height; } } }