Advertisement
Zunesha

Shader para rotacionar imagem 2d como simulando 3d Godot 4

Oct 28th, 2024
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.63 KB | Gaming | 0 0
  1. Shader para rotacionar  imagem 2d como simulando 3d Godot 4:
  2.  
  3. // Hey this is Hei! This shader "fakes" a 3D-camera perspective on CanvasItems.
  4. // License: MIT
  5.  
  6. shader_type canvas_item;
  7.  
  8. // Camera FOV
  9. uniform float fov : hint_range(1, 179) = 90;
  10. uniform bool cull_back = true;
  11. uniform float y_rot : hint_range(-180, 180) = 0.0;
  12. uniform float x_rot : hint_range(-180, 180) = 0.0;
  13. // At 0, the image retains its size when unrotated.
  14. // At 1, the image is resized so that it can do a full
  15. // rotation without clipping inside its rect.
  16. uniform float inset : hint_range(0, 1) = 0.0;
  17. // Consider changing this to a uniform and changing it from code
  18.  
  19. varying flat vec2 o;
  20. varying vec3 p;
  21.  
  22.  
  23.  
  24. // Creates rotation matrix
  25. void vertex(){
  26.     float sin_b = sin(y_rot / 180.0 * PI);
  27.     float cos_b = cos(y_rot / 180.0 * PI);
  28.     float sin_c = sin(x_rot / 180.0 * PI);
  29.     float cos_c = cos(x_rot / 180.0 * PI);
  30.    
  31.     mat3 inv_rot_mat;
  32.     inv_rot_mat[0][0] = cos_b;
  33.     inv_rot_mat[0][1] = 0.0;
  34.     inv_rot_mat[0][2] = -sin_b;
  35.    
  36.     inv_rot_mat[1][0] = sin_b * sin_c;
  37.     inv_rot_mat[1][1] = cos_c;
  38.     inv_rot_mat[1][2] = cos_b * sin_c;
  39.    
  40.     inv_rot_mat[2][0] = sin_b * cos_c;
  41.     inv_rot_mat[2][1] = -sin_c;
  42.     inv_rot_mat[2][2] = cos_b * cos_c;
  43.    
  44.    
  45.     float t = tan(fov / 360.0 * PI);
  46.     p = inv_rot_mat * vec3((UV - 0.5), 0.5 / t);
  47.     float v = (0.5 / t) + 0.5;
  48.     p.xy *= v * inv_rot_mat[2].z;
  49.     o = v * inv_rot_mat[2].xy;
  50.  
  51.     VERTEX += (UV - 0.5) / TEXTURE_PIXEL_SIZE * t * (1.0 - inset);
  52. }
  53.  
  54. void fragment(){
  55.     if (cull_back && p.z <= 0.0) discard;
  56.     vec2 uv = (p.xy / p.z).xy - o;
  57.     COLOR = texture(TEXTURE, uv + 0.5);
  58.     COLOR.a *= step(max(abs(uv.x), abs(uv.y)), 0.5);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement