package { import flare.basic.*; import flare.core.*; import flare.materials.*; import flare.materials.filters.*; import flare.system.*; import flash.display.*; import flash.events.*; import flash.geom.*; import flash.utils.*; /** * Animate texture alpha. * * @author Ariel Nehmad */ public class Test31_ChangeTextureAlpha extends Sprite { private var scene:Scene3D; private var material:Shader3D; private var elsa:Pivot3D; public function Test31_ChangeTextureAlpha() { scene = new Viewer3D(this); scene.autoResize = true; scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent ); scene.addChildFromFile( "elsa.zf3d" ); scene.backgroundColor = 0xffffff; } private function completeEvent(e:Event):void { elsa = scene.getChildByName( "elsa" ); // since we're going to change object alpha, we need to make sure to render it after other opaque objects. elsa.setLayer( 1 ); // gets the reference to the material. material = elsa.getMaterialByName( "mElsa" ) as Shader3D; material.filters[0].alpha = 0.0; material.transparent = true; scene.addEventListener( Scene3D.RENDER_EVENT, renderEvent ); } private function renderEvent(e:Event):void { // this is not really required, but it is an useful trick to enable better trasnparency on // complex object surfaces. drawing the object first without color, but storing its depth values // into the depth buffer, allows you to draw the transpareny only on the nearest pixels to the camera. // try comment / uncomment these 3 lines to see the difference. scene.context.setColorMask( false, false, false, false ); elsa.draw(); scene.context.setColorMask( true, true, true, true ); // finally changes the texture alpha. var textureMap:TextureMapFilter = material.filters[0]; textureMap.alpha = Math.sin( getTimer() / 1000 ) * 0.5 + 0.5; } } }