stylized 1-bit rendering
This commit is contained in:
299
shaders/shared.wgsl
Normal file
299
shaders/shared.wgsl
Normal file
@@ -0,0 +1,299 @@
|
||||
struct VertexInput {
|
||||
@location(0) position: vec3<f32>,
|
||||
@location(1) normal: vec3<f32>,
|
||||
@location(2) uv: vec2<f32>,
|
||||
@location(3) instance_model_0: vec4<f32>,
|
||||
@location(4) instance_model_1: vec4<f32>,
|
||||
@location(5) instance_model_2: vec4<f32>,
|
||||
@location(6) instance_model_3: vec4<f32>,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) clip_position: vec4<f32>,
|
||||
@location(0) world_position: vec3<f32>,
|
||||
@location(1) world_normal: vec3<f32>,
|
||||
@location(2) light_space_position: vec4<f32>,
|
||||
}
|
||||
|
||||
struct Uniforms {
|
||||
model: mat4x4<f32>,
|
||||
view: mat4x4<f32>,
|
||||
projection: mat4x4<f32>,
|
||||
light_view_projection: mat4x4<f32>,
|
||||
camera_position: vec3<f32>,
|
||||
height_scale: f32,
|
||||
time: f32,
|
||||
shadow_bias: f32,
|
||||
light_direction: vec3<f32>,
|
||||
}
|
||||
|
||||
@group(0) @binding(0)
|
||||
var<uniform> 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<f32>;
|
||||
|
||||
@group(0) @binding(4)
|
||||
var dither_sampler: sampler;
|
||||
|
||||
@group(0) @binding(5)
|
||||
var flowmap_texture: texture_2d<f32>;
|
||||
|
||||
@group(0) @binding(6)
|
||||
var flowmap_sampler: sampler;
|
||||
|
||||
const PI: f32 = 3.14159265359;
|
||||
const TERRAIN_BOUNDS: vec2<f32> = vec2<f32>(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>) -> f32 {
|
||||
let p3 = fract(vec3<f32>(p.x, p.y, p.x) * 0.13);
|
||||
let p3_dot = dot(p3, vec3<f32>(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<f32>,
|
||||
tile_center: vec2<f32>,
|
||||
tile_size: f32,
|
||||
direction_to_light: vec2<f32>,
|
||||
distance_to_light: f32,
|
||||
perpendicular_to_light: vec2<f32>,
|
||||
rotation_t: f32,
|
||||
line_direction: vec2<f32>,
|
||||
perpendicular_to_line: vec2<f32>,
|
||||
octave_index: f32,
|
||||
offset: vec2<f32>,
|
||||
local_pos: vec2<f32>,
|
||||
}
|
||||
|
||||
fn compute_perpendicular(dir: vec2<f32>) -> vec2<f32> {
|
||||
return normalize(vec2<f32>(-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>) -> f32 {
|
||||
let proj_coords = light_space_pos.xyz / light_space_pos.w;
|
||||
let ndc_coords = proj_coords * vec3<f32>(0.5, -0.5, 1.0) + vec3<f32>(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<f32>, tile_scale: f32, direction: vec2<f32>, distance: f32) -> f32 {
|
||||
let world_pos_2d = vec2<f32>(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>(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>(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<f32>, point_light: vec3<f32>, tile_scale: f32) -> f32 {
|
||||
let world_pos_2d = vec2<f32>(world_pos.x, world_pos.z);
|
||||
let light_pos_2d = vec2<f32>(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<f32>, normal: vec3<f32>, point_light: vec3<f32>, tile_scale: f32, shadow: f32) -> f32 {
|
||||
let world_pos_2d = vec2<f32>(world_pos.x, world_pos.z);
|
||||
let light_pos_2d = vec2<f32>(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<f32>, tile_scale: f32) -> f32 {
|
||||
let world_pos_2d = vec2<f32>(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<f32>(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<f32>, normal: vec3<f32>, tile_scale: f32, shadow: f32) -> f32 {
|
||||
let world_pos_2d = vec2<f32>(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<f32>(x, y));
|
||||
let distance_to_path = flowmap_sample.b;
|
||||
|
||||
let light_dir_3d = normalize(vec3<f32>(-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);
|
||||
}
|
||||
@@ -1,97 +1,38 @@
|
||||
struct VertexInput {
|
||||
@location(0) position: vec3<f32>,
|
||||
@location(1) normal: vec3<f32>,
|
||||
@location(2) uv: vec2<f32>,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) clip_position: vec4<f32>,
|
||||
@location(0) world_position: vec3<f32>,
|
||||
@location(1) world_normal: vec3<f32>,
|
||||
}
|
||||
|
||||
struct Uniforms {
|
||||
model: mat4x4<f32>,
|
||||
view: mat4x4<f32>,
|
||||
projection: mat4x4<f32>,
|
||||
}
|
||||
|
||||
@group(0) @binding(0)
|
||||
var<uniform> uniforms: Uniforms;
|
||||
|
||||
@vertex
|
||||
fn vs_main(input: VertexInput) -> VertexOutput {
|
||||
var output: VertexOutput;
|
||||
|
||||
let world_pos = uniforms.model * vec4<f32>(input.position, 1.0);
|
||||
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.world_normal = (uniforms.model * vec4<f32>(input.normal, 0.0)).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;
|
||||
}
|
||||
|
||||
fn bayer_2x2_dither(value: f32, screen_pos: vec2<f32>) -> f32 {
|
||||
let pattern = array<f32, 4>(
|
||||
0.0/4.0, 2.0/4.0,
|
||||
3.0/4.0, 1.0/4.0
|
||||
);
|
||||
let x = i32(screen_pos.x) % 2;
|
||||
let y = i32(screen_pos.y) % 2;
|
||||
let index = y * 2 + x;
|
||||
return select(0.0, 1.0, value > pattern[index]);
|
||||
}
|
||||
|
||||
fn bayer_4x4_dither(value: f32, screen_pos: vec2<f32>) -> f32 {
|
||||
let pattern = array<f32, 16>(
|
||||
0.0/16.0, 8.0/16.0, 2.0/16.0, 10.0/16.0,
|
||||
12.0/16.0, 4.0/16.0, 14.0/16.0, 6.0/16.0,
|
||||
3.0/16.0, 11.0/16.0, 1.0/16.0, 9.0/16.0,
|
||||
15.0/16.0, 7.0/16.0, 13.0/16.0, 5.0/16.0
|
||||
);
|
||||
let x = i32(screen_pos.x) % 4;
|
||||
let y = i32(screen_pos.y) % 4;
|
||||
let index = y * 4 + x;
|
||||
return select(0.0, 1.0, value > pattern[index]);
|
||||
}
|
||||
|
||||
fn bayer_8x8_dither(value: f32, screen_pos: vec2<f32>) -> f32 {
|
||||
let pattern = array<f32, 64>(
|
||||
0.0/64.0, 32.0/64.0, 8.0/64.0, 40.0/64.0, 2.0/64.0, 34.0/64.0, 10.0/64.0, 42.0/64.0,
|
||||
48.0/64.0, 16.0/64.0, 56.0/64.0, 24.0/64.0, 50.0/64.0, 18.0/64.0, 58.0/64.0, 26.0/64.0,
|
||||
12.0/64.0, 44.0/64.0, 4.0/64.0, 36.0/64.0, 14.0/64.0, 46.0/64.0, 6.0/64.0, 38.0/64.0,
|
||||
60.0/64.0, 28.0/64.0, 52.0/64.0, 20.0/64.0, 62.0/64.0, 30.0/64.0, 54.0/64.0, 22.0/64.0,
|
||||
3.0/64.0, 35.0/64.0, 11.0/64.0, 43.0/64.0, 1.0/64.0, 33.0/64.0, 9.0/64.0, 41.0/64.0,
|
||||
51.0/64.0, 19.0/64.0, 59.0/64.0, 27.0/64.0, 49.0/64.0, 17.0/64.0, 57.0/64.0, 25.0/64.0,
|
||||
15.0/64.0, 47.0/64.0, 7.0/64.0, 39.0/64.0, 13.0/64.0, 45.0/64.0, 5.0/64.0, 37.0/64.0,
|
||||
63.0/64.0, 31.0/64.0, 55.0/64.0, 23.0/64.0, 61.0/64.0, 29.0/64.0, 53.0/64.0, 21.0/64.0
|
||||
);
|
||||
let x = i32(screen_pos.x) % 8;
|
||||
let y = i32(screen_pos.y) % 8;
|
||||
let index = y * 8 + x;
|
||||
return select(0.0, 1.0, value > pattern[index]);
|
||||
}
|
||||
|
||||
|
||||
@fragment
|
||||
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let light_pos = vec3<f32>(5.0, 5.0, 5.0);
|
||||
let light_color = vec3<f32>(1.0, 1.0, 1.0);
|
||||
let object_color = vec3<f32>(1.0, 1.0, 1.0);
|
||||
let shadow = sample_shadow_map(input.light_space_position);
|
||||
|
||||
let ambient_strength = 0.3;
|
||||
let ambient = ambient_strength * light_color;
|
||||
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);
|
||||
|
||||
let norm = normalize(input.world_normal);
|
||||
let light_dir = normalize(vec3<f32>(1.0, -1.0, 1.0));
|
||||
let diff = max(dot(norm, light_dir), 0.0);
|
||||
let diffuse = diff * light_color;
|
||||
|
||||
let result = (ambient + diffuse) * object_color;
|
||||
|
||||
let dithered_r = bayer_8x8_dither(result.r, input.clip_position.xy);
|
||||
let dithered_g = bayer_8x8_dither(result.g, input.clip_position.xy);
|
||||
let dithered_b = bayer_8x8_dither(result.b, input.clip_position.xy);
|
||||
|
||||
return vec4<f32>(dithered_r, dithered_g, dithered_b, 1.0);
|
||||
return vec4<f32>(brightness, brightness, brightness, 1.0);
|
||||
}
|
||||
|
||||
@@ -1,120 +1,70 @@
|
||||
struct VertexInput {
|
||||
@location(0) position: vec3<f32>,
|
||||
@location(1) normal: vec3<f32>,
|
||||
@location(2) uv: vec2<f32>,
|
||||
}
|
||||
|
||||
struct VertexOutput {
|
||||
@builtin(position) clip_position: vec4<f32>,
|
||||
@location(0) world_position: vec3<f32>,
|
||||
@location(1) world_normal: vec3<f32>,
|
||||
@location(2) uv: vec2<f32>,
|
||||
}
|
||||
|
||||
struct Uniforms {
|
||||
model: mat4x4<f32>,
|
||||
view: mat4x4<f32>,
|
||||
projection: mat4x4<f32>,
|
||||
height_scale: f32,
|
||||
time: f32,
|
||||
}
|
||||
|
||||
@group(0) @binding(0)
|
||||
var<uniform> uniforms: Uniforms;
|
||||
|
||||
@group(0) @binding(1)
|
||||
var height_texture: texture_2d<f32>;
|
||||
|
||||
@group(0) @binding(2)
|
||||
var height_sampler: sampler;
|
||||
|
||||
@vertex
|
||||
fn vs_main(input: VertexInput) -> VertexOutput {
|
||||
var output: VertexOutput;
|
||||
|
||||
let height = textureSampleLevel(height_texture, height_sampler, input.uv, 0.0).r;
|
||||
let instance_model = mat4x4<f32>(
|
||||
input.instance_model_0,
|
||||
input.instance_model_1,
|
||||
input.instance_model_2,
|
||||
input.instance_model_3
|
||||
);
|
||||
|
||||
var displaced_pos = input.position;
|
||||
displaced_pos.y += height * uniforms.height_scale;
|
||||
|
||||
let texel_size = vec2<f32>(1.0 / 512.0, 1.0 / 512.0);
|
||||
let height_left = textureSampleLevel(height_texture, height_sampler, input.uv - vec2<f32>(texel_size.x, 0.0), 0.0).r;
|
||||
let height_right = textureSampleLevel(height_texture, height_sampler, input.uv + vec2<f32>(texel_size.x, 0.0), 0.0).r;
|
||||
let height_down = textureSampleLevel(height_texture, height_sampler, input.uv - vec2<f32>(0.0, texel_size.y), 0.0).r;
|
||||
let height_up = textureSampleLevel(height_texture, height_sampler, input.uv + vec2<f32>(0.0, texel_size.y), 0.0).r;
|
||||
|
||||
let dh_dx = (height_right - height_left) * uniforms.height_scale;
|
||||
let dh_dz = (height_up - height_down) * uniforms.height_scale;
|
||||
|
||||
let normal = normalize(vec3<f32>(-dh_dx, 1.0, -dh_dz));
|
||||
|
||||
let world_pos = uniforms.model * vec4<f32>(displaced_pos, 1.0);
|
||||
let world_pos = instance_model * vec4<f32>(input.position, 1.0);
|
||||
output.world_position = world_pos.xyz;
|
||||
output.world_normal = normalize((uniforms.model * vec4<f32>(normal, 0.0)).xyz);
|
||||
output.clip_position = uniforms.projection * uniforms.view * world_pos;
|
||||
output.uv = input.uv;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
fn hash(p: vec2<f32>) -> f32 {
|
||||
var p3 = fract(vec3<f32>(p.xyx) * 0.1031);
|
||||
p3 += dot(p3, p3.yzx + 33.33);
|
||||
return fract((p3.x + p3.y) * p3.z);
|
||||
}
|
||||
|
||||
fn should_glitter(screen_pos: vec2<f32>, time: f32) -> bool {
|
||||
let pixel_pos = floor(screen_pos);
|
||||
let h = hash(pixel_pos);
|
||||
let time_offset = h * 6283.18;
|
||||
let sparkle_rate = 0.2;
|
||||
let sparkle = sin(time * sparkle_rate + time_offset) * 0.5 + 0.5;
|
||||
let threshold = 0.95;
|
||||
return sparkle > threshold && h > 0.95;
|
||||
}
|
||||
|
||||
fn bayer_8x8_dither(value: f32, screen_pos: vec2<f32>) -> f32 {
|
||||
let pattern = array<f32, 64>(
|
||||
0.0/64.0, 32.0/64.0, 8.0/64.0, 40.0/64.0, 2.0/64.0, 34.0/64.0, 10.0/64.0, 42.0/64.0,
|
||||
48.0/64.0, 16.0/64.0, 56.0/64.0, 24.0/64.0, 50.0/64.0, 18.0/64.0, 58.0/64.0, 26.0/64.0,
|
||||
12.0/64.0, 44.0/64.0, 4.0/64.0, 36.0/64.0, 14.0/64.0, 46.0/64.0, 6.0/64.0, 38.0/64.0,
|
||||
60.0/64.0, 28.0/64.0, 52.0/64.0, 20.0/64.0, 62.0/64.0, 30.0/64.0, 54.0/64.0, 22.0/64.0,
|
||||
3.0/64.0, 35.0/64.0, 11.0/64.0, 43.0/64.0, 1.0/64.0, 33.0/64.0, 9.0/64.0, 41.0/64.0,
|
||||
51.0/64.0, 19.0/64.0, 59.0/64.0, 27.0/64.0, 49.0/64.0, 17.0/64.0, 57.0/64.0, 25.0/64.0,
|
||||
15.0/64.0, 47.0/64.0, 7.0/64.0, 39.0/64.0, 13.0/64.0, 45.0/64.0, 5.0/64.0, 37.0/64.0,
|
||||
63.0/64.0, 31.0/64.0, 55.0/64.0, 23.0/64.0, 61.0/64.0, 29.0/64.0, 53.0/64.0, 21.0/64.0
|
||||
);
|
||||
let x = i32(screen_pos.x) % 8;
|
||||
let y = i32(screen_pos.y) % 8;
|
||||
let index = y * 8 + x;
|
||||
return select(0.2, 1.0, value > pattern[index]);
|
||||
}
|
||||
|
||||
@fragment
|
||||
fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
|
||||
let light_dir = normalize(vec3<f32>(-0.5, -1.0, -0.5));
|
||||
let light_color = vec3<f32>(1.0, 1.0, 1.0);
|
||||
let object_color = vec3<f32>(1.0, 1.0, 1.0);
|
||||
let debug = 0u;
|
||||
|
||||
let ambient_strength = 0.2;
|
||||
let ambient = ambient_strength * light_color;
|
||||
|
||||
let norm = normalize(input.world_normal);
|
||||
let diff = max(dot(norm, -light_dir), 0.0);
|
||||
let diffuse = diff * light_color;
|
||||
|
||||
let result = (ambient + diffuse) * object_color;
|
||||
|
||||
var dithered_r = bayer_8x8_dither(result.r, input.clip_position.xy);
|
||||
var dithered_g = bayer_8x8_dither(result.g, input.clip_position.xy);
|
||||
var dithered_b = bayer_8x8_dither(result.b, input.clip_position.xy);
|
||||
|
||||
let is_grey_or_black = dithered_r == 0.0 || (dithered_r == dithered_g && dithered_g == dithered_b);
|
||||
if (is_grey_or_black && should_glitter(input.clip_position.xy, uniforms.time)) {
|
||||
dithered_r = 1.0;
|
||||
dithered_g = 1.0;
|
||||
dithered_b = 1.0;
|
||||
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);
|
||||
}
|
||||
|
||||
return vec4<f32>(dithered_r, dithered_g, dithered_b, 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user