-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathParticleAnimationSet.as
388 lines (327 loc) · 13.1 KB
/
ParticleAnimationSet.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package away3d.animators
{
import flash.display3D.*;
import flash.utils.*;
import away3d.*;
import away3d.animators.data.*;
import away3d.animators.nodes.*;
import away3d.core.base.*;
import away3d.core.base.data.*;
import away3d.core.managers.*;
import away3d.entities.*;
import away3d.materials.passes.*;
use namespace arcane;
/**
* The animation data set used by particle-based animators, containing particle animation data.
*
* @see away3d.animators.ParticleAnimator
*/
public class ParticleAnimationSet extends AnimationSetBase implements IAnimationSet
{
/** @private */
arcane var _animationRegisterCache:AnimationRegisterCache;
//all other nodes dependent on it
private var _timeNode:ParticleTimeNode;
/**
* Property used by particle nodes that require compilation at the end of the shader
*/
public static const POST_PRIORITY:int = 9;
/**
* Property used by particle nodes that require color compilation
*/
public static const COLOR_PRIORITY:int = 18;
private var _animationSubGeometries:Dictionary = new Dictionary(true);
private var _particleNodes:Vector.<ParticleNodeBase> = new Vector.<ParticleNodeBase>();
private var _localDynamicNodes:Vector.<ParticleNodeBase> = new Vector.<ParticleNodeBase>();
private var _localStaticNodes:Vector.<ParticleNodeBase> = new Vector.<ParticleNodeBase>();
private var _totalLenOfOneVertex:int = 0;
//set true if has an node which will change UV
public var hasUVNode:Boolean;
//set if the other nodes need to access the velocity
public var needVelocity:Boolean;
//set if has a billboard node.
public var hasBillboard:Boolean;
//set if has an node which will apply color multiple operation
public var hasColorMulNode:Boolean;
//set if has an node which will apply color add operation
public var hasColorAddNode:Boolean;
/**
* Initialiser function for static particle properties. Needs to reference a function with teh following format
*
* <code>
* function initParticleFunc(prop:ParticleProperties):void
* {
* //code for settings local properties
* }
* </code>
*
* Aside from setting any properties required in particle animation nodes using local static properties, the initParticleFunc function
* is required to time node requirements as they may be needed. These properties on the ParticleProperties object can include
* <code>startTime</code>, <code>duration</code> and <code>delay</code>. The use of these properties is determined by the setting
* arguments passed in the constructor of the particle animation set. By default, only the <code>startTime</code> property is required.
*/
public var initParticleFunc:Function;
/**
* Creates a new <code>ParticleAnimationSet</code>
*
* @param [optional] usesDuration Defines whether the animation set uses the <code>duration</code> data in its static properties function to determine how long a particle is visible for. Defaults to false.
* @param [optional] usesLooping Defines whether the animation set uses a looping timeframe for each particle determined by the <code>startTime</code>, <code>duration</code> and <code>delay</code> data in its static properties function. Defaults to false. Requires <code>usesDuration</code> to be true.
* @param [optional] usesDelay Defines whether the animation set uses the <code>delay</code> data in its static properties function to determine how long a particle is hidden for. Defaults to false. Requires <code>usesLooping</code> to be true.
*/
public function ParticleAnimationSet(usesDuration:Boolean = false, usesLooping:Boolean = false, usesDelay:Boolean = false)
{
//automatically add a particle time node to the set
addAnimation(_timeNode = new ParticleTimeNode(usesDuration, usesLooping, usesDelay));
}
/**
* Returns a vector of the particle animation nodes contained within the set.
*/
public function get particleNodes():Vector.<ParticleNodeBase>
{
return _particleNodes;
}
/**
* @inheritDoc
*/
override public function addAnimation(node:AnimationNodeBase):void
{
var i:int;
var n:ParticleNodeBase = node as ParticleNodeBase;
n.processAnimationSetting(this);
if (n.mode == ParticlePropertiesMode.LOCAL_STATIC) {
n.dataOffset = _totalLenOfOneVertex;
_totalLenOfOneVertex += n.dataLength;
_localStaticNodes.push(n);
} else if (n.mode == ParticlePropertiesMode.LOCAL_DYNAMIC) {
_localDynamicNodes.push(n);
}
for (i = _particleNodes.length - 1; i >= 0; i--)
if (_particleNodes[i].priority <= n.priority)
break;
_particleNodes.splice(i + 1, 0, n);
super.addAnimation(node);
}
/**
* @inheritDoc
*/
public function activate(stage3DProxy : Stage3DProxy, pass : MaterialPassBase) : void
{
_animationRegisterCache = pass.animationRegisterCache;
}
/**
* @inheritDoc
*/
public function deactivate(stage3DProxy : Stage3DProxy, pass : MaterialPassBase) : void
{
var context : Context3D = stage3DProxy.context3D;
var offset:int = _animationRegisterCache.vertexAttributesOffset;
var used:int = _animationRegisterCache.numUsedStreams;
for (var i:int = offset; i < used; i++)
context.setVertexBufferAt(i, null);
}
/**
* @inheritDoc
*/
public function getAGALVertexCode(pass : MaterialPassBase, sourceRegisters : Vector.<String>, targetRegisters : Vector.<String>, profile : String) : String
{
//grab animationRegisterCache from the materialpassbase or create a new one if the first time
_animationRegisterCache = pass.animationRegisterCache ||= new AnimationRegisterCache(profile);
//reset animationRegisterCache
_animationRegisterCache.vertexConstantOffset = pass.numUsedVertexConstants;
_animationRegisterCache.vertexAttributesOffset = pass.numUsedStreams;
_animationRegisterCache.varyingsOffset = pass.numUsedVaryings;
_animationRegisterCache.fragmentConstantOffset = pass.numUsedFragmentConstants;
_animationRegisterCache.hasUVNode = hasUVNode;
_animationRegisterCache.needVelocity = needVelocity;
_animationRegisterCache.hasBillboard = hasBillboard;
_animationRegisterCache.sourceRegisters = sourceRegisters;
_animationRegisterCache.targetRegisters = targetRegisters;
_animationRegisterCache.needFragmentAnimation = pass.needFragmentAnimation;
_animationRegisterCache.needUVAnimation = pass.needUVAnimation;
_animationRegisterCache.hasColorAddNode = hasColorAddNode;
_animationRegisterCache.hasColorMulNode = hasColorMulNode;
_animationRegisterCache.reset();
var code:String = "";
code += _animationRegisterCache.getInitCode();
var node:ParticleNodeBase;
for each(node in _particleNodes)
if (node.priority < POST_PRIORITY)
code += node.getAGALVertexCode(pass, _animationRegisterCache);
code += _animationRegisterCache.getCombinationCode();
for each(node in _particleNodes)
if (node.priority >= POST_PRIORITY && node.priority < COLOR_PRIORITY)
code += node.getAGALVertexCode(pass, _animationRegisterCache);
code += _animationRegisterCache.initColorRegisters();
for each(node in _particleNodes)
if (node.priority >= COLOR_PRIORITY)
code += node.getAGALVertexCode(pass, _animationRegisterCache);
code += _animationRegisterCache.getColorPassCode();
return code;
}
/**
* @inheritDoc
*/
public function getAGALUVCode(pass : MaterialPassBase, UVSource : String, UVTarget:String) : String
{
var code:String = "";
if (hasUVNode)
{
_animationRegisterCache.setUVSourceAndTarget(UVSource, UVTarget);
code += "mov " + _animationRegisterCache.uvTarget + ".xy," + _animationRegisterCache.uvAttribute.toString() + "\n";
var node:ParticleNodeBase;
for each(node in _particleNodes)
{
code += node.getAGALUVCode(pass, _animationRegisterCache);
}
code += "mov " + _animationRegisterCache.uvVar.toString() + "," + _animationRegisterCache.uvTarget + ".xy\n";
}
else
{
code += "mov " + UVTarget + "," + UVSource + "\n";
}
return code;
}
/**
* @inheritDoc
*/
public function getAGALFragmentCode(pass : MaterialPassBase, shadedTarget : String, profile : String) : String
{
return _animationRegisterCache.getColorCombinationCode(shadedTarget);
}
/**
* @inheritDoc
*/
public function doneAGALCode(pass : MaterialPassBase):void
{
_animationRegisterCache.setDataLength();
//set vertexZeroConst,vertexOneConst,vertexTwoConst
_animationRegisterCache.setVertexConst(_animationRegisterCache.vertexZeroConst.index, 0, 1, 2, 0);
}
/**
* @inheritDoc
*/
override public function get usesCPU() : Boolean
{
return false;
}
/**
* @inheritDoc
*/
override public function cancelGPUCompatibility() : void
{
}
override public function dispose():void
{
var subGeometry:AnimationSubGeometry;
for each(subGeometry in _animationSubGeometries)
{
subGeometry.dispose();
}
super.dispose();
}
/** @private */
arcane function generateAnimationSubGeometries(mesh:Mesh):void
{
if (initParticleFunc == null)
throw(new Error("no initParticleFunc set"));
var geometry:ParticleGeometry = mesh.geometry as ParticleGeometry;
if (!geometry)
throw(new Error("Particle animation can only be performed on a ParticleGeometry object"));
var i:int, j:int;
var animationSubGeometry:AnimationSubGeometry;
var newAnimationSubGeometry:Boolean;
var subGeometry:ISubGeometry;
var subMesh:SubMesh;
var localNode:ParticleNodeBase;
for (i = 0; i < mesh.subMeshes.length; i++)
{
subMesh = mesh.subMeshes[i];
subGeometry = subMesh.subGeometry;
if (mesh.shareAnimationGeometry)
{
animationSubGeometry = _animationSubGeometries[subGeometry];
if (animationSubGeometry) {
subMesh.animationSubGeometry = animationSubGeometry;
continue;
}
}
animationSubGeometry = subMesh.animationSubGeometry = new AnimationSubGeometry();
if (mesh.shareAnimationGeometry)
_animationSubGeometries[subGeometry] = animationSubGeometry;
newAnimationSubGeometry = true;
//create the vertexData vector that will be used for local node data
animationSubGeometry.createVertexData(subGeometry.numVertices, _totalLenOfOneVertex);
}
if (!newAnimationSubGeometry)
return;
var particles:Vector.<ParticleData> = geometry.particles;
var particlesLength:uint = particles.length;
var numParticles:uint = geometry.numParticles;
var particleProperties:ParticleProperties = new ParticleProperties();
var particle:ParticleData;
var oneDataLen:int;
var oneDataOffset:int;
var counterForVertex:int;
var counterForOneData:int;
var oneData:Vector.<Number>;
var numVertices:uint;
var vertexData:Vector.<Number>;
var vertexLength:uint;
var startingOffset:uint;
var vertexOffset:uint;
//default values for particle param
particleProperties.total = numParticles;
particleProperties.startTime = 0;
particleProperties.duration = 1000;
particleProperties.delay = 0.1;
i = 0;
j = 0;
while (i < numParticles)
{
particleProperties.index = i;
//call the init function on the particle parameters
initParticleFunc(particleProperties);
//create the next set of node properties for the particle
for each (localNode in _localStaticNodes)
localNode.generatePropertyOfOneParticle(particleProperties);
//loop through all particle data for the curent particle
while (j < particlesLength && (particle = particles[j]).particleIndex == i) {
//find the target animationSubGeometry
for each(subMesh in mesh.subMeshes)
{
if (subMesh.subGeometry == particle.subGeometry)
{
animationSubGeometry = subMesh.animationSubGeometry;
break;
}
}
numVertices = particle.numVertices;
vertexData = animationSubGeometry.vertexData;
vertexLength = numVertices * _totalLenOfOneVertex;
startingOffset = animationSubGeometry.numProcessedVertices * _totalLenOfOneVertex;
//loop through each static local node in the animation set
for each (localNode in _localStaticNodes) {
oneData = localNode.oneData;
oneDataLen = localNode.dataLength;
oneDataOffset = startingOffset + localNode.dataOffset;
//loop through each vertex set in the vertex data
for (counterForVertex = 0; counterForVertex < vertexLength; counterForVertex+=_totalLenOfOneVertex) {
vertexOffset = oneDataOffset + counterForVertex;
//add the data for the local node to the vertex data
for (counterForOneData = 0; counterForOneData < oneDataLen; counterForOneData++)
vertexData[vertexOffset + counterForOneData] = oneData[counterForOneData];
}
}
//store particle properties if they need to be retreived for dynamic local nodes
if (_localDynamicNodes.length)
animationSubGeometry.animationParticles.push(new ParticleAnimationData(i, particleProperties.startTime, particleProperties.duration, particleProperties.delay, particle));
animationSubGeometry.numProcessedVertices += numVertices;
//next index
j++;
}
//next particle
i++;
}
}
}
}