pbr_main.wgsl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Main Forward PBR Pass for Low-Poly Terrain, Ruins, Wrecks & Articulated Soldiers
  2. // Features:
  3. // - 4-Tap Catmull-Rom Bicubic B-Spline filtering + 16x Aniso on low-res 128x128 shader textures
  4. // - Dynamic puddle wetness modulation reflecting sky, clouds, and ruins via Dual Paraboloid
  5. // - IGN-rotated Vogel Disk PCF Sun Shadows
  6. // - Real-Time 2D Projected Volumetric Cloud Shadows
  7. // - Dynamic muzzle flash & explosion point lights + winter valley mist
  8. @group(0) @binding(0) var<uniform> u: GlobalUniforms;
  9. @group(0) @binding(1) var<storage, read> lights: array<GpuPointLight>;
  10. @group(0) @binding(2) var mat_albedo: texture_2d_array<f32>;
  11. @group(0) @binding(3) var mat_normal: texture_2d_array<f32>;
  12. @group(0) @binding(4) var mat_orm: texture_2d_array<f32>;
  13. @group(0) @binding(5) var mat_sampler: sampler;
  14. @group(0) @binding(6) var shadow_tex: texture_depth_2d;
  15. @group(0) @binding(7) var shadow_sampler: sampler_comparison;
  16. @group(0) @binding(8) var cloud_shadow_tex: texture_2d<f32>;
  17. @group(0) @binding(9) var linear_clamp_sampler: sampler;
  18. @group(0) @binding(10) var dp_tex: texture_2d_array<f32>;
  19. struct SoldierAppearanceGpu {
  20. jacket_tint_mat: vec4<f32>,
  21. trouser_tint_mat: vec4<f32>,
  22. skin_hl: vec4<f32>,
  23. };
  24. @group(1) @binding(0) var<storage, read> soldier_bones: array<mat4x4<f32>>;
  25. @group(1) @binding(1) var<storage, read> soldier_appearances: array<SoldierAppearanceGpu>;
  26. struct PbrOut {
  27. @builtin(position) position: vec4<f32>,
  28. @location(0) world_pos: vec3<f32>,
  29. @location(1) world_normal: vec3<f32>,
  30. @location(2) uv: vec2<f32>,
  31. @location(3) params: vec4<f32>, // x = mat_layer, y = puddle_mask, z = ao, w = tint
  32. @location(4) tint_hl: vec4<f32>, // rgb = actor tint, w = highlight
  33. };
  34. struct StaticIn {
  35. @location(0) position: vec3<f32>,
  36. @location(1) normal: vec3<f32>,
  37. @location(2) uv: vec2<f32>,
  38. @location(3) params: vec4<f32>,
  39. };
  40. @vertex
  41. fn vs_static(v: StaticIn) -> PbrOut {
  42. var out: PbrOut;
  43. out.position = u.view_proj * vec4<f32>(v.position, 1.0);
  44. out.world_pos = v.position;
  45. out.world_normal = v.normal;
  46. out.uv = v.uv;
  47. out.params = v.params;
  48. out.tint_hl = vec4<f32>(1.0, 1.0, 1.0, 0.0);
  49. return out;
  50. }
  51. struct ActorIn {
  52. @location(0) position: vec3<f32>,
  53. @location(1) normal: vec3<f32>,
  54. @location(2) uv: vec2<f32>,
  55. @location(3) mat_id: u32,
  56. @location(4) col0: vec4<f32>,
  57. @location(5) col1: vec4<f32>,
  58. @location(6) col2: vec4<f32>,
  59. @location(7) col3: vec4<f32>,
  60. @location(8) tint_hl: vec4<f32>,
  61. @location(9) extra: vec4<f32>,
  62. };
  63. @vertex
  64. fn vs_actor(v: ActorIn) -> PbrOut {
  65. var out: PbrOut;
  66. let model = mat4x4<f32>(v.col0, v.col1, v.col2, v.col3);
  67. let wp = (model * vec4<f32>(v.position, 1.0)).xyz;
  68. let wn = normalize((model * vec4<f32>(v.normal, 0.0)).xyz);
  69. let mat_layer = select(f32(v.mat_id), v.extra.x, v.extra.x >= 0.0);
  70. out.position = u.view_proj * vec4<f32>(wp, 1.0);
  71. out.world_pos = wp;
  72. out.world_normal = wn;
  73. out.uv = v.uv;
  74. out.params = vec4<f32>(mat_layer, 0.05, 0.92, 1.0);
  75. out.tint_hl = v.tint_hl;
  76. return out;
  77. }
  78. struct SkinnedActorIn {
  79. @location(0) position: vec3<f32>,
  80. @location(1) normal: vec3<f32>,
  81. @location(2) uv: vec2<f32>,
  82. @location(3) bone_indices: vec2<u32>,
  83. @location(4) bone_weights: vec2<f32>,
  84. @location(5) region_id: u32,
  85. };
  86. @vertex
  87. fn vs_skinned(v: SkinnedActorIn, @builtin(instance_index) inst: u32) -> PbrOut {
  88. var out: PbrOut;
  89. let base_bone = inst * 12u;
  90. let m0 = soldier_bones[base_bone + v.bone_indices.x];
  91. let m1 = soldier_bones[base_bone + v.bone_indices.y];
  92. let skin = m0 * v.bone_weights.x + m1 * v.bone_weights.y;
  93. let wp = (skin * vec4<f32>(v.position, 1.0)).xyz;
  94. let wn = normalize((skin * vec4<f32>(v.normal, 0.0)).xyz);
  95. let app = soldier_appearances[inst];
  96. var mat_layer = app.jacket_tint_mat.w;
  97. var tint = app.jacket_tint_mat.rgb;
  98. if (v.region_id == 0u) {
  99. // Skin / face -> Sandbag weave toned by skin tint
  100. mat_layer = 7.0;
  101. tint = app.skin_hl.rgb;
  102. } else if (v.region_id == 2u) {
  103. // Trousers -> independent trouser camo & tint
  104. mat_layer = app.trouser_tint_mat.w;
  105. tint = app.trouser_tint_mat.rgb;
  106. } else if (v.region_id == 3u) {
  107. // Boots & cold-weather gloves -> dark Gunmetal/leather
  108. mat_layer = 11.0;
  109. tint = vec3<f32>(0.65, 0.62, 0.58);
  110. }
  111. out.position = u.view_proj * vec4<f32>(wp, 1.0);
  112. out.world_pos = wp;
  113. out.world_normal = wn;
  114. out.uv = v.uv;
  115. out.params = vec4<f32>(mat_layer, 0.04, 0.94, 1.0);
  116. out.tint_hl = vec4<f32>(tint, app.skin_hl.w);
  117. return out;
  118. }
  119. fn world_micro_grain(wp: vec3<f32>) -> f32 {
  120. let p = wp * 3.7;
  121. return 0.94 + 0.12 * fract(sin(dot(floor(p), vec3<f32>(12.9898, 78.233, 37.719))) * 43758.5453);
  122. }
  123. @fragment
  124. fn fs_main(in: PbrOut) -> @location(0) vec4<f32> {
  125. let layer = i32(clamp(in.params.x + 0.5, 0.0, 20.0));
  126. let use_bicubic = u.cloud_wind.w > 0.5;
  127. var albedo_s: vec4<f32>;
  128. var normal_s: vec4<f32>;
  129. var orm_s: vec4<f32>;
  130. if (use_bicubic) {
  131. albedo_s = sample_bicubic_array(mat_albedo, mat_sampler, in.uv, layer);
  132. normal_s = sample_bicubic_array(mat_normal, mat_sampler, in.uv, layer);
  133. orm_s = sample_bicubic_array(mat_orm, mat_sampler, in.uv, layer);
  134. } else {
  135. albedo_s = textureSample(mat_albedo, mat_sampler, in.uv, layer);
  136. normal_s = textureSample(mat_normal, mat_sampler, in.uv, layer);
  137. orm_s = textureSample(mat_orm, mat_sampler, in.uv, layer);
  138. }
  139. var albedo = albedo_s.rgb * in.params.w * in.tint_hl.rgb * world_micro_grain(in.world_pos);
  140. let ao = orm_s.r * in.params.z;
  141. var roughness = orm_s.g;
  142. let metallic = orm_s.b;
  143. // Perturb geometric low-poly normal with filtered procedural normal map
  144. let base_n = normalize(in.world_normal);
  145. let up_ref = select(vec3<f32>(1.0, 0.0, 0.0), vec3<f32>(0.0, 1.0, 0.0), abs(base_n.y) < 0.9);
  146. let t_approx = normalize(cross(up_ref, base_n));
  147. let b_approx = cross(base_n, t_approx);
  148. let n_ts = normal_s.xyz * 2.0 - 1.0;
  149. var N = normalize(t_approx * n_ts.x * 0.65 + b_approx * n_ts.y * 0.65 + base_n * max(n_ts.z, 0.25));
  150. // Puddle wetness modulation -> glossy cold-water puddles reflecting DPR sky/ruins!
  151. let puddle = clamp(in.params.y * u.sun_color.w, 0.0, 1.0) * smoothstep(0.55, 0.92, base_n.y);
  152. albedo = mix(albedo, albedo * 0.48, puddle * 0.75);
  153. roughness = mix(roughness, 0.045, puddle);
  154. N = normalize(mix(N, base_n, puddle * 0.85));
  155. let V = normalize(u.camera_pos.xyz - in.world_pos);
  156. let sun_d = normalize(u.sun_dir.xyz);
  157. // Vogel Disk PCF sun shadow * 2D Projected Cloud Shadow
  158. let pcf_taps = u32(clamp(u.cloud_wind.z, 8.0, 32.0));
  159. let sun_vis = sample_sun_shadow_vogel(
  160. shadow_tex, shadow_sampler, u.sun_view_proj,
  161. in.world_pos, base_n, sun_d, in.position.xy, pcf_taps
  162. );
  163. let cloud_vis = sample_cloud_shadow(cloud_shadow_tex, linear_clamp_sampler, in.world_pos, u.cloud_shadow_bounds);
  164. let direct_sun = eval_pbr_direct(
  165. N, V, sun_d, albedo, roughness, metallic,
  166. u.sun_color.rgb * sun_vis * cloud_vis
  167. );
  168. // Dynamic muzzle flashes & explosion lights
  169. var direct_dyn = vec3<f32>(0.0);
  170. let lcount = min(u.flags.x, 64u);
  171. for (var i = 0u; i < lcount; i = i + 1u) {
  172. let lp = lights[i].pos_radius;
  173. let to_l = lp.xyz - in.world_pos;
  174. let d = length(to_l);
  175. if (d < lp.w) {
  176. let atten = pow(1.0 - d / lp.w, 2.0);
  177. let L = to_l / max(d, 0.001);
  178. direct_dyn += eval_pbr_direct(
  179. N, V, L, albedo, roughness, metallic,
  180. lights[i].color_intensity.rgb * lights[i].color_intensity.w * atten
  181. );
  182. }
  183. }
  184. // Hemisphere winter sky ambient + Dual Paraboloid IBL Specular Reflections!
  185. let sky_amb = mix(vec3<f32>(0.10, 0.12, 0.14), vec3<f32>(0.34, 0.40, 0.48), N.y * 0.5 + 0.5);
  186. let diffuse_ibl = albedo * (1.0 - metallic) * sky_amb * ao * mix(0.68, 1.0, cloud_vis);
  187. let R = reflect(-V, N);
  188. let dp_env = sample_dual_paraboloid(dp_tex, linear_clamp_sampler, R, roughness);
  189. let ndv = max(dot(N, V), 0.001);
  190. let f0 = mix(vec3<f32>(0.04), albedo, metallic);
  191. let fresnel_ibl = f0 + (max(vec3<f32>(1.0 - roughness), f0) - f0) * pow(1.0 - ndv, 5.0);
  192. let spec_ibl = dp_env * fresnel_ibl * ao * mix(0.65, 1.25, puddle + metallic * 0.5);
  193. var color = direct_sun + direct_dyn + diffuse_ibl + spec_ibl;
  194. // Cold Caucasus exponential distance & valley height mist (scaled cleanly for 3 km horizons)
  195. let cam_dist = length(in.world_pos - u.camera_pos.xyz);
  196. let height_mist = exp(-max(in.world_pos.y - 1.0, 0.0) * 0.045);
  197. let fog_scale = select(1.0, 0.20, u.screen_params.w > 1500.0);
  198. let fog_amount = 1.0 - exp(-cam_dist * (0.0020 + 0.0016 * height_mist) * fog_scale);
  199. let fog_col = eval_winter_sky(normalize(in.world_pos - u.camera_pos.xyz), sun_d, u.sun_dir.w);
  200. color = mix(color, fog_col, clamp(fog_amount, 0.0, 0.90));
  201. return vec4<f32>(color, 1.0);
  202. }