有一件事情在Flash IDE中看起来特别容易,但在代码中很难做到的是勾画出有机形状。在IDE中,您可以使用墨水桶工具在某物周围画一个中风。使用任何东西,但代码似乎更棘手。我见过的一种方法是在所讨论的形状中添加一个辉光过滤器,只是混淆了强度。但是如果我只想显示轮廓呢?在有机形状周围绘制轮廓
我想要做的是收集构成形状边缘的所有点,然后连接点。我实际上已经用我编写的一个快速和肮脏的边缘检测脚本来收集所有的点。所以现在我有一个矢量化妆的所有要点。我如何以适当的顺序连接它们,使它看起来像原始对象?
对于任何人谁是有兴趣的,这里是我的边缘检测脚本:
// Create a new sprite which we'll use for our outline
var sp:Sprite = new Sprite();
var radius:int = 50;
sp.graphics.beginFill(0x00FF00, 1);
sp.graphics.drawCircle(0, 0, radius);
sp.graphics.endFill();
sp.x = stage.stageWidth/2;
sp.y = stage.stageHeight/2;
// Create a bitmap data object to draw our vector data
var bmd:BitmapData = new BitmapData(sp.width, sp.height, true, 0);
// Use a transform matrix to translate the drawn clip so that none of its
// pixels reside in negative space. The draw method will only draw starting
// at 0,0
var mat:Matrix = new Matrix(1, 0, 0, 1, radius, radius);
bmd.draw(sp, mat);
// Pass the bitmap data to an actual bitmap
var bmp:Bitmap = new Bitmap(bmd);
// Add the bitmap to the stage
addChild(bmp);
// Grab all of the pixel data from the bitmap data object
var pixels:Vector.<uint> = bmd.getVector(bmd.rect);
// Setup a vector to hold our stroke points
var points:Vector.<Point> = new Vector.<Point>;
// Loop through all of the pixels of the bitmap data object and
// create a point instance for each pixel location that isn't
// transparent.
var l:int = pixels.length;
for(var i:int = 0; i < l; ++i)
{
// Check to see if the pixel is transparent
if(pixels[i] != 0)
{
var pt:Point;
// Check to see if the pixel is on the first or last
// row. We'll grab everything from these rows to close the outline
if(i <= bmp.width || i >= (bmp.width * bmp.height) - bmp.width)
{
pt = new Point();
pt.x = int(i % bmp.width);
pt.y = int(i/bmp.width);
points.push(pt);
continue;
}
// Check to see if the current pixel is on either extreme edge
if(int(i % bmp.width) == 0 || int(i % bmp.width) == bmp.width - 1)
{
pt = new Point();
pt.x = int(i % bmp.width);
pt.y = int(i/bmp.width);
points.push(pt);
continue;
}
// Check to see if the previous or next pixel are transparent,
// if so save the current one.
if(i > 0 && i < bmp.width * bmp.height)
{
if(pixels[i - 1] == 0 || pixels[i + 1] == 0)
{
pt = new Point();
pt.x = int(i % bmp.width);
pt.y = int(i/bmp.width);
points.push(pt);
}
}
}
}
我正在测试我用来获取边缘的代码,除非我做错了它似乎很快。根据格兰特斯金纳的测试工具,它可以在9.63ms或9631ms内运行1000次迭代。如果有人有时间仔细检查,我将不胜感激。我使用的代码如下:http://pastebin.com/s59FPGvB – 2010-03-21 17:46:37
在空位图中生成效果可能与我之后的效果最接近。你只需要将原始位图中的变换对象应用到空白对象上?这甚至工作吗? – 2010-03-21 18:13:57