Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4.  
  5. [ExecuteInEditMode]
  6. public class ParticlePerlinTurbulence
  7. : MonoBehaviour
  8. {
  9. private ParticleSystem system;
  10. private ParticleSystem.Particle[] particles;
  11.  
  12. [Range(0.0f, 50.0f)]
  13. public float Force = 10.0f;
  14.  
  15. [Range(0.01f, 100.0f)]
  16. public float Frequency = 4.0f;
  17.  
  18. public bool UseRandomSeed = true;
  19.  
  20. public AnimationCurve OverLifetime = AnimationCurve.Linear(0.0f, 1.0f, 1.0f, 1.0f);
  21.  
  22. public void Start()
  23. {
  24. OnScriptReload();
  25. }
  26.  
  27. public void OnScriptReload()
  28. {
  29. system = GetComponent<ParticleSystem>();
  30. particles = new ParticleSystem.Particle[1024];
  31. }
  32.  
  33. #if UNITY_EDITOR
  34. public void OnEnable()
  35. {
  36. OnScriptReload();
  37. }
  38. #endif
  39.  
  40. public void Update()
  41. {
  42. var particleCount = system.GetParticles(particles);
  43. var dt = Time.deltaTime;
  44. var seedFactor = UseRandomSeed ? 1.0f : 0.0f;
  45.  
  46. #if UNITY_EDITOR
  47. dt = 1.0f / 90.0f;
  48. #endif
  49.  
  50. for (int i = 0; i < particleCount; ++i)
  51. {
  52. var pos = particles[i].position;
  53. var force = Force;
  54. var freq = Frequency;
  55.  
  56. pos += Vector3.one * (float)(system.randomSeed % 1024) * seedFactor;
  57.  
  58. var noiseX = Mathf.PerlinNoise(pos.x * freq, pos.y * freq);
  59. var noiseY = Mathf.PerlinNoise(pos.z * freq, pos.x * freq);
  60. var noiseZ = Mathf.PerlinNoise(pos.y * freq, pos.z * freq);
  61.  
  62. noiseX = noiseX * 2.0f - 1.0f;
  63. noiseY = noiseY * 2.0f - 1.0f;
  64. noiseZ = noiseZ * 2.0f - 1.0f;
  65.  
  66. var targetVelocity = new Vector3(noiseX, noiseY, noiseZ) * force;
  67.  
  68. var t = 1.0f - particles[i].lifetime / particles[i].startLifetime;
  69. var apply = OverLifetime.Evaluate(t);
  70.  
  71. particles[i].velocity += targetVelocity * dt * apply;
  72. }
  73.  
  74. system.SetParticles(particles, particleCount);
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement