@@ -3,34 +3,11 @@ import super::super::util::{
3
3
decodeRGBA,
4
4
};
5
5
import super::util::{
6
- create_ray,
7
- get_triangle_intersection_mt,
8
- get_interpolated_color,
9
- get_sky_color,
10
- random_in_hemisphere,
11
- get_triangle_data,
12
- get_vertex,
13
- get_material,
14
- Ray,
15
- HitInfo,
16
- SceneParams,
17
- Material,
18
- Vertex,
19
- Triangle,
6
+ sample_radiance_with_lighting,
20
7
MAX_BOUNCES,
21
8
MAX_LOW_FRAME_BOUNCES,
22
9
LOW_BOUNCE_FRAMES,
23
10
CLEAR_COLOR,
24
- FLT_MAX,
25
- SUN_INTENSITY,
26
- materials,
27
- material_stride,
28
- vertices,
29
- vertex_stride,
30
- vertex_normal_offset,
31
- vertex_material_offset,
32
- indices,
33
- sun_direction,
34
11
};
35
12
36
13
@group(3) @binding(0) @if(wasm) var result: texture_storage_2d<r32float, read_write>;
@@ -41,39 +18,16 @@ import super::util::{
41
18
42
19
43
20
44
- fn trace_triangles(ray: Ray) -> HitInfo {
45
- let num_triangles = u32(arrayLength(&indices) / 3u);
46
21
47
- var nearest_hit_info: HitInfo;
48
- nearest_hit_info.did_hit = false;
49
- nearest_hit_info.t = FLT_MAX;
50
22
51
- for (var i: u32 = 0u; i < num_triangles; i = i + 1u) {
52
- let triangle: Triangle = get_triangle_data(i);
53
- let hit_info: HitInfo = get_triangle_intersection_mt(triangle, ray);
54
-
55
- if hit_info.did_hit {
56
- if hit_info.t < nearest_hit_info.t {
57
- nearest_hit_info = hit_info;
58
- nearest_hit_info.i = i;
59
- }
60
- }
61
- }
62
-
63
- return nearest_hit_info;
23
+ fn get_camera_position(uv: vec2f) -> vec3f {
24
+ return (camera_to_world * vec4f(0.0, 0.0, 0.0, 1.0)).xyz;
64
25
}
65
26
66
- fn get_interpolated_color_from_hit(hit_info: HitInfo) -> vec4f {
67
- let triangle: Triangle = get_triangle_data(hit_info.i);
68
- return vec4f(get_interpolated_color(hit_info, triangle), 1.0);
69
- }
70
-
71
- fn create_camera_ray(uv: vec2f) -> Ray {
72
- let origin: vec3f = (camera_to_world * vec4f(0.0, 0.0, 0.0, 1.0)).xyz;
27
+ fn get_camera_direction(uv: vec2f) -> vec3f {
73
28
var direction = (camera_inverse_projection * vec4f(uv, 0.0, 1.0)).xyz;
74
29
direction = (camera_to_world * vec4f(direction, 0.0)).xyz;
75
- direction = normalize(direction);
76
- return create_ray(origin, direction);
30
+ return normalize(direction);
77
31
}
78
32
79
33
@compute
@@ -103,9 +57,6 @@ fn main(@builtin(global_invocation_id) id: vec3u) {
103
57
// Reference: https://github.com/gfx-rs/wgpu?tab=readme-ov-file#coordinate-systems
104
58
let uv = ((vec2f(id.xy) + 0.5) / dims * 2.0 - 1.0) * vec2f(1.0, -1.0);
105
59
106
- // Get a ray for the UVs
107
- var ray: Ray = create_camera_ray(uv);
108
-
109
60
// Load the previous frame's color
110
61
var prev_color: vec4f;
111
62
@if(wasm) {
@@ -114,55 +65,15 @@ fn main(@builtin(global_invocation_id) id: vec3u) {
114
65
prev_color = textureLoad(result, coords);
115
66
}
116
67
117
- let scene_params = SceneParams(
118
- material_stride,
119
- vertex_stride,
120
- vertex_normal_offset,
121
- vertex_material_offset,
122
- sun_direction,
123
- );
124
-
125
- // Trace the ray against the triangles
126
- var ray_color: vec4f = vec4f(0.0);
127
- var ray_throughput: vec4f = vec4f(1.0);
128
-
68
+
129
69
var max_bounces = MAX_BOUNCES;
130
70
if frame_count < LOW_BOUNCE_FRAMES {
131
71
max_bounces = MAX_LOW_FRAME_BOUNCES;
132
72
}
133
73
134
- for (var bounce = 0u; bounce <= max_bounces; bounce += 1u) {
135
- if bounce == max_bounces {
136
- // Set ray color as black if the ray has bounced too many times
137
- ray_color = vec4f(0.0);
138
- break;
139
- }
140
-
141
- let hit_info: HitInfo = trace_triangles(ray);
142
- if hit_info.did_hit {
143
- let tri_color = get_interpolated_color_from_hit(hit_info);
144
- ray_color += tri_color * ray_throughput;
145
- ray_throughput *= tri_color;
146
-
147
- // Use frame number in random seed for temporal variation
148
- ray = create_ray(
149
- hit_info.p + hit_info.normal * 0.001, // Move the origin slightly to avoid self-intersection
150
- random_in_hemisphere(hit_info.normal, hit_info.p + vec3f(f32(frame_count) * 0.1))
151
- );
152
- } else {
153
- if bounce == 0u {
154
- // If ray misses all triangles, return the sky color
155
- ray_color = get_sky_color(ray, scene_params);
156
- } else {
157
- let ray_sun_intensity = max(0.0, dot(scene_params.sun_direction, ray.direction)) * SUN_INTENSITY;
158
- ray_color *= ray_sun_intensity * ray_throughput;
159
- }
160
-
161
- break;
162
- }
163
- }
164
-
165
- ray_color = clamp(ray_color, vec4f(0.0), vec4f(1.0));
74
+ var camera_position = get_camera_position(uv);
75
+ var camera_direction = get_camera_direction(uv);
76
+ var ray_color: vec4f = sample_radiance_with_lighting(camera_position, camera_direction, max_bounces, f32(frame_count) * 0.1);
166
77
167
78
// Blend with previous frame
168
79
let blend_factor = 1.0 / pow(f32(frame_count + 1), 1.05);
0 commit comments