com.borealkis.filters.Filter (Abstract)

package com.borealkiss.filters{
    import flash.display.Shader;
    import flash.filters.ShaderFilter;

    /**
     * Abstract class must be determined as dynamic.
     */ 
    public dynamic class Filter extends ShaderFilter{
        /**
         * Constructor
         */ 
        public function Filter(shader:Shader=null){
            super(shader);
        }

        protected function setData(name:String,...params):void{
            this.shader.data[name].value = params;
        }
    }
}

com.borealkis.filters.SpotlightFilter.as

/**
 * SpotlightFilter class.
 * 
 * @date    29/10/2008
 * @version 1.0
 * @author    borealkiss    
 * @see     http://blog.boreal-kiss.com/
 * 
 * Note the original Pixel Bender filter data by Ryan Phelan.
 * @see http://www.rphelan.com/2008/10/28/spotlight-pixel-bender-filter/
 */
package com.borealkiss.filters{
    import flash.utils.ByteArray;
    import flash.display.Shader;
    import com.borealkiss.filters.Filter;
    import com.borealkiss.filters.SpotlightData;

    public class SpotlightFilter extends Filter{
        private const AMBIENT_COLOR:String     = "ambientColor";
        private const POSITION:String         = "position";    
        private const SPOT_COLOR:String     = "spotColor";    
        private const ANGLE:String             = "angle";        
        private const AZIMUTH:String         = "azimuth";    
        private const CONE_ANGLE:String     = "coneAngle";    
        private const DISTANCE:String         = "distance";    
        private const INTENSITY:String         = "intensity";    
        private var _ambientColor:Array;//(0-1,0-1,0-1), default of (0.05,0.05,0.1).
        private var _position:Array;    //(0-1000,0-1000), default of (100,100).
        private var _spotColor:Array;    //(0-1,0-1,0-1    ), default of (1,0.95,0.85).
        private var _angle:Number;        //0-360, default of 240.
        private var _azimuth:Number;    //0-90, default of 20.
        private var _coneAngle:Number;    //0.1-10, default of 2.
        private var _distance:Number;    //0.1-50, default of 10.
        private var _intensity:Number;    //0.01-50, default of 15.
        
        /**
         * Constructor
         * 
         * @param posX Center of the filter along the x-axis.
         * @param posY Center of the filter along the y-axis.
         * @param angle Spotlight angular direction.
         * @param intensity Intensity of the spotlight.
         */       
        public function SpotlightFilter(posX:Number=100,posY:Number=100,angle:Number=240,intensity:Number=15){
            super(new Shader(ByteArray(new SpotlightData())));
            
            this.position = [posX,posY];
            this.ambientColor = [0.05,0.05,0.1];
            this.spotColor = [1,0.95,0.85];
            this.angle = angle;
            this.intensity = intensity;
            this.azimuth = 20;
            this.coneAngle = 2;
            this.distance = 10;
        }
        
        public function set ambientColor(value:Array):void{
            _ambientColor = value;
            super.setData(AMBIENT_COLOR, _ambientColor[0],_ambientColor[1],_ambientColor[2]);
        }
        public function get ambientColor():Array{
            return _ambientColor;
        }
        
        public function set position(value:Array):void{
            _position = value;
            //Set default z-value of 100.
            super.setData(POSITION, _position[0],_position[1],100);
        }
        public function get position():Array{
            return _position;
        }
        
        public function set spotColor(value:Array):void{
            _spotColor = value;
            super.setData(SPOT_COLOR, _spotColor[0],_spotColor[1],_spotColor[2]);
        }
        public function get spotColor():Array{
            return _spotColor;
        }
        
        public function set angle(value:Number):void{
            _angle = value;
            super.setData(ANGLE, _angle);
        }
        public function get angle():Number{
            return _angle;
        }
        
        public function set azimuth(value:Number):void{
            _azimuth = value;
            super.setData(AZIMUTH, _azimuth);
        }
        public function get azimuth():Number{
            return _azimuth;
        }
        
        public function set coneAngle(value:Number):void{
            _coneAngle = value;
            super.setData(CONE_ANGLE, _coneAngle);
        }
        public function get coneAngle():Number{
            return _coneAngle;
        }
        
        public function set distance(value:Number):void{
            _distance = value;
            super.setData(DISTANCE, _distance);
        }
        public function get distance():Number{
            return _distance;
        }
        
        public function set intensity(value:Number):void{
            _intensity = value;
            super.setData(INTENSITY, _intensity);
        }
        public function get intensity():Number{
            return _intensity;
        }
    }
}

com.borealkis.filters.SpotlightData.as

package com.borealkiss.filters{
    import mx.core.ByteArrayAsset;
    
    [ (source="com/borealkiss/filters/assets/spotlight/spotlight.pbj",
                                        mimeType="application/octet-stream")]
    public class SpotlightData extends ByteArrayAsset{
    }
}
Powered by blog Boreal Kiss 2008.