root/as3/FLARToolKit/trunk/libs/Papervision3D/src/org/papervision3d/core/effects/BitmapFireEffect.as

リビジョン 3288, 8.3 kB (コミッタ: rokubou, コミット時期: 3 年 前)

Add Papervison3D.

Line 
1 /**
2 * ...
3 * @author Default
4 * @version 0.1
5 */
6
7 package org.papervision3d.core.effects {
8         import org.papervision3d.view.layer.BitmapEffectLayer;
9        
10         import flash.display.BitmapData;
11         import flash.display.DisplayObject;
12         import flash.filters.BlurFilter;
13         import flash.filters.ColorMatrixFilter;
14         import flash.filters.DisplacementMapFilter;
15         import flash.geom.ColorTransform;
16         import flash.geom.Matrix;
17         import flash.geom.Point;       
18
19         public class BitmapFireEffect extends AbstractEffect{
20                
21                 private var layer:BitmapEffectLayer;
22                 private var _fadeRate:Number=0.4;
23                 private var _distortionScale:Number=0.4;
24                 private var _distortion:Number=0.5;
25                 private var _flameHeight:Number=0.3;
26                 private var _flameSpread:Number=0.3;
27                 private var _blueFlame:Boolean = false;
28                 private var _smoke:Number = 0;
29                
30         // private properties:
31                 // display elements:
32                 private var displayBmp:BitmapData;
33                 private var scratchBmp:BitmapData;
34                 private var perlinBmp:BitmapData;
35                
36                 // geom:
37                 private var mtx:Matrix;
38                 private var pnt:Point;
39                 private var drawColorTransform:ColorTransform;
40                
41                 // filters:
42                 private var fireCMF:ColorMatrixFilter;
43                 private var dispMapF:DisplacementMapFilter;
44                 private var blurF:BlurFilter;
45                
46                 // other
47                 private var endCount:Number;
48                 private var bmpsValid:Boolean=false;
49                 private var perlinValid:Boolean=false;
50                 private var filtersValid:Boolean=false;
51                 private var _target:DisplayObject;
52                
53                
54                 public function BitmapFireEffect(r:Number = 1, g:Number = 1, b:Number = 1, a:Number= 1){
55                        
56                        
57                         mtx = new Matrix();
58                         pnt = new Point();
59                        
60                 }
61                
62
63                 public override function attachEffect(layer:BitmapEffectLayer):void{
64                        
65                         this.layer = BitmapEffectLayer(layer);
66                         target = layer.drawLayer;
67                        
68                 }
69                
70                 public override function postRender():void{
71                        
72                         doFire();
73                         //layer.canvas.applyFilter(layer.canvas, layer.canvas.rect, new Point(), fade);
74                        
75                 }
76
77
78                  public function set width(value:Number):void {
79                         //layer.canvas.width;
80                 }
81                  public function get width():Number {
82                         return layer.canvas.width;
83                 }
84                
85                  public function set height(value:Number):void {
86                 ;
87                 }
88                  public function get height():Number {
89                         return layer.canvas.height;
90                 }
91
92
93 [Inspectable(defaultValue=0.4,name='fadeRate (0-1)')]
94                 /**
95          * Sets the rate that flames fade as they move up. 0 is slowest, 1 is fastest.
96          *
97          * @default 0.4
98                  */
99                 public function set fadeRate(value:Number):void {
100                         filtersValid &&= (value == _fadeRate);
101                         _fadeRate = value;
102                 }
103                 public function get fadeRate():Number {
104                         return _fadeRate;
105                 }
106                
107                 [Inspectable(defaultValue=0.4,name='distortionScale (0-1)')]
108                 /**
109          * Sets the scale of flame distortion. 0.1 is tiny and chaotic, 1 is large and smooth.
110          *
111          * @default 0.4
112                  */
113                 public function set distortionScale(value:Number):void {
114                         perlinValid &&= (value == _distortionScale);
115                         _distortionScale = value;
116                 }
117                 public function get distortionScale():Number {
118                         return _distortionScale;
119                 }
120                
121                 [Inspectable(defaultValue=0.4,name='distortion (0-1)')]
122                 /**
123          * Sets the amount of distortion. 0.1 is little, 1 is chaotic.
124          *
125          * @default 0.4
126                  */
127                 public function set distortion(value:Number):void {
128                         filtersValid &&= (value == _fadeRate);
129                         _distortion = value;
130                 }
131                 public function get distortion():Number {
132                         return _distortion;
133                 }
134                
135                 [Inspectable(defaultValue=0.3,name='flameHeight (0-1)')]
136                 /**
137          * Sets the how high the flame will burn. 0 is zero gravity, 1 is a bonfire.
138          *
139          * @default 0.3
140                  */
141                 public function set flameHeight(value:Number):void {
142                         perlinValid &&= (value == _flameHeight);
143                         _flameHeight = value;
144                 }
145                 public function get flameHeight():Number {
146                         return _flameHeight;
147                 }
148                
149                 [Inspectable(defaultValue=0.3,name='flameSpread (0-1)')]
150                 /**
151          * Sets the how much the fire will spread out around the target. 0 is no spread, 1 is a lot.
152          *
153          * @default 0.3
154                  */
155                 public function set flameSpread(value:Number):void {
156                         filtersValid &&= (value == _flameSpread);
157                         _flameSpread = value;
158                 }
159                 public function get flameSpread():Number {
160                         return _flameSpread;
161                 }
162                
163                 [Inspectable(defaultValue=false,name='blueFlame')]
164                 /**
165          * Indicates whether it should use a blue or red flame.
166          *
167          * @default false
168                  */
169                 public function set blueFlame(value:Boolean):void {
170                         filtersValid &&= (value == _blueFlame);
171                         _blueFlame = value;
172                 }
173                 public function get blueFlame():Boolean {
174                         return _blueFlame;
175                 }
176                
177                 [Inspectable(defaultValue=0,name='smoke (0-1)')]
178                 /**
179          * Sets the amount of smoke. 0 little, 1 lots.
180          *
181          * @default 0
182                  */
183                 public function set smoke(value:Number):void {
184                         filtersValid &&= (value == _smoke);
185                         _smoke = value;
186                 }
187                 public function get smoke():Number {
188                         return _smoke;
189                 }
190                
191                
192                 [Inspectable(defaultValue='',name='target')]
193                 /**
194          * Sets the amount of smoke. 0 little, 1 lots.
195          *
196          * @default
197                  */
198                 public function set targetName(value:String):void {
199
200                 }
201                
202                 /**
203          * Defines the shape of the fire. The fire will burn upwards, so it should be near the bottom, and centered in the FireFX component.
204          *
205          * @default
206                  */
207                 public function set target(value:DisplayObject):void {
208                         _target = value;
209                         clear();
210                 }
211                 public function get target():DisplayObject {
212                         return _target;
213                 }
214                
215                 /**
216          * Clears the fire.
217                  */
218                 public function clear():void {
219                         if (displayBmp) {
220                                 displayBmp.fillRect(displayBmp.rect,0);
221                         }
222                 }
223                
224
225                
226                 public function stopFire():void {
227                         // let the fire burn down for 20 frames:
228                         if (endCount == 0) { endCount = 20; }
229                 }
230                
231                
232                 private function updateBitmaps():void {
233                         if (displayBmp) {
234                                 displayBmp.dispose();
235                                 displayBmp = null;
236                                 scratchBmp.dispose();
237                                 scratchBmp = null;
238                                 perlinBmp.dispose();
239                                 perlinBmp = null;
240                         }
241                        
242                         displayBmp = layer.canvas;
243                         scratchBmp = displayBmp.clone();
244                         perlinBmp = new BitmapData(width*3, height*3, false, 0);
245                        
246                        
247                         updatePerlin();
248                         updateFilters();
249                         bmpsValid = true;
250                 }
251                
252                 private function updatePerlin():void {
253                         perlinBmp.perlinNoise(30*_distortionScale,20*_distortionScale,1,-Math.random()*1000|0,false,true,1|2,false);
254                         perlinBmp.colorTransform(perlinBmp.rect,new ColorTransform(1,  1-_flameHeight*0.5  ,1,1,0,0,0,0));
255                         perlinValid = true;
256                 }
257                
258                 private function updateFilters():void {
259                         if (_blueFlame) {
260                                 fireCMF = new ColorMatrixFilter([0.8-0.55*_fadeRate,0,0,0,0,
261                                                                                                  0,0.93-0.48*_fadeRate,0,0,0,
262                                                                                                  0,0.1,0.96-0.35*_fadeRate,0,0,
263                                                                                                  0,0.1,0,1,-25+_smoke*24]);
264                                 drawColorTransform = new ColorTransform(0,0,0,1,210,240,255,0);
265                         } else {
266                                 fireCMF = new ColorMatrixFilter([0.96-0.35*_fadeRate,0.1,0,0,-1,
267                                                                                                  0,0.9-0.45*_fadeRate,0,0,0,
268                                                                                                  0,0,0.8-0.55*_fadeRate,0,0,
269                                                                                                  0,0.1,0,1,-25+_smoke*24]);
270                                 drawColorTransform = new ColorTransform(0,0,0,1,255,255,210,0);
271                         }
272                         dispMapF = new DisplacementMapFilter(perlinBmp,pnt,1,2,14*_distortion,-30,"clamp");
273                         blurF = new BlurFilter(32*_flameSpread,32*_flameSpread,1);
274                        
275                         filtersValid = true;
276                 }
277                
278                
279                
280                 private function startFire():void {
281                         endCount = 0;
282                        
283                 }
284                
285                 private function doFire():void {
286                         if (_target == null) { return; }
287                         if (!bmpsValid) { updateBitmaps(); }
288                         if (!perlinValid) { updatePerlin(); }
289                         if (!filtersValid) { updateFilters(); }
290                         if (endCount == 0) {
291                                 var drawMtx:Matrix = _target.transform.matrix;
292
293                                 scratchBmp.fillRect(scratchBmp.rect,0);
294                                 drawColorTransform.alphaOffset = -Math.random()*200|0;
295                                 scratchBmp.draw(_target,drawMtx,drawColorTransform,"add");
296                                 scratchBmp.applyFilter(scratchBmp,scratchBmp.rect,pnt,blurF);
297                                 displayBmp.draw(scratchBmp,mtx,null,"add");
298                         }
299                         dispMapF.mapPoint = new Point( -Math.random()*(perlinBmp.width-displayBmp.width)|0, -Math.random()*(perlinBmp.height-displayBmp.height)|0 );
300                         displayBmp.applyFilter(displayBmp,displayBmp.rect,pnt,dispMapF);
301                         displayBmp.applyFilter(displayBmp,displayBmp.rect,pnt,fireCMF);
302                        
303                         //if (endCount != 0 && --endCount == 0) {
304                         //      removeEventListener(Event.ENTER_FRAME,doFire);
305                         }
306                 }
307                
308        
309         }
310        
311
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。