Back to Home

ESO Lua File v100035

libraries/zo_particles/zo_particlesystem.lua

[◄ back to folders ]
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
--Particle System
ZO_ParticleSystem = ZO_Object:Subclass()
ZO_ParticleSystem.particleClassToPool = {}
function ZO_ParticleSystem:New(...)
    local obj = ZO_Object.New(self)
    obj:Initialize(...)
    return obj
end
function ZO_ParticleSystem:Initialize(particleClass)
    if not ZO_ParticleSystem.particleClassToPool[particleClass] then
        local Factory = function()
            return particleClass:New()
        end
        local Reset = function(particle)
            particle:Stop()
        end
        local pool = ZO_ObjectPool:New(Factory, Reset)
        ZO_ParticleSystem.particleClassToPool[particleClass] = pool
    end
    self.particlePool = ZO_MetaPool:New(ZO_ParticleSystem.particleClassToPool[particleClass])
    self.parameters = {}
    self.startPrimeS = 0
end
function ZO_ParticleSystem:SetDuration(durationS)
    self.durationS = durationS
end
function ZO_ParticleSystem:SetStartPrimeS(primeS)
    self.startPrimeS = primeS
end
function ZO_ParticleSystem:SetParticlesPerSecond(particlesPerSecond)
    self.particlesPerSecond = particlesPerSecond
    if particlesPerSecond > 0 then
        self.secondsBetweenParticles = 1 / particlesPerSecond
    else
        self.secondsBetweenParticles = nil
    end
end
function ZO_ParticleSystem:SetBurst(numParticles, durationS, phaseS, cycleDurationS)
    self.burstNumParticles = numParticles
    self.burstDurationS = durationS
    self.burstPhaseS = phaseS
    self.burstCycleDurationS = cycleDurationS
end
function ZO_ParticleSystem:IsBurstMode()
    return self.burstNumParticles ~= nil
end
function ZO_ParticleSystem:SetBurstEasing(easingFunction)
end
function ZO_ParticleSystem:SetParentControl(parentControl)
    self.parentControl = parentControl
end
--This is the sound that will play on start, or on each burst if it's a burst system
function ZO_ParticleSystem:SetSound(sound)
    self.sound = sound
end
function ZO_ParticleSystem:SetOnParticleStartCallback(callback)
end
function ZO_ParticleSystem:SetOnParticleStopCallback(callback)
end
--This function takes N parameter names and one value generator. The result of the value generator will be applied to the
--named parameters when the particle is created. A simple example is, SetParticleParameter("alpha", ZO_UniformRangeGenerator:New(0, 1)).
--This would set the parameter "alpha" to a random value between 0 and 1 on creation. For a more complex example,
--SetParticleParameter("x", "y", "z", ZO_RandomSpherePoint:New(10)). This would generate a random point on a sphere of size 10 and then
--set each of "x", "y", and "z" on the particle. This allows for one generator to produce multiple linked values. You can also use a direct
--value instead of using a generator class if you just want to use the value always. For example, SetParticle("scale", 1.5).
function ZO_ParticleSystem:SetParticleParameter(...)
    local numArguments = select("#", ...)
    if numArguments >= 2 then
        --See if we already have a generator for these parameter names. We assume that they won't do something like setting generators for
        --both "x" and "x" , "y".
        local existingKey
        for parameterNames, _ in pairs(self.parameters) do
            local match = true
            for i = 1, numArguments - 1 do
                local name = select(i, ...)
                if parameterNames[i] ~= name then
                    match = false
                    break
                end
            end
            if match then
                existingKey = parameterNames
                break
            end
        end
        local parameterNames
        if existingKey then
            parameterNames = existingKey
        else        
            parameterNames = {}
            for i = 1, numArguments - 1 do
                local name = select(i, ...)
                table.insert(parameterNames, name)
            end
        end
        local valueGenerator = select(numArguments, ...)
        self.parameters[parameterNames] = valueGenerator
    end
end
function ZO_ParticleSystem:Start()
    if not self.running then
        PARTICLE_SYSTEM_MANAGER:AddParticleSystem(self)
        -- With priming, we pretend like the system had started some time ago and let the system play catch up
        -- This allows for situations like an emitter seeming like it had already been emitting before we ever showed the scene,
        -- so that the player doesn't see it filling out in the beginning
        self.startTimeS = GetGameTimeSeconds() - self.startPrimeS
        self.lastUpdateS = self.startTimeS
        self.unusedDeltaS = self.startPrimeS
        self.running = true
        self.finishing = false
        if not self:IsBurstMode() then
            PlaySound(self.sound)
        end
    else
        self.finishing = false
    end
end
function ZO_ParticleSystem:SpawnParticles(numParticlesToSpawn, startTimeS, endTimeS, intervalS)
    if numParticlesToSpawn == 0 then
        return
    end
    
    local MAX_PARTICLES_TO_SPAWN_PER_FRAME = 300
    numParticlesToSpawn = zo_min(numParticlesToSpawn, MAX_PARTICLES_TO_SPAWN_PER_FRAME)
    if not intervalS then
        intervalS = (endTimeS - startTimeS) / numParticlesToSpawn
    end
    local nowS = GetGameTimeSeconds()
    local particleSpawnTimeS = startTimeS + intervalS
    for particleSpawnIndex = 1, numParticlesToSpawn do
        local particle, key = self.particlePool:AcquireObject()
        particle:ResetParameters()
        particle:SetKey(key)
        local isParticleAlreadyDead = false
        -- This is the prime for the individual particle, not for the system
        local spawnTimePrimeS = 0
        for parameterNames, valueGenerator in pairs(self.parameters) do
            local valueGeneratorIsObject = type(valueGenerator) == "table" and valueGenerator.GetValue ~= nil
            if valueGeneratorIsObject then
                valueGenerator:Generate()
            end
            for i, parameterName in ipairs(parameterNames) do
                if parameterName == "DurationS" then
                    local durationS = valueGeneratorIsObject and valueGenerator:GetValue(i) or valueGenerator
                    if particleSpawnTimeS + durationS < nowS then
                        -- Don't bother starting up a particle that's effectively already dead
                        isParticleAlreadyDead = true
                        break
                    end
                elseif parameterName == "PrimeS" then
                    spawnTimePrimeS = valueGeneratorIsObject and valueGenerator:GetValue(i) or valueGenerator
                end
                if valueGeneratorIsObject then
                    particle:SetParameter(parameterName, valueGenerator:GetValue(i))
                else
                    particle:SetParameter(parameterName, valueGenerator)
                end
            end
        end
        if isParticleAlreadyDead then
            self.particlePool:ReleaseObject(key)
        else
            self:StartParticle(particle, particleSpawnTimeS - spawnTimePrimeS, nowS)
        end
        
        particleSpawnTimeS = particleSpawnTimeS + intervalS
    end
end
function ZO_ParticleSystem:StartParticle(particle, startTimeS, nowS)
    particle:Start(self.parentControl, startTimeS, nowS)
    if self.onParticleStartCallback then
        self.onParticleStartCallback(particle)
    end
end
function ZO_ParticleSystem:StopParticle(particle)
    if self.onParticleStopCallback then
        self.onParticleStopCallback(particle)
    end
    self.particlePool:ReleaseObject(particle:GetKey())
end
do
    local g_removeParticles = {}
    function ZO_ParticleSystem:OnUpdate(timeS)
        local deltaS = timeS - self.lastUpdateS
        
        local durationS = self.durationS
        if durationS then
            if timeS - self.startTimeS > durationS then
                self:Finish()
            end
        end
        if self.finishing then
            if not next(self.particlePool:GetActiveObjects()) then
                self:Stop()
            end
        else
            --Spawn New Particles
            if self:IsBurstMode() then
                local elapsedTimeS = timeS - self.startTimeS
                if elapsedTimeS > 0 then
                    local lastElapsedUpdateTimeS = self.lastUpdateS - self.startTimeS
                    -- How far into the current cycle are we?
                    local timeInCycleS = elapsedTimeS % self.burstCycleDurationS
                    -- When did the current cycle begin?
                    local timeCycleStartedS = timeS - timeInCycleS
                    -- New burst
                    if self.lastUpdateS <= timeCycleStartedS then
                        self.burstNumSpawned = 0
                        self.burstStartTimeS = timeCycleStartedS + self.burstPhaseS
                        self.burstStopTimeS = self.burstStartTimeS + self.burstDurationS
                    end
                    --We're after when we would have started emitting, and we haven't emitted enough particles
                    if self.burstNumSpawned < self.burstNumParticles and timeInCycleS > self.burstPhaseS then
                        local progress = 1
                        if timeS < self.burstStopTimeS then
                            progress = (timeS - self.burstStartTimeS) / self.burstDurationS
                        end
                        if self.burstEasingFunction then
                            progress = self.burstEasingFunction(progress)
                        end
                        local numParticlesThatShouldBeSpawned = zo_round(progress * self.burstNumParticles)
                        local numParticlesToSpawn = numParticlesThatShouldBeSpawned - self.burstNumSpawned
                        --Play the sound the first time particles start bursting
                        if self.burstNumSpawned == 0 and numParticlesToSpawn > 0 then
                            PlaySound(self.sound)
                        end
                        self.burstNumSpawned = numParticlesThatShouldBeSpawned
                        local startTimeS = zo_max(self.lastUpdateS, self.burstStartTimeS)
                        local endTimeS = zo_min(timeS, self.burstStopTimeS)
                        self:SpawnParticles(numParticlesToSpawn, startTimeS, endTimeS)
                    end
                end
            elseif self.particlesPerSecond > 0 then
                local secondsSinceLastParticle = deltaS + self.unusedDeltaS
                local numParticlesToSpawn = zo_floor(secondsSinceLastParticle / self.secondsBetweenParticles)
                --Any "partial" particles that are left over we store off as unused delta time and then add that into the next update.
                local processedDeltaS = numParticlesToSpawn * self.secondsBetweenParticles
                self.unusedDeltaS = secondsSinceLastParticle - processedDeltaS
                self:SpawnParticles(numParticlesToSpawn, timeS - secondsSinceLastParticle, timeS, self.secondsBetweenParticles)
            end
        end
        --Update Particles
        for _, particle in pairs(self.particlePool:GetActiveObjects()) do
            if particle:IsDone(timeS) then
                table.insert(g_removeParticles, particle)
            else
                particle:OnUpdate(timeS)
            end
        end
        --Remove Dead Particles
        if #g_removeParticles then
            for _, particle in ipairs(g_removeParticles) do
                self:StopParticle(particle)
            end
            ZO_ClearNumericallyIndexedTable(g_removeParticles)
        end
        self.lastUpdateS = timeS
    end
end
--Stop and kill all particles
function ZO_ParticleSystem:Stop()
    if self.running then
        PARTICLE_SYSTEM_MANAGER:RemoveParticleSystem(self)
        self.particlePool:ReleaseAllObjects()
        self.running = false
        self.finishing = false
    end
end
--Stop making new particles but let existing particles finish
function ZO_ParticleSystem:Finish()
    if self.running then
        self.finishing = true
    end
end
--Scene Graph Particle System
ZO_SceneGraphParticleSystem = ZO_ParticleSystem:Subclass()
function ZO_SceneGraphParticleSystem:New(...)
    return ZO_ParticleSystem.New(self, ...)
end
function ZO_SceneGraphParticleSystem:Initialize(particleClass, parentNode)
    ZO_ParticleSystem.Initialize(self, particleClass)
    self.parentNode = parentNode
end
function ZO_SceneGraphParticleSystem:StartParticle(particle, startTimeS, nowS)
    particle:SetParentNode(self.parentNode)
    ZO_ParticleSystem.StartParticle(self, particle, startTimeS, nowS)
end
--Control Particle System
ZO_ControlParticleSystem = ZO_ParticleSystem
--Particle System Manager
ZO_ParticleSystemManager = ZO_Object:Subclass()
function ZO_ParticleSystemManager:New(...)
    local obj = ZO_Object.New(self)
    obj:Initialize(...)
    return obj
end
function ZO_ParticleSystemManager:Initialize()
    self.texturePool = ZO_ControlPool:New("ZO_ParticleTexture", nil, "ZO_ParticleTexture")
    self.animationTimelinePool = ZO_AnimationPool:New("ZO_ParticleAnimationTimeline")
    self.buildingAnimationTimelinePlaybackTypes = {}
    self.buildingAnimationTimelineLoopCounts = {}
    self.activeParticleSystems = {}
    EVENT_MANAGER:RegisterForUpdate("ZO_ParticleSystemManager", 0, function(timeMS) self:OnUpdate(timeMS / 1000) end)
end
function ZO_ParticleSystemManager:OnUpdate(timeS)
    for _, particleSystem in ipairs(self.activeParticleSystems) do
        particleSystem:OnUpdate(timeS)
    end
end
function ZO_ParticleSystemManager:AddParticleSystem(particleSystem)
    table.insert(self.activeParticleSystems, particleSystem)
end
function ZO_ParticleSystemManager:RemoveParticleSystem(particleSystem)
    for i, searchParticleSystem in ipairs(self.activeParticleSystems) do
        if searchParticleSystem == particleSystem then
            table.remove(self.activeParticleSystems, i)
            break
        end
    end
end
function ZO_ParticleSystemManager:AcquireTexture()
    local textureControl, key = self.texturePool:AcquireObject()
    textureControl.key = key
    return textureControl
end
function ZO_ParticleSystemManager:ReleaseTexture(textureControl)
    self.texturePool:ReleaseObject(textureControl.key)
end
function ZO_ParticleSystemManager:GetAnimation(control, playbackInfo, animationType, easingFunction, durationS, offsetS)
    --Collect all of the timelines until FinishBuildingAnimationTimelines is called
    if not self.buildingAnimationTimelines then
        self.buildingAnimationTimelines = {}
    end
    local playbackType = ANIMATION_PLAYBACK_ONE_SHOT
    local loopCount = 1
    if playbackInfo then
        playbackType = playbackInfo.playbackType or ANIMATION_PLAYBACK_ONE_SHOT
        loopCount = playbackInfo.loopCount or 1
    end
    offsetS = offsetS or 0
    local timeline
    --One shot animations all belong to the same timeline. LOOP and PING_PONG animations use separate timelines so they can LOOP and PING_PONG at their own durations.
    if playbackType == ANIMATION_PLAYBACK_ONE_SHOT then
        for i = 1, #self.buildingAnimationTimelines do
            if self.buildingAnimationTimelinePlaybackTypes[i] == playbackType and self.buildingAnimationTimelineLoopCounts[i] == loopCount then
                timeline = self.buildingAnimationTimelines[i]
                break
            end
        end
    end
    if not timeline then
        local key
        timeline, key = self.animationTimelinePool:AcquireObject()
        timeline.key = key
        timeline:SetPlaybackType(playbackType, loopCount)
        table.insert(self.buildingAnimationTimelines, timeline)
        table.insert(self.buildingAnimationTimelinePlaybackTypes, playbackType)
        table.insert(self.buildingAnimationTimelineLoopCounts, loopCount)
    end
    local animation = timeline:GetFirstAnimationOfType(animationType)
    if not animation then
        animation = timeline:InsertAnimation(animationType, control, offsetS)
    else
        animation:SetAnimatedControl(control)
        animation:SetEnabled(true)
        timeline:SetAnimationOffset(animation, offsetS)
    end
    animation:SetDuration(durationS * 1000)
    animation:SetEasingFunction(easingFunction)
    return animation
end
function ZO_ParticleSystemManager:FinishBuildingAnimationTimelines()
    if self.buildingAnimationTimelines then
        ZO_ClearNumericallyIndexedTable(self.buildingAnimationTimelinePlaybackTypes)
        ZO_ClearNumericallyIndexedTable(self.buildingAnimationTimelineLoopCounts)
        local timelines = self.buildingAnimationTimelines
        self.buildingAnimationTimelines = nil
        return timelines
    else
        return nil
    end
end
function ZO_ParticleSystemManager:ReleaseAnimationTimelines(animationTimelines)
    for _, animationTimeline in ipairs(animationTimelines) do
        for i = 1, animationTimeline:GetNumAnimations() do
            local animation = animationTimeline:GetAnimation(i)
            animation:SetEnabled(false)
        end
        self.animationTimelinePool:ReleaseObject(animationTimeline.key)
    end
end
PARTICLE_SYSTEM_MANAGER = ZO_ParticleSystemManager:New()