守望者--AIR技术交流

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: ANE FlasCC 炼金术
查看: 2573|回复: 1

[技术资料] 你好,三角形

[复制链接]
  • TA的每日心情
    擦汗
    2018-4-10 15:18
  • 签到天数: 447 天

    [LV.9]以坛为家II

    1742

    主题

    2094

    帖子

    13万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    562
    贡献
    29
    金币
    52623
    钢镚
    1422

    开源英雄守望者

    发表于 2015-3-11 10:19:51 | 显示全部楼层 |阅读模式
    本帖最后由 破晓 于 2015-3-11 10:24 编辑

    在本文中,你将研究一个能够正常运行的基于Stage3D API的ActionScript应用程序。 首先,你需要学会如何正确地对一个准备就绪的Stage3D构建环境进行配置。 一旦范例项目建立,你将了解如何在ActionScript中对Stage3D进行初始化,以及如何使用Stage3D来创建和渲染一个由单一彩色三角形组成的超简单3D场景。

    最后,你需要查看一下应用纹理映射(texture mapping)的过程,并且你需要回顾一下一个有纹理映射几何图形的Stage3D应用程序。



    建立构建环境

    当你开始构建Stage3D应用程序时,第一个任务包括准备你的构建环境并确保其设置正确。

    尽管肯定能够使用基础的Flex SDK从命令行中构建Stage3D应用程序,但是使用集成工具,例如Flash Builder 4.5,却常常更加便捷。 本文中提供的步骤将侧重于使用Flash Builder 4.5。

    你可以通过下载和安装最新版本的Flex 4.5 SDK(版本4.5.0.20967或者更高版本)开始建立构建环境。 在本文一开始的要求(Requirements)部分已经提供下载Flex 4.5 SDK的链接。

    接下来,将要求(Requirements)部分中链接的新的Flash Player 11版本的playerglobal.swc文件下载并安装至Flex 4.5 SDK中。 复制该SWC文件并将它粘帖到你刚下载的Flex SDK中的文件夹结构中:


    1. <font face="inherit"><font style="font-size: 13.63636302947998px"><Flex SDK root folder>\frameworks\libs\player\11.0</font></font>
    复制代码
    注意:你必须手动地创建“11.0”文件夹,然后将SWC文件复制到其中。 如果有必要的话,将该文件重新命名为playerglobal.swc。 SDK的未来版本将包括Flash Player 11版本的playerglobal.swc文件;一旦更新的SDK推出,那么这个额外的步骤将不再有存在的必要。在此之后,将你刚下载的最新的Flex 4.5 SDK添加到你的Flash Builder 4.5环境中。 为了完成上述任务,选中Preferences > Flash Builder > Installed Flex SDKs。 使用相应的界面来添加新的Flex 4.5 SDK。很显然,你还需要下载并安装Flash Player版本11,以便于支持Stage3D! 如果你还没有这样做的话,那么,现在就使用要求(Requirements)部分的链接下载并安装它。当你设置好你刚下载的含有playerglobal.swc文件的新的Flex 4.5 SDK并将它添加到你的Flex Builder安装路径之后,你可以开始创建一个新的ActionScript 项目。此外,你还需要将你项目的目标版本配置为SWF版本13。打开Project Properties并单击ActionScript Compiler标签。 将“Additional compiler arguments”设置为“-swf-version=13”(不带引号)。 此外,在同一面板中,仔细检查确认应用程序的目标版本是Flash Player版本11。最后,为了使得Flash Player能够真正地使用3D硬件加速功能,你还需要将WMODE设置为"direct"。 在Flash Builder中,打开index.template.html文件并且找出将params传递到SWF文件的相应位置。 添加下面代码:
    1. params.wmode = “direct”;
    复制代码
    图1所示的截图说明了在何处添加该行代码。




    图1. 在index.template.htm文件中设置wmode = "direct"


    该显示设置可以引用将Flash Player作为目标的ActionScript。 当你编译一个AIR应用程序时,你需要将应用程序描述符中的renderMode元素设置为direct。现在编译你的应用程序,以便确保它能够正常运行。 它应该仅仅只显示一个空白的窗口。

    初始化Stage3D
    当你建立好ActionScript应用程序之后,你需要做的第一件事情就是初始化Stage3D。为了能够实现3D渲染功能,你需要一个Context3D类的实例,它基本上可以用作一个3D渲染表面。因此,在构造函数中,添加下面的代码:
    1. public function HelloTriangleColored()
    2. {                        
    3.         stage.stage3Ds[0].addEventListener( Event.CONTEXT3D_CREATE, initMolehill );
    4.         stage.stage3Ds[0].requestContext3D();                        
    5. }
    复制代码
    上述代码仅使用Stage3D API请求一个Context3D实例,并且注册一个事件侦听器。 当Context3D实例准备就绪时,该事件将回调initStage3D 函数。当initStage3D被调用以后,通过调用Context3D::configureBackBuffer方法来正确配置你的Context3D将显得非常重要。
    1. protected function initMolehill(e:Event):void
    2. {
    3.         context3D = stage.stage3Ds[0].context3D;                        
    4.         context3D.configureBackBuffer(800, 600, 2, true);
    5.         ...
    6. }
    复制代码
    上述代码指定了你使用的渲染视口为800 x 600像素,它含有最低级别的锯齿消除功能(anti-aliasing)(第三参数),并且为该渲染表面创建了相应的深度和模版缓冲区(stencil buffer)(第四参数)。

    创建一个彩色三角形几何图形

    在本章节中,你将创建一些3D几何图形(需要渲染的3D对象)。 就本范例而言,你将创建一个可能是最简单的几何图形:一个彩色三角形。为了实现这一任务,你需要一个Vertex Buffer,并且你应该为顶点位置(x,y,z)和顶点颜色(r,g,b)定义Vertex Attributes。 每一个顶点均有6个组件。 首先,你应该将相应的Vertex Buffer数据定义到一个矢量中,如下所示:
    1. protected function initMolehill(e:Event):void
    2. {
    3.         ...
    4.         var vertices:Vector.<Number> = Vector.<Number>([
    5.         -0.3,-0.3,0, 1, 0, 0, // x, y, z, r, g, b
    6.         -0.3, 0.3, 0, 0, 1, 0,
    7.         0.3, 0.3, 0, 0, 0, 1]);
    8.         ...
    9. }
    复制代码
    然后,创建一个你可以用来将Vertex Buffer数据上传给GPU的VertexBuffer3D实例。
    1. protected var vertexbuffer:VertexBuffer3D;
    2. ...
    3. protected function initMolehill(e:Event):void
    4. {
    5.         ...
    6.         // Create VertexBuffer3D. 3 vertices, of 6 Numbers each
    7.         vertexbuffer:VertexBuffer3D = context3D.createVertexBuffer(3, 6);
    8.         // Upload VertexBuffer3D to GPU. Offset 0, 3 vertices
    9.         vertexbuffer.uploadFromVector(vertices, 0, 3);        
    10.         ...
    11. }
    复制代码
    此外,你还需要一个Index Buffer来定义你的三角形。 在本例中,这个单独的三角形将仅仅由顶点0、1和2组成。 与Vertex Buffer相似,Index Buffer也必须上传给GPU。 为了完成这一任务,你需要使用IndexBuffer3D类:
    1. protected var indexbuffer:IndexBuffer3D;
    2. ...
    3. protected function initMolehill(e:Event):void
    4. {
    5.         ...
    6.         var indices:Vector.<uint> = Vector.<uint>([0, 1, 2]);
    7.         // Create IndexBuffer3D. Total of 3 indices. 1 triangle of 3 vertices
    8.         indexbuffer = context3D.createIndexBuffer(3);                        
    9.         // Upload IndexBuffer3D to GPU. Offset 0, count 3
    10.         indexbuffer.uploadFromVector (indices, 0, 3);        
    11.         ...
    12. }
    复制代码
    上面的代码定义了相应的几何图形。 现在,你需要一个Vertex和一个Fragment Shader。为简单起见,你应该使用在之前一篇文章中讨论过的相同的Shader程序,该文章包含于标题为 什么是AGAL(What is AGAL)
    的系列教程。 Vertex Shader只不过按照一个从ActionScript传入的转换矩阵对顶点进行转换,然后将顶点颜色沿着渲染管线(rendering pipeline)传递给Fragment Shader。
    1. m44 op, va0, vc0
    2. mov v0, va1
    复制代码
    Fragment Shader从自己的输入获取内插颜色(interpolated color),并且将它作为输出颜色进行传递。
    1. mov oc, v0
    复制代码
    你需要使用AGAL Mini汇编程序(AGAL Mini Assembler)
    将Shader代码汇编至对象代码中,然后使用Program3D API类将Shader上传至GPU中。
    1. protected function initMolehill(e:Event):void
    2. {
    3.         ...
    4. var vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();
    5.         vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
    6.                 "m44 op, va0, vc0\n" + // pos to clipspace
    7.                 "mov v0, va1" // copy color
    8.         );                        

    9. var fragmentShaderAssembler : AGALMiniAssembler= new AGALMiniAssembler();
    10.         fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,
    11.         
    12.                 "mov oc, v0 "
    13.         );

    14.         program = context3D.createProgram();
    15. program.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
    16.         ...
    17. }
    复制代码
    渲染场景
    至此,可以对相应的场景进行渲染。 在本章节中,你将建立一个渲染循环。 你只需要创建一个onRender函数即可,利用ENTER_FRAME事件能够在每一帧中调用该函数。
    1. protected function onRender(e:Event):void
    2. {
    3.         if ( !context3D )
    4.                 return;
    5.         ...
    6. }
    复制代码
    在每一个帧渲染器开始处,你都将调用Context3D::clear.。 这一操作可以利用我们传入的背景色清除渲染颜色缓冲区(内容被渲染的表面)(因为与Context3D相关联的深度和模版缓冲区将被清除)。 使用下面给出的代码传入一个白色的背景:
    1. protected function onRender(e:Event):void
    2. {
    3.         ...        
    4.         context3D.clear ( 1, 1, 1, 1 );
    5.         ...
    6. }
    复制代码
    在每一帧中,你必须使用已上传的Shader启用Program3D以及VertexBuffer3D功能,以便将Vertex Attributes与适当的 Shader Attribute Register建立关联,正如在之前的文章什么是AGAL(What is AGAL)
    中讨论的一样。此外,你还需要传入相应的转换矩阵以便Vertex Shader使用。 让我们使用一个在每一帧中都发生变化的旋转矩阵,使得我们的三角形能够一点点地旋转…
    1. protected function onRender(e:Event):void
    2. {
    3.                         ...        
    4.         // vertex position to attribute register 0
    5.         context3D.setVertexBufferAt (0, vertexbuffer, 0,         Context3DVertexBufferFormat.FLOAT_3);
    6.         // color to attribute register 1
    7.         context3D.setVertexBufferAt(1, vertexbuffer, 3,         Context3DVertexBufferFormat.FLOAT_3);
    8.         // assign shader program
    9.         context3D.setProgram(program);

    10.         var m:Matrix3D = new Matrix3D();
    11.         m.appendRotation(getTimer()/40, Vector3D.Z_AXIS);
    12.         context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
    13.         ...        
    14. }
    复制代码
    在完成这个设置之后,就到了执行实际渲染任务的时候。 你需要调用Context3D::drawTriangles 方法,以便在Index Buffer中传递;该操作能够将三角形渲染至渲染表面(颜色缓冲区)。最后,当你完成该帧上场景的所有3D对象的渲染任务后(在本范例中,只有一个对象),你将需要调用Context3D::present。 该方法将告诉Stage3D,应用程序已经完成帧的渲染并且该帧能够在屏幕上显示。
    1. protected function onRender(e:Event):void
    2. {
    3.         ...        
    4.         context3D.drawTriangles(indexbuffer);
    5.         
    6.         context3D.present();                                
    7. }
    复制代码
    运行Hello Triangle Colored应用程序以便观察最终的结果,并且忙里偷闲欣赏一下你的杰作(参见图2)。





    图2. 已完成的Hello Triangle Colored应用程序

    下面是用来创建Hello Triangle Colored应用程序的完整代码范例:
    1. package
    2. {
    3.         import com.adobe.utils.AGALMiniAssembler;
    4.         
    5.         import flash.display.Sprite;
    6.         import flash.display3D.Context3D;
    7.         import flash.display3D.Context3DProgramType;
    8.         import flash.display3D.Context3DVertexBufferFormat;
    9.         import flash.display3D.IndexBuffer3D;
    10.         import flash.display3D.Program3D;
    11.         import flash.display3D.VertexBuffer3D;
    12.         import flash.events.Event;
    13.         import flash.geom.Matrix3D;
    14.         import flash.geom.Rectangle;
    15.         import flash.geom.Vector3D;
    16.         import flash.utils.getTimer;
    17.         
    18.         [SWF(width="800", height="600", frameRate="60", backgroundColor="#FFFFFF")]
    19.         public class HelloTriangleColored extends Sprite
    20.         {
    21.                 protected var context3D:Context3D;
    22.                 protected var program:Program3D;
    23.                 protected var vertexbuffer:VertexBuffer3D;
    24.                 protected var indexbuffer:IndexBuffer3D;
    25.                
    26.                 public function HelloTriangleColored()
    27.                 {                        
    28.                         stage.stage3Ds[0].addEventListener( Event.CONTEXT3D_CREATE, initMolehill );
    29.                         stage.stage3Ds[0].requestContext3D();
    30.                         
    31.                         addEventListener(Event.ENTER_FRAME, onRender);
    32.                         
    33.                 }
    34.                
    35.                 protected function initMolehill(e:Event):void
    36.                 {
    37.                         context3D = stage.stage3Ds[0].context3D;                        
    38.                         context3D.configureBackBuffer(800, 600, 1, true);
    39.                         
    40.                         var vertices:Vector.<Number> = Vector.<Number>([
    41.                                 -0.3,-0.3,0, 1, 0, 0, // x, y, z, r, g, b
    42.                                 -0.3, 0.3, 0, 0, 1, 0,
    43.                                 0.3, 0.3, 0, 0, 0, 1]);
    44.                         
    45.                         // Create VertexBuffer3D. 3 vertices, of 6 Numbers each
    46.                         vertexbuffer = context3D.createVertexBuffer(3, 6);
    47.                         // Upload VertexBuffer3D to GPU. Offset 0, 3 vertices
    48.                         vertexbuffer.uploadFromVector(vertices, 0, 3);                                
    49.                         
    50.                         var indices:Vector.<uint> = Vector.<uint>([0, 1, 2]);
    51.                         
    52.                         // Create IndexBuffer3D. Total of 3 indices. 1 triangle of 3 vertices
    53.                         indexbuffer = context3D.createIndexBuffer(3);                        
    54.                         // Upload IndexBuffer3D to GPU. Offset 0, count 3
    55.                         indexbuffer.uploadFromVector (indices, 0, 3);                        
    56.                         
    57.                         var vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();
    58.                         vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
    59.                                 "m44 op, va0, vc0\n" + // pos to clipspace
    60.                                 "mov v0, va1" // copy color
    61.                         );                        
    62.                         
    63.                         var fragmentShaderAssembler : AGALMiniAssembler= new AGALMiniAssembler();
    64.                         fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,
    65.                                 
    66.                                 "mov oc, v0"
    67.                         );
    68.                         
    69.                         program = context3D.createProgram();
    70.                         program.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
    71.                 }        
    72.                
    73.                 protected function onRender(e:Event):void
    74.                 {
    75.                         if ( !context3D )
    76.                                 return;
    77.                         
    78.                         context3D.clear ( 1, 1, 1, 1 );
    79.                         
    80.                         // vertex position to attribute register 0
    81.                         context3D.setVertexBufferAt (0, vertexbuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
    82.                         // color to attribute register 1
    83.                         context3D.setVertexBufferAt(1, vertexbuffer, 3, Context3DVertexBufferFormat.FLOAT_3);
    84.                         // assign shader program
    85.                         context3D.setProgram(program);
    86.                         
    87.                         var m:Matrix3D = new Matrix3D();
    88.                         m.appendRotation(getTimer()/40, Vector3D.Z_AXIS);
    89.                         context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
    90.                         
    91.                         context3D.drawTriangles(indexbuffer);
    92.                         
    93.                         context3D.present();                        
    94.                 }
    95.         }
    96. }
    复制代码
    应用纹理映射(texture mapping)

    你在本文前一部分建立的Hello Triangle Colored应用程序渲染了一个彩色的三角形。 该三角形的颜色被定义为Vertex Attributes,它们是Vertex Buffer的组成部分。 该三角形是彩色几何图形的一个范例,并且相应的颜色是基于顶点(per-vertex)定义的。在本章节中,你将充分了解一种不同的渲染几何图形的方式,它使用一种被称为纹理映射(texture mapping)的通用技术。 纹理映射(Texture mapping)是指将图像(纹理)应用于几何图形的过程。 你可以将这种纹理图像(texture image)想象成一张图文并茂的纸,有点像墙纸。 定义一个三角形(或者,范围更广一些,一个3D对象),并将这种图文并茂的纸覆盖在该对象的表面。通过使用这样的策略,你可以渲染一个3D对象,就好像它真地包含该图文并茂纹理的所有微小细节。 实际上,该纹理细节不是几何图形的一部分。 这一视觉复杂性仅仅是已应用的纹理图像(texture image)的图文并茂细节。当你应用纹理映射(texture mapping)时,你需要指定纹理元素需要放置在几何图形顶端的确切位置。 基本上,当你使用纹理图像(texture image)对几何图形进行覆盖时,你需要创建一个精确的映射,用来定义每一个纹理图像的像素应该落在3D几何图形上的准确位置。使用UV坐标将一种纹理在3D几何图形上进行合理排列的方式涉及到基于顶点(per-vertex)指定相应的映射:对于每一个顶点,你需要指定一对2D坐标,用(U,V)表示,该坐标将定义与该特定顶点相对应的纹理图像(texture image)的点。 因此,这些UV坐标在Vertex Buffer中将被指定为Vertex Attributes,并且Vertex Shader将以输入流的方式接收它们。然后,由于Vertex Shader通常都是伴随Vertex Attributes出现的,Vertex Shader将UV坐标作为输出沿着渲染管线(rendering pipeline)传递出来,并且Rasterizer对它们进行内插操作(如需了解更多细节,请参见本系列前面的一篇文章,其标题为Vertex和Fragment Shaders(Vertex and Fragment Shaders)。 在这种方式下,Fragment Shader能够为每一个三角形Fragment接收合适的UV坐标值(对于每一个将被渲染的像素)。 因此,每一个已渲染的三角形的每一像素都被映射到一个特定的纹理像素(也称为纹理元素( texture element),或者纹(texel))。换句话说,通过指定UV坐标,你已经创建一个3D几何图形和纹理图像之间的映射。 这就是术语纹理映射(texture mapping)背后的概念。


    在Stage3D API中使用Texture类
    Stage3D API中的Texture类包括应用纹理的支持功能。纹理图像(texture image)首先必须上传至GPU内存以便在渲染过程中使用。 你可以利用下面代码,使用Texture类将纹理图像(texture image)上传至GPU:
    1. protected var texture:Texture;
    2. ...

    3. protected function initMolehill(e:Event):void
    4. {
    5.         ...

    6.         var bitmap:Bitmap = new TextureBitmap();
    7.         texture = context3D.createTexture(bitmap.bitmapData.width, bitmap.bitmapData.height, Context3DTextureFormat.BGRA, false);

    8.         texture.uploadFromBitmapData(bitmap.bitmapData);
    9.         ...
    10. }
    复制代码
    正如上面讨论的那样,Vertex Shader将UV纹理坐标作为一个Vertex Attribute接收下来,并且沿着渲染管线(rendering pipeline)将它们作为输出进行传递,这样,能够正确地对它们进行内插操作并且将它们传递至Fragment Shader。 除了Attribute Register 1包含UV坐标,而不是颜色值之外,Vertex Shader与上面描述的彩色三角形范例项目非常相似。
    1. m44 op, va0, vc0
    2. mov v0, va1
    复制代码
    Fragment Shader接收内插的UV坐标并使用它们以及通过一个Texture Sampler对纹理进行取样。让我们假定纹理与ActionScript以及ActionScript 0都是相关联的。 在这种情况下,Fragment Shader将是:
    1. tex ft1, v0, fs0 <2d>
    2. mov oc, ft1
    复制代码
    Fragment Shader的第一行使用Texture Sampler 0和存储于可变寄存器0(varying register 0)中的UV坐标对纹理进行取样,并且将相应的结果复制到Temporary Register 1中。 第二行仅仅将Temporary Register 1(已取样的纹理)中的内容复制到输出。修改Hello Triangle Colored应用程序以便应用纹理映射(texture map)在本章节中,你需要修改之前的Hello Triangle应用程序,以便它能够使用纹理映射(texture mapping)。在应用程序中需要更新的第一件事情是为纹理添加一个图像。 使用下面的代码来到导入一个外部的纹理图像(texture image):
    1. [Embed( source = "RockSmooth.jpg" )]
    2. protected const TextureBitmap:Class;
    3. ...
    复制代码
    你还需要更改一下Vertex Buffer的定义。 你需要提供UV坐标,而不是传递颜色Vertex Attribute:
    1. protected function initMolehill(e:Event):void
    2. {
    3.         ...
    4.         var vertices:Vector.<Number> = Vector.<Number>([
    5.                 -0.3,-0.3,0, 1, 0, // x, y, z, u, v
    6.                 -0.3, 0.3, 0, 0, 1,
    7.                 0.3, 0.3, 0, 1, 1]);
    8.         
    9.         // Create VertexBuffer3D. 3 vertices, of 5 Numbers each
    10.         vertexbuffer = context3D.createVertexBuffer(3, 5);
    11.         // Upload VertexBuffer3D to GPU. Offset 0, 3 vertices
    12.         vertexbuffer.uploadFromVector(vertices, 0, 3);
    13.         ...
    14. }        
    复制代码
    注意,UV坐标是在0和1之间定义的,因此,(U,V) = (0,0) 表示纹理图像(texture image)的左下角,而(U,V) = (1,1)表示右上角。然后,渲染循环(rendering loop)启用Texture对象,并将它与 Texture Sampler 0关联,而Fragment Shader能够使用Texture Sampler 0:
    1. protected function onRender(e:Event):void
    2. {
    3.         ...                                
    4.         // assign texture to texture sampler 0
    5.         context3D.setTextureAt(0, texture);        
    6.         ...               
    7. }
    复制代码


    当完成这些变更之后,再一次运行该应用程序以便看一看在你创建的Stage3D应用程序中显示的带有纹理的三角形(参见图3)。








    图3. 已完成的Hello Triangle Textured应用程序

    下面是用于创建Hello Textured Triangle应用程序的完整代码范例:
    1. package
    2. {
    3.         import com.adobe.utils.AGALMiniAssembler;
    4.         
    5.         import flash.display.Bitmap;
    6.         import flash.display.Sprite;
    7.         import flash.display3D.Context3D;
    8.         import flash.display3D.Context3DProgramType;
    9.         import flash.display3D.Context3DTextureFormat;
    10.         import flash.display3D.Context3DVertexBufferFormat;
    11.         import flash.display3D.IndexBuffer3D;
    12.         import flash.display3D.Program3D;
    13.         import flash.display3D.VertexBuffer3D;
    14.         import flash.display3D.textures.Texture;
    15.         import flash.events.Event;
    16.         import flash.geom.Matrix3D;
    17.         import flash.geom.Rectangle;
    18.         import flash.geom.Vector3D;
    19.         import flash.utils.getTimer;
    20.         
    21.         [SWF(width="800", height="600", frameRate="60", backgroundColor="#FFFFFF")]
    22.         public class HelloTriangleTextured extends Sprite
    23.         {
    24.                 [Embed( source = "RockSmooth.jpg" )]
    25.                 protected const TextureBitmap:Class;

    26.                 protected var texture:Texture;
    27.                
    28.                 protected var context3D:Context3D;
    29.                 protected var program:Program3D;
    30.                 protected var vertexbuffer:VertexBuffer3D;
    31.                 protected var indexbuffer:IndexBuffer3D;
    32.                
    33.                 public function HelloTriangleTextured()
    34.                 {                        
    35.                         stage.stage3Ds[0].addEventListener( Event.CONTEXT3D_CREATE, initMolehill );
    36.                         stage.stage3Ds[0].requestContext3D();
    37.                         
    38.                         addEventListener(Event.ENTER_FRAME, onRender);
    39.                 }
    40.                
    41.                 protected function initMolehill(e:Event):void
    42.                 {
    43.                         context3D = stage.stage3Ds[0].context3D;                        
    44.                         context3D.configureBackBuffer(800, 600, 1, true);
    45.                         
    46.                         var vertices:Vector.<Number> = Vector.<Number>([
    47.                                 -0.3,-0.3,0, 1, 0, // x, y, z, u, v
    48.                                 -0.3, 0.3, 0, 0, 1,
    49.                                 0.3, 0.3, 0, 1, 1]);
    50.                         
    51.                         // Create VertexBuffer3D. 3 vertices, of 5 Numbers each
    52.                         vertexbuffer = context3D.createVertexBuffer(3, 5);
    53.                         // Upload VertexBuffer3D to GPU. Offset 0, 3 vertices
    54.                         vertexbuffer.uploadFromVector(vertices, 0, 3);                                
    55.                         
    56.                         var indices:Vector.<uint> = Vector.<uint>([0, 1, 2]);
    57.                         
    58.                         // Create IndexBuffer3D. Total of 3 indices. 1 triangle of 3 vertices
    59.                         indexbuffer = context3D.createIndexBuffer(3);                        
    60.                         // Upload IndexBuffer3D to GPU. Offset 0, count 3
    61.                         indexbuffer.uploadFromVector (indices, 0, 3);                        
    62.                         
    63.                         var bitmap:Bitmap = new TextureBitmap();
    64.                         texture = context3D.createTexture(bitmap.bitmapData.width, bitmap.bitmapData.height, Context3DTextureFormat.BGRA, false);
    65.                         texture.uploadFromBitmapData(bitmap.bitmapData);                        
    66.                         
    67.                         var vertexShaderAssembler : AGALMiniAssembler = new AGALMiniAssembler();
    68.                         vertexShaderAssembler.assemble( Context3DProgramType.VERTEX,
    69.                                 "m44 op, va0, vc0\n" + // pos to clipspace
    70.                                 "mov v0, va1" // copy UV
    71.                         );                        
    72.                         
    73.                         var fragmentShaderAssembler : AGALMiniAssembler= new AGALMiniAssembler();
    74.                         fragmentShaderAssembler.assemble( Context3DProgramType.FRAGMENT,
    75.                                 "tex ft1, v0, fs0 <2d>\n" +
    76.                                 "mov oc, ft1"
    77.                         );
    78.                         
    79.                         program = context3D.createProgram();
    80.                         program.upload( vertexShaderAssembler.agalcode, fragmentShaderAssembler.agalcode);
    81.                 }        
    82.                
    83.                 protected function onRender(e:Event):void
    84.                 {
    85.                         if ( !context3D )
    86.                                 return;
    87.                         
    88.                         context3D.clear ( 1, 1, 1, 1 );
    89.                         
    90.                         // vertex position to attribute register 0
    91.                         context3D.setVertexBufferAt (0, vertexbuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
    92.                         // UV to attribute register 1
    93.                         context3D.setVertexBufferAt(1, vertexbuffer, 3, Context3DVertexBufferFormat.FLOAT_2);
    94.                         // assign texture to texture sampler 0
    95.                         context3D.setTextureAt(0, texture);                                
    96.                         // assign shader program
    97.                         context3D.setProgram(program);
    98.                         
    99.                         var m:Matrix3D = new Matrix3D();
    100.                         m.appendRotation(getTimer()/40, Vector3D.Z_AXIS);
    101.                         context3D.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0, m, true);
    102.                         
    103.                         context3D.drawTriangles(indexbuffer);
    104.                         
    105.                         context3D.present();                        
    106.                 }
    107.         }
    108. }
    复制代码

    下一步阅读方向
    在本文中,你使用了在Stage3D系列的前面文章中学到的概念,最终掌握了相应的核心过程并且创建了两个能够完全正常运行的基于Stage3D的ActionScript应用程序。 即使相应的范例应用程序只创建一个由单一三角形组成的简单场景,但是所有的使用Stage3D的概念都已包含在内。 从今以后,当你构建Stage3D应用程序时,你只会感觉到一切变得更深入和更有趣。在Stage3D 系列的下一篇文章中,你将了解一个3D渲染的基本话题:使用透视图(working with perspective)。与此同时,一定要访问下面的Developer Centers 以便找到更多教程和范例项目:








    More Like This




    本文来自:http://www.adobe.com/cn/devnet/f ... hello-triangle.html






















    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2019-9-12 17:23
  • 签到天数: 383 天

    [LV.9]以坛为家II

    21

    主题

    57

    帖子

    4万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    1066
    贡献
    0
    金币
    6358
    钢镚
    21
    发表于 2015-3-11 12:26:11 | 显示全部楼层
    推推
    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    
    关闭

    站长推荐上一条 /4 下一条

    QQ|手机版|Archiver|网站地图|小黑屋|守望者 ( 京ICP备14061876号

    GMT+8, 2024-3-29 21:26 , Processed in 0.337692 second(s), 35 queries .

    守望者AIR

    守望者AIR技术交流社区

    本站成立于 2014年12月31日

    快速回复 返回顶部 返回列表