struct VertexInput { @location(0) position: vec3, @location(1) normal: vec3, @location(2) uv: vec2, @location(3) instance_model_0: vec4, @location(4) instance_model_1: vec4, @location(5) instance_model_2: vec4, @location(6) instance_model_3: vec4, } struct VertexOutput { @builtin(position) clip_position: vec4, @location(0) world_position: vec3, @location(1) world_normal: vec3, @location(2) light_space_position: vec4, } struct Uniforms { model: mat4x4, view: mat4x4, projection: mat4x4, light_view_projection: mat4x4, camera_position: vec3, height_scale: f32, time: f32, shadow_bias: f32, light_direction: vec3, } @group(0) @binding(0) var uniforms: Uniforms; @group(0) @binding(1) var shadow_map: texture_depth_2d; @group(0) @binding(2) var shadow_sampler: sampler_comparison; @group(0) @binding(3) var dither_texture_array: texture_2d_array; @group(0) @binding(4) var dither_sampler: sampler; @group(0) @binding(5) var flowmap_texture: texture_2d; @group(0) @binding(6) var flowmap_sampler: sampler; const PI: f32 = 3.14159265359; const TERRAIN_BOUNDS: vec2 = vec2(1000.0, 1000.0); const LINE_THICKNESS: f32 = 0.1; const OCTAVE_STEPS: f32 = 4.0; const STROKE_LENGTH: f32 = 0.8; fn hash2(p: vec2) -> f32 { let p3 = fract(vec3(p.x, p.y, p.x) * 0.13); let p3_dot = dot(p3, vec3(p3.y + 3.333, p3.z + 3.333, p3.x + 3.333)); return fract((p3.x + p3.y) * p3_dot); } fn rand(p: vec2f) -> f32 { return fract(sin(dot(p, vec2f(12.9898, 78.233))) * 43758.5453); } struct StrokeData { world_pos_2d: vec2, tile_center: vec2, tile_size: f32, direction_to_light: vec2, distance_to_light: f32, perpendicular_to_light: vec2, rotation_t: f32, line_direction: vec2, perpendicular_to_line: vec2, octave_index: f32, offset: vec2, local_pos: vec2, } fn compute_perpendicular(dir: vec2) -> vec2 { return normalize(vec2(-dir.y, dir.x)); } fn compute_rotation_t(distance_to_light: f32) -> f32 { return pow(min(max(distance_to_light - 1.0 / OCTAVE_STEPS, 0.0) * OCTAVE_STEPS, 1.0), 2.0); } fn sample_shadow_map(light_space_pos: vec4) -> f32 { let proj_coords = light_space_pos.xyz / light_space_pos.w; let ndc_coords = proj_coords * vec3(0.5, -0.5, 1.0) + vec3(0.5, 0.5, 0.0); if ndc_coords.x < 0.0 || ndc_coords.x > 1.0 || ndc_coords.y < 0.0 || ndc_coords.y > 1.0 || ndc_coords.z < 0.0 || ndc_coords.z > 1.0 { return 1.0; } let depth = ndc_coords.z - uniforms.shadow_bias; let shadow = textureSampleCompare(shadow_map, shadow_sampler, ndc_coords.xy, depth); return shadow; } fn hatching_lighting(world_pos: vec3, tile_scale: f32, direction: vec2, distance: f32) -> f32 { let world_pos_2d = vec2(world_pos.x, world_pos.z); let tile_size = 1.0 / tile_scale; let base_tile_center = floor(world_pos_2d / tile_size) * tile_size + tile_size * 0.5; var min_lighting = 1.0; for (var tile_y: i32 = -1; tile_y <= 1; tile_y++) { for (var tile_x: i32 = -1; tile_x <= 1; tile_x++) { let tile_center = base_tile_center + vec2(f32(tile_x), f32(tile_y)) * tile_size; let perpendicular_to_light = compute_perpendicular(direction); let t = compute_rotation_t(distance); let parallel = mix(perpendicular_to_light, direction, t / 2.0); let perpendicular = compute_perpendicular(parallel); let octave_index = round((1.0 - pow(distance, 2.0)) * OCTAVE_STEPS); let spacing = LINE_THICKNESS * 1.5; var max_offset: i32; switch i32(octave_index) { case default { max_offset = 0; } case 3 { max_offset = 3; } case 2 { max_offset = 9; } case 1 { max_offset = 9; } } for (var i: i32 = -max_offset; i <= max_offset; i++) { let random = rand(tile_center + vec2(f32(i), f32(-i))); var chance: f32; switch i32(octave_index) { case 1, default: { chance = 1.0; } case 2: { chance = 0.5; } case 3: { chance = 0.8; } } if random > chance { continue; } let offset = perpendicular * f32(i) * tile_size / f32(max_offset); let local_pos = world_pos_2d - tile_center - offset; let stroke_data = StrokeData( world_pos_2d, tile_center, tile_size, direction, distance, perpendicular_to_light, t, parallel, perpendicular, octave_index, offset, local_pos, ); let lighting = line_stroke_lighting(stroke_data); min_lighting = min(min_lighting, lighting); } } } return min_lighting; } fn point_lighting(world_pos: vec3, point_light: vec3, tile_scale: f32) -> f32 { let world_pos_2d = vec2(world_pos.x, world_pos.z); let light_pos_2d = vec2(point_light.x, point_light.z); let tile_size = 1.0 / tile_scale; let tile_center = floor(world_pos_2d / tile_size) * tile_size + tile_size * 0.5; let direction_to_point = light_pos_2d - tile_center; let distance_to_point = min(length(direction_to_point) / 60.0, 1.0); let direction_normalized = normalize(direction_to_point); return hatching_lighting(world_pos, tile_scale, direction_normalized, distance_to_point); } fn point_lighting_with_shadow(world_pos: vec3, normal: vec3, point_light: vec3, tile_scale: f32, shadow: f32) -> f32 { let world_pos_2d = vec2(world_pos.x, world_pos.z); let light_pos_2d = vec2(point_light.x, point_light.z); let tile_size = 1.0 / tile_scale; let tile_center = floor(world_pos_2d / tile_size) * tile_size + tile_size * 0.5; let direction_to_point_3d = normalize(point_light - world_pos); let diffuse = max(0.0, dot(normalize(normal), direction_to_point_3d)); let direction_to_point = light_pos_2d - tile_center; let distance_to_point = min(length(direction_to_point) / 60.0, 1.0); let direction_normalized = normalize(direction_to_point); let lighting_intensity = shadow * diffuse; let darkness = 1.0 - lighting_intensity; let combined_distance = min(distance_to_point + darkness * 0.5, 1.0); return hatching_lighting(world_pos, tile_scale, direction_normalized, combined_distance); } fn line_stroke_lighting(data: StrokeData) -> f32 { let octave_normalized = data.octave_index / OCTAVE_STEPS; if data.octave_index > 3.0 { return 1.0; } else if data.octave_index < 1.0 { return 0.0; } let noise = hash2(data.tile_center + data.offset) * 2.0 - 1.0; var noise_at_octave = noise; var jitter: f32; switch i32(data.octave_index) { case default { noise_at_octave = noise; jitter = 1.0; } case 2 { noise_at_octave = noise / 10.0; jitter = 1.0; } case 3 { noise_at_octave = noise / 20.0; jitter = 0.5; } } let line = mix(data.perpendicular_to_light, data.direction_to_light, data.rotation_t / (2.0 + noise_at_octave)); let perpendicular_to_line = compute_perpendicular(line); let parallel_coord = dot(data.local_pos, line); let perpendicular_coord = dot(data.local_pos, perpendicular_to_line); let line_half_width = LINE_THICKNESS * (1.0 - octave_normalized * 0.5); let straight_section_half_length = max(0.0, data.tile_size * 0.4 - line_half_width); let parallel_jitter = (rand(data.tile_center + data.offset * 123.456) * 2.0 - 1.0) * data.tile_size * jitter; let jittered_parallel_coord = parallel_coord - parallel_jitter; let overhang = max(0.0, abs(jittered_parallel_coord) - straight_section_half_length); let effective_distance = sqrt(overhang * overhang + perpendicular_coord * perpendicular_coord); return step(line_half_width, effective_distance); } fn flowmap_path_lighting(world_pos: vec3, tile_scale: f32) -> f32 { let world_pos_2d = vec2(world_pos.x, world_pos.z); let tile_size = 1.0 / tile_scale; let tile_center = floor(world_pos_2d / tile_size) * tile_size + tile_size * 0.5; let flowmap_uv = (tile_center + TERRAIN_BOUNDS * 0.5) / TERRAIN_BOUNDS; let flowmap_sample = textureSampleLevel(flowmap_texture, flowmap_sampler, flowmap_uv, 0.0); let x = flowmap_sample.r * 2.0 - 1.0; let y = flowmap_sample.g * 2.0 - 1.0; let direction_to_path = normalize(vec2(x, y)); let distance_to_path = flowmap_sample.b; return hatching_lighting(world_pos, tile_scale, direction_to_path, distance_to_path); } fn flowmap_path_lighting_with_shadow(world_pos: vec3, normal: vec3, tile_scale: f32, shadow: f32) -> f32 { let world_pos_2d = vec2(world_pos.x, world_pos.z); let tile_size = 1.0 / tile_scale; let tile_center = floor(world_pos_2d / tile_size) * tile_size + tile_size * 0.5; let flowmap_uv = (tile_center + TERRAIN_BOUNDS * 0.5) / TERRAIN_BOUNDS; let flowmap_sample = textureSampleLevel(flowmap_texture, flowmap_sampler, flowmap_uv, 0.0); let x = flowmap_sample.r * 2.0 - 1.0; let y = flowmap_sample.g * 2.0 - 1.0; let direction_to_path = normalize(vec2(x, y)); let distance_to_path = flowmap_sample.b; let light_dir_3d = normalize(vec3(-x, 100.0, -y)); let diffuse = max(0.0, dot(normalize(normal), light_dir_3d)); let lighting_intensity = diffuse * shadow; let darkness = 1.0 - lighting_intensity; let combined_distance = min(distance_to_path + darkness * 0.5, 1.0); return hatching_lighting(world_pos, tile_scale, direction_to_path, combined_distance); }