#[set(everything)] const constants: { texel_size: float2; }; #[set(everything)] const sampler_linear: sampler; #[set(everything)] const height_map: tex2d; struct vert_in { pos: float2; } struct vert_out { pos: float4; tex: float2; } fun depth_to_normal_pass_vert(input: vert_in): vert_out { var output: vert_out; output.tex = input.pos.xy * 0.5 + 0.5; output.tex.y = 1.0 - output.tex.y; output.pos = float4(input.pos.xy, 0.0, 1.0); return output; } fun depth_to_normal_pass_frag(input: vert_out): float4 { var ts: float2 = constants.texel_size; var h00: float = sample_lod(height_map, sampler_linear, input.tex + float2(-2.0, -2.0) * ts, 0.0).r; var h01: float = sample_lod(height_map, sampler_linear, input.tex + float2(-1.0, -2.0) * ts, 0.0).r; var h02: float = sample_lod(height_map, sampler_linear, input.tex + float2( 0.0, -2.0) * ts, 0.0).r; var h03: float = sample_lod(height_map, sampler_linear, input.tex + float2( 1.0, -2.0) * ts, 0.0).r; var h04: float = sample_lod(height_map, sampler_linear, input.tex + float2( 2.0, -2.0) * ts, 0.0).r; var h10: float = sample_lod(height_map, sampler_linear, input.tex + float2(-2.0, -1.0) * ts, 0.0).r; var h11: float = sample_lod(height_map, sampler_linear, input.tex + float2(-1.0, -1.0) * ts, 0.0).r; var h12: float = sample_lod(height_map, sampler_linear, input.tex + float2( 0.0, -1.0) * ts, 0.0).r; var h13: float = sample_lod(height_map, sampler_linear, input.tex + float2( 1.0, -1.0) * ts, 0.0).r; var h14: float = sample_lod(height_map, sampler_linear, input.tex + float2( 2.0, -1.0) * ts, 0.0).r; var h20: float = sample_lod(height_map, sampler_linear, input.tex + float2(-2.0, 0.0) * ts, 0.0).r; var h21: float = sample_lod(height_map, sampler_linear, input.tex + float2(-1.0, 0.0) * ts, 0.0).r; var h23: float = sample_lod(height_map, sampler_linear, input.tex + float2( 1.0, 0.0) * ts, 0.0).r; var h24: float = sample_lod(height_map, sampler_linear, input.tex + float2( 2.0, 0.0) * ts, 0.0).r; var h30: float = sample_lod(height_map, sampler_linear, input.tex + float2(-2.0, 1.0) * ts, 0.0).r; var h31: float = sample_lod(height_map, sampler_linear, input.tex + float2(-1.0, 1.0) * ts, 0.0).r; var h32: float = sample_lod(height_map, sampler_linear, input.tex + float2( 0.0, 1.0) * ts, 0.0).r; var h33: float = sample_lod(height_map, sampler_linear, input.tex + float2( 1.0, 1.0) * ts, 0.0).r; var h34: float = sample_lod(height_map, sampler_linear, input.tex + float2( 2.0, 1.0) * ts, 0.0).r; var h40: float = sample_lod(height_map, sampler_linear, input.tex + float2(-2.0, 2.0) * ts, 0.0).r; var h41: float = sample_lod(height_map, sampler_linear, input.tex + float2(-1.0, 2.0) * ts, 0.0).r; var h42: float = sample_lod(height_map, sampler_linear, input.tex + float2( 0.0, 2.0) * ts, 0.0).r; var h43: float = sample_lod(height_map, sampler_linear, input.tex + float2( 1.0, 2.0) * ts, 0.0).r; var h44: float = sample_lod(height_map, sampler_linear, input.tex + float2( 2.0, 2.0) * ts, 0.0).r; var dx: float = 2.0 * (h00 - h04) + 1.0 * (h01 - h03) + 3.0 * (h10 - h14) + 2.0 * (h11 - h13) + 4.0 * (h20 - h24) + 3.0 * (h21 - h23) + 3.0 * (h30 - h34) + 2.0 * (h31 - h33) + 2.0 * (h40 - h44) + 1.0 * (h41 - h43); var dy: float = 2.0 * (h40 - h00) + 3.0 * (h41 - h01) + 4.0 * (h42 - h02) + 3.0 * (h43 - h03) + 2.0 * (h44 - h04) + 1.0 * (h30 - h10) + 2.0 * (h31 - h11) + 3.0 * (h32 - h12) + 2.0 * (h33 - h13) + 1.0 * (h34 - h14); var normal: float3 = normalize(float3(dx, dy, 1.4375)); normal = normal * 0.5 + 0.5; return float4(normal.x, normal.y, normal.z, 1.0); } #[pipe] struct pipe { vertex = depth_to_normal_pass_vert; fragment = depth_to_normal_pass_frag; }