Advertisement
samuelmq

Godot 4 Shader Functions

Oct 19th, 2022 (edited)
797
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //#define TAU 6.283185307179586
  2. float circle(vec2 uv, float radius, float feather)
  3. {
  4.     return smoothstep(radius, radius + feather, length(uv - vec2(0.5)));
  5. }
  6.  
  7. float rectangle (vec2 uv, float width, float height){
  8.     vec2 uv_cartesian = uv * 2.0 - 1.0;
  9.     vec2 uv_reflected = abs(uv_cartesian);
  10.     float dfx = step(width,uv_reflected.x);
  11.     float dfy = step(height, uv_reflected.y);
  12.     return max(dfx,dfy);
  13. }
  14. float polygon(vec2 uv, float width, int sides)
  15. {
  16.     uv = uv * 2.0 - 1.0;
  17.     float angle = atan(uv.x, uv.y);
  18.     float radius = TAU / float(sides); 
  19.     float dist = cos(floor(0.5 + angle / radius) * radius - angle)* length(uv) ;
  20.     float poly = step(width, dist);
  21.     return poly;
  22. }
  23. vec2 polar_coordinates(vec2 uv, vec2 center)
  24. {
  25.     vec2 dir = uv - center;
  26.     float radius = length(dir) * 2.0;
  27.     float angle = atan(dir.y, dir.x) / TAU;
  28.     return (vec2(radius, angle));
  29. }
  30. vec2 rotation ( vec2 uv, vec2 center, float angle){
  31.     mat2 rotation = mat2(
  32.                     vec2(cos(angle), -sin(angle)),
  33.                     vec2(sin(angle), cos(angle))
  34.                     );
  35.     uv -= center;
  36.     uv *= rotation;
  37.     uv += center;
  38.     return uv;               
  39. }
  40. vec2 scale(vec2 uv, float x, float y)
  41. {
  42.     mat2 scale = mat2(
  43.                 vec2(x, 0.0),
  44.                 vec2(0.0, y)
  45.                 );
  46.    
  47.     uv -= 0.5;
  48.     uv = uv * scale;
  49.     uv += 0.5;
  50.     return uv;
  51. }
  52. float angleVectors(vec3 v1, vec3 v2){
  53.     return acos(dot(normalize(v1),normalize(v2)));
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement