Smoke effect in Away3D 4

Software: Away3D 4.x

Avatar
kurono, Sr. Member
Posted: 31 August 2011 07:36 AM   Total Posts: 103

Hello, Away3D Team!

Last day I tried to implement simple smoke-like effect based on the Sprite3D class.

smokePuff.as

package 
{
 import flash
.events.Event;
 
import flash.geom.Vector3D;

 
import away3d.containers.View3D;
 
import away3d.entities.Sprite3D;
 
import away3d.materials.BitmapMaterial;
 
 public class 
smokePuff extends Sprite3D
 {
  [Embed
(source="/../embed/puff.png")]
  
private var _texture:Class;
  
  private var 
_sc:Number 1.3;
  private var 
_view:View3D;
  private var 
_mtl:BitmapMaterial;
 
  public function 
smokePuff(view:View3Dpos:Vector3D{
   _view 
view;
   
_mtl = new BitmapMaterial(new flake_texture().bitmapDatatruefalsefalse);
   
_mtl.alphaBlending true;

   
super(_mtl16 16 Math.random(), 16 16 Math.random());
   
_mtl.alpha 1;
   
this.position pos;
   
_view.scene.addChild(this);
   
_view.addEventListener(Event.ENTER_FRAMEloopfalse0true);
  
}
  
  
private function loop(e:Event):void {
   this
.scale(_sc);
   
   if (
_mtl.alpha 0){
    remove
();
   
else {
    _mtl
.alpha -= .1;
   
}
  }
  
  
private function remove():void {
   _view
.removeEventListener(Event.ENTER_FRAMEloop);
   
_view.scene.removeChild(this);
// should I do this???
   
this.dispose(true);
   
_mtl.dispose(true);
  
}
 }
 

Then in main class I call smokePuff each frame as follows:

...
for (var 
i:int 04i++){
      
var sm:smokePuff = new smokePuff(_viewSMOKE_INIT_POS.add(new Vector3D(25 50 Math.random(), -250 50 Math.random(), 25 50 Math.random())));
     
}
... 

where SMOKE_INIT_POS is smokes’ initial position


For first 5 seconds it works fine, but then… it dramatically slows down! It seems, that no one of just created smokePuffs removed from both view3D and PC’s memory. The question is WHY question, coz I used to remove it (remove():void).
Please, help to solve my problem and sorry for bad english…

   

Avatar
Apprentice, Jr. Member
Posted: 31 August 2011 09:16 AM   Total Posts: 45   [ # 1 ]

As far as I know this is an open issue.

But the way you are doing it is not the best solution anyway.
What you should probably be doing is keeping a pool of your particles. Create them on initialization. Then when you need to show a particle, pick one from the pool and show it. When it’s done, hide it and return it to the pool.
In this way you avoid creating and deleting the particle and material every time you need one which is slow.

—Apprentice

   

Avatar
kurono, Sr. Member
Posted: 31 August 2011 09:50 AM   Total Posts: 103   [ # 2 ]

Thanks a lot, man! Your solution is pretty elegant rather than brute-force I used. You made my day!
Another one strange thing is that for 2D version everything was fine.

package
{
 import flash
.display.Bitmap;
 
import flash.display.BitmapData;
 
import flash.display.BlendMode;
 
import flash.display.MovieClip;
 
import flash.events.Event;
 
 public class 
effect2d extends Bitmap
 {
 
  
private var drX:Number;
  private var 
drY:Number;
  private var 
drZ:Number;
  private var 
t:Number 0;
  private var 
r:Number;
  private var 
x_0:int;
  private var 
y_0:int;
  
  public function 
effect2d(x0:int 0y0:int 0):void {
   x_0 
x0;
   
y_0 y0;
   
super(new BitmapData(2035false0x000000));
   
+= x_0;
   
+= y_0;
   
blendMode BlendMode.MULTIPLY;
   
alpha Math.random();
   
100 Math.random();
   
drX Math.random();
   
drY Math.random();
   
drZ Math.random();
   
scaleX scaleY Math.random();
   
addEventListener(Event.ENTER_FRAMEloopfalse0true);
  
}
  
  
private function loop(e:Event):void {
   alpha 
-= .005;
   
= -Math.sin(t);
   
= -Math.cos(t) + x_0;
   
-= 5;
   
+= 1;
   
scaleX scaleY += .01;
   
rotationX += drX;
   
rotationY += drY;
   
rotationZ += drZ;
   
   
+= .1;
 
   if (
alpha 0)
    
remove();
  
}
  
  
private function remove():void{
   removeEventListener
(Event.ENTER_FRAMEloop);
   
parent.removeChild(this);
  
}
 }
 
   

felix, Newbie
Posted: 20 October 2011 01:44 AM   Total Posts: 4   [ # 3 ]

It may be that creating multiple BitmapMaterials is eating memory and slowing performance. Try creating one material and sharing between your Sprite3Ds.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X