PixelChipCode

Flow Field Particle System (GML)

Oct 6th, 2024 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. By @pixelchipcode
  2.  
  3. Flow Field Particle System (GML)
  4.  
  5. Creates a particle system that moves according to a dynamic flow field. Each particle is influenced by perlin-like 3D noise, creating swirling, organic patterns. Here's a breakdown of what each part does:
  6.  
  7. Create Event:
  8. Initializes particle_count particles with randomized properties like position, speed, direction, color, and size.
  9. Sets up a flow field grid that influences particle movement.
  10. Uses a custom simple_noise_3d function for dynamic noise to simulate complex flow patterns.
  11.  
  12. Step Event:
  13. Updates the flow field with changing noise over time, making the movement dynamic and ever-changing.
  14. Updates each particle's direction based on the flow field and smoothly transitions to new positions.
  15. Gradually changes each particle's hue over time for a continuous, colorful effect.
  16.  
  17. Draw Event:
  18. Draws all particles onto a surface with a semi-transparent black rectangle to create a fading trail effect.
  19. The result is a glowing, dynamic flow of particles that are influenced by a constantly shifting flow field.
  20. Feel free to tweak the particle_count, flow_field_resolution, and color properties to see how different settings change the visuals. This setup is great for learning about flow fields, noise, and creating visually engaging effects.
  21.  
  22. = = = = =
  23.  
  24. /// Create Event
  25. function simple_noise_3d(x, y, z) {
  26. return (sin(x * 10 + noise_seed) * sin(y * 10 + noise_seed) * sin(z * 10 + noise_seed)) * 0.5 + 0.5;
  27. }
  28.  
  29. particle_count = 10000;
  30. particles = array_create(particle_count);
  31. flow_field_resolution = 22;
  32. flow_field_width = ceil(room_width / flow_field_resolution);
  33. flow_field_height = ceil(room_height / flow_field_resolution);
  34. flow_field = array_create(flow_field_width * flow_field_height);
  35. for (var i = 0; i < particle_count; i++) {
  36. particles[i] = {
  37. x: random(room_width),
  38. y: random(room_height),
  39. speed: random_range(0.5, 2),
  40. direction: random(360),
  41. color: make_color_hsv(255, 200, 255),
  42. size: random_range(0.3, 1)
  43. };
  44. }
  45. time = 0;
  46. time_speed = 0.002;
  47. surface = -1;
  48. noise_seed = random(10000);
  49.  
  50. = = = = =
  51.  
  52. /// Step Event
  53. time += time_speed;
  54. // Update flow field
  55. for (var i = 0; i < array_length(flow_field); i++) {
  56. flow_field[i] = simple_noise_3d((i % flow_field_width) * 0.1, (i div flow_field_width) * 0.1, time) * 360;
  57. }
  58. // Update particles
  59. for (var i = 0; i < particle_count; i++) {
  60. var p = particles[i];
  61. var index = floor(p.x / flow_field_resolution) + floor(p.y / flow_field_resolution) * flow_field_width;
  62.  
  63. // Update particle direction and position
  64. p.direction = lerp(p.direction, flow_field[index], 0.1);
  65. p.x = (p.x + lengthdir_x(p.speed, p.direction) + room_width) % room_width;
  66. p.y = (p.y + lengthdir_y(p.speed, p.direction) + room_height) % room_height;
  67.  
  68. // Slowly change particle color over time
  69. var hue = color_get_hue(p.color);
  70. hue = (hue + 0.1) % 255;
  71. p.color = make_color_hsv(hue, 200, 255);
  72. }
  73.  
  74. = = = = =
  75.  
  76. /// Draw Event
  77. if (!surface_exists(surface)) {
  78. surface = surface_create(room_width, room_height);
  79. }
  80. surface_set_target(surface);
  81. // Draw a semi-transparent black rectangle to create trail effect
  82. draw_set_alpha(0.1);
  83. draw_rectangle_color(0, 0, room_width, room_height, c_black, c_black, c_black, c_black, false);
  84. draw_set_alpha(1);
  85. gpu_set_blendmode(bm_add);
  86. for (var i = 0; i < particle_count; i++) {
  87. var p = particles[i];
  88. draw_circle_color(p.x, p.y, p.size, p.color, p.color, false);
  89. }
  90. gpu_set_blendmode(bm_normal);
  91. surface_reset_target();
  92. draw_surface(surface, 0, 0);
Advertisement
Add Comment
Please, Sign In to add comment