Guest User

Untitled

a guest
Aug 10th, 2023
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. struct SceneUniforms {
  2. perspective: mat4x4<f32>,
  3. view: mat4x4<f32>,
  4. inverse_perspective_view: mat4x4<f32>,
  5. camera_pos: vec4<f32>
  6. }
  7.  
  8. @group(0) @binding(0) var<uniform> scene: SceneUniforms;
  9.  
  10. @group(1) @binding(0) var depth_t: texture_depth_2d;
  11. @group(1) @binding(1) var normal_t: texture_2d<f32>;
  12. @group(1) @binding(2) var sample_kernel_t: texture_2d<f32>;
  13. @group(1) @binding(3) var random_noise_t: texture_2d<f32>;
  14.  
  15. @vertex
  16. fn vs_main(@builtin(vertex_index) index: u32) -> @builtin(position) vec4<f32> {
  17. var vertex_positions = array<vec2<f32>, 6>(
  18. vec2<f32>(-1.0, -1.0),
  19. vec2<f32>(1.0, 1.0),
  20. vec2<f32>(-1.0, 1.0),
  21. vec2<f32>(-1.0, -1.0),
  22. vec2<f32>(1.0, -1.0),
  23. vec2<f32>(1.0, 1.0)
  24. );
  25.  
  26. return vec4<f32>(vertex_positions[index], 0.0, 1.0);
  27. }
  28.  
  29. fn framebuffer_to_uv(texcoord: vec2<f32>) -> vec2<f32> {
  30. return texcoord / vec2<f32>(textureDimensions(depth_t));
  31. }
  32.  
  33. fn vs_position_from_depth(texcoord: vec2<f32>) -> vec3<f32> {
  34. let snapped_texcoords = vec2<u32>(texcoord);
  35. let uv = framebuffer_to_uv(texcoord);
  36. let z = textureLoad(depth_t, snapped_texcoords, 0);
  37. let x = uv.x * 2.0 - 1.0;
  38. let y = (1.0 - uv.y) * 2.0 - 1.0;
  39.  
  40. let projected_pos = vec4<f32>(x, y, z, 1.0);
  41. let vs_pos = scene.view * scene.inverse_perspective_view * projected_pos;
  42.  
  43. return vs_pos.xyz / vs_pos.www;
  44. }
  45.  
  46. fn texcoord_wrap(texcoord: vec2<f32>, scale: vec2<f32>) -> vec2<u32> {
  47. return vec2<u32>(texcoord % scale);
  48. }
  49.  
  50. @fragment
  51. fn fs_main(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> {
  52. let radius = 1.0;
  53.  
  54. let origin = vs_position_from_depth(position.xy);
  55. var normal = (scene.view * vec4<f32>(textureLoad(normal_t, vec2<u32>(position.xy), 0).xyz * 2.0 - 1.0, 1.0)).xyz;
  56. normal = normalize(normal);
  57.  
  58. let rotation = textureLoad(random_noise_t, texcoord_wrap(position.xy, vec2<f32>(4.0, 4.0)), 0).xyz * 2.0 - 1.0;
  59. let tangent = normalize(rotation - normal * dot(rotation, normal));
  60. let bitangent = cross(normal, tangent);
  61. let tbn = mat3x3<f32>(tangent, bitangent, normal);
  62.  
  63. var occlusion = 0.0;
  64.  
  65. for (var x = 0.0; x < 4.0; x += 1.0) {
  66. for (var y = 0.0; y < 4.0; y += 1.0) {
  67. var sample = textureLoad(sample_kernel_t, vec2<u32>(u32(x), u32(y)), 0).xyz;
  68. sample = tbn * sample;
  69. sample = sample * radius + origin;
  70.  
  71. var offset = vec4<f32>(sample, 1.0);
  72. offset = scene.perspective * offset;
  73. offset.x = (offset.x / offset.w) * 0.5 + 0.5;
  74. offset.y = (offset.y / offset.w) * 0.5 + 0.5;
  75.  
  76. var sample_depth = textureLoad(depth_t, vec2<u32>(offset.xy )* textureDimensions(depth_t), 0);
  77.  
  78. var range_check = select(1.0, 0.0, abs(origin.z - sample_depth) < radius);
  79. occlusion += (select(1.0, 0.0, sample_depth <= sample.z)) * range_check;
  80. }
  81. }
  82.  
  83. occlusion = 1.0 - (occlusion / 16.0);
  84. return vec4<f32>(occlusion, 0.0, 0.0, 1.0);
  85. }
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment