4 ms·
Excuse me if this is a newbie question, but > In sample(x, y), we only care about the ray of light that passes through (x, y, camera_z). So why bother with all
by kccqzy 4y ago
Excuse me if this is a newbie question, but
> In sample(x, y), we only care about the ray of light that passes through (x, y, camera_z). So why bother with all the other rays? We'll shoot the ray backwards! We'll start it at (x, y, camera_z), and at each step, query the SDF to get the distance from the ray's current point to the scene (in any direction). If the distance is less than some threshold, we hit the scene! Otherwise, we can safely "march" the ray forward by whatever distance is returned, since we know the scene is at least that distance away in the forward direction.
Doesn't this assume all the rays are only in the z direction? We are assuming the light source is directly in front of the camera? The code actually only increments z so it's basically only capable of determining occlusion, right?
- laurentlb 4y agoFrom what I see, it's a simplification: rays start from (x, y, -10) and follow the z axis. This is very limited and I guess you'll lose the perspective. Exercise for the reader: - Update the sample function, pass the ray direction as argument. - Compute the direction for each ray. Rays should have a slightly different direction, based on the camera field of view. When you move the camera back or increase the field of view, the donut should get smaller. Next exercise: move the camera around the donut (but still looking at it).
- d110af5ccf 4y agoIt's an orthographic projection. Ever play an isometric game? https://en.wikipedia.org/wiki/Orthographic_projection#/media/File:Various_projections_of_cube_above_plane.svg https://en.wikipedia.org/wiki/Orthographic_projection#/media...
- ww520 4y agoThe blog's explanation with sun and light ray is confusing. Ray marching is for finding the interception of the object surface and the camera’s viewing direction. Whether there’s a sun or light is irrelevant. The ray and the light source are two different things. The ray is from the camera to a point in the space. The light source is at a different location; it’s for computing light reflection on the object. It's kind of weird and confusing to increment z only. It should increment the ray. E.g. camera_pos = vec3(0.0, 0.0, -5.0) // eye is -5.0 in front of the screen screen_pos = vec3(x, y, 0.0) // screen plane lays at 0.0 on z-axis. ray_direction = normalize(screen_pos - camera_pos) For ray marching along a ray, each step extends the ray by a certain amount. marched_distance = 0.0 for 0 to N ray_end = camera_pos + marched_distance * ray_direction ray_end_to_object_distance = sdf_sphere(ray_end) if (ray_end_to_object_distance < 0.0001) return ray_end // ray_end hit the object surface marched_distance += ray_end_to_object_distance return null // ray never met the object after N steps. The object's reflection at the returned ray_end point can be computed using the surface normal at the point, the light source position, and the camera_pos.