首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >as3 -设置纹理α值

as3 -设置纹理α值
EN

Stack Overflow用户
提问于 2014-10-29 09:25:58
回答 1查看 580关注 0票数 1

对于Actionscript 3“绘图应用程序”,我希望能够选择一个纹理并设置它的透明度。因此,我试图设置一个纹理的α-透明度。但这不管用。

我所做的:

  1. 首先,我使用graphics.linestyle()来设置线的厚度和阿尔法值。
  2. 之后,我(a)加载一个png,(b)读取它是bitmapData,(c)然后与lineBitmapStyle一起使用它。

结果:

在绘制线条时(使用moveTo、lineTo等),线条使用纹理,但忽略lineStyle设置的"Alpha“。

我做错了什么?

代码语言:javascript
复制
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, setTexture);        

setTexture(e:Event):void 
{
  e.currentTarget.removeEventListener(e.type, arguments.callee);

  //Try 1: Trying to set the Alpha-Trasparency with "lineStyle"-Command:
  myDrawContainer.graphics.lineBitmapStyle(5, 0xFF0000, 0,6);

  //Try 2: Trying to set the Alpha-Transparency by changing the Alpha-Value of the loaded content:
  myLoader.content.alpha = 0.6;

  //Getting the BitmapData of the Image:
  BitmapDataOfMyTexture = Bitmap(LoaderInfo(e.target).content).bitmapData

  //"Using" the TBitmapData as "Color/Texture" for my Drawing:
  myDrawContainer.graphics.lineBitmapStyle( BitmapDataOfMyTexture );

  //Test-Drawing:
  myDrawContainer.graphics.moveTo( 0, 0 );
  myDrawContainer.graphics.moveTo( 500, 500 ); //-> RESULT: Textured Line WITHOUT Transparency! 

}

结果:我得到了一条使用纹理但没有透明度的线条。

(更新)解决方案:(感谢DodgerThud)

要设置/更改加载图像的Alpha通道,不使用"lineStyle“,但是.

  1. 创建一个新的彩色转换对象
  2. 然后将它的"alphaMultiplier"-attribute设置为特定的alphaChannel
  3. 然后通过使用加载的BitmapData的"colorTransform"-Method,将这个新创建的彩色this对象应用到加载的BitmapData。

但:

代码语言:javascript
复制
This does NOT work with images that don't have an alpha-Channel or don't have their alpha-channel activated. Those images only get DARKER when lowering the alpha-Channel. In those cases you have to do this:

  1. 首先,我用" NEW“创建了新的BitmapData对象,将其宽度和高度设置为加载图像的宽度和高度,并将其第3个参数设置为TRUE (=透明性: ON)。所以你得到了一个“容器”,它激活了透明度。
  2. 然后,在这个“容器”-Object上使用“-Object”来填充加载的BitmapData对象的像素。
  3. 在此之后,使用"colorTransform"-Object的上述方法带来了预期的结果。

,下面是工作代码:

代码语言:javascript
复制
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, setTexture);

setTexture(e:Event):void 
{
  e.currentTarget.removeEventListener(e.type, arguments.callee);

  //Getting the BitmapData of the Image:
  BitmapDataOfMyTexture = Bitmap(LoaderInfo(e.target).content).bitmapData

  //Create an ADDITIONAL BitmapData-Object with 3rd  
  //argument set to TRUE and with same width and height
  //as the LOADED image:
  var BMDContainerWithAlphaActivated:BitmapData;
  BMDContainerWithAlphaActivated = new BitmapData(BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height, true, 0xFFFFFF);

  //Copy the pixels of the loaded image into the newly created 
  //"BitmapData-Container with activated AlphaChannel":
  BMDContainerWithAlphaActivated.copyPixels(BitmapDataOfMyTexture, new Rectangle(0, 0, BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height), new Point(0,0))

  //Modify the Alpha-Value (of the NEW BitmapData-Object):
  var colorChanges:ColorTransform = new ColorTransform();
      colorChanges.alphaMultiplier = 0.3;
  BMDContainerWithAlphaActivated.colorTransform(new Rectangle(0, 0, BitmapDataOfMyTexture.width, BitmapDataOfMyTexture.height), colorChanges);  


  //"Using" the (NEW) BitmapData as "Color/Texture" for my Drawing:
  myDrawContainer.graphics.lineBitmapStyle( BMDContainerWithAlphaActivated );

  //Test-Drawing:
  myDrawContainer.graphics.moveTo( 0, 0 );
  myDrawContainer.graphics.moveTo( 500, 500 ); //-> RESULT: Textured Line WITH Transparency 0.3!        
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-29 10:55:12

啊,我明白了,这比我最初想的要复杂一些。

好的,查看文档 for lineBitmapStyle向我显示该函数需要以下参数:lineBitmapStyle(bitmap:BitmapData, matrix:Matrix = null, repeat:Boolean = true, smooth:Boolean = false)

现在,矩阵,重复和平滑将无助于我们在这里(矩阵用于转换,即定位,旋转等),但位图:BitmapData可能。我们需要做的是在将载入的PNG文件传递给lineBitmapStyle之前先操作它的lineBitmapStyle。遗憾的是,我们不能直接在BMD上设置alpha值,所以我们可以尝试colorTransform它。

这是未经测试的代码,但我认为这是正确的方法:

代码语言:javascript
复制
..
//store the bitmapdata in a seperate local variable
var bmd:BitmapData = LoaderInfo(e.target).content;
//create a ColorTransform Object to change the values of the BMD
var cTransform:ColorTransform = new ColorTransform();
//now here I am unsure, manipulating the alpha value of the BMD
cTransform.alphaMultiplier = 0.6;
//defining the rectangle dimensions of the bmd, we want it to be over the entire texture
var rect:Rectangle = new Rectangle(0,0,bmd.width,bmd.height);
//apply the colorTransformation on the BMD
bmd.colorTransform(rect,cTransform);
...
//the now manipulated BMD gets set as lineBitmapStyle
myDrawContainer.graphics.lineBitmapStyle(bmd);

现在我想了一下,也许我们可以在BMD上设置alpha值,首先创建一个Bitmap,在那里设置alpha值,然后使用Bitmap的位映射数据。如下所示:

代码语言:javascript
复制
var bmd:BitmapData = LoaderInfo(e.target).content;
var bm:Bitmap = new Bitmap(bmd);
bm.alpha = 0.6;
myDrawContainer.graphics.lineBitmapStyle(bm.bitmapData);

好的,上面的第一个片段似乎就是这样做的,但是BitmapData的BitmapData值必须是真的。考虑到您自己并不直接创建BitmapData,而且值是假的,我们在这里遇到了一个非常棘手的情况。

另一种方法是创建一个允许透明性的额外位图数据,并对其上加载的图像的位图数据进行draw()

代码语言:javascript
复制
var bmdSource:BitmapData = LoaderInfo(e.target).content;
var bmd:BitmapData = new BitmapData(bmdSource.width, bmdSource.height,true,0xffffffff);
var cTransform:ColorTransform = new ColorTransform();
cTransform.alphaMultiplier = 0.6;
var rect:Rectangle = new Rectangle(0,0,bmd.width,bmd.height);
bmd.colorTransform(rect,cTransform);
//now we have a completely white bitmapdata bmd, with an alpha value of 0.6
//we draw the contents of the bmdSource onto bmd, the alpha value effect should carry over
bmd.draw(bmdSource);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26626656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档