LINE1 10 20 30 40 50 60 70 80 90 1001struct VertexOutput {2 @builtin(position) position: vec4<f32>,3};45struct Frame {6 dimensions: vec2<f32>,7};89@group(0) @binding(0) var<uniform> frame: Frame;1011@vertex12fn vertex_main(@builtin(vertex_index) vertex_index: u32) -> VertexOutput {13 var positions = array<vec2<f32>, 3>(14 vec2<f32>(-1.0, -1.0),15 vec2<f32>(3.0, -1.0),16 vec2<f32>(-1.0, 3.0),17 );18 var output: VertexOutput;19 output.position = vec4<f32>(positions[vertex_index], 0.0, 1.0);20 return output;21}2223struct Hit {24 distance: f32,25 normal: vec3<f32>,26 color: vec3<f32>,27};2829fn sphere_hit(origin: vec3<f32>, direction: vec3<f32>, center: vec3<f32>, radius: f32) -> f32 {30 let offset = origin - center;31 let half_b = dot(offset, direction);32 let discriminant = half_b * half_b - dot(direction, direction) * (dot(offset, offset) - radius * radius);33 if discriminant < 0.0 {34 return -1.0;35 }36 let root = sqrt(discriminant);37 let near = (-half_b - root) / dot(direction, direction);38 if near > 0.001 {39 return near;40 }41 let far = (-half_b + root) / dot(direction, direction);42 if far > 0.001 {43 return far;44 }45 return -1.0;46}4748fn scene_hit(origin: vec3<f32>, direction: vec3<f32>) -> Hit {49 var hit: Hit;50 hit.distance = 1.0e20;51 hit.normal = vec3<f32>(0.0, 0.0, 0.0);52 hit.color = vec3<f32>(0.0, 0.0, 0.0);5354 let first = sphere_hit(origin, direction, vec3<f32>(-0.78, 0.36, -1.25), 0.62);55 if first > 0.0 && first < hit.distance {56 let point = origin + direction * first;57 hit.distance = first;58 hit.normal = normalize(point - vec3<f32>(-0.78, 0.36, -1.25));59 hit.color = vec3<f32>(0.82, 0.22, 0.14);60 }6162 let second = sphere_hit(origin, direction, vec3<f32>(0.72, 0.23, -0.72), 0.48);63 if second > 0.0 && second < hit.distance {64 let point = origin + direction * second;65 hit.distance = second;66 hit.normal = normalize(point - vec3<f32>(0.72, 0.23, -0.72));67 hit.color = vec3<f32>(0.12, 0.48, 0.82);68 }6970 if direction.y < -0.0001 {71 let floor_distance = (-0.28 - origin.y) / direction.y;72 if floor_distance > 0.001 && floor_distance < hit.distance {73 let point = origin + direction * floor_distance;74 let checker = (i32(floor(point.x)) + i32(floor(point.z))) % 2;75 hit.distance = floor_distance;76 hit.normal = vec3<f32>(0.0, 1.0, 0.0);77 if checker == 0 {78 hit.color = vec3<f32>(0.72, 0.68, 0.57);79 } else {80 hit.color = vec3<f32>(0.19, 0.22, 0.25);81 }82 }83 }84 return hit;85}8687fn trace(origin: vec3<f32>, direction: vec3<f32>) -> vec3<f32> {88 let hit = scene_hit(origin, direction);89 if hit.distance > 1.0e19 {90 let blend = 0.5 * (normalize(direction).y + 1.0);91 return mix(vec3<f32>(0.12, 0.16, 0.23), vec3<f32>(0.52, 0.72, 0.91), clamp(blend, 0.0, 1.0));92 }9394 let point = origin + direction * hit.distance;95 let light_direction = normalize(vec3<f32>(-0.48, 0.83, 0.31));96 let shadow = scene_hit(point + hit.normal * 0.003, light_direction);97 let is_shadowed = shadow.distance < 1.0e19;98 let diffuse = max(dot(hit.normal, light_direction), 0.0);99 let lighting = select(0.18 + diffuse * 0.82, 0.18, is_shadowed);100 return hit.color * lighting + vec3<f32>(0.035, 0.04, 0.05);101}102103fn sample_offset(index: u32) -> vec2<f32> {104 switch index % 4u {105 case 0u: { return vec2<f32>(0.25, 0.25); }106 case 1u: { return vec2<f32>(0.75, 0.25); }107 case 2u: { return vec2<f32>(0.25, 0.75); }108 default: { return vec2<f32>(0.75, 0.75); }109 }110}111112@fragment113fn fragment_main(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> {114 let dimensions = frame.dimensions;115 let pixel = position.xy;116 let aspect = dimensions.x / dimensions.y;117 let camera = vec3<f32>(0.0, 0.92, 4.2);118 var color = vec3<f32>(0.0, 0.0, 0.0);119 for (var index = 0u; index < 4u; index += 1u) {120 let sample_position = pixel + sample_offset(index);121 let screen_x = (2.0 * sample_position.x / dimensions.x - 1.0) * aspect;122 let screen_y = 1.0 - 2.0 * sample_position.y / dimensions.y;123 let direction = normalize(vec3<f32>(screen_x * 0.78, screen_y * 0.78 - 0.12, -1.35));124 color += trace(camera, direction);125 }126 color *= 0.25;127 return vec4<f32>(clamp(color, vec3<f32>(0.0), vec3<f32>(1.0)), 1.0);128}