71 lines
2.7 KiB
WebGPU Shading Language
71 lines
2.7 KiB
WebGPU Shading Language
@vertex
|
|
fn vs_main(input: VertexInput) -> VertexOutput {
|
|
var output: VertexOutput;
|
|
|
|
let instance_model = mat4x4<f32>(
|
|
input.instance_model_0,
|
|
input.instance_model_1,
|
|
input.instance_model_2,
|
|
input.instance_model_3
|
|
);
|
|
|
|
let world_pos = instance_model * vec4<f32>(input.position, 1.0);
|
|
output.world_position = world_pos.xyz;
|
|
output.clip_position = uniforms.projection * uniforms.view * world_pos;
|
|
|
|
let normal_matrix = mat3x3<f32>(
|
|
instance_model[0].xyz,
|
|
instance_model[1].xyz,
|
|
instance_model[2].xyz
|
|
);
|
|
output.world_normal = normalize(normal_matrix * input.normal);
|
|
|
|
output.light_space_position = uniforms.light_view_projection * world_pos;
|
|
|
|
return output;
|
|
}
|
|
|
|
@fragment
|
|
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
|
|
let debug = 0u;
|
|
|
|
if debug == 1u {
|
|
let flowmap_uv = (vec2<f32>(input.world_position.x, input.world_position.z) + TERRAIN_BOUNDS * 0.5) / TERRAIN_BOUNDS;
|
|
let flowmap_sample = textureSampleLevel(flowmap_texture, flowmap_sampler, flowmap_uv, 0.0).rgb;
|
|
return vec4<f32>(flowmap_sample, 1.0);
|
|
}
|
|
|
|
if debug == 2u {
|
|
let world_pos_2d = vec2<f32>(input.world_position.x, input.world_position.z);
|
|
let tile_size = 10.0;
|
|
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).rgb;
|
|
let x = (flowmap_sample.r) * 2.0 - 1.0;
|
|
let y = flowmap_sample.g * 2.0 - 1.0;
|
|
let direction_to_path = normalize(vec2<f32>(x, y));
|
|
let perpendicular_to_path = normalize(vec2<f32>(-direction_to_path.y, direction_to_path.x));
|
|
|
|
let local_pos = world_pos_2d - tile_center;
|
|
|
|
let arrow_scale = 0.05;
|
|
let parallel_coord = dot(local_pos, direction_to_path) * arrow_scale;
|
|
let perpendicular_coord = dot(local_pos, perpendicular_to_path) * arrow_scale;
|
|
|
|
let to_path = step(0.95, fract(parallel_coord));
|
|
let to_perp = step(0.95, fract(perpendicular_coord));
|
|
|
|
return vec4<f32>(to_perp, to_perp, to_perp, 1.0);
|
|
}
|
|
|
|
let shadow = sample_shadow_map(input.light_space_position);
|
|
|
|
let tile_scale = 1.0;
|
|
let flowmap_strokes = flowmap_path_lighting_with_shadow(input.world_position, input.world_normal, tile_scale, shadow);
|
|
let point_strokes = point_lighting_with_shadow(input.world_position, input.world_normal, vec3<f32>(0.0, 100.0, 0.0), tile_scale, shadow);
|
|
let brightness = max(flowmap_strokes, point_strokes);
|
|
|
|
return vec4<f32>(brightness, brightness, brightness, 1.0);
|
|
}
|