MetaEllipse的公式如下:
为了效率去掉开方运算的公式:
PixelBender代码:
<languageVersion : 1.0;> kernel MetaEllipse <namespace : "MetaEllipse"; 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)/(2.0*(ball1.x-coord.x)*(ball1.x-coord.x) + 5.0*(ball1.y-coord.y)*(ball1.y-coord.y)); sum += (ball2.z)/(2.0*(ball2.x-coord.x)*(ball2.x-coord.x) + 5.0*(ball2.y-coord.y)*(ball2.y-coord.y)); sum += (ball3.z)/(2.0*(ball3.x-coord.x)*(ball3.x-coord.x) + 5.0*(ball3.y-coord.y)*(ball3.y-coord.y)); sum += (ball4.z)/(2.0*(ball4.x-coord.x)*(ball4.x-coord.x) + 5.0*(ball4.y-coord.y)*(ball4.y-coord.y)); sum += (ball5.z)/(2.0*(ball5.x-coord.x)*(ball5.x-coord.x) + 5.0*(ball5.y-coord.y)*(ball5.y-coord.y)); sum += (ball6.z)/(2.0*(ball6.x-coord.x)*(ball6.x-coord.x) + 5.0*(ball6.y-coord.y)*(ball6.y-coord.y)); sum += (ball7.z)/(2.0*(ball7.x-coord.x)*(ball7.x-coord.x) + 5.0*(ball7.y-coord.y)*(ball7.y-coord.y)); sum += (ball8.z)/(2.0*(ball8.x-coord.x)*(ball8.x-coord.x) + 5.0*(ball8.y-coord.y)*(ball8.y-coord.y)); sum += (ball9.z)/(2.0*(ball9.x-coord.x)*(ball9.x-coord.x) + 5.0*(ball9.y-coord.y)*(ball9.y-coord.y)); sum += (ball10.z)/(2.0*(ball10.x-coord.x)*(ball10.x-coord.x) + 5.0*(ball10.y-coord.y)*(ball10.y-coord.y)); if(sum >= minThreshold){ dst.rgb = float3(255,255,255); } } }
AS类PixelBenderManyMetaEllipses:
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 PixelBenderManyMetaEllipses extends Sprite{ [Embed(source='assets/MetaEllipse10.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 numbers:int=10; private var balls:Array = []; private var b1:Metaball; private var b2:Metaball; private var b3:Metaball; private var b4:Metaball; private var b5:Metaball; private var b6:Metaball; private var b7:Metaball; private var b8:Metaball; private var b9:Metaball; private var b10:Metaball; public function PixelBenderManyMetaEllipses(){ 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
转载请注明:陈童的博客 » MetaEllipse