在Flash Builder 4 beta中使用Pixel Bender作为数值运算引擎 (四)
使用Pixel BEnder进行更加复杂的运算
Pixel Bender kernel语言同样包括用于更加复杂的数值处理的函数:
· sin(x) - Returns the sine of x.
· cos(x) – Returns cosine of x.
· tan(x) – Returns the tangent of x.
· asin(x) – Returns the arcsine (inverse sine) of x.
· acos(x) – Returns the arccosine (inverse cosine) of x.
· atan(x) – Returns the arctangent (inverse tangent) of x.
· exp(x) - Returns ex.
· log(x) – Returns the natural logarithm of x.
· pow(x, y) - Returns xy.
· sqrt(x) – Returns the positive square root of x.
第二个例子中,我们会创建并利用一个使用了cos()的kernel。
创建该kernel
与上一节创建 SimpleCalculator.pbj的方式类似,我们使用下面的代码来创建CosCalculator.pbj。
<languageVersion : 1.0;> kernel CosCalculator < namespace : "pixelBender"; vendor : "Elad Elrom"; version : 1; description : "Cos Calculator"; > { input image1 src; output pixel3 result; void evaluatePixel() { pixel1 value = pixel1(cos(sample(src, outCoord()))); result = pixel3(value, 0.0, 0.0); } }
建立Flex应用
用下面的代码建立一个新的Flex应用。这些代码和上一节的例子十分相似,主要的不同在于这次只传入了一列数值(上一节是两列)。
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768"> <fx:Script> <![CDATA[ import mx.collections.IList; [Embed(source="CosCalculator.pbj", mimeType="application/octet-stream")] protected function initializeHandler(list:IList):void { var shader:Shader = new Shader(new KernelClass()); var shaderJob:ShaderJob; byteArray = convertListToByteArray( list ); width = byteArray.length >> 2; height = 1; shader.data.src.width = width; shader.data.src.height = height; shader.data.src.input = byteArray; shaderJob = new ShaderJob(shader, result, width, height); shaderJob.start(); } { result.position = 0; for(i = 0; i < length; i += 4) { num = result.readFloat(); if(i % 3 == 0) listDest.dataProvider.addItem( num ); } } /** * Static method to convert the list object into byte array object. * * @param array * @return * */ { for (i; i<len; i++) { } retVal.position = 0; return retVal; } ]]> </fx:Script> <s:List id="listSrc" width="57" height="158"> <s:ArrayCollection> <fx:Object>1</fx:Object> <fx:Object>2</fx:Object> <fx:Object>3</fx:Object> <fx:Object>4</fx:Object> <fx:Object>5</fx:Object> <fx:Object>6</fx:Object> <fx:Object>7</fx:Object> </s:ArrayCollection> </s:List> <s:Button x="71" y="12" label="Calculate >" click="initializeHandler(listSrc.dataProvider)"/> <s:List id="listDest" width="200" height="158" x="160" y="0"> <s:ArrayCollection /> </s:List> </s:Application>
当你编译好该应用后,在浏览器中运行它并点击Calculate来查看结果(见图5)。
注意:cos()的输入参数代表弧度

图5. 运行在浏览器中的使用cos()计算的例子

.gif)
.gif)




.gif)
发表新评论