Writing a custom Material Method

Software: Away3D 4.x

andreahmed, Member
Posted: 16 October 2013 01:31 PM   Total Posts: 62

Hi all,

I’m to write a custom material method, but unfortunately, there are no tutorials to explain that :(. I tried to add the following blur code, but I don’t see any output.

package  
{
 import com
.greensock.OverwriteManager;
 
 
import flash.display3D.Context3D;
 
import flash.display3D.Context3DProgramType;
 
import flash.geom.Matrix3D;
 
 
import away3d.arcane;
 
import away3d.cameras.Camera3D;
 
import away3d.core.base.IRenderable;
 
import away3d.core.managers.Stage3DProxy;
 
import away3d.materials.compilation.ShaderRegisterCache;
 
import away3d.materials.compilation.ShaderRegisterElement;
 
import away3d.materials.methods.EffectMethodBase;
 
import away3d.materials.methods.MethodVO;
 
import away3d.textures.Texture2DBase;
 
 use namespace 
arcane;
 
 
/**
  * FogMethod provides a method to add distance-based fog to a material.
  */
 
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:Texture2DBase;
  
/**
   * Creates a new FogMethod object.
   * @param minDistance The distance from which the fog starts appearing.
   * @param maxDistance The distance at which the fog is densest.
   * @param fogColor The colour of the fog.
   */
  
public function BlurMethodtexture:Texture2DBase)
  
{
   super
();
   
   
m_texture texture;
   
_amount 20;
    
   
  
}
  
  
/**
   * @inheritDoc
   */
  
override arcane function initVO(vo:MethodVO):void
  {
   vo
.needsProjection true;
  
}
  
  
/**
   * @inheritDoc
   */
  
override arcane function initConstants(vo:MethodVO):void
  {
   
   
  }
  
  
  
/**
   * @inheritDoc
   */
  
arcane override function activate(vo:MethodVOstage3DProxy:Stage3DProxy):void
  {
   
   
var context Context3D stage3DProxy._context3D;
   
   var 
data:Vector.<Number> = vo.fragmentData;
   var 
index:int vo.fragmentConstantsIndex;
  
   
// todo: must be normalized using view size ratio instead of texture
   
var invH:Number 1/128;
   
   
data[0] _amount*.5*invH;
   
data[1] _realStepSize*invH;
   
data[2] 1/10;
   
   
context.setTextureAt(0m_texture.getTextureForStage3D(stage3DProxy));
     
   
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT0data1);
     
   
  
}
   
   
/**
   * @inheritDoc
   */
  
arcane override function getFragmentCode(vo:MethodVOregCache:ShaderRegisterCachetargetReg:ShaderRegisterElement):String
  {   
   
   
var code:String  =""
   
   var 
textureReg:ShaderRegisterElement regCache.getFreeTextureReg();
   var 
temp:ShaderRegisterElement regCache.getFreeFragmentVectorTemp();
   var 
uvReg:ShaderRegisterElement =  _sharedRegisters.uvVarying;
   
vo.texturesIndex textureReg.index;
   
   var 
textsampler:String =  getTex2DSampleCode(votemptextureRegm_textureuvReg);
     
   
     
code += textsampler;
    
   
   
calculateStepSize();
   
    
    
    for (var 
x:Number _realStepSize<= _amount+= _realStepSize
    
{
    code 
+= "add ft1.y, ft1.y, fc0.y n";
     
code += "tex ft2, v1, fs1 <2d,linear,clamp>n" +
     
"add ft1, ft1, ft2 n";
     ++
numSamples;
    

    
     code 
+= "mul ft0, ft1, fc0.zn";
  
 
    
    return 
code;
   
}
  
  
   
  
  
private function calculateStepSize():void
  {
   _realStepSize 
_stepSize 0_stepSize :
    
_amount MAX_AUTO_SAMPLES_amount/MAX_AUTO_SAMPLES :
    
1;
  
}
  
  
   
  
 }
 
 
 
 
}
import away3d
.materials.methods

 

   

andreahmed, Member
Posted: 16 October 2013 03:17 PM   Total Posts: 62   [ # 1 ]

its terribly hard! a lot of crashes!  I investigated the free registers, and I used all the free registers, still I get weird fragement results, like black textures.

Is there any other easier way to achieve that task ?!

   

andreahmed, Member
Posted: 16 October 2013 03:48 PM   Total Posts: 62   [ # 2 ]

I actually tried a lot but with no success, I wish the moderators help us with writing a small tutorial of writing custom methods smile

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X