- 最后登录
- 2021-9-15
- 注册时间
- 2011-7-15
- 阅读权限
- 100
- 积分
- 50625
- 纳金币
- 53202
- 精华
- 32
|
pstatus"> 本帖最后由 驰骋的风 于 2013-7-2 16:06 编辑
本测试基于A3d引擎(由于使用的是最新的Actionscript API,所以需要flash player11,那个链接里也有下载)下面是部分源码:
package- {
- import alternativa.engine3d.controllers.SimpleObjectController;
- import alternativa.engine3d.core.VertexAttributes;
- import alternativa.engine3d.lights.DirectionalLight;
- import alternativa.engine3d.materials.FillMaterial;
- import alternativa.engine3d.materials.Material;
- import alternativa.engine3d.materials.TextureMaterial;
- import alternativa.engine3d.materials.VertexLightTextureMaterial;
- import alternativa.engine3d.objects.Mesh;
- import alternativa.engine3d.resources.BitmapTextureResource;
- import alternativa.engine3d.resources.Geometry;
-
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.sensors.Geolocation;
-
- [SWF(frameRate="60")]
- public class WaveTest extends AlternativaTemplate
- {
- [Embed(source="texture.jpg")]
- static private const EmbedTexture:Class;
-
- private var mesh:Mesh;
- private var col:Number=150;
- private var row:Number=150;
- private var ds:Number=1000/col;
-
- private var directionalLight:DirectionalLight;
- private var directionalLight1:DirectionalLight;
- private var material:VertexLightTextureMaterial;
-
- private var vertices:Vector.;
- private var indices:Vector.;
- private var uvt:Vector.;
-
- private var controller:SimpleObjectController;
- private var t:Number=0;
- public function WaveTest()
- {
- initLight();
-
- //构建顶点属性
-
- makeGeo();
-
- camera.y=-300;
- camera.z=550;
- camera.x=ds*row*0.5;
- camera.rotationX=deg2rad(-130);
-
- rootContainer.addChild(mesh);
-
- /* for(var i:int=0;i {
- var a:Mesh=mesh.clone() as Mesh;
- a.z+=2*(i+1);
- rootContainer.addChild(a);
- }
-
- var b:Mesh=mesh.clone() as Mesh;
- b.z+=200;
- rootContainer.addChild(b);
- */ controller=new SimpleObjectController(stage,mesh,200);
-
- }
- //*/
-
- override protected function onEnterFrame(e:Event):void
- {
- for(var i:int=0;i {
- for(var j:int=0;j {
- // vertices[(i*col+j)*3+2]=25*(Math.sin((i-j+t)/(col+row)*Math.PI*7));
- 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));
- }
- }
- t++;
- // mesh.geometry.updateVertexBufferInContextFromVector(1,vertices,0,col*row);
- var vertex:Vector.=new Vector.();
- vertex.length=mesh.geometry.numVertices*8;
- if (1){ //upload way 1
- mesh.geometry.setAttributeValues(VertexAttributes.POSITION, vertices);
- mesh.geometry.upload(stage3D.context3D);
- } else { //upload way 2
- for (j = 0; j < mesh.geometry.numVertices; j++){
- vertex[uint(j * 8)] = vertices[uint(j * 3)];
- vertex[uint(j * 8 + 1)] = vertices[uint(j * 3 + 1)];
- vertex[uint(j * 8 + 2)] = vertices[uint(j * 3 + 2)];
- vertex[uint(j * 8)+3]=uvt[j*2];
- vertex[uint(j * 8)+4]=uvt[j*2+1];
- }
- mesh.geometry.updateVertexBufferInContextFromVector(0, vertex, 0, mesh.geometry.numVertices);
- }
-
- controller.update();
- }
- private function makeGeo():void
- {
- var attributes:Array = new Array();
- attributes[0] = VertexAttributes.POSITION;
- attributes[1] = VertexAttributes.POSITION;
- attributes[2] = VertexAttributes.POSITION;
- attributes[3] = VertexAttributes.TEXCOORDS[0];
- attributes[4] = VertexAttributes.TEXCOORDS[0];
- attributes[5] = VertexAttributes.NORMAL;
- attributes[6] = VertexAttributes.NORMAL;
- attributes[7] = VertexAttributes.NORMAL;
-
- var geometry:Geometry = new Geometry();
- geometry.addVertexStream(attributes);//添加顶点属性到几何体
- geometry.numVertices = col*row;//顶点数量
-
- preparePositionAndUV(geometry);
- prepareIndices(geometry);
-
-
- // geometry.setAttributeValues(VertexAttributes.NORMAL,Vector.(normal));
-
- mesh = new Mesh();//创建一个网格
- mesh.geometry = geometry;//网格几何体赋值
-
- var texture:BitmapTextureResource = new BitmapTextureResource(new EmbedTexture().bitmapData);
- var material:Material = new VertexLightTextureMaterial(texture);
- var m:TextureMaterial=new TextureMaterial(texture);
- // var m:FillMaterial = new FillMaterial(0xFF0000);//搞个材质填充
- mesh.addSurface(m, 0,2*(col-1)*(row-1));//创建Surface
- mesh.calculateBoundBox();//计算(必须)
-
- }
-
- private function prepareIndices(geometry:Geometry):void
- {
-
- indices=new Vector.();
- for(var i:int=0;i {
- for(var j:int=0;j {
- 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);
- }
- }
- geometry.indices =indices;
- }
- private function preparePositionAndUV(geometry:Geometry):void
- {
- vertices =new Vector.();
- uvt=new Vector.();
-
- for(var i:int=0;i {
- for(var j:int =0;j {
- vertices.push(i*ds,j*ds,0);
- uvt.push(0.08*row*i/(row-1),0.08*col*(1-j/(col-1)));
- }
- }
-
-
- geometry.setAttributeValues(VertexAttributes.POSITION,vertices);
- //指定几何体由哪些顶点构成
- geometry.setAttributeValues(VertexAttributes.TEXCOORDS[0], uvt);//为顶点分配uv贴图坐标
-
-
- }
-
-
- private function initLight():void
- {
- //主光源,大概在左上角
- directionalLight = new DirectionalLight(0xffffff);
- directionalLight.x = 0;
- directionalLight.y = -100;
- directionalLight.z = 50;
- directionalLight.rotationX = -90*Math.PI/180;
- // directionalLight.rotationZ = -70*Math.PI/180;
- rootContainer.addChild(directionalLight);
-
- //辅光源,大概在右下角,如果没有这个辅光的话,没有被主光源照到的
- //的位置全黑,效果很不好
- directionalLight1 = new DirectionalLight(0x4B4B4B);
- directionalLight1.x = 500;
- directionalLight1.y = -300;
- directionalLight1.z = -200;
- directionalLight1.rotationX = -45 * Math.PI / 180;
- directionalLight1.rotationZ = 45 * Math.PI / 180;
- rootContainer.addChild(directionalLight1);
- }
-
- }
- }
- 下面是基类AlternativaTemplate:
- package
- {
- import alternativa.engine3d.core.Camera3D;
- import alternativa.engine3d.core.Object3D;
- import alternativa.engine3d.core.Resource;
- import alternativa.engine3d.core.View;
- import alternativa.engine3d.materials.FillMaterial;
- import alternativa.engine3d.objects.Sprite3D;
- import alternativa.engine3d.primitives.Box;
-
- import flash.display.Sprite;
- import flash.display.Stage3D;
- import flash.display.StageAlign;
- import flash.display.StageScaleMode;
- import flash.events.Event;
-
- public class AlternativaTemplate extends Sprite
- {
- protected var rootContainer:Object3D = new Object3D();
-
- protected var camera:Camera3D;
- protected var stage3D:Stage3D;
- public function AlternativaTemplate()
- {
- stage.align = StageAlign.TOP_LEFT;
- stage.scaleMode = StageScaleMode.NO_SCALE;
-
-
- // CAMERA
- camera = new Camera3D(0.1, 10000);
- camera.view = new View(stage.stageWidth, stage.stageHeight);
- addChild(camera.view);
- addChild(camera.diagram);
- rootContainer.addChild(camera);
-
-
-
-
- //*/
- // INIT Stage3D
- stage3D = stage.stage3Ds[0];
- stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreate);
- stage3D.requestContext3D();
-
-
- }
-
- private function enterFrameHandler(event:Event):void
- {
-
- onEnterFrame(event);
- camera.render(stage3D);
- }
-
- protected function onEnterFrame(e:Event):void
- {
- // 请在子类中覆盖;
- }
- protected function onContextCreate(event:Event):void
- {
- for each(var resource:Resource in rootContainer.getResources(***e)){
- resource.upload(stage3D.context3D);
-
- }
-
- stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
- stage.addEventListener(Event.RESIZE,onResize);
-
-
-
- }
- //使视口适应窗口的变化。。。
- private function onResize(event:Event):void
- {
- camera.view.width=stage.stageWidth;
- camera.view.height=stage.stageHeight;
- }
-
- //两个实用的弧度和角度转换函数。。。
- protected function rad2deg(radnum:Number):Number{
- return radnum*180/Math.PI;
- }
- protected function deg2rad(degnum:Number):Number
- {
- return degnum*Math.PI/180;
- }
- }
- }
复制代码 分享到这边,更多尽在web3D纳金网http://www.narkii.com/ |
|