注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

FlyingWind

 
 
 

日志

 
 
 
 

3D Flash 简单例子  

2009-06-01 20:07:07|  分类: Flash制作教程 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

ND3D_V0.2(2008-08-13) 下载

(说明:ND3D不同版本之间并不兼容,使用此例子时请注意版本号和发布日期)

在屏幕左边移动鼠标,图片向左移动

在屏幕右边移动鼠标,图片向右移动

滚动鼠标滚轮,放大或缩小(由于鼠标滚动事件被浏览器占用可能无法看到效果,请下载欣赏)

(如果下面的flash无法显示,说明链接失效,如果你对此flash有兴趣,请下载源代码欣赏:包括flash)

由于网格划分太大效果不是很好

 

此例子基于3D flash引擎 ND3D V0.2   开发,flashdevelop开发平台。

下载源代码

Album3D类 

package com.flyingwind.album
{
 
 import flash.display.Sprite;
 import flash.display.BitmapData
 import flash.geom.ColorTransform;
 import flash.geom.Matrix;
 import flash.events.Event;
 import flash.events.MouseEvent;
 import flash.display.GradientType;
 

 import de.nulldesign.nd3d.objects.PointCamera;
 import de.nulldesign.nd3d.renderer.Renderer; 
 import de.nulldesign.nd3d.objects.Mesh;
 import de.nulldesign.nd3d.objects.SimpleCube;
 import de.nulldesign.nd3d.material.Material;
 
 import org.flashdevelop.utils.FlashConnect ;

 /**
  * ...
  * @author FlyingWind
  * @link http://colorfuldiary.blog.163.com
  */
 public class Album3D extends Sprite
 {
  private var cam:PointCamera;
  private var renderer:Renderer;
  private var renderList:Array;
  private var bitmaps:Array;
  
  [Embed("../../../../assets/00.jpg")]
  private var MyTexture0:Class; 
  [Embed("../../../../assets/01.jpg")]
  private var MyTexture1:Class; 
  [Embed("../../../../assets/02.jpg")]
  private var MyTexture2:Class; 
  [Embed("../../../../assets/03.jpg")]
  private var MyTexture3:Class; 
  [Embed("../../../../assets/04.jpg")]
  private var MyTexture4:Class; 
  [Embed("../../../../assets/bg.jpg")]
  private var BG:Class; 
  
  public function Album3D():void {
   var m:Matrix = new Matrix();
   m.rotate(Math.PI / 2);
   graphics.beginGradientFill(GradientType.LINEAR, [0x000000, 0xFFFFFF], [100, 100], [125, 255], m);
   graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
   graphics.endFill();
   
   renderer = new Renderer(Sprite(addChild(new Sprite())));
   renderer.additiveMode = false;
   
   cam = new PointCamera(800, 600);
   //cam.angleX = 5;
   //cam.angleY = 30;
   
   cam.zOffset =10;
 
   renderList = [];
   bitmaps = [];
   for (var i:int = 0; i < 2; i++) {
   var texture1:BitmapData = new MyTexture0().bitmapData;
   var texture2:BitmapData = new MyTexture1().bitmapData;
   var texture3:BitmapData = new MyTexture2().bitmapData;
   var texture4:BitmapData = new MyTexture3().bitmapData;
   var texture5:BitmapData = new MyTexture4().bitmapData;
   bitmaps.push(texture1,texture2,texture3,texture4,texture5);
   }
   var bg:BitmapData = new BG().bitmapData;
   //texture1.colorTransform(texture1.rect, new ColorTransform(0.3, 0.5, 0.6, 1, 0, 0, 0, 0));
   
   //var af:Mesh = new SimpleCube(new Material(0xFF9900, 1, bitmaps[0], true, false, true),400);
   var af:Mesh = new AlbumFrame(bitmaps,500, 375, bg);
   
   renderList.push(af);
   stage.addEventListener(MouseEvent.MOUSE_MOVE, onRenderScene);
   stage.addEventListener(MouseEvent.MOUSE_WHEEL, wheelHandler);
   cam.zOffset = -AlbumFrame(renderList[0]).getRadial() * 2 / 3;
   renderer.render(renderList, cam);
  }
  
  private function onRenderScene(evt:MouseEvent):void
  {
   Mesh(renderList[0]).angleY += (mouseX - cam.vpX) * .0001;
     
   renderer.render(renderList, cam);
  }
  private function wheelHandler(event:MouseEvent):void {
   var a:Number = AlbumFrame(renderList[0]).getRadial();
   if (event.delta > 0) {//向前滚
    if (cam.zOffset > 0 ) {
     //cam.zOffset = 0;
    }else {
     cam.zOffset += 8;
    }    
   }else {//向后滚
    if (cam.zOffset < -a ) {
     //cam.zOffset = -a;
    }else {
     cam.zOffset -= 8;
    }    
   }
   renderer.render(renderList, cam);
  }

 }

 

 

AlbumFrame类

 

package com.flyingwind.album
{
 import flash.display.BitmapData
 import de.nulldesign.nd3d.material.Material;
 import de.nulldesign.nd3d.objects.Mesh;
 import de.nulldesign.nd3d.geom.Vertex;
 import de.nulldesign.nd3d.geom.UV;
 //import org.flashdevelop.utils.FlashConnect ;
 /**
  * ...
  * @author
  */
 public class AlbumFrame extends Mesh
 {
  /*每一张照片的角度*/
  private var angle:Number = 0;
  /*相片的数目*/
  private var size:int = 0;
  /*外切圆半径*/
  private var radial:Number = 0;
  
  /**
   *
   * @param materials: BitmapData 对象数组
   * @param width: 图片宽度
   * @param height: 图片高度
   */
  public function AlbumFrame(bitmaps:Array, width:int, height:int,bg:BitmapData)
  {
   this.size = bitmaps.length;
   this.angle = 2 * Math.PI / size;
   this.radial = (width / 2) / (Math.sin(angle / 2));
   //FlashConnect.trace("start");
   /*生成顶点*/
   var v:Array = [];   

   for (var i:int = 0; i < size; i++) {
    var v0:Vertex = new Vertex(radial * Math.cos(angle * i), -height / 2, radial * Math.sin(angle * i));
    var v1:Vertex = new Vertex(radial * Math.cos(angle * i), height / 2, radial * Math.sin(angle * i) );
    v.push(v0, v1);
    //FlashConnect.trace(v0.toString()+"  "+ v1.toString());

   }
   //FlashConnect.trace("V="+v.length);
   /*生成Material*/
   var materials:Array = [];
   for (var k:int = 0; k < size; k++) {
    var m:Material = new Material(0x000000, 1, BitmapData(bitmaps[k]), true, true, true, true);
    materials.push(m);
   }
   //FlashConnect.trace("M="+materials.length);
   for (var j:int = 0; j < size-1; j++) {
    var s:int = j * 2;
    addFace(v[s+2], v[s+1], v[s], Material(materials[j]), [new UV(0, 0), new UV(1, 1), new UV(1, 0)]);
    addFace(v[s + 2], v[s + 3], v[s + 1], Material(materials[j]), [new UV(0, 0), new UV(0, 1), new UV(1, 1)]);
   }
   s = (size-1) * 2;
   addFace(v[0], v[s+1], v[s], Material(materials[j]), [new UV(0, 0), new UV(1, 1), new UV(1, 0)]);
   addFace(v[0], v[1], v[s + 1], Material(materials[j]), [new UV(0, 0), new UV(0, 1), new UV(1, 1)]);
   
   var bgm:Material = new Material(0x000000, 1, bg, true, true, true, true);
   var center:Vertex = new Vertex(0, height / 2, 0);
   for (var z:int = 0; z < size-1; z++) {
    addFace(center, v[z*2+1], v[(z+1)*2+1], bgm, [new UV(0, 0), new UV(1, 1), new UV(1, 0)]);
   }
   addFace(center, v[(size-1)*2+1], v[1], bgm, [new UV(0, 0), new UV(1, 1), new UV(1, 0)]);
  }
  
  public function getMaxZoffset():Number {
   return radial * Math.cos(angle / 2);
  }
  public function getRadial():Number {
   return radial;
  }
  
 }
 
}

 

                                                                                      FlyingWind    2009/06/01

  评论这张
 
阅读(931)| 评论(9)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018