3

I'm using a DisplacementMapFilter to created a globe-like effect on a flat map. My problem is, I also want to sync some labels to this map. I have the x/y coordinates for their locations on the flat map, but I need to map them to the now-displaced image.

I would like to be able to do this using the BitmapData that contains the displacement map, so that changing the Bitmap changes both the displacement filter and the label locations. Also, the labels will not be static, and accuracy is fairly important.

Alexis King
  • 43,109
  • 15
  • 131
  • 205

1 Answers1

2

There is a formula in DisplacementMapFilter reference:

dstPixel[x, y] =
  srcPixel[
    x + ((componentX(x, y) - 128) * scaleX) / 256,
    y + ((componentY(x, y) - 128) *scaleY) / 256)
  ]

componentX/Y are color channels in the bitmap (you can bind any channel to coordinates).
As I understand, you need to shift map labels as filter would do. Just take label coordinates (x, y), sample source bitmap with getPixel32(x, y). Then you need to figure out which bytes to take for x, y - I guess by default it would be R, G components, respectively. Then use formula to get displaced label coordinates.
Note: getPixel32 returns uint color in ARGB format. Use shift operator (>>) to get color components:

uint ARGB = bitmap.getPixel32(x, y);
int B = ARGB & 0xFF;
int G = (ARGB >> 8) & 0xFF;
int R = (ARGB >> 16) & 0xFF;
alxx
  • 9,897
  • 4
  • 26
  • 41
  • 1
    Thanks, but I found that this algorithm provided is a lie. It's `x - ((componentX(x,...` and `y - ((componentY(x...`, not `+`. With that, it works nicely. – Alexis King Dec 14 '11 at 02:17