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 BlurMethod( texture: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:MethodVO, stage3DProxy: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(0, m_texture.getTextureForStage3D(stage3DProxy));
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, data, 1);
}
/**
* @inheritDoc
*/
arcane override function getFragmentCode(vo:MethodVO, regCache:ShaderRegisterCache, targetReg: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(vo, temp, textureReg, m_texture, uvReg);
code += textsampler;
calculateStepSize();
for (var x:Number = _realStepSize; x <= _amount; x += _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;