-
Notifications
You must be signed in to change notification settings - Fork 115
/
PlanetaryParticle.cs
executable file
·197 lines (178 loc) · 7.77 KB
/
PlanetaryParticle.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
/**
* Kopernicus Planetary System Modifier
* -------------------------------------------------------------
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* This library is intended to be used as a plugin for Kerbal Space Program
* which is copyright 2011-2017 Squad. Your usage of Kerbal Space Program
* itself is governed by the terms of its EULA, not the license above.
*
* https://kerbalspaceprogram.com
*/
#pragma warning disable CS0618 // Disable warnings about the deprecated particle system
using System;
using System.IO;
using System.Linq;
using UnityEngine;
namespace Kopernicus
{
namespace Components
{
/// <summary>
/// Class to create a particle effect on a planet
/// </summary>
public class PlanetParticleEmitter : MonoBehaviour
{
// Components
public ParticleEmitter emitter;
public ParticleAnimator animator;
public ParticleRenderer renderer;
public MeshFilter filter;
// Variables
public String target = "None";
public Single speedScale = 0f;
public Single minEmission, maxEmission;
public Single minEnergy, maxEnergy;
public Single minSize, maxSize;
public Single sizeGrow;
public Color[] colorAnimation;
public Texture2D mainTexture;
public Vector3 randomVelocity;
public Vector3 scale = Vector3.one;
public Mesh mesh;
public Boolean collideable;
public Vector3 force = Vector3.zero;
/// <summary>
/// Attaches a Planet Particle Emitter to a host, that has a meshfilter attached
/// </summary>
public static PlanetParticleEmitter Create(GameObject host)
{
// Create the GameObject
GameObject emitter = GetWorldParticleCollider();
emitter.transform.parent = host.transform;
emitter.transform.localPosition = Vector3.zero;
emitter.SetLayerRecursive(10);
emitter.name = "Particles";
// Add the Particle Emitter
return emitter.AddComponent<PlanetParticleEmitter>();
}
/// <summary>
/// The main initialisation. Here we create the subcomponents.
/// </summary>
void Awake()
{
if (!GetComponent<ParticleEmitter>())
{
emitter = gameObject.AddComponent<MeshParticleEmitter>();
emitter.useWorldSpace = false;
emitter.emit = true;
}
else
{
emitter = GetComponent<ParticleEmitter>();
}
if (!GetComponent<ParticleAnimator>())
{
animator = gameObject.AddComponent<ParticleAnimator>();
animator.doesAnimateColor = true;
}
else
{
animator = GetComponent<ParticleAnimator>();
}
if (!GetComponent<ParticleRenderer>())
{
renderer = gameObject.AddComponent<ParticleRenderer>();
renderer.material = new Material(Shader.Find("Particles/Alpha Blended"));
}
else
{
renderer = GetComponent<ParticleRenderer>();
}
filter = !GetComponent<MeshFilter>() ? gameObject.AddComponent<MeshFilter>() : GetComponent<MeshFilter>();
}
/// <summary>
/// The position of our target
/// </summary>
public Transform targetTransform { get; set; }
/// <summary>
/// Updates the target position and emits the particles
/// </summary>
void Update()
{
// Update the values
emitter.minSize = minSize;
emitter.maxSize = maxSize;
animator.sizeGrow = sizeGrow;
animator.colorAnimation = colorAnimation;
renderer.material.mainTexture = mainTexture;
filter.mesh = filter.sharedMesh = mesh ?? transform.parent.GetComponent<MeshFilter>().sharedMesh;
animator.force = force;
// We have a target
if (target != "None")
{
if (targetTransform == null) targetTransform = PSystemManager.Instance.localBodies.Find(b => b.transform.name == target).scaledBody.transform;
Vector3 speed = targetTransform.position;
speed -= transform.parent.position;
speed *= speedScale;
emitter.minEnergy = minEnergy / TimeWarp.CurrentRate;
emitter.maxEnergy = maxEnergy / TimeWarp.CurrentRate;
emitter.maxEmission = maxEmission * TimeWarp.CurrentRate;
emitter.minEmission = minEmission * TimeWarp.CurrentRate;
emitter.rndVelocity = randomVelocity * TimeWarp.CurrentRate;
speed *= TimeWarp.CurrentRate;
emitter.worldVelocity = speed;
}
transform.localScale = scale;
}
/// <summary>
/// Detect Particle collisions
/// </summary>
void OnParticleCollision(GameObject other)
{
// If we dont want collisions, abort
if (!collideable)
return;
// Don't collide with the planet
if (other == transform.parent.gameObject)
return;
// We need a rigidbody
if (!other.GetComponent<Rigidbody>())
return;
// Do funny things
Particle partice = emitter.particles.OrderBy(p => Vector3.Distance(other.transform.position, p.position)).First();
other.GetComponent<Rigidbody>().AddForceAtPosition(partice.velocity.normalized * partice.energy, partice.position, ForceMode.Impulse);
partice.energy = 0;
// Events
Events.OnParticleCollision.Fire(this, other);
}
/// <summary>
/// Returns a copy of the world particle collider prefab
/// </summary>
public static GameObject GetWorldParticleCollider()
{
Stream stream = typeof(PlanetParticleEmitter).Assembly.GetManifestResourceStream("Kopernicus.Components.Assets.WorldParticleCollider.unity3d");
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (Int32)stream.Length);
AssetBundle bundle = AssetBundle.LoadFromMemory(buffer);
GameObject collider = Instantiate(bundle.LoadAsset("WorldParticleCollider", typeof(GameObject))) as GameObject;
bundle.Unload(true);
return collider;
}
}
}
}
#pragma warning restore CS0618