├── README.md ├── LICENSE └── ParticlePlexus.cs /README.md: -------------------------------------------------------------------------------- 1 | # Particle Plexus Tutorial 2 | Script created during the particle plexus tutorial (https://youtu.be/ruNPkuYT1Ck). 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Mirza Beig 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ParticlePlexus.cs: -------------------------------------------------------------------------------- 1 | 2 | using UnityEngine; 3 | using System.Collections.Generic; 4 | 5 | [RequireComponent(typeof(ParticleSystem))] 6 | public class ParticlePlexus : MonoBehaviour 7 | { 8 | public float maxDistance = 1.0f; 9 | 10 | public int maxConnections = 5; 11 | public int maxLineRenderers = 100; 12 | 13 | new ParticleSystem particleSystem; 14 | ParticleSystem.Particle[] particles; 15 | 16 | ParticleSystem.MainModule particleSystemMainModule; 17 | 18 | public LineRenderer lineRendererTemplate; 19 | List lineRenderers = new List(); 20 | 21 | Transform _transform; 22 | 23 | void Start() 24 | { 25 | particleSystem = GetComponent(); 26 | particleSystemMainModule = particleSystem.main; 27 | } 28 | 29 | void LateUpdate() 30 | { 31 | int maxParticles = particleSystemMainModule.maxParticles; 32 | 33 | if (particles == null || particles.Length < maxParticles) 34 | { 35 | particles = new ParticleSystem.Particle[maxParticles]; 36 | } 37 | 38 | int lrIndex = 0; 39 | int lineRendererCount = lineRenderers.Count; 40 | 41 | if (lineRendererCount > maxLineRenderers) 42 | { 43 | for (int i = maxLineRenderers; i < lineRendererCount; i++) 44 | { 45 | Destroy(lineRenderers[i].gameObject); 46 | } 47 | 48 | int removedCount = lineRendererCount - maxLineRenderers; 49 | lineRenderers.RemoveRange(maxLineRenderers, removedCount); 50 | 51 | lineRendererCount -= removedCount; 52 | } 53 | 54 | if (maxConnections > 0 && maxLineRenderers > 0) 55 | { 56 | particleSystem.GetParticles(particles); 57 | int particleCount = particleSystem.particleCount; 58 | 59 | float maxDistanceSqr = maxDistance * maxDistance; 60 | 61 | ParticleSystemSimulationSpace simulationSpace = particleSystemMainModule.simulationSpace; 62 | 63 | switch (simulationSpace) 64 | { 65 | case ParticleSystemSimulationSpace.Local: 66 | { 67 | _transform = transform; 68 | break; 69 | } 70 | case ParticleSystemSimulationSpace.Custom: 71 | { 72 | _transform = particleSystemMainModule.customSimulationSpace; 73 | break; 74 | } 75 | case ParticleSystemSimulationSpace.World: 76 | { 77 | _transform = transform; 78 | break; 79 | } 80 | default: 81 | { 82 | throw new System.NotSupportedException( 83 | 84 | string.Format("Unsupported simulation space '{0}'.", 85 | System.Enum.GetName(typeof(ParticleSystemSimulationSpace), particleSystemMainModule.simulationSpace))); 86 | } 87 | } 88 | 89 | for (int i = 0; i < particleCount; i++) 90 | { 91 | if (lrIndex == maxLineRenderers) 92 | { 93 | break; 94 | } 95 | 96 | Vector3 p1_position = particles[i].position; 97 | 98 | int connections = 0; 99 | 100 | for (int j = i + 1; j < particleCount; j++) 101 | { 102 | Vector3 p2_position = particles[j].position; 103 | float distanceSqr = Vector3.SqrMagnitude(p1_position - p2_position); 104 | 105 | if (distanceSqr <= maxDistanceSqr) 106 | { 107 | LineRenderer lr; 108 | 109 | if (lrIndex == lineRendererCount) 110 | { 111 | lr = Instantiate(lineRendererTemplate, _transform, false); 112 | lineRenderers.Add(lr); 113 | 114 | lineRendererCount++; 115 | } 116 | 117 | lr = lineRenderers[lrIndex]; 118 | 119 | lr.enabled = true; 120 | lr.useWorldSpace = simulationSpace == ParticleSystemSimulationSpace.World ? true : false; 121 | 122 | lr.SetPosition(0, p1_position); 123 | lr.SetPosition(1, p2_position); 124 | 125 | lrIndex++; 126 | connections++; 127 | 128 | if (connections == maxConnections || lrIndex == maxLineRenderers) 129 | { 130 | break; 131 | } 132 | } 133 | } 134 | } 135 | } 136 | 137 | for (int i = lrIndex; i < lineRendererCount; i++) 138 | { 139 | lineRenderers[i].enabled = false; 140 | } 141 | } 142 | } 143 | --------------------------------------------------------------------------------