
项目源文件下载
2 | import away3d.animators.*; |
3 | import away3d.animators.data.*; |
4 | import away3d.animators.nodes.*; |
5 | import away3d.containers.*; |
6 | import away3d.controllers.*; |
7 | import away3d.core.base.*; |
9 | import away3d.entities.*; |
10 | import away3d.events.AssetEvent; |
11 | import away3d.library.assets.AssetType; |
12 | import away3d.lights.PointLight; |
13 | import away3d.loaders.Loader3D; |
14 | import away3d.loaders.misc.AssetLoaderContext; |
15 | import away3d.loaders.parsers.AWD2Parser; |
16 | import away3d.loaders.parsers.Parsers; |
17 | import away3d.materials.*; |
18 | import away3d.materials.lightpickers.StaticLightPicker; |
19 | import away3d.primitives.*; |
20 | import away3d.textures.BitmapTexture; |
21 | import away3d.tools.helpers.*; |
22 | import away3d.utils.*; |
24 | import flash.display.*; |
25 | import flash.events.*; |
27 | import flash.utils.Timer; |
29 | [SWF(backgroundColor= "#000000" , frameRate= "60" )] |
30 | public class Away3d_Particles4 extends Sprite{ |
31 | [Embed(source= "House/head.awd" ,mimeType= "application/octet-stream" )] |
32 | public static var HouseModel:Class; |
34 | [Embed(source= "House/head_diffuse.jpg" )] |
35 | public static var HouseTexture:Class; |
37 | private var view:View3D; |
38 | private var cameraController:HoverController; |
40 | private var data:Vector.<Vector3D>; |
41 | private const SIZE: int = 2 ; |
42 | private const TIME: int = 10000 ; |
43 | private var state: int ; |
44 | private var mesh:Mesh; |
46 | private var light:PointLight; |
47 | private var lightPicker:StaticLightPicker; |
48 | private var particleAnimator:ParticleAnimator |
50 | private var move: Boolean = false ; |
51 | private var lastPanAngle: Number ; |
52 | private var lastTiltAngle: Number ; |
53 | private var lastMouseX: Number ; |
54 | private var lastMouseY: Number ; |
55 | private var angle: Number = 0 ; |
57 | public function Away3d_Particles4(){ |
58 | stage.scaleMode = StageScaleMode.NO_SCALE; |
59 | stage.align = StageAlign.TOP_LEFT; |
64 | cameraController = new HoverController(view.camera, null , 45 , 20 , 1000 , 5 ); |
66 | addChild( new AwayStats(view)); |
71 | addEventListener(Event.ENTER_FRAME, onEnterFrame); |
72 | stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown); |
73 | stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp); |
74 | stage.addEventListener(Event.RESIZE, onResize); |
79 | private function loadModel(): void { |
80 | Parsers.enableAllBundled(); |
82 | var loader:Loader3D = new Loader3D(); |
83 | var assetLoaderContext:AssetLoaderContext = new AssetLoaderContext(); |
84 | assetLoaderContext.mapUrlToData( "nullg0.jpg" , new HouseTexture()); |
91 | loader.addEventListener(AssetEvent.ASSET_COMPLETE,onAssetComplete(name)); |
92 | loader.loadData( new HouseModel(),assetLoaderContext, null , new AWD2Parser()); |
96 | private function onAssetComplete(materialName: String ):Function{ |
97 | var func:Function = function (e:AssetEvent): void { |
98 | if (e.asset.assetType == AssetType.MESH){ |
99 | mesh = e.asset as Mesh; |
105 | var timer:Timer = new Timer(TIME); |
106 | timer.addEventListener(TimerEvent.TIMER,onTimer); |
110 | else if (e.asset.assetType == AssetType.MATERIAL){ |
111 | var _material:TextureMaterial = e.asset as TextureMaterial; |
112 | _material.texture = new BitmapTexture( new HouseTexture().bitmapData); |
113 | _material.lightPicker = lightPicker; |
114 | _material.gloss = 185 ; |
115 | _material.specular = 0.2 ; |
121 | private function onTimer(e:Event): void { |
124 | particleAnimator.playbackSpeed = 0 ; |
125 | particleAnimator.resetTime( 0 ); |
128 | particleAnimator.playbackSpeed = 1 ; |
131 | particleAnimator.playbackSpeed = - 1 ; |
138 | private function initTextData(): void { |
139 | data = new Vector.<Vector3D>; |
140 | var vertices:Vector.< Number > = mesh.geometry.subGeometries[ 0 ].vertexPositionData; |
141 | for ( var i: int = 0 ;i < vertices.length;i+= 3 ){ |
142 | var point:Vector3D = new Vector3D(vertices[i],vertices[i+ 1 ],vertices[i+ 2 ]); |
148 | private function initLights(): void { |
149 | light = new PointLight(); |
150 | light.color = 0xffffff ; |
154 | view.scene.addChild(light); |
155 | lightPicker = new StaticLightPicker([light]); |
158 | private function initParticles(): void { |
159 | var cube:Geometry = new CubeGeometry(SIZE,SIZE,SIZE); |
160 | var geometrySet:Vector.<Geometry> = new Vector.<Geometry>(); |
161 | for ( var i: int = 0 ; i < data.length; i++){ |
162 | geometrySet.push(cube); |
164 | var particleGeometry:Geometry = ParticleGeometryHelper.generateGeometry(geometrySet); |
166 | var particleAnimationSet:ParticleAnimationSet = new ParticleAnimationSet(); |
167 | particleAnimationSet.addAnimation( new ParticlePositionNode(ParticlePropertiesMode.LOCAL_STATIC)); |
168 | particleAnimationSet.addAnimation( new ParticleVelocityNode(ParticlePropertiesMode.LOCAL_STATIC)); |
169 | particleAnimationSet.initParticleFunc = initParticleFunc; |
171 | var material:ColorMaterial = new ColorMaterial( 0xffffff ); |
172 | material.alphaPremultiplied = true ; |
173 | material.lightPicker = lightPicker; |
175 | var particleMesh:Mesh = new Mesh(particleGeometry, material); |
176 | particleAnimator = new ParticleAnimator(particleAnimationSet); |
177 | particleMesh.animator = particleAnimator; |
178 | particleAnimator.start(); |
179 | view.scene.addChild(particleMesh); |
182 | private function initParticleFunc(prop:ParticleProperties): void { |
184 | var degree1: Number = Math.random() * Math.PI * 2 ; |
185 | var degree2: Number = Math.random() * Math.PI * 2 ; |
188 | prop[ParticleVelocityNode.VELOCITY_VECTOR3D] = new Vector3D(r * Math.sin(degree1) * Math.cos(degree2), r * Math.cos(degree1) * Math.cos(degree2), r * Math.sin(degree2)); |
190 | prop[ParticlePositionNode.POSITION_VECTOR3D] = data[prop.index]; |
193 | private function onEnterFrame(event:Event): void { |
195 | cameraController.panAngle = 0.3 *(stage.mouseX - lastMouseX) + lastPanAngle; |
196 | cameraController.tiltAngle = 0.3 *(stage.mouseY - lastMouseY) + lastTiltAngle; |
201 | private function onMouseDown(event:MouseEvent): void { |
202 | lastPanAngle = cameraController.panAngle; |
203 | lastTiltAngle = cameraController.tiltAngle; |
204 | lastMouseX = stage.mouseX; |
205 | lastMouseY = stage.mouseY; |
207 | stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); |
210 | private function onMouseUp(event:MouseEvent): void { |
212 | stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); |
215 | private function onStageMouseLeave(event:Event): void { |
217 | stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave); |
220 | private function onResize(event:Event = null ): void { |
221 | view.width = stage.stageWidth; |
222 | view.height = stage.stageHeight; |
转载请注明:陈童的博客 » 使用Away3d实现模型的粒子化