Best way to switch betwen materials

Software: Away3D 4.x

andreahmed, Member
Posted: 15 October 2013 08:51 AM   Total Posts: 62

Hi All,
What is the best way to switch between custom materials and normal materials at runtime.

I use something like that

_m_finalText = new  Vector.<MaterialBase>();
  
  
  for (var 
j:int 0bitmaps.lengthj++) 
  
{
   _m_finalText
.push(new TrivialTextureMaterial ( new BitmapTexture(bitmaps[j] ) ) );
  

is it possible at the same time to modify the m_finalText vector to use normal texture materials at run time, without adding another vector of other normal textures and another vector of custom materials ?

   

andreahmed, Member
Posted: 15 October 2013 10:03 AM   Total Posts: 62   [ # 1 ]

After understanding the very well written code I have done the followings to solve the issue:

public class TrivialTextureMaterial extends MaterialBase
 { 
  
public var m_currtexture:Texture2DBase;
  
  public function 
TrivialTextureMaterial(texture Texture2DBase)
  
{
   m_currtexture 
texture;
   
addPass(new TrivialTexturePass(texture));
  
}
  
  
public function addBlurPass():void
  {
   addPass
(new MotionBlur_Material(m_currtexture));
  
}
  
  
public function addNormalPass():void
  {
   addPass
(new TrivialTexturePass(m_currtexture));
  
}
 } 

Then at each frame, you just add the normal Pass which is passing the normal textures to the fragment shader, or adding a blur passing.

The engine is really very well written, and architected.

   

Avatar
GoroMatsumoto, Sr. Member
Posted: 15 October 2013 10:10 AM   Total Posts: 166   [ # 2 ]

Basically, I think that your code is not so bad.
Does it fire any errors?

How about using Vector.<BitmapData>?
You need only one material instance for each material classes,
and one texture instance.

And swapping their texture’s bitmapdata on the fly.

Like this:

//init
bitmaps = new Vector.<BitmapData>(bd.lengthtrue);
commonTexture = new BitmapTexture(bitmaps[0]);
material[0] = new TextureMaterial(commonTexture);
material[1] = new TrivialTextureMaterial(commonTexture);

//update
commonTexture.bitmapdata bitmaps[i];
commonTexture.invalidateContent(); 

If you want to use different properties for each bitmaps,
You should create a custom class that have bitmap and properties.

class TextureFactor
{
   
public function TextureFactor(bd:Bitmapdataspecular:Number, ...)
   
{
 
   }

edit:
My reply was too late:)

 

   

andreahmed, Member
Posted: 15 October 2013 10:48 AM   Total Posts: 62   [ # 3 ]

Thanks. I just applied the fragement shader to each texture at runtime.

   

andreahmed, Member
Posted: 16 October 2013 08:04 AM   Total Posts: 62   [ # 4 ]

I have got a major problem. When I try to switch from blur pass to normal texture pass, I get a major drop in the FPS, and when I keep switching from the those states, the FPS gets more drop until it becomes 8~FPS

public class TrivialTextureMaterial extends MaterialBase
 { 
  
public var m_currtexture:Texture2DBase;
  
  public var 
m_MotionBlurPass:MaterialPassBase;
  public var 
m_NormalPass:MaterialPassBase;
  
  public function 
TrivialTextureMaterial(texture Texture2DBase)
  
{
   m_currtexture 
texture;
   
m_MotionBlurPass = new MotionBlur_Material(m_currtexture);
   
m_NormalPass     = new TrivialTexturePass(m_currtexture);
   
   
addNormalPass(); // activate normal texture first
    
  
}
  
  
public function addBlurPass():void
  {
   addPass
(m_MotionBlurPass);
  
  
}
  
  
public function addNormalPass():void
  {
   addPass
(m_NormalPass);
  
}
 } 

when I want to add a blur pass, I just call the texture’s addBlurPass

I also add just one pass to just one texture out of 180 textures, blur and normal pass,  and the FPS still drops to 5FPS when I try to switch many times.. that’s weird. it kills it every frame, until I get 5FPS!

   

Avatar
GoroMatsumoto, Sr. Member
Posted: 16 October 2013 09:54 AM   Total Posts: 166   [ # 5 ]

I can’t know about all of your other codes.
But you may misunderstanding for something on rendering process.

Anyway, at first, if you want to make blur on a texture,
you don’t need making a new material class.
You should the blur class as MaterialMethod.

For example, see the FogMethod class.
And you can modify it as the blur class.

Or if you have enough money,
I think that you can ask Away team for it.
http://awaystudios.com/

   

andreahmed, Member
Posted: 16 October 2013 10:06 AM   Total Posts: 62   [ # 6 ]

The blur is already working. The problem is when I want to apply the blur to a texture for example,  I just call addBlurPass();, which calls addPass(m_MotionBlurPass).  Doing that on purpose, make the engine so slow after many times.

Do you mean I have to addPasses at init for all texture materials, but how do I activate it, when I want to blur the texture ?

here the init Materials

private function intiMaterials():void
  {
   bitmaps 
= new Vector.<BitmapData>();
   var 
matrix:Matrix = new Matrix();
   
   for (var 
i:int 03i++) 
   

    
    bitmaps
.push( new reelTexture_1().bitmapData); 
    
bitmaps.push( new reelTexture_2().bitmapData);  
    
bitmaps.push( new reelTexture_3().bitmapData);
    
bitmaps.push( new reelTexture_4().bitmapData);
    
bitmaps.push( new reelTexture_5().bitmapData);
    
bitmaps.push( new reelTexture_6().bitmapData);
    
bitmaps.push( new reelTexture_7().bitmapData);
    
bitmaps.push( new reelTexture_8().bitmapData);
    
bitmaps.push( new reelTexture_9().bitmapData);   
    
bitmaps.push( new reelTexture_10().bitmapData);   
    
bitmaps.push( new reelTexture_11().bitmapData);  
    
bitmaps.push( new reelTexture_12().bitmapData);
    
   
}
   
    
    
   render 
= new BitmapData 128 128 false0xFFFFFF );
   
   
   
_m_finalSymbols  = new Vector.<TrivialTextureMaterial>();
   for (var 
j:int 0bitmaps.lengthj++) 
   
{
    _m_finalSymbols
.push(new TrivialTextureMaterial( new BitmapTexture(bitmaps[j] ) ) );
   
}
   
    


Then If I want to blur a texture or make it normal I do that

/* Switch textures to blurry ones by applying the fragmenet shader*/
  
public function addBlurforAllSymbols():void
  {
   
   
for each (var t:TrivialTextureMaterial in _m_finalSymbols
   
{
    
//t.addBlurPass();
    
   
}
   
  }
  
  
/* Switch textures to normal  ones by applying the fragmenet shader pass*/
  
public function addNormalTextureforAllSymbols():void
  {
   
for each (var t:TrivialTextureMaterial in _m_finalSymbols
   
{
    
//t.addNormalPass();
   

The problem is when calling the above functions several times, the FPS drop a lot.  What is wrong with my approach technically ?

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X