Sharing my BLUR Method

Software: Away3D 4.x

andreahmed, Member
Posted: 17 October 2013 02:10 PM   Total Posts: 62

Hi All,
After hacking, debugging the framework, I managed to add a blur method ( Vertical).

Here is the code, and thanks for everyone that helped,  Especially GoroMatsumoto, Fabian.

//Vertical Blur Effect Method, By Ahmed Saleh Tolba. 
package 
{
   
 import flash
.display3D.Context3D;
 
import flash.display3D.Context3DProgramType;
  
 
import away3d.arcane;
  
import away3d.core.managers.Stage3DProxy;
 
import away3d.materials.TextureMaterial;
 
import away3d.materials.compilation.ShaderRegisterCache;
 
import away3d.materials.compilation.ShaderRegisterElement;
 
import away3d.materials.methods.EffectMethodBase;
 
import away3d.materials.methods.MethodVO;
  
 use namespace 
arcane;
 
 public class 
BlurMethod extends EffectMethodBase
 {
  
private static var MAX_AUTO_SAMPLES:int 15;
  private var 
_amount:uint;
  private var 
_stepSize:int 1;
  private var 
_realStepSize:Number;
  private var 
numSamples:int 1;
  
  private var 
m_texture:TextureMaterial;
  private var 
data:Vector.<Number>; 
  
  public function 
BlurMethod( )
  
{
   super
();
    
_amount 19;
   
calculateStepSize();
   
  
}
  
  
public function setTexture(texture:TextureMaterial)
  
{
   m_texture 
texture;
  
}
  
  
public function set Amountamount:Number):void
  {
   
   this
._amount amount;
  
}
  
  
public function get Amount():Number { return _amount}
  
  
  
public function set stepSizestepSize:Number):void
  {
   
   this
._realStepSize stepSize;
  
}
  
  
public function ActivateNormalValues():void
  {
   this
._amount  10;  
   
this._realStepSize 1;
  
}
  
  
public function ActivateBlurryValues():void
  {
   this
._amount  10
   
this._realStepSize 10.1;
  
}
  
  
public function get stepSize():Number { return _realStepSize}
  
  
  
/**
   * @inheritDoc
   */
  
override arcane function initVO(vo:MethodVO):void
  {
   vo
.needsProjection true;
  
}
  
  
/**
   * @inheritDoc
   */
  
override arcane function initConstants(vo:MethodVO):void
  {
   
   
var dataV Vector.<Number> = vo.fragmentData;
   
   
dataV[0] 0;
   
dataV[1] 0;
   
dataV[2] 0;
   
dataV[3] 1;
   
   
   
  
}
  
  
  
/**
   * @inheritDoc
   */
  
arcane override function activate(vo:MethodVOstage3DProxy:Stage3DProxy):void
  {
   
   
var context Context3D stage3DProxy._context3D;
   
   
   
   var 
dataV Vector.<Number> = vo.fragmentData;
   
   
// todo: must be normalized using view size ratio instead of texture
   
var invH:Number 1/512;
   
   
   var 
texIndex:int vo.texturesIndex;
   
super.activate(vostage3DProxy);
   
texIndex+=1;
   
context.setTextureAt(0m_texture.texture.getTextureForStage3D(stage3DProxy));
   
   
dataV[0] _amount*invH;
   
dataV[1] _realStepSize*invH;
   
dataV[2] 1/_amount;
   
   
   
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT0dataV1);
   
   
   
   
   
  
}
  
  
/**
   * @inheritDoc
   */
  
arcane override function getFragmentCode(vo:MethodVOregCache:ShaderRegisterCachetargetReg:ShaderRegisterElement):String
  {  
   
   
   
var code:String  ="mov ft1, v1n";
   
code += "tex ft2, ft1, fs0 <2d,linear,miplinear,clamp>n";
   
   
calculateStepSize();
   
   
trace("amount of blur in shader",_amount);
   
   for (var 
x:Number _realStepSize<= _amount+= _realStepSize
   
{
    code 
+= "add ft1.y, ft1.y, fc0.y n";
    
    
code += "tex ft3, ft1, fs0 <2d,linear,miplinear,clamp>n";
    
code += "add ft2, ft2, ft3 n";
    ++
numSamples;
   

   
   code 
+= "mul ft5, ft2, fc0.zn";
   
code += " mov ft0,ft5n";
   
   
   
   
   return 
code;
  
}
  
  
  
  
  
private function calculateStepSize():void
  {
   _realStepSize 
_stepSize 0_stepSize :
    
_amount MAX_AUTO_SAMPLES_amount/MAX_AUTO_SAMPLES :
    
1;
  
}
   
  
 }
  

 

   

Avatar
GoroMatsumoto, Sr. Member
Posted: 17 October 2013 05:58 PM   Total Posts: 166   [ # 1 ]

Good job!

   

andreahmed, Member
Posted: 18 October 2013 07:29 AM   Total Posts: 62   [ # 2 ]

I’m wondering what is the best way to remove a method, and re-use it again if I want… ?

   

Avatar
GoroMatsumoto, Sr. Member
Posted: 18 October 2013 09:51 AM   Total Posts: 166   [ # 3 ]

What do you mean?
You just make a instance of your method,
then add/remove it, right?

   

andreahmed, Member
Posted: 18 October 2013 09:52 AM   Total Posts: 62   [ # 4 ]

yes, but when you remove it, how do you add it again ? make new instances ?

   

Avatar
GoroMatsumoto, Sr. Member
Posted: 18 October 2013 10:07 AM   Total Posts: 166   [ # 5 ]

Can’t you reuse the instance?
I think that “material.removeMethod” just get rid of the method pass
from rendering passes of a material.

// Declare as private.
private var yourMethod:YourMethod;

// Instance
yourMethod = new YourMethod();

// If you want to remove or add it,
material.addMethod(yourMethod);
material.removeMethod(yourMethod);
material.addMethod(yourMethod); 
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X