Recent

Author Topic: Metaballs: Let's get together(raylib)  (Read 258 times)

Guva

  • Full Member
  • ***
  • Posts: 173
  • 🌈 ZX-Spectrum !!!
Metaballs: Let's get together(raylib)
« on: April 24, 2025, 05:11:13 am »
Metaballs: Let's get together !!!
Conversion shaders from https://www.shadertoy.com/view/4lKXzd

metaballs.vs
Code: C  [Select][+][-]
  1. #version 330
  2.  
  3. // Входные атрибуты
  4. layout(location = 0) in vec3 vertexPosition;
  5. layout(location = 1) in vec2 vertexTexCoord;
  6. layout(location = 2) in vec3 vertexNormal;
  7. layout(location = 3) in vec4 vertexColor;
  8.  
  9. // Выходные переменные
  10. out vec2 fragCoord;
  11.  
  12. // Uniform-переменные
  13. uniform mat4 mvp;
  14.  
  15. void main()
  16. {
  17.     fragCoord = vertexPosition.xy;
  18.     gl_Position = mvp * vec4(vertexPosition, 1.0);
  19. }
  20.  

metaballs.fs
Code: C  [Select][+][-]
  1. #version 330
  2.  
  3. // Входные переменные из вершинного шейдера
  4. in vec2 fragCoord;
  5. out vec4 fragColor;
  6.  
  7. // Uniform-переменные
  8. uniform float time;
  9. uniform vec2 resolution;
  10.  
  11. const float TWO_PI = 6.28318530718;
  12. const float vertices = 8.;
  13. const float startIndex = vertices;
  14. const float endIndex = vertices * 2.;
  15.  
  16. float metaballs(vec2 uv, float time) {
  17.     float timeOsc = sin(time);
  18.     float size = 0.5;
  19.     float radSegment = TWO_PI / vertices;
  20.     for(float i = startIndex; i < endIndex; i++) {
  21.         float rads = i * radSegment;
  22.         float radius = 1. + 1.5 * sin(time + rads * 1.);
  23.         vec2 ctrlPoint = radius * vec2(sin(rads), cos(rads));
  24.         size += 1. / pow(i, distance(uv, ctrlPoint));
  25.     }
  26.     return size;
  27. }
  28.  
  29. void main() {
  30.     vec2 uv = (2. * fragCoord - resolution.xy) / resolution.y;
  31.     uv *= 3.;
  32.     float col = metaballs(uv, time);
  33.     col = smoothstep(0., fwidth(col)*1.5, col - 1.);
  34.     fragColor = vec4(1. - sqrt(vec3(col)), 1.);
  35. }
  36.  

MetaballsExample
Code: Pascal  [Select][+][-]
  1. program MetaballsExample;
  2.  
  3. uses
  4.   raylib, math;
  5.  
  6. const
  7.   ScreenWidth = 800;
  8.   ScreenHeight = 600;
  9.  
  10. var
  11.   Shader: TShader;
  12.   TimeLoc: Integer;
  13.   ResLoc: Integer;
  14.   Time: Single;
  15.   Resolution: array[0..1] of Single; // Массив для передачи разрешения
  16.  
  17. begin
  18.   // Инициализация окна
  19.   InitWindow(ScreenWidth, ScreenHeight, 'Metaballs Example with Raylib');
  20.   SetTargetFPS(60);
  21.  
  22.   // Загрузка шейдера
  23.   Shader := LoadShader('metaballs.vs', 'metaballs.fs');
  24.  
  25.   // Получение location uniform-переменных
  26.   TimeLoc := GetShaderLocation(Shader, 'time');
  27.   ResLoc := GetShaderLocation(Shader, 'resolution');
  28.  
  29.   // Установка разрешения
  30.   Resolution[0] := ScreenWidth;
  31.   Resolution[1] := ScreenHeight;
  32.   SetShaderValue(Shader, ResLoc, @Resolution, SHADER_UNIFORM_VEC2);
  33.  
  34.   while not WindowShouldClose do
  35.   begin
  36.     // Обновление времени
  37.     Time := GetTime();
  38.     SetShaderValue(Shader, TimeLoc, @Time, SHADER_UNIFORM_FLOAT);
  39.  
  40.     // Отрисовка
  41.     BeginDrawing();
  42.       ClearBackground(BLACK);
  43.       BeginShaderMode(Shader);
  44.         // Рисуем прямоугольник на весь экран для применения шейдера
  45.         DrawRectangle(0, 0, ScreenWidth, ScreenHeight, WHITE);
  46.       EndShaderMode();
  47.  
  48.       // Вывод FPS
  49.       DrawFPS(10, 10);
  50.     EndDrawing();
  51.   end;
  52.  
  53.   // Очистка
  54.   UnloadShader(Shader);
  55.   CloseWindow();
  56. end.
  57.  

 

TinyPortal © 2005-2018