研究了2周的Starling ,期间看了几本关于Shader的书,感觉已经略懂了啊! 于是试着把水波AGAL移植进Starling的滤镜中,结果意料之外的顺利,在研究Starling的FragmentFilter的时候发现在其 设置纹理->绘制三角形->后紧跟的是清空纹理. 这个我之前完全没想到要这样做, 也许是优化? 反正不重写这个方法的话还会导致context3D丢失. 仔细想想的话,这货其实不能仅仅称为滤镜怎么简单,甚至可以扩展层uv动画工具类了的说,比如残影啊,地震抖动啊 等等…
话不多说,直接上demo. 地图层是starling 并附带上了水波滤镜, 在文章最后会开源. 然后中间层away3D. 顶层还是starling的UI层.
demo中调用了A*寻路,因为路径编辑器是花了5分钟定制写的…画的非常偷懒,如果人物移动的时候突然丢失context3D了,说明是寻路报错了.直接刷新就好了.
DEMO地址:http://www.dreamfairy.cn/blog/work/flash/3d/everymounthgame03/myRpg.html
水波滤镜代码:请将前一篇文章AGAL 笔记01中的 柏林噪音图配合使用 http://www.dreamfairy.cn/blog/?p=1316?
[cc lang=”actionscript3″ nowrap=”false”]
package starling.filters
{
import flash.display3D.Context3D;
import flash.display3D.Context3DProgramType;
import flash.display3D.Program3D;
import flash.utils.getTimer;
import starling.core.Starling;
import starling.textures.Texture;
/********************************************************
* @Author:苍白的茧
* @WebSite:http:www.dreamfairy.cn
********************************************************/
public class WaveFilter extends FragmentFilter
{
public function WaveFilter(waveTexture : Texture)
{
m_waveTexture = waveTexture;
}
protected override function createPrograms():void
{
var vertexProgramCode:String =
“m44 op, va0, vc0 n” +
“mov v0, va1 n”;
var fragmengProgramCode : String =
“mul ft0, v0, fc5.xn”+
“add ft0,ft0,fc5.y n”+
“mul ft1, v0, fc5.xn”+
“add ft1,ft1,fc5.y n”+
“tex ft2, ft0, fs1 <2d,linear,repeat>n”+
“mul ft2, ft2, fc6.xn”+
“sub ft2, ft2, fc6.yn”+
“tex ft3, ft1, fs1 <2d,linear,repeat>n”+
“mul ft3, ft3, fc6.xn”+
“sub ft3, ft3, fc6.yn”+
“add ft4, ft2, ft3n”+
“nrm ft4.xyz, ft4n”+
“mul ft4, ft4, fc5.wn”+
“add ft5, v0,ft4n”+
“tex ft1, ft5, fs0 <2d,linear,repeat>n” +
“mov oc, ft1n”;
m_waveProgram = assembleAgal(fragmengProgramCode, vertexProgramCode);
}
public override function dispose():void
{
if(m_waveProgram)m_waveProgram.dispose();
m_waveTexture.dispose();
super.dispose();
}
protected override function activate(pass:int, context:Context3D, texture:starling.textures.Texture):void
{
// already set by super class:
//
// vertex constants 0-3: mvpMatrix (3D)
// vertex attribute 0: vertex position (FLOAT_2)
// vertex attribute 1: texture coordinates (FLOAT_2)
// texture 0: input texture
context.setTextureAt(1,m_waveTexture.base);
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,4,Vector.
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,5,Vector.
context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT,6,Vector.
context.setProgram(m_waveProgram);
}
protected override function deactivate(pass:int, context:Context3D, texture:Texture):void
{
context.setTextureAt(1,null);
}
private var m_waveTexture : Texture;
private var m_waveProgram : Program3D;
}
}
[/cc]
3D图形学也研究了几周了,接下里开始专注模型的格式,解析,差值,优化,uv动画上了.
下个月的游戏就决定以模型为主了~