Chillzy

Untitled

Nov 6th, 2024
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.55 KB | Source Code | 0 0
  1. // Based on: https://github.com/frozein/VkGalaxy/blob/main/assets/shaders/particle_generate.comp
  2. #version 450
  3.  
  4. layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  5.  
  6. #define PI 3.14159
  7.  
  8. struct particle
  9. {
  10.     vec2 pos;
  11.     float height;
  12.     float angle;
  13.     float tilt;
  14.     float vel;
  15.     float opacity;
  16.     float temp;
  17. };
  18.  
  19. layout (std140, binding = 0) buffer stars
  20. {
  21.     particle particles[];
  22. }   particles;
  23.  
  24. layout (push_constant) uniform galaxy
  25. {
  26.     uint particle_count;
  27.     float max_rad, bulge_rad;
  28.     float angle, eccentricity;
  29.     float base_height, height;
  30.     float dust_temp;
  31.     float min_star_opacity, max_star_opacity, min_dust_opacity, max_dust_opacity;
  32.     float speed;
  33. }   galaxy_params;
  34.  
  35. // http://www.gamedev.net/topic/592001-random-number-generation-based-on-time-in-hlsl/
  36. #define RANDOM_IA 16807
  37. #define RANDOM_IM 2147483647
  38. #define RANDOM_AM 1.0 / float(RANDOM_IM)
  39. #define RANDOM_IQ 127773
  40. #define RANDOM_IR 2836
  41. #define RANDOM_MASK 123459876
  42.  
  43. int _SEED = 0;
  44.  
  45. void srand_cycle()
  46. {
  47.     _SEED ^= RANDOM_MASK;
  48.     int k = _SEED / RANDOM_IQ;
  49.     _SEED = RANDOM_IA * (_SEED - k * RANDOM_IQ) - RANDOM_IR * k;
  50.  
  51.     if (_SEED < 0)
  52.         _SEED += RANDOM_IM;
  53.  
  54.     _SEED ^= RANDOM_MASK;
  55. }
  56.  
  57. void srand_set(int seed)
  58. {
  59.     _SEED = seed;
  60.     srand_cycle();
  61. }
  62.  
  63. float rand()
  64. {
  65.     srand_cycle();
  66.     return RANDOM_AM * _SEED;
  67. }
  68.  
  69. float ease_in_exp(float x)
  70. {
  71.     return x <= 0.0 ? 0.0 : pow(2, 10.0 * x - 10.0);
  72. }
  73.  
  74. float ease_in_circ(float x)
  75. {
  76.     return x >= 1.0 ? 1.0 : 1.0 - sqrt(1.0 - x * x);
  77. }
  78.  
  79. float rand_height()
  80. {
  81.     float r = ease_in_circ(rand());
  82.     if(rand() < 0.5)
  83.         r *= -1;
  84.  
  85.     return galaxy_params.base_height + (galaxy_params.height * 0.5) * r;
  86. }
  87.  
  88. float rand_height_bulge(float rad)
  89. {
  90.     float r = ease_in_circ(rand());
  91.     if(rand() < 0.5)
  92.         r *= -1;
  93.  
  94.     float bound = (galaxy_params.height * 0.5) + (galaxy_params.height * 0.5) * cos(PI * rad / galaxy_params.bulge_rad);
  95.  
  96.     return galaxy_params.base_height + bound * r;
  97. }
  98.  
  99. float rand_height_bulge_dust(float rad)
  100. {
  101.     float r = ease_in_circ(rand());
  102.     if(rand() < 0.5)
  103.         r *= -1;
  104.  
  105.     float bound = (galaxy_params.height * 0.5) * cos(PI * rad / galaxy_params.bulge_rad);
  106.  
  107.     return galaxy_params.base_height + bound * r;
  108. }
  109.  
  110. void main(void)
  111. {
  112.     srand_set(int(gl_GlobalInvocationID.x));
  113.     particle p;
  114.     float speed = galaxy_params.speed;
  115.  
  116.     if(gl_GlobalInvocationID.x < galaxy_params.particle_count) // Star
  117.     {
  118.         float rad = ease_in_exp(rand()) * galaxy_params.max_rad;
  119.  
  120.         p.pos = vec2(rad, galaxy_params.eccentricity * rad);
  121.         p.angle = rand() * 2.0 * PI;
  122.         p.tilt = (rad / galaxy_params.max_rad) * galaxy_params.angle;
  123.         p.vel = -speed * sqrt(1.0 / rad);
  124.         p.opacity = galaxy_params.min_star_opacity + (galaxy_params.max_star_opacity - galaxy_params.min_star_opacity) * rand();
  125.  
  126.         if(rad < galaxy_params.bulge_rad)
  127.             p.height = rand_height_bulge(rad);
  128.         else
  129.             p.height = rand_height();
  130.     }
  131.     else // Dust
  132.     {
  133.         float rad;
  134.         if(gl_GlobalInvocationID.x % 2 == 0)
  135.             rad = rand() * galaxy_params.max_rad;
  136.         else
  137.             rad = ease_in_exp(rand()) * galaxy_params.max_rad;
  138.  
  139.         p.pos = vec2(rad, galaxy_params.eccentricity * rad);
  140.         p.angle = rand() * 2.0 * PI;
  141.         p.tilt = (rad / galaxy_params.max_rad) * galaxy_params.angle;
  142.         p.vel = -speed * sqrt(1.0 / rad);
  143.         p.temp = galaxy_params.dust_temp + 2.0 * rad;
  144.         p.opacity = galaxy_params.min_dust_opacity + (galaxy_params.max_dust_opacity - galaxy_params.min_dust_opacity) * rand();
  145.  
  146.         if(rad < galaxy_params.bulge_rad)
  147.             p.height = rand_height_bulge_dust(rad);
  148.         else
  149.             p.height = galaxy_params.base_height;
  150.     }
  151.  
  152.     particles.particles[gl_GlobalInvocationID.x] = p;
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment