Material alpha + alphaThreshold

Software: Away3D 4.x

Artemis, Newbie
Posted: 23 February 2012 11:12 PM   Total Posts: 6

Straight to the point:
When i was using only the alpha value of TextureMaterial everything was working just fine, but i’ve wanted to “fake-animate” the clouds using alphaThreshold value and the textures displays only white or transparent pixels (it’s not using rgb values from bitmapData).


Is this a bug or i can not use both (alpha & alphaThreshold) at the same time?

 

   

inSertCodE, Sr. Member
Posted: 23 February 2012 11:21 PM   Total Posts: 105   [ # 1 ]

alpha takes in to account the source alpha of each pixel from the input data.

alphaThreshold on the other hand doesn’t take in to account the different levels of alpha (visibility). Its either visible or not. There is no transparency.

   

Artemis, Newbie
Posted: 23 February 2012 11:41 PM   Total Posts: 6   [ # 2 ]

Thanks for reply smile

Just for clarification - alphaThreshold is just checking out if given pixel has a transparency above/below a certaing value and makes the pixel either fully opaque or fully transparent?

If yes than another question - is there any way to combine those methods so for every pixel below the threshold it would be fully transparent and for every pixel above the threshold it would stay with unchanged transparency value?

   

inSertCodE, Sr. Member
Posted: 24 February 2012 12:28 AM   Total Posts: 105   [ # 3 ]
Artemis - 23 February 2012 11:41 PM

alphaThreshold is just checking out if given pixel has a transparency above/below a certaing value and makes the pixel either fully opaque or fully transparent?

Yes, something like that.

Artemis - 23 February 2012 11:41 PM

is there any way to combine those methods so for every pixel below the threshold it would be fully transparent and for every pixel above the threshold it would stay with unchanged transparency value?

I don’t think so.

The only way, I can think of, to do exactly what you need is to use BitmapData functions getPixel32 and setPixel32.
Loop thorough all pixels, get their alpha and if its below the threshold set it to 0, if its above leave it. If you want it done at a GPU level you’ll have to write a shader which is much more complex.

getPixel32 and setPixels32 use the color as uint value in this order (alpha, red, green, blue) each with value 0-255.
Here are some functions to turn the uint to understandable values and put those back in uint so you can setPuxel32 them:

private function extractRed(c:uint):uint {
   
return (( >> 16 ) & 0xFF);
  
}
  
private function extractGreen(c:uint):uint {
   
return ( (>> 8) & 0xFF );
  
}
  
private function extractBlue(c:uint):uint {
   
return ( 0xFF );
  
}
  
function extractAlpha(c:uint):uint {
   
return (( >> 24 ) & 0xFF);
  
}
  
private function combineARGB(a:uint,r:uint,g:uint,b:uint):uint {
   
return ( (<< 24) | ( << 16 ) | ( << ) | );
  

...oh what the hell I’ll write down the main function too ;)

public function AlphaCut(bitmap_base:BitmapDatathreshHold:uint):BitmapData {
   
var tempPixel:uint;
   var 
tempAlpha:uint;
   
   if(
threshHold || threshHold 254) throw new Error("threshHold should be between 1 and 254!");
   for (var 
i:uint 0bitmap_base.heighti++){
    
for (var l:uint 0bitmap_base.widthl++){
     tempPixel 
bitmap_base.getPixel32(li);
     
     
tempAlpha extractAlpha(tempPixel);
     if(
tempAlpha threshHold){
      tempAlpha 
0;
      
tempPixel combineARGB(tempAlphaextractRed(tempPixel), extractGreen(tempPixel), extractBlue(tempPixel))
      
bitmap_base.setPixel32(litempPixel)
     
}
    }
   }
   
return bitmap_base;
  

PS.use this with alpha only, not alphaThreshold

   

Artemis, Newbie
Posted: 24 February 2012 01:12 AM   Total Posts: 6   [ # 4 ]

Thanks for such a comprehensive reply smile
I haven’t even considered using any CPU extensive methods to achieve this effect.

Fortunately it wasn’t to hard to get what i was looking for - i only had to uncomment one line from getFragmentPostLightingCode of current BasicDiffuseMethod (last “div” from alphaThreshold AGAL code).

Thanks for all the help smile


//—
I’ve added a property “preserveAlphaOnThreshold” to the BasicDiffuseMethod.
If it’s set to false - the method works normal (alpha values set to 0 or 1)
If it’s set to true - alpha values >= alphaThreshold stays unchanged.

Maybe someone from the team will add this “feature” in the next beta release? wink

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X