查看: 2465|回复: 3
打印 上一主题 下一主题

【Flash3D测试】4万个三角形作波浪运动

[复制链接]

3795

主题

2

听众

5万

积分

版主

Rank: 7Rank: 7Rank: 7

纳金币
53202
精华
32

活跃会员 优秀版主 推广达人 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2012-10-19 10:24:29 |只看该作者 |倒序浏览
pstatus"> 本帖最后由 驰骋的风 于 2013-7-2 16:06 编辑




本测试基于A3d引擎(由于使用的是最新的Actionscript API,所以需要flash player11,那个链接里也有下载)下面是部分源码:



package
  1. {
  2.         import alternativa.engine3d.controllers.SimpleObjectController;
  3.         import alternativa.engine3d.core.VertexAttributes;
  4.         import alternativa.engine3d.lights.DirectionalLight;
  5.         import alternativa.engine3d.materials.FillMaterial;
  6.         import alternativa.engine3d.materials.Material;
  7.         import alternativa.engine3d.materials.TextureMaterial;
  8.         import alternativa.engine3d.materials.VertexLightTextureMaterial;
  9.         import alternativa.engine3d.objects.Mesh;
  10.         import alternativa.engine3d.resources.BitmapTextureResource;
  11.         import alternativa.engine3d.resources.Geometry;
  12.                
  13.         import flash.display.Sprite;
  14.         import flash.events.Event;
  15.         import flash.sensors.Geolocation;
  16.                
  17.         [SWF(frameRate="60")]
  18.         public class WaveTest extends AlternativaTemplate
  19.         {
  20.                 [Embed(source="texture.jpg")]
  21.                 static private const EmbedTexture:Class;
  22.                         
  23.                 private var mesh:Mesh;
  24.                 private var col:Number=150;
  25.                 private var row:Number=150;
  26.                 private var ds:Number=1000/col;
  27.                         
  28.                 private var directionalLight:DirectionalLight;
  29.                 private var directionalLight1:DirectionalLight;
  30.                 private var material:VertexLightTextureMaterial;
  31.                         
  32.                 private var vertices:Vector.;
  33.                 private var indices:Vector.;
  34.                 private var uvt:Vector.;
  35.                         
  36.                 private var controller:SimpleObjectController;
  37.                 private var t:Number=0;
  38.                 public function WaveTest()
  39.                 {
  40.                         initLight();
  41.                                 
  42.                         //构建顶点属性
  43.                                 
  44.                         makeGeo();         
  45.                                 
  46.                         camera.y=-300;
  47.                         camera.z=550;
  48.                         camera.x=ds*row*0.5;
  49.                         camera.rotationX=deg2rad(-130);
  50.                                 
  51.                         rootContainer.addChild(mesh);      
  52.                                 
  53.         /*                for(var i:int=0;i                        {
  54.                                 var a:Mesh=mesh.clone() as Mesh;
  55.                                 a.z+=2*(i+1);
  56.                                 rootContainer.addChild(a);
  57.                         }
  58.                                        
  59.                         var b:Mesh=mesh.clone() as Mesh;
  60.                         b.z+=200;
  61.                         rootContainer.addChild(b);
  62.         */                controller=new SimpleObjectController(stage,mesh,200);
  63.                                 
  64.                 }
  65.                 //*/
  66.                         
  67.                 override protected function onEnterFrame(e:Event):void
  68.                 {
  69.                         for(var i:int=0;i                        {
  70.                                 for(var j:int=0;j                                {
  71.                 //                        vertices[(i*col+j)*3+2]=25*(Math.sin((i-j+t)/(col+row)*Math.PI*7));
  72.                                         vertices[(i*col+j)*3+2]=25*(Math.sin((i+0.5*j+t)/(col+row)*Math.PI*8)+0.21*Math.sin((i-j+t)/(col+row)*Math.PI*15));
  73.                                 }
  74.                         }
  75.                         t++;
  76.         //                mesh.geometry.updateVertexBufferInContextFromVector(1,vertices,0,col*row);
  77.                 var vertex:Vector.=new Vector.();
  78.                 vertex.length=mesh.geometry.numVertices*8;
  79.                         if (1){ //upload way 1  
  80.                                 mesh.geometry.setAttributeValues(VertexAttributes.POSITION, vertices);  
  81.                                 mesh.geometry.upload(stage3D.context3D);  
  82.                         } else { //upload way 2  
  83.                                 for (j = 0; j < mesh.geometry.numVertices; j++){  
  84.                                         vertex[uint(j * 8)] = vertices[uint(j * 3)];  
  85.                                         vertex[uint(j * 8 + 1)] = vertices[uint(j * 3 + 1)];  
  86.                                         vertex[uint(j * 8 + 2)] = vertices[uint(j * 3 + 2)];  
  87.                                         vertex[uint(j * 8)+3]=uvt[j*2];
  88.                                         vertex[uint(j * 8)+4]=uvt[j*2+1];
  89.                                 }  
  90.                                 mesh.geometry.updateVertexBufferInContextFromVector(0, vertex, 0, mesh.geometry.numVertices);  
  91.                         }
  92.                                 
  93.                         controller.update();
  94.                 }
  95.                 private function makeGeo():void
  96.                 {
  97.                         var  attributes:Array = new Array();
  98.                         attributes[0] = VertexAttributes.POSITION;
  99.                         attributes[1] = VertexAttributes.POSITION;
  100.                         attributes[2] = VertexAttributes.POSITION;
  101.                         attributes[3] = VertexAttributes.TEXCOORDS[0];
  102.                         attributes[4] = VertexAttributes.TEXCOORDS[0];
  103.                         attributes[5] = VertexAttributes.NORMAL;
  104.                         attributes[6] = VertexAttributes.NORMAL;
  105.                         attributes[7] = VertexAttributes.NORMAL;
  106.                                 
  107.                         var geometry:Geometry = new Geometry();
  108.                         geometry.addVertexStream(attributes);//添加顶点属性到几何体
  109.                         geometry.numVertices = col*row;//顶点数量
  110.                                 
  111.                         preparePositionAndUV(geometry);
  112.                         prepareIndices(geometry);
  113.                                 
  114.                                 
  115.         //                geometry.setAttributeValues(VertexAttributes.NORMAL,Vector.(normal));               
  116.                                 
  117.                         mesh = new Mesh();//创建一个网格
  118.                         mesh.geometry = geometry;//网格几何体赋值
  119.                                 
  120.                         var texture:BitmapTextureResource = new BitmapTextureResource(new EmbedTexture().bitmapData);
  121.                         var material:Material = new VertexLightTextureMaterial(texture);
  122.                         var m:TextureMaterial=new TextureMaterial(texture);
  123.                 //        var m:FillMaterial = new FillMaterial(0xFF0000);//搞个材质填充                        
  124.                         mesh.addSurface(m, 0,2*(col-1)*(row-1));//创建Surface
  125.                         mesh.calculateBoundBox();//计算(必须)
  126.                                 
  127.                 }
  128.                         
  129.                 private function prepareIndices(geometry:Geometry):void
  130.                 {
  131.                                 
  132.                         indices=new Vector.();
  133.                         for(var i:int=0;i                        {
  134.                                 for(var j:int=0;j                                {
  135.                                         indices.push(i*col+j,(i+1)*col+j,i*col+j+1,i*col+j+1,(i+1)*col+j,(i+1)*col+j+1);
  136.                                 }
  137.                         }
  138.                         geometry.indices =indices;  
  139.                 }
  140.                 private function preparePositionAndUV(geometry:Geometry):void
  141.                 {
  142.                         vertices =new Vector.();
  143.                         uvt=new Vector.();
  144.                                 
  145.                         for(var i:int=0;i                        {
  146.                                 for(var j:int =0;j                                {
  147.                                         vertices.push(i*ds,j*ds,0);
  148.                                         uvt.push(0.08*row*i/(row-1),0.08*col*(1-j/(col-1)));
  149.                                 }
  150.                         }
  151.                                 
  152.                                 
  153.                         geometry.setAttributeValues(VertexAttributes.POSITION,vertices);
  154.                         //指定几何体由哪些顶点构成
  155.                         geometry.setAttributeValues(VertexAttributes.TEXCOORDS[0], uvt);//为顶点分配uv贴图坐标
  156.                                 
  157.                                 
  158.                 }
  159.                         
  160.                         
  161.                 private function initLight():void
  162.                 {
  163.                         //主光源,大概在左上角
  164.                         directionalLight = new DirectionalLight(0xffffff);
  165.                         directionalLight.x = 0;
  166.                         directionalLight.y = -100;
  167.                         directionalLight.z = 50;
  168.                         directionalLight.rotationX = -90*Math.PI/180;
  169.                         //        directionalLight.rotationZ = -70*Math.PI/180;
  170.                         rootContainer.addChild(directionalLight);
  171.                                 
  172.                         //辅光源,大概在右下角,如果没有这个辅光的话,没有被主光源照到的
  173.                         //的位置全黑,效果很不好
  174.                         directionalLight1 = new DirectionalLight(0x4B4B4B);
  175.                         directionalLight1.x = 500;
  176.                         directionalLight1.y = -300;
  177.                         directionalLight1.z = -200;
  178.                         directionalLight1.rotationX = -45 * Math.PI / 180;
  179.                         directionalLight1.rotationZ = 45 * Math.PI / 180;
  180.                         rootContainer.addChild(directionalLight1);
  181.                 }
  182.                         
  183.         }
  184. }


  185. 下面是基类AlternativaTemplate:

  186. package
  187. {
  188.         import alternativa.engine3d.core.Camera3D;
  189.         import alternativa.engine3d.core.Object3D;
  190.         import alternativa.engine3d.core.Resource;
  191.         import alternativa.engine3d.core.View;
  192.         import alternativa.engine3d.materials.FillMaterial;
  193.         import alternativa.engine3d.objects.Sprite3D;
  194.         import alternativa.engine3d.primitives.Box;
  195.             
  196.         import flash.display.Sprite;
  197.         import flash.display.Stage3D;
  198.         import flash.display.StageAlign;
  199.         import flash.display.StageScaleMode;
  200.         import flash.events.Event;
  201.             
  202.         public class AlternativaTemplate extends Sprite
  203.         {
  204.                 protected var rootContainer:Object3D = new Object3D();
  205.                     
  206.                 protected var camera:Camera3D;
  207.                 protected var stage3D:Stage3D;
  208.                 public function AlternativaTemplate()
  209.                 {
  210.                         stage.align = StageAlign.TOP_LEFT;
  211.                         stage.scaleMode = StageScaleMode.NO_SCALE;
  212.                            
  213.                            
  214.                         // CAMERA
  215.                         camera = new Camera3D(0.1, 10000);
  216.                         camera.view = new View(stage.stageWidth, stage.stageHeight);
  217.                         addChild(camera.view);
  218.                         addChild(camera.diagram);
  219.                         rootContainer.addChild(camera);
  220.                     
  221.                     
  222.                            
  223.    
  224.                         //*/
  225.                         // INIT Stage3D
  226.                         stage3D = stage.stage3Ds[0];
  227.                         stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
  228.                         stage3D.requestContext3D();
  229.                            
  230.                            
  231.                 }
  232.                     
  233.                 private function enterFrameHandler(event:Event):void
  234.                 {                                               
  235.                     
  236.                         onEnterFrame(event);
  237.                         camera.render(stage3D);
  238.                 }
  239.                     
  240.                 protected function onEnterFrame(e:Event):void
  241.                 {
  242.                         //        请在子类中覆盖;
  243.                 }
  244.                 protected function onContextCreate(event:Event):void
  245.                 {
  246.                         for each(var resource:Resource in rootContainer.getResources(***e)){
  247.                                 resource.upload(stage3D.context3D);
  248.                                     
  249.                         }
  250.                            
  251.                         stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  252.                         stage.addEventListener(Event.RESIZE,onResize);
  253.                            
  254.                            
  255.                            
  256.                 }
  257.                 //使视口适应窗口的变化。。。
  258.                 private function onResize(event:Event):void
  259.                 {
  260.                         camera.view.width=stage.stageWidth;
  261.                         camera.view.height=stage.stageHeight;
  262.                 }
  263.                     
  264.                 //两个实用的弧度和角度转换函数。。。
  265.                 protected function rad2deg(radnum:Number):Number{
  266.                         return radnum*180/Math.PI;
  267.                 }               
  268.                 protected function deg2rad(degnum:Number):Number
  269.                 {
  270.                         return degnum*Math.PI/180;
  271.                 }
  272.         }
  273. }
复制代码
分享到这边,更多尽在web3D纳金网http://www.narkii.com/
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

10

主题

3

听众

760

积分

初级设计师

Rank: 3Rank: 3

纳金币
3
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-10-19 12:20:01 |只看该作者
不错不错 虽然没研究过A3D
回复

使用道具 举报

0

主题

1

听众

2458

积分

中级设计师

Rank: 5Rank: 5

纳金币
0
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2014-2-22 18:16:53 |只看该作者
Thanks for sharing
回复

使用道具 举报

0

主题

2

听众

3238

积分

中级设计师

Rank: 5Rank: 5

纳金币
0
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2014-2-27 14:51:39 |只看该作者
感谢分享
回复

使用道具 举报

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

关闭

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

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-4 03:32 , Processed in 0.095357 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部