如果想创建更多的Metaball,就需要修改PixelBender写的代码,但PixelBender针对Flash Player并不支持循环,使得代码显得繁琐。PixelBender的代码如下:
<languageVersion : 1.0;> kernel Metaballs <namespace : "Metaballs"; vendor : "Chen Tong"; version : 1; description : "Fast Metaballs"; > { parameter float minThreshold < minValue:float(0.0); maxValue:float(2.0); defaultValue:float(0.9); description: "minThreshold"; >; parameter float3 ball1 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(50.0,50.0,20.0); description: "ball1, params, x,y,radius"; >; parameter float3 ball2 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball2, params, x,y,radius"; >; parameter float3 ball3 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball3, params, x,y,radius"; >; parameter float3 ball4 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball4, params, x,y,radius"; >; parameter float3 ball5 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball5, params, x,y,radius"; >; parameter float3 ball6 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball6, params, x,y,radius"; >; parameter float3 ball7 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball7, params, x,y,radius"; >; parameter float3 ball8 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball8, params, x,y,radius"; >; parameter float3 ball9 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball9, params, x,y,radius"; >; parameter float3 ball10 < minValue:float3(0.0,0.0,0.0); maxValue:float3(640.0,480.0,50.0); defaultValue:float3(100.0,100.0,20.0); description: "ball10, params, x,y,radius"; >; input image4 src; output pixel4 dst; void evaluatePixel(){ dst = sampleNearest(src,outCoord()); dst.rbg = float3(0,0,0); float2 coord = outCoord(); float sum = 0.0; sum += (ball1.z)/((ball1.x-coord.x)*(ball1.x-coord.x)+(ball1.y-coord.y)*(ball1.y-coord.y)); sum += (ball2.z)/((ball2.x-coord.x)*(ball2.x-coord.x)+(ball2.y-coord.y)*(ball2.y-coord.y)); sum += (ball3.z)/((ball3.x-coord.x)*(ball3.x-coord.x)+(ball3.y-coord.y)*(ball3.y-coord.y)); sum += (ball4.z)/((ball4.x-coord.x)*(ball4.x-coord.x)+(ball4.y-coord.y)*(ball4.y-coord.y)); sum += (ball5.z)/((ball5.x-coord.x)*(ball5.x-coord.x)+(ball5.y-coord.y)*(ball5.y-coord.y)); sum += (ball6.z)/((ball6.x-coord.x)*(ball6.x-coord.x)+(ball6.y-coord.y)*(ball6.y-coord.y)); sum += (ball7.z)/((ball7.x-coord.x)*(ball7.x-coord.x)+(ball7.y-coord.y)*(ball7.y-coord.y)); sum += (ball8.z)/((ball8.x-coord.x)*(ball8.x-coord.x)+(ball8.y-coord.y)*(ball8.y-coord.y)); sum += (ball9.z)/((ball9.x-coord.x)*(ball9.x-coord.x)+(ball9.y-coord.y)*(ball9.y-coord.y)); sum += (ball10.z)/((ball10.x-coord.x)*(ball10.x-coord.x)+(ball10.y-coord.y)*(ball10.y-coord.y)); if(sum >= minThreshold){ dst.rgb = float3(255,255,255); } } }
测试代码如下:
package{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Shader;
import flash.display.ShaderPrecision;
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.filters.ShaderFilter;
import flash.utils.ByteArray;
[SWF(width=640, height=480)]
public class PixelBenderManyMetaballs extends Sprite{
[Embed(source=’assets/Metaball10.pbj’, mimeType=’application/octet-stream’)]
private static const MetaballsFilter:Class;
private var bmp:Bitmap;
private var bmpd:BitmapData=new BitmapData(640, 480, false, 0);
private var blur:BlurFilter=new BlurFilter(10, 10, 1);
private var metaballsFilter:ShaderFilter;
private var balls:Array = [];
private var numbers:int=10;
public function PixelBenderManyMetaballs(){
bmp=new Bitmap(bmpd);
bmp.smoothing=true;
addChild(bmp);
var ba:ByteArray=new MetaballsFilter() as ByteArray;
var s:Shader=new Shader(ba);
metaballsFilter=new ShaderFilter(s);
metaballsFilter.shader.data.src.image=bmpd;
for(var i:int=0;i
转载请注明:陈童的博客 » 更多的Metaball