forked from simeonradivoev/Galaxia-Runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Galaxy.cs
588 lines (546 loc) · 17.6 KB
/
Galaxy.cs
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// ----------------------------------------------------------------
// Galaxia
// ©2016 Simeon Radivoev
// Written by Simeon Radivoev (simeonradivoev@gmail.com)
// ----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
namespace Galaxia
{
/// <summary>
/// The Component that holds and manages visualization of <see cref="Galaxia.GalaxyPrefab"/>
/// </summary>
public sealed class Galaxy : MonoBehaviour
{
#region Public
#endregion
#region Private
[SerializeField]
[HideInInspector]
private List<Particles> particles;
[SerializeField,HideInInspector]
private GalaxyGenerationType m_generationType;
[SerializeField,HideInInspector]
private bool m_gpu = true;
[SerializeField,HideInInspector] private bool m_render_galaxy = true;
[SerializeField, HideInInspector] private bool m_frustumCulling = true;
[SerializeField]
private GalaxyPrefab m_galaxy;
[SerializeField,HideInInspector]
private bool m_saveMeshes;
[SerializeField, HideInInspector]
private bool m_saveParticles;
#endregion
#region Events
private RenderEventHandler m_preRenderHandler;
private RenderEventHandler m_postRenderHandler;
private RenderEvent lastPreRenderEvent = new RenderEvent(false);
#endregion
#region Constructors
void OnEnable()
{
SmartParticleInitialization();
if (m_preRenderHandler == null)
m_preRenderHandler = new RenderEventHandler();
if (m_postRenderHandler == null)
m_postRenderHandler = new RenderEventHandler();
}
#endregion
#region Methods
#region Particle Generation and Update
/// <summary>
/// Initializes or updates the galaxy and each of it's particles only when necessary.
/// </summary>
public void SmartParticleInitialization()
{
if (particles == null)
{
GenerateParticles();
return;
}
//if the particles components were destroyed without us knowing
if (particles.Any(t => !t))
{
GenerateParticles();
return;
}
foreach (var particle in particles)
{
particle.NeedsRebuild = !m_gpu;
particle.NeedsUpdate = particle.CalculateNeedsUpdate();
}
}
/// <summary>
/// Generates <see cref="Galaxia.Particles"/> to all the <see cref="Galaxia.ParticlesPrefab"/> in the galaxy
/// </summary>
/// <remarks>
/// This function only generates the particles, not the Meshes
/// </remarks>
public void GenerateParticles()
{
DestroyParticles();
if (!GalaxyPrefab) return;
foreach (ParticlesPrefab prefab in GalaxyPrefab.Where(prefab => prefab != null))
{
GenerateParticles(prefab);
}
}
/// <summary>
/// Same as the <see cref="Galaxia.Galaxy.GenerateParticles()"/> but for a specific <see cref="Galaxia.ParticlesPrefab"/>
/// </summary>
/// <param name="prefab">The Particles Prefab to use for the generation</param>
public void GenerateParticles(ParticlesPrefab prefab)
{
if (GalaxyPrefab != null)
{
if (prefab != null)
{
GameObject obj = new GameObject(prefab.name, typeof(Particles));
obj.transform.SetParent(transform);
Particles p = obj.GetComponent<Particles>();
p.Init();
p.Generate(prefab, GalaxyPrefab, GPU);
particles.Add(p);
prefab.CreateMaterial(GalaxyPrefab, GPU);
prefab.UpdateMaterial(GalaxyPrefab);
}
else
{
Debug.LogError("Particle Prefab was destroyed or missing");
}
}else
{
Debug.LogWarning("No Prefab assigned");
}
}
/// <summary>
/// Destroys all the <see cref="Galaxia.Particles"/> components in the Galaxy.
/// </summary>
public void DestroyParticles()
{
//Debug.Log("Particles Cleared");
if (particles != null)
{
//the particles remove themselves from the galaxy
for (int i = particles.Count - 1; i >= 0; i--)
{
particles[i]?.Destroy();
}
particles.Clear();
foreach(Transform t in transform)
{
DestroyImmediate(t.gameObject);
}
}
else
{
particles = new List<Particles>();
}
}
/// <summary>
/// Destroys all <see cref="Galaxia.Particles"/> with a given prefab.
/// </summary>
/// <param name="prefab">particle prefab to search for</param>
public void DestroyParticles(ParticlesPrefab prefab)
{
if (particles != null)
{
//the particles remove themselves from the galaxy
for (int i = particles.Count-1; i >= 0; i--)
{
if (!particles[i] || particles[i].Prefab != prefab) continue;
particles[i].Destroy();
}
}
else
{
particles = new List<Particles>();
}
}
internal void RemoveParticles(Particles item)
{
particles?.Remove(item);
}
/// <summary>
/// Marks all the <see cref="Galaxia.Particles"/> for Update, next frame.
/// </summary>
public void UpdateParticles()
{
GalaxyPrefab?.Distributor.RecreateCurves();
foreach (Particles particle in particles)
{
if(particle && GalaxyPrefab != null)
{
particle.NeedsUpdate = true;
}
}
}
/// <summary>
/// Forces all <see cref="Galaxia.Particles"/> to update Immediately
/// </summary>
public void UpdateParticlesImmediately()
{
GalaxyPrefab?.Distributor.RecreateCurves();
if(GalaxyPrefab == null) return;
ForceUpdateParticleComponents();
}
private void ForceUpdateParticleComponents()
{
//particles components may remove themselves if invalid, that's why we need backwards iteration
for (int i = particles.Count - 1; i >= 0; i--)
{
particles[i]?.UpdateParticles();
}
}
/// <summary>
/// Marks a <see cref="Galaxia.Particles"/> with a given <see cref="Galaxia.ParticlesPrefab"/> for Update, next frame.
/// </summary>
/// <param name="prefab">The <see cref="Galaxia.ParticlesPrefab"/> to search for</param>
public void UpdateParticles(ParticlesPrefab prefab)
{
foreach (Particles particle in particles)
{
if (particle && m_galaxy != null && particle.Prefab == prefab)
{
particle.NeedsUpdate = true;
}
}
}
/// <summary>
/// Forces all <see cref="Galaxia.Particles"/> with a given <see cref="Galaxia.ParticlesPrefab"/> to update Immediately.
/// </summary>
/// <param name="prefab">The <see cref="Galaxia.ParticlesPrefab"/> to search for</param>
public void UpdateParticlesImmediately(ParticlesPrefab prefab)
{
//particles components may remove themselves if invalid, that's why we need backwards iteration
for (int i = particles.Count - 1; i >= 0; i--)
{
if (particles[i] && m_galaxy != null && particles[i].Prefab == prefab)
{
particles[i].UpdateParticles();
}
}
}
#endregion
void Update()
{
if (GalaxyPrefab != null)
{
foreach (Particles particle in particles)
{
if (particle)
{
particle.UpdateMaterials();
GalaxyPrefab.Distributor.UpdateMaterial(particle.Prefab.Material);
}
else
{
Debug.LogWarning("Particle Component was destroyed");
GenerateParticles();
return;
}
}
}
}
/// <summary>
/// Called by unity when the object is drawn by any camera.
/// </summary>
/// <remarks>
/// This method is the main rendering method for the galaxy.
/// It will not be called if the component is disabled or the Pre render event is used.
/// </remarks>
private void OnRenderObject()
{
if (!isActiveAndEnabled) return;
foreach (var particle in particles)
{
if(!particle) continue;
lastPreRenderEvent.Used = false;
m_preRenderHandler.Invoke(particle, lastPreRenderEvent);
if (!lastPreRenderEvent.Used)
{
particle.Render();
}
}
}
/// <summary>
/// Draws the Galaxy and all the <see cref="Galaxia.Particles"/> in the Galaxy Now
/// </summary>
/// <remarks>
/// <example>
/// <code>
/// private void OnRenderObject()
/// {
/// foreach (var particle in particles)
/// {
/// particle.Render();
/// }
/// }
///}
/// </code>
/// </example>
/// </remarks>
///
public void DrawNow()
{
if (GalaxyPrefab != null && GPU)
{
foreach (Particles particle in particles)
{
if(particle)
{
particle.UpdateMaterials();
if (particle.Prefab != null)
particle.DrawNow();
}
}
}
}
/// <summary>
/// Sends the Galaxy and all the <see cref="Galaxia.Particles"/> for Rendering
/// </summary>
public void Draw()
{
if (GalaxyPrefab != null && GPU)
{
foreach (Particles particle in particles)
{
if (particle && particle.Prefab != null)
particle.Draw();
}
}
}
/// <summary>
/// </summary>
private void OnDrawGizmosSelected()
{
Bounds? bounds = null;
foreach (var particle in particles)
{
if(!particle) continue;
Bounds? particleBounds = particle.RenderBounds;
if (!particleBounds.HasValue) continue;
if (!bounds.HasValue)
{
bounds = particleBounds;
}
else
{
bounds.Value.Encapsulate(particleBounds.Value);
}
}
if (bounds.HasValue)
{
bounds = transform.TransformBounds(bounds.Value);
Gizmos.color = new Color(0.7f,0.8f,1,0.5f);
Gizmos.DrawWireCube(bounds.Value.center, bounds.Value.size);
}
}
/// <summary>
/// </summary>
private void OnDrawGizmos()
{
if (!Application.isPlaying && m_render_galaxy)
{
DrawNow();
}
}
#endregion
#region Getters And Setters
/// <summary>
/// A Utility function for getting the position of the <see cref="Galaxia.Galaxy"/>.
/// </summary>
public Vector3 Position { get { return transform.position; } set { transform.position = value; } }
/// <summary>
/// List of <see cref="Galaxia.Particles"/> that holds the Components responsible for the individual visualization of the <see cref="Galaxia.ParticlesPrefab"/>.
/// </summary>
public List<Particles> Particles { get { return particles; } }
/// <summary>
/// This dictates if the <see cref="Galaxia.Galaxy"/> updates automatically when a Property is changed.
/// If it is set to manual, <see cref="Galaxia.Galaxy.GenerateParticles()"/> or <see cref="Galaxia.Galaxy.UpdateParticles(ParticlesPrefab)"/> must be called every time the galaxy is changed.
/// </summary>
public GalaxyGenerationType GenerationType { get { return m_generationType; } set { m_generationType = value; } }
/// <summary>
/// The meat of the galaxy. This is a holder for galaxy specific particle properties.
/// It holds a list of <see cref="Galaxia.ParticlesPrefab"/> as well as the <see cref="Galaxia.ParticleDistributor"/>.
/// </summary>
public GalaxyPrefab GalaxyPrefab
{
get { return m_galaxy; }
set
{
if(m_galaxy != value && m_generationType == GalaxyGenerationType.Automatic)
{
m_galaxy = value;
GenerateParticles();
}
else
{
m_galaxy = value;
}
}
}
/// <summary>
/// Returns if DirectX 11 is supported on the software.
/// If it is available you can disable it from here.
/// </summary>
[Obsolete("New Function is GPU")]
public bool DirectX11
{
get { return GPU; }
set
{
GPU = value;
}
}
/// <summary>
/// Will use Custom particles, rendered on the GPU
/// if not, it will use the Unity Particle System
/// </summary>
public bool GPU
{
get { return m_gpu; }
set
{
if (m_gpu != value)
{
m_gpu = value;
if (!m_gpu)
{
foreach (var particle in particles)
{
particle?.CleanMeshes();
}
}
if (m_generationType == GalaxyGenerationType.Automatic)
GenerateParticles();
}
}
}
/// <summary>
/// Should the Generated meshes be saved in the scene or prefab.
/// This only works with GPU particles. As they generated meshes.
/// </summary>
public bool SaveMeshes
{
get { return m_saveMeshes; }
set
{
if (m_saveMeshes == value) return;
m_saveMeshes = value;
ForceUpdateParticleComponents();
}
}
/// <summary>
/// Should the Generated Particles be saved in the scene or prefab.
/// This works with all particles, as it saves the data.
/// </summary>
public bool SaveParticles
{
get { return m_saveParticles; }
set
{
if (m_saveParticles == value) return;
m_saveParticles = value;
ForceUpdateParticleComponents();
}
}
/// <summary>
/// Should the galaxy be rendered. This disables the automatic rendering of the galaxy and does not disable the <see cref="Galaxia.Galaxy.Draw()"/> and <see cref="Galaxia.Galaxy.DrawNow()"/> methods.
/// </summary>
public bool RenderGalaxy
{
get { return m_render_galaxy; }
set
{
if (m_render_galaxy == value) return;
m_render_galaxy = value;
ForceUpdateParticleComponents();
}
}
/// <summary>
/// Enable FrustumCulling.
/// This determines if the galaxy is rendered when outside the camera's frustum. This only applies to the GPU particles and not the Unity ones.
/// </summary>
public bool FrustumCulling
{
get { return m_frustumCulling; }
set { m_frustumCulling = value; }
}
/// <summary>
/// Returns the last pre-render event
/// </summary>
public RenderEvent LastPreRenderEvent => lastPreRenderEvent;
/// <summary>
/// Checks if the currently utilized Graphics API is OpenGL instead of DirectX
/// </summary>
public static bool OpenGL => SystemInfo.graphicsDeviceVersion.Contains("OpenGL");
/// <summary>
/// Checks if the DirectX is the current Graphics API and if so then is it >= than DX11
/// </summary>
public static bool SupportsDirectX11 => !OpenGL && SystemInfo.graphicsShaderLevel >= 40;
/// <summary>
/// A utility method for setting the time of all <see cref="Galaxia.Particles"/> on the <see cref="Galaxia.Galaxy"/>.
/// Used mainly for animations.
/// </summary>
/// <param name="time">The time of the particles.</param>
public void SetParticlesTime(float time)
{
foreach (var particle in particles)
{
if(particle)
particle.Time = time;
}
}
/// <summary>
/// On Pre Render Event Handler. This is called before each <see cref="Galaxia.Particles"/> is rendered.
/// If this event is used then the <see cref="Galaxia.Particles"/> won't be rendered.
/// </summary>
public RenderEventHandler OnPreRender => m_preRenderHandler ?? (m_preRenderHandler = new RenderEventHandler());
/// <summary>
/// On Post Render Event Handler. This is called after each <see cref="Galaxia.Particles"/> is rendered.
/// </summary>
public RenderEventHandler OnPostRender => m_postRenderHandler ?? (m_postRenderHandler = new RenderEventHandler());
#endregion
#region enums
/// <summary>
/// Used for specifying the Galaxy Particle Generation Type.
/// </summary>
public enum GalaxyGenerationType
{
/// <summary>
/// generates particles when the Galaxy Prefab properties change.
/// </summary>
Automatic,
/// <summary>
/// used to manually generate particles.
/// </summary>
Manual
}
#endregion
/// <summary>
/// Galaxy Render Event
/// </summary>
public class RenderEvent
{
/// <summary>
/// Mark the render event as Used. This will make the galaxy skip it's own rendering.
/// Good for handling galaxy rendering elsewhere
/// </summary>
public bool Used;
public RenderEvent(bool used)
{
Used = used;
}
}
/// <summary>
/// Render Event Implementation
/// </summary>
public class RenderEventHandler : UnityEvent<Particles, RenderEvent>
{
}
}
}