Created
December 28, 2011 02:21
-
-
Save N-Carter/1525865 to your computer and use it in GitHub Desktop.
Space dust particles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class SpaceDust : MonoBehaviour | |
{ | |
public float m_Size = 1.0f; // Halfwidth of the particle volume | |
public float m_MinBrightness = 0.2f; | |
public float m_MaxBrightness = 1.0f; | |
protected ParticleEmitter m_ParticleEmitter; | |
protected Particle[] m_Particles; | |
protected int m_NumParticles; | |
protected float m_TotalWidth; | |
protected Vector3 m_PreviousPosition; | |
protected Vector3 m_Direction; | |
protected void Start() | |
{ | |
m_ParticleEmitter = particleEmitter; | |
m_NumParticles = (int)m_ParticleEmitter.maxEmission; | |
m_Particles = new Particle[m_NumParticles]; | |
m_TotalWidth = m_Size * 2.0f; | |
Vector3 transformPosition = transform.position; | |
for(int index = 0; index < m_NumParticles; ++index) | |
{ | |
Particle particle = new Particle(); | |
particle.position = transformPosition + new Vector3( | |
Random.value * m_TotalWidth - m_Size, Random.value * m_TotalWidth - m_Size, Random.value * m_TotalWidth - m_Size); | |
particle.energy = 1.0f; | |
particle.size = Random.Range(m_ParticleEmitter.minSize, m_ParticleEmitter.maxSize); | |
particle.color = Color.white * Random.Range(m_MinBrightness, m_MaxBrightness); | |
m_Particles[index] = particle; | |
} | |
} | |
protected void FixedUpdate() | |
{ | |
// This works better than Camera.velocity when the camera is being moved in FixedUpdate: | |
m_Direction = m_PreviousPosition - transform.position; | |
m_PreviousPosition = transform.position; | |
} | |
protected void LateUpdate() | |
{ | |
Vector3 transformPosition = transform.position; | |
for(int index = 0; index < m_NumParticles; ++index) | |
{ | |
Vector3 particlePosition = m_Particles[index].position; | |
Vector3 offset = transformPosition - particlePosition; | |
if(offset.x > m_Size) | |
particlePosition.x += m_TotalWidth; | |
else if(offset.x < -m_Size) | |
particlePosition.x -= m_TotalWidth; | |
if(offset.y > m_Size) | |
particlePosition.y += m_TotalWidth; | |
else if(offset.y < -m_Size) | |
particlePosition.y -= m_TotalWidth; | |
if(offset.z > m_Size) | |
particlePosition.z += m_TotalWidth; | |
else if(offset.z < -m_Size) | |
particlePosition.z -= m_TotalWidth; | |
m_Particles[index].position = particlePosition; | |
m_Particles[index].velocity = m_Direction; | |
} | |
m_ParticleEmitter.particles = m_Particles; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment