weapon_shader.tres 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. [gd_resource type="Shader" format=3 uid="uid://cscvr3086tglu"]
  2. [resource]
  3. code = "// NOTE: Shader automatically converted from Godot Engine 4.2.2.stable.mono's StandardMaterial3D.
  4. /*
  5. shader_type spatial;
  6. render_mode blend_mix,depth_draw_opaque,cull_disabled,diffuse_burley,specular_schlick_ggx;
  7. uniform vec4 albedo : source_color;
  8. uniform sampler2D texture_albedo : source_color,filter_nearest_mipmap,repeat_enable;
  9. uniform float point_size : hint_range(0,128);
  10. uniform float roughness : hint_range(0,1);
  11. uniform sampler2D texture_metallic : hint_default_white,filter_nearest_mipmap,repeat_enable;
  12. uniform vec4 metallic_texture_channel;
  13. uniform sampler2D texture_roughness : hint_roughness_r,filter_nearest_mipmap,repeat_enable;
  14. uniform float specular;
  15. uniform float metallic;
  16. uniform vec3 uv1_scale;
  17. uniform vec3 uv1_offset;
  18. uniform vec3 uv2_scale;
  19. uniform vec3 uv2_offset;
  20. uniform float fov = 90;
  21. void vertex() {
  22. UV=UV*uv1_scale.xy+uv1_offset.xy;
  23. float scale = 1.0 / tan(fov * 0.5 * PI / 180.0);
  24. PROJECTION_MATRIX[0][0] = scale / (VIEWPORT_SIZE.x / VIEWPORT_SIZE.y);
  25. PROJECTION_MATRIX[1][1] = -scale;
  26. POSITION = PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(VERTEX.xyz, 1.0);
  27. POSITION.z = mix(POSITION.z, 0, 0.99);
  28. }
  29. void fragment() {
  30. vec2 base_uv = UV;
  31. vec4 albedo_tex = texture(texture_albedo,base_uv);
  32. ALBEDO = albedo.rgb * albedo_tex.rgb;
  33. float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);
  34. METALLIC = metallic_tex * metallic;
  35. vec4 roughness_texture_channel = vec4(1.0,0.0,0.0,0.0);
  36. float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
  37. ROUGHNESS = roughness_tex * roughness;
  38. SPECULAR = specular;
  39. }
  40. */
  41. // NOTE: Shader automatically converted from Godot Engine 4.2.1.stable's StandardMaterial3D.
  42. shader_type spatial;
  43. render_mode blend_mix,depth_draw_opaque,cull_disabled,diffuse_burley,specular_schlick_ggx;
  44. uniform vec4 albedo : source_color = vec4(1., 1., 1., 1.);
  45. uniform sampler2D texture_albedo : source_color,filter_linear_mipmap,repeat_enable;
  46. uniform float point_size : hint_range(0,128) = 1.0;
  47. uniform float roughness : hint_range(0,1) = 1.0;
  48. uniform sampler2D texture_metallic : hint_default_white,filter_linear_mipmap,repeat_enable;
  49. uniform vec4 metallic_texture_channel = vec4(0., 0., 0., 0.);
  50. uniform sampler2D texture_roughness : hint_roughness_g,filter_linear_mipmap,repeat_enable;
  51. uniform float specular = 0.5;
  52. uniform float metallic = 1.0;
  53. uniform sampler2D texture_normal : hint_roughness_normal,filter_linear_mipmap,repeat_enable;
  54. uniform float normal_scale : hint_range(-16,16) = 1.0;
  55. uniform vec3 uv1_scale = vec3(1., 1., 1.);
  56. uniform vec3 uv1_offset = vec3(0., 0., 0.);
  57. uniform vec3 uv2_scale = vec3(1., 1., 1.);
  58. uniform vec3 uv2_offset = vec3(0., 0., 0.);
  59. uniform float viewmodel_fov = 75.0f;
  60. void vertex() {
  61. UV=UV*uv1_scale.xy+uv1_offset.xy;
  62. /* VIEW MODEL Z CLIP FIX CODE */
  63. if (viewmodel_fov > 0.0) {
  64. float onetanfov = 1.0f / tan(0.5f * (viewmodel_fov * PI / 180.0f));
  65. float aspect = VIEWPORT_SIZE.x / VIEWPORT_SIZE.y;
  66. // Optional: Modify projection matrix to change the FOV
  67. PROJECTION_MATRIX[1][1] = -onetanfov;
  68. PROJECTION_MATRIX[0][0] = onetanfov / aspect;
  69. }
  70. POSITION = PROJECTION_MATRIX * MODELVIEW_MATRIX * vec4(VERTEX.xyz, 1.0);
  71. // Mix vertex Z super close to clip plane, still maintaining some distance
  72. //POSITION.z = mix(POSITION.z, 0.0, 0.999); // Old version. Mix towards 0.0 (near z plane)
  73. // Above doesn't work for Godot 4.3 Z was reversed: https://godotengine.org/article/introducing-reverse-z/
  74. // Now we need to mix towards 1.0, but in clip space division by POSITION.w still hasn't happened.
  75. // So we mix z towards POSITION.w so that after perspective divsion, it will come out to 1.0 (near buffer since Z is reversed)
  76. POSITION.z = mix(POSITION.z, POSITION.w, 0.9); // Could be higher mix val like 0.999 but I found 0.9 prevented 99% clipping w/ less artifacts than 0.999.
  77. /* END VIEW MODEL Z CLIP FIX CODE */
  78. }
  79. // POSITION.xyz = POSITION.xyz / POSITION.w;
  80. void fragment() {
  81. // https://docs.godotengine.org/en/stable/tutorials/shaders/shader_reference/spatial_shader.html#fragment-built-ins
  82. // in vec3 VERTEX
  83. // Vertex that comes from vertex function (default, in view space).
  84. // You can also do the depth mix in fragment shader, but I found it to cause more artifacts.
  85. //vec4 clipSpace = PROJECTION_MATRIX * vec4(VERTEX, 1.0);
  86. //vec3 ndc = clipSpace.xyz / clipSpace.w;
  87. //DEPTH = mix((ndc.z + 1.0) / 2.0, 1.0, 0.9);
  88. vec2 base_uv = UV;
  89. vec4 albedo_tex = texture(texture_albedo,base_uv);
  90. ALBEDO = albedo.rgb * albedo_tex.rgb;
  91. float metallic_tex = dot(texture(texture_metallic,base_uv),metallic_texture_channel);
  92. METALLIC = metallic_tex * metallic;
  93. vec4 roughness_texture_channel = vec4(0.0,1.0,0.0,0.0);
  94. float roughness_tex = dot(texture(texture_roughness,base_uv),roughness_texture_channel);
  95. ROUGHNESS = roughness_tex * roughness;
  96. SPECULAR = specular;
  97. NORMAL_MAP = texture(texture_normal,base_uv).rgb;
  98. NORMAL_MAP_DEPTH = normal_scale;
  99. }"