/
Template.as
125 lines (102 loc) · 4.15 KB
/
Template.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package
{
import a3dparticle.animators.actions.color.ChangeColorByLifeGlobal;
import a3dparticle.animators.actions.position.OffsetPositionLocal;
import a3dparticle.animators.actions.velocity.VelocityLocal;
import a3dparticle.generater.MutiWeightGenerater;
import a3dparticle.particle.ParticleColorMaterial;
import a3dparticle.particle.ParticleParam;
import a3dparticle.particle.ParticleSample;
import a3dparticle.ParticlesContainer;
import away3d.containers.View3D;
import away3d.debug.AwayStats;
import away3d.primitives.SphereGeometry;
import away3d.debug.WireframeAxesGrid;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Vector3D;
/**
* ...
* @author liaocheng.Email:liaocheng210@126.com
*/
[SWF(width="1024", height="768", frameRate="60")]
public class Template extends Sprite
{
protected var _view:View3D;
private var particle:ParticlesContainer;
public function Template():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, onStageResize);
// entry point
_view = new View3D();
_view.width = 1024;
_view.height = 768;
_view.antiAlias = 4;
addChild(_view);
addEventListener(Event.ENTER_FRAME, onRender);
addChild(new AwayStats(_view));
new HoverDragController(_view.camera, stage);
_view.scene.addChild(new WireframeAxesGrid(4,1000));
initScene();
}
private function initScene():void
{
//step 1: we create some samples which group the geometry and material.
//the geometry can be got from the primitives of away3d or models which is createed by tools like max or maya .
//the material can be ParticleColorMaterial which set color directly or ParticleBitmapMaterial which set color using texture.
var material:ParticleColorMaterial = new ParticleColorMaterial();
var sphereLarge:SphereGeometry = new SphereGeometry(10, 6, 6);
var sphereSmall:SphereGeometry = new SphereGeometry(5, 4, 4);
var sample1:ParticleSample = new ParticleSample(sphereLarge.subGeometries[0], material);
var sample2:ParticleSample = new ParticleSample(sphereSmall.subGeometries[0], material);
//step 2: we create a generater which group the samples.
//The generater will provide a samples list for container.
var generater:MutiWeightGenerater = new MutiWeightGenerater([sample1, sample2], [2, 1], 1000);
//step 3: we create a container and set some attributes.
particle = new ParticlesContainer();
particle.loop = true;
particle.hasDuringTime = true;
//step 4:we add some actions to the container.
var action1:VelocityLocal = new VelocityLocal();
particle.addAction(action1);
var action2:OffsetPositionLocal = new OffsetPositionLocal();
particle.addAction(action2);
var action3:ChangeColorByLifeGlobal = new ChangeColorByLifeGlobal(new ColorTransform(1, 1, 0.2, 0.8, 0, 0), new ColorTransform(1, 0, 0, 0.1, 0, 0) );
particle.addAction(action3);
//step 5:we set the param function whose return value will be used for actions
particle.initParticleFun = initParticleParam;
//finally,we generate the particles,and start
particle.generate(generater);
particle.start();
_view.scene.addChild(particle);
//we can clone it to create many instances to add different position.
}
private function initParticleParam(param:ParticleParam):void
{
param.startTime = Math.random() * 3;
param.duringTime = Math.random() * 2 + 1;
param["VelocityLocal"] = new Vector3D(Math.random() * 100 - 50, Math.random() * 300, Math.random() * 100 - 50);
param["OffsetPositionLocal"] = new Vector3D(Math.random() * 70 - 35, 0, Math.random() * 70 - 35);
}
private function onRender(e:Event):void
{
_view.render();
}
private function onStageResize(e:Event):void
{
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
}
}
}