gpt4 book ai didi

actionscript-3 - DisplacementMapFilter 是如何工作的?

转载 作者:行者123 更新时间:2023-12-04 06:53:04 25 4
gpt4 key购买 nike

我是 ActionScript 的初学者。如果我理解正确,DisplacementMapFilter 根据“MAP 图像”中相应像素位置的颜色从“源图像”移动像素。

问题是我的目标图像包含源图像中没有的像素!

例如,我使用此 BitMapData 获取 UNICOLOR 10 * 10 像素“源图像”:

sourceBitmap = new BitmapData(BITMAP_WIDTH, BITMAP_HEIGHT, false, 0x000002);

produce:

0 1 2 3 4 5 6 7 8 9
[002,002,002,002,002,002,002,002,002,002] Row 0
[002,002,002,002,002,002,002,002,002,002] Row 1
[002,002,002,002,002,002,002,002,002,002] Row 2
[002,002,002,002,002,002,002,002,002,002] Row 3
[002,002,002,002,002,002,002,002,002,002] Row 4
[002,002,002,002,002,002,002,002,002,002] Row 5
[002,002,002,002,002,002,002,002,002,002] Row 6
[002,002,002,002,002,002,002,002,002,002] Row 7
[002,002,002,002,002,002,002,002,002,002] Row 8
[002,002,002,002,002,002,002,002,002,002] Row 9

现在,我使用这个 BLACK 位移 MAP 并添加一个小的蓝色方块:
displacementBitmap = new BitmapData(BITMAP_WIDTH,BITMAP_HEIGHT,false,0x000000);
for(i=5;i<10;i++)
for(j=5;j<10;j++)
displacementBitmap.setPixel(i,j,255);

produce:

0 1 2 3 4 5 6 7 8 9
[000,000,000,000,000,000,000,000,000,000] Row 0
[000,000,000,000,000,000,000,000,000,000] Row 1
[000,000,000,000,000,000,000,000,000,000] Row 2
[000,000,000,000,000,000,000,000,000,000] Row 3
[000,000,000,000,000,000,000,000,000,000] Row 4
[000,000,000,000,000,255,255,255,255,255] Row 5
[000,000,000,000,000,255,255,255,255,255] Row 6
[000,000,000,000,000,255,255,255,255,255] Row 7
[000,000,000,000,000,255,255,255,255,255] Row 8
[000,000,000,000,000,255,255,255,255,255] Row 9

结果:
displacementFilter = new DisplacementMapFilter();
displacementFilter.alpha=0;
displacementFilter.color=0;
displacementFilter.mapPoint=new Point(0,0);
displacementFilter.scaleX=1;
displacementFilter.scaleY=1;
displacementFilter.componentX = flash.display.BitmapDataChannel.BLUE;
displacementFilter.componentY = flash.display.BitmapDataChannel.BLUE;
displacementFilter.mapBitmap = displacementBitmap;

destinationBitmap = new BitmapData(BITMAP_WIDTH,BITMAP_HEIGHT,false,0xFFFFFFFF);
destinationBitmap.applyFilter(
sourceBitmap.bitmapData,
new Rectangle(0,0,BITMAP_WIDTH,BITMAP_HEIGHT),
new Point(0,0),
displacementFilter
);

produce:

0 1 2 3 4 5 6 7 8 9
[002,002,002,002,002,002,002,002,002,002] Row 0
[002,002,002,002,002,002,002,002,002,002] Row 1
[002,002,002,002,002,002,002,002,002,002] Row 2
[002,002,002,002,002,002,002,002,002,002] Row 3
[002,002,002,002,002,002,002,002,002,002] Row 4
[002,002,002,002,002,001,001,001,001,001] Row 5
[002,002,002,002,002,001,001,001,001,001] Row 6
[002,002,002,002,002,001,001,001,001,001] Row 7
[002,002,002,002,002,001,001,001,001,001] Row 8
[002,002,002,002,002,001,001,001,001,001] Row 9

所以我不明白为什么我有源图像中不存在的“001”像素。

非常感谢你。

最佳答案

置换图过滤器使用 this formula 决定从源位图中抓取哪个像素:

dstPixel[x, y] = srcPixel[x + ((componentX(x, y) - 128) * scaleX) / 256, y + ((componentY(x, y) - 128) *scaleY) / 256)
dstPixel是目标位图, srcPixel是源位图, componentX/Y来自位移位图。映射是由分量 channel 离128多远来决定的。注意128是没有变换的中性值,使用的是同一个源像素。

例如,让我们考虑在您的情况下 (1, 1) 处的像素。您的组件 channel 是蓝色的,置换贴图中 (1, 1) 处的蓝色 channel 的值为 0。因此我们按 (0 - 128)/256 或 -0.5 进行置换。所以我们从源位图中抓取的最后一个像素是 (0.5, 0.5)。

但是使用半个像素是什么意思呢?原来Flash使用的是 bilinear filtering以平滑结果。您最终会在 (0,0) 和 (1,1) 之间的四个像素中混合颜色。

这就是您看到源图像中没有的奇数像素的原因。尽管源图像中的所有像素都是相同的颜色,但当 Flash 在颜色之间进行插值时,似乎存在一些浮点误差,因此有时您会得到一个稍微偏离的值。请注意,如果您在置换位图中使用一个“干净地”除以 128 的值,例如 0 或 64,那么您将获得正确的结果。但是如果你使用像 255 或 19 这样的值,你会得到一些错误。

不幸的是,似乎没有任何方法可以关闭过滤并使用简单的最近邻采样。因此,如果您需要保证不引入新颜色,则必须仔细选择置换贴图中的值。

或者,您可以使用 Pixel Bender创建一个可以满足您需求的内核。 Pixel Bender 允许您从源位图进行最近邻采样。

关于actionscript-3 - DisplacementMapFilter 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9692083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com