| 1 | //! 3D WGSL shader source and GPU vertex/uniform buffer layouts. |
| 2 | |
| 3 | use bytemuck::{Pod, Zeroable}; |
| 4 | |
| 5 | /// Point and spot lights each get this many shader slots. |
| 6 | pub(super) const MAX_LIGHTS: usize = 8; |
| 7 | /// Directional lights get this many shader slots. |
| 8 | pub(super) const MAX_DIR_LIGHTS: usize = 4; |
| 9 | /// Point lights that can cast shadows in one render. |
| 10 | pub(super) const MAX_SHADOW_LIGHTS: usize = 4; |
| 11 | /// Edge length in texels of each cube-map face. |
| 12 | pub(super) const SHADOW_MAP_SIZE: u32 = 1024; |
| 13 | |
| 14 | #[repr(C)] |
| 15 | #[derive(Clone, Copy, Debug, Pod, Zeroable)] |
| 16 | pub struct Vertex3D { |
| 17 | pub pos: [f32; 3], |
| 18 | pub normal: [f32; 3], |
| 19 | pub uv: [f32; 2], |
| 20 | } |
| 21 | |
| 22 | #[repr(C)] |
| 23 | #[derive(Clone, Copy, Pod, Zeroable)] |
| 24 | pub(super) struct MeshUniform { |
| 25 | pub(super) mvp: [f32; 16], |
| 26 | pub(super) model: [f32; 16], |
| 27 | /// Camera view matrix (used by `MtlChanBool` Eye-space position/normal operands). |
| 28 | pub(super) view: [f32; 16], |
| 29 | /// DoD `[origin_x, origin_y, canvas_w, canvas_h]` (zeros if standard texture) |
| 30 | pub(super) dod: [f32; 4], |
| 31 | /// `[use_diffuse_tex, lighting_enabled, specular_intensity, flip_normals]` |
| 32 | pub(super) flags: [f32; 4], |
| 33 | /// Diffuse color multiplier `(r, g, b, a)` |
| 34 | pub(super) diffuse_col: [f32; 4], |
| 35 | /// Specular color `(r, g, b, shininess / roughness / ward_spread_u)` |
| 36 | pub(super) specular_col: [f32; 4], |
| 37 | /// Ambient light `(r, g, b, 0)` |
| 38 | pub(super) ambient: [f32; 4], |
| 39 | /// Directional light directions TO light `(dx, dy, dz, 0)` |
| 40 | pub(super) dir_to_light: [[f32; 4]; MAX_DIR_LIGHTS], |
| 41 | /// Directional light colors * intensity `(r, g, b, 0)` |
| 42 | pub(super) dir_color: [[f32; 4]; MAX_DIR_LIGHTS], |
| 43 | /// Camera world position `(cx, cy, cz, 0)` |
| 44 | pub(super) cam_pos: [f32; 4], |
| 45 | /// `[num_point_lights, num_spot_lights, num_dir_lights, receives_shadows]` |
| 46 | pub(super) light_counts: [f32; 4], |
| 47 | /// UV affine matrix, column-major `[m00, m10, m01, m11]` |
| 48 | pub(super) uv_xform: [f32; 4], |
| 49 | /// UV translation, wrap mode and unlit light-pass count `[tx, ty, wrap, passes]` |
| 50 | pub(super) uv_off_wrap: [f32; 4], |
| 51 | /// `[bump_scale, bump_mode, bump_only, spec_model: 0=Blinn 1=Phong 2=CookTorrance 3=Pbr 4=Ward]` |
| 52 | pub(super) mat_fx: [f32; 4], |
| 53 | /// Bump UV affine matrix, column-major `[m00, m10, m01, m11]` |
| 54 | pub(super) bump_uv_xform: [f32; 4], |
| 55 | /// Bump UV translation, wrap mode and presence `[tx, ty, wrap, has_bump_tex]` |
| 56 | pub(super) bump_off_wrap: [f32; 4], |
| 57 | /// Bump DoD `[origin_x, origin_y, canvas_w, canvas_h]` (zeros if standard texture) |
| 58 | pub(super) bump_dod: [f32; 4], |
| 59 | /// `[bump_clamp_z, bump_channel, bump_filter_size, 0]` |
| 60 | pub(super) bump_params: [f32; 4], |
| 61 | /// `[diffuse_color_alpha, refractive_index, do_fresnel, ward_spread_v]` |
| 62 | pub(super) mtl_params: [f32; 4], |
| 63 | /// Environment inverse rotation col 0 + `env_mode` (`0=none, 1=SphereMap, 2=MtlReflect`) |
| 64 | pub(super) env_rot_0: [f32; 4], |
| 65 | /// Environment inverse rotation col 1 + `env_map_mode` (`0=equirect, 1=angular`) |
| 66 | pub(super) env_rot_1: [f32; 4], |
| 67 | /// Environment inverse rotation col 2 + `env_tex_slot` (`0=solid, 1=mat_tex, 2=bump_tex`) |
| 68 | pub(super) env_rot_2: [f32; 4], |
| 69 | /// `[start_angle_norm, inv_angle_span, strength_variability, constant_strength]` |
| 70 | pub(super) env_angles: [f32; 4], |
| 71 | /// `[glancing_strength, face_on_strength, falloff, refract_ior]` |
| 72 | pub(super) refl_params: [f32; 4], |
| 73 | /// `[refl_tint_r, refl_tint_g, refl_tint_b, has_bg_material]` |
| 74 | pub(super) refl_tint: [f32; 4], |
| 75 | /// `[refract_tint_r, refract_tint_g, refract_tint_b, 0]` |
| 76 | pub(super) refract_tint: [f32; 4], |
| 77 | /// `MtlChanBool` operand A indices `[r, g, b, a]` |
| 78 | pub(super) cb_ops_a: [f32; 4], |
| 79 | /// `MtlChanBool` operation indices `[r, g, b, a]` |
| 80 | pub(super) cb_ops: [f32; 4], |
| 81 | /// `MtlChanBool` operand B indices `[r, g, b, a]` |
| 82 | pub(super) cb_ops_b: [f32; 4], |
| 83 | /// `MtlChanBool` foreground material constant RGBA |
| 84 | pub(super) cb_fg_col: [f32; 4], |
| 85 | /// `[cb_enabled, cb_coord_space (0=Model,1=World,2=Eye), cb_fg_tex_slot (0=none,2=bump_tex), 0]` |
| 86 | pub(super) cb_flags: [f32; 4], |
| 87 | /// Point light positions `(x, y, z, decay_type: 0=none, 1=linear, 2=quad)` |
| 88 | pub(super) point_pos: [[f32; 4]; MAX_LIGHTS], |
| 89 | /// Point light colors * intensity `(r, g, b, 0)` |
| 90 | pub(super) point_col: [[f32; 4]; MAX_LIGHTS], |
| 91 | /// Spot light positions `(x, y, z, cos_inner)` |
| 92 | pub(super) spot_pos: [[f32; 4]; MAX_LIGHTS], |
| 93 | /// Spot light forward directions `(dx, dy, dz, cos_outer)` |
| 94 | pub(super) spot_dir: [[f32; 4]; MAX_LIGHTS], |
| 95 | /// Spot light colors * intensity `(r, g, b, decay_type)` |
| 96 | pub(super) spot_col: [[f32; 4]; MAX_LIGHTS], |
| 97 | } |
| 98 | |
| 99 | #[repr(C)] |
| 100 | #[derive(Clone, Copy, Pod, Zeroable)] |
| 101 | pub(super) struct ShadowUniform { |
| 102 | /// Cube-face view-projections, six per shadow slot (`+X -X +Y -Y +Z -Z`). |
| 103 | pub(super) face_vp: [[f32; 16]; 6 * MAX_SHADOW_LIGHTS], |
| 104 | /// Per point light `(shadow slot or -1, density, 0, 0)`. |
| 105 | pub(super) point: [[f32; 4]; MAX_LIGHTS], |
| 106 | } |
| 107 | |
| 108 | #[repr(C)] |
| 109 | #[derive(Clone, Copy, Pod, Zeroable)] |
| 110 | pub(super) struct DepthToEyeZUniform { |
| 111 | /// `[P[2][2], P[2][3], P[3][2], P[3][3]]` from the camera projection matrix. |
| 112 | pub(super) proj_zw: [f32; 4], |
| 113 | /// `[EMPTY_Z_SENTINEL, 0.0, 0.0, 0.0]`. |
| 114 | pub(super) params: [f32; 4], |
| 115 | } |
| 116 | |
| 117 | /// Compute shader converting `Depth32Float` NDC depth (`[0, 1]`) into exact eye-space `Z` |
| 118 | /// (`R32Float`, negative in front of camera, `EMPTY_Z_SENTINEL` on empty sky pixels). |
| 119 | pub(super) const DEPTH_TO_EYE_Z_SHADER: &str = r#" |
| 120 | struct DepthToEyeZParams { |
| 121 | proj_zw: vec4<f32>, |
| 122 | params: vec4<f32>, |
| 123 | }; |
| 124 | |
| 125 | @group(0) @binding(0) var depth_tex: texture_depth_2d; |
| 126 | @group(0) @binding(1) var out_z: texture_storage_2d<r32float, write>; |
| 127 | @group(0) @binding(2) var<uniform> u: DepthToEyeZParams; |
| 128 | |
| 129 | @compute @workgroup_size(8, 8, 1) |
| 130 | fn depth_to_eye_z(@builtin(global_invocation_id) gid: vec3<u32>) { |
| 131 | let dims = textureDimensions(out_z); |
| 132 | if (gid.x >= dims.x || gid.y >= dims.y) { |
| 133 | return; |
| 134 | } |
| 135 | let d = textureLoad(depth_tex, vec2<i32>(gid.xy), 0); |
| 136 | var z_eye = u.params.x; |
| 137 | if (d < 1.0 - 1e-6) { |
| 138 | let p22 = u.proj_zw.x; |
| 139 | let p23 = u.proj_zw.y; |
| 140 | let p32 = u.proj_zw.z; |
| 141 | let p33 = u.proj_zw.w; |
| 142 | let denom = p22 - d * p23; |
| 143 | if (abs(denom) > 1e-12) { |
| 144 | z_eye = (d * p33 - p32) / denom; |
| 145 | } |
| 146 | } |
| 147 | textureStore(out_z, vec2<i32>(gid.xy), vec4<f32>(z_eye, 0.0, 0.0, 0.0)); |
| 148 | } |
| 149 | "#; |
| 150 | |
| 151 | /// UV affine plus wrap (`off_wrap.z`: 0 clamp, 1 wrap, 2 mirror), shared by both pipelines. |
| 152 | pub(super) const UV_XFORM_WGSL: &str = r#" |
| 153 | fn wrap_uv_only(uv_in: vec2<f32>, wrap_f: f32) -> vec2<f32> { |
| 154 | let wrap = i32(wrap_f + 0.5); |
| 155 | if (wrap == 1) { |
| 156 | return fract(uv_in); |
| 157 | } else if (wrap == 2) { |
| 158 | return vec2<f32>(1.0) - abs(fract(uv_in * 0.5) * 2.0 - vec2<f32>(1.0)); |
| 159 | } |
| 160 | return clamp(uv_in, vec2<f32>(0.0), vec2<f32>(1.0)); |
| 161 | } |
| 162 | |
| 163 | fn apply_uv_xform(raw_uv: vec2<f32>, m: vec4<f32>, off_wrap: vec4<f32>) -> vec2<f32> { |
| 164 | let uv = vec2<f32>(m.x * raw_uv.x + m.z * raw_uv.y, m.y * raw_uv.x + m.w * raw_uv.y) + off_wrap.xy; |
| 165 | let wrap = i32(off_wrap.z + 0.5); |
| 166 | if (wrap == 1 || wrap == 2) { |
| 167 | return wrap_uv_only(uv, off_wrap.z); |
| 168 | } |
| 169 | return uv; |
| 170 | } |
| 171 | "#; |
| 172 | |
| 173 | /// Per-caster `model` plus per-face view-projection and light position; writes the distance to |
| 174 | /// the light so the main pass can compare it against a fragment's own distance. Fusion's |
| 175 | /// `RendererOpenGL` shadow pass renders geometry depth regardless of texture or material alpha. |
| 176 | pub(super) const SHADOW_SHADER: &str = r#" |
| 177 | struct Caster { model: mat4x4<f32> }; |
| 178 | struct Face { vp: mat4x4<f32>, light_pos: vec4<f32> }; |
| 179 | |
| 180 | @group(0) @binding(0) var<uniform> caster: Caster; |
| 181 | @group(1) @binding(0) var<uniform> face: Face; |
| 182 | |
| 183 | struct VsOut { |
| 184 | @builtin(position) clip: vec4<f32>, |
| 185 | @location(0) world_pos: vec3<f32>, |
| 186 | }; |
| 187 | |
| 188 | @vertex |
| 189 | fn vs_shadow(@location(0) pos: vec3<f32>) -> VsOut { |
| 190 | let wp = caster.model * vec4<f32>(pos, 1.0); |
| 191 | return VsOut(face.vp * wp, wp.xyz); |
| 192 | } |
| 193 | |
| 194 | @fragment |
| 195 | fn fs_shadow(in: VsOut) -> @location(0) f32 { |
| 196 | return distance(in.world_pos, face.light_pos.xyz); |
| 197 | } |
| 198 | "#; |
| 199 | |
| 200 | /// WGSL source; `MAX_LIGHTS`, `MAX_DIR_LIGHTS`, `MAX_SHADOW_LIGHTS` and `SHADOW_MAP_SIZE` are |
| 201 | /// substituted with the Rust constants at pipeline creation. |
| 202 | pub(super) const SHADER_3D: &str = r#" |
| 203 | struct MeshUniform { |
| 204 | mvp: mat4x4<f32>, |
| 205 | model: mat4x4<f32>, |
| 206 | view: mat4x4<f32>, |
| 207 | dod: vec4<f32>, |
| 208 | flags: vec4<f32>, |
| 209 | diffuse_col: vec4<f32>, |
| 210 | specular_col: vec4<f32>, |
| 211 | ambient: vec4<f32>, |
| 212 | dir_to_light: array<vec4<f32>, MAX_DIR_LIGHTS>, |
| 213 | dir_color: array<vec4<f32>, MAX_DIR_LIGHTS>, |
| 214 | cam_pos: vec4<f32>, |
| 215 | light_counts: vec4<f32>, |
| 216 | uv_xform: vec4<f32>, |
| 217 | uv_off_wrap: vec4<f32>, |
| 218 | mat_fx: vec4<f32>, |
| 219 | bump_uv_xform: vec4<f32>, |
| 220 | bump_off_wrap: vec4<f32>, |
| 221 | bump_dod: vec4<f32>, |
| 222 | bump_params: vec4<f32>, |
| 223 | mtl_params: vec4<f32>, |
| 224 | env_rot_0: vec4<f32>, |
| 225 | env_rot_1: vec4<f32>, |
| 226 | env_rot_2: vec4<f32>, |
| 227 | env_angles: vec4<f32>, |
| 228 | refl_params: vec4<f32>, |
| 229 | refl_tint: vec4<f32>, |
| 230 | refract_tint: vec4<f32>, |
| 231 | cb_ops_a: vec4<f32>, |
| 232 | cb_ops: vec4<f32>, |
| 233 | cb_ops_b: vec4<f32>, |
| 234 | cb_fg_col: vec4<f32>, |
| 235 | cb_flags: vec4<f32>, |
| 236 | point_pos: array<vec4<f32>, MAX_LIGHTS>, |
| 237 | point_col: array<vec4<f32>, MAX_LIGHTS>, |
| 238 | spot_pos: array<vec4<f32>, MAX_LIGHTS>, |
| 239 | spot_dir: array<vec4<f32>, MAX_LIGHTS>, |
| 240 | spot_col: array<vec4<f32>, MAX_LIGHTS>, |
| 241 | }; |
| 242 | |
| 243 | struct ShadowUniform { |
| 244 | face_vp: array<mat4x4<f32>, 6 * MAX_SHADOW_LIGHTS>, |
| 245 | point: array<vec4<f32>, MAX_LIGHTS>, |
| 246 | }; |
| 247 | |
| 248 | override BACK_PASS: bool = false; |
| 249 | |
| 250 | @group(0) @binding(0) var<uniform> u: MeshUniform; |
| 251 | @group(0) @binding(1) var mat_tex: texture_2d<f32>; |
| 252 | @group(0) @binding(2) var mat_samp: sampler; |
| 253 | @group(0) @binding(3) var<uniform> shadow: ShadowUniform; |
| 254 | @group(0) @binding(4) var shadow_maps: texture_2d_array<f32>; |
| 255 | @group(0) @binding(5) var bump_tex: texture_2d<f32>; |
| 256 | |
| 257 | struct VsIn { |
| 258 | @location(0) pos: vec3<f32>, |
| 259 | @location(1) normal: vec3<f32>, |
| 260 | @location(2) uv: vec2<f32>, |
| 261 | }; |
| 262 | |
| 263 | struct VsOut { |
| 264 | @builtin(position) clip: vec4<f32>, |
| 265 | @location(0) uv: vec2<f32>, |
| 266 | @location(1) world_pos: vec3<f32>, |
| 267 | @location(2) world_n: vec3<f32>, |
| 268 | @location(3) local_pos: vec3<f32>, |
| 269 | @location(4) local_n: vec3<f32>, |
| 270 | }; |
| 271 | |
| 272 | // Cofactor matrix of the upper-left 3x3 of `m` (inverse-transpose up to scale), so normals stay |
| 273 | // perpendicular under non-uniform scale. Dividing by |det| instead of det keeps mirrored |
| 274 | // transforms pointing outward. |
| 275 | fn normal_matrix(m: mat4x4<f32>) -> mat3x3<f32> { |
| 276 | let c0 = m[0].xyz; |
| 277 | let c1 = m[1].xyz; |
| 278 | let c2 = m[2].xyz; |
| 279 | let sign = select(-1.0, 1.0, dot(c0, cross(c1, c2)) >= 0.0); |
| 280 | return mat3x3<f32>(cross(c1, c2), cross(c2, c0), cross(c0, c1)) * sign; |
| 281 | } |
| 282 | |
| 283 | @vertex |
| 284 | fn vs_mesh(v: VsIn) -> VsOut { |
| 285 | var out: VsOut; |
| 286 | let wp = u.model * vec4<f32>(v.pos, 1.0); |
| 287 | out.clip = u.mvp * vec4<f32>(v.pos, 1.0); |
| 288 | out.uv = v.uv; |
| 289 | out.world_pos = wp.xyz; |
| 290 | let wn = normal_matrix(u.model) * v.normal; |
| 291 | let nlen = length(wn); |
| 292 | var n_norm = select(vec3<f32>(0.0, 0.0, 1.0), wn / nlen, nlen > 1e-5); |
| 293 | var ln = v.normal; |
| 294 | if (u.flags.w > 0.5) { |
| 295 | n_norm = -n_norm; |
| 296 | ln = -ln; |
| 297 | } |
| 298 | out.world_n = n_norm; |
| 299 | out.local_pos = v.pos; |
| 300 | out.local_n = ln; |
| 301 | return out; |
| 302 | } |
| 303 | |
| 304 | fn light_attenuation(dist: f32, decay_type: f32) -> f32 { |
| 305 | let dt = i32(decay_type + 0.5); |
| 306 | let d = max(dist, 1e-3); |
| 307 | if (dt == 1) { |
| 308 | return 1.0 / d; |
| 309 | } else if (dt == 2) { |
| 310 | return 1.0 / (d * d); |
| 311 | } |
| 312 | return 1.0; |
| 313 | } |
| 314 | |
| 315 | /// Fraction of PCF taps where the cube map of point light `i` records a closer occluder. |
| 316 | fn point_shadow_occlusion(i: i32, world_pos: vec3<f32>, n: vec3<f32>, light_pos: vec3<f32>) -> f32 { |
| 317 | let params = shadow.point[i]; |
| 318 | if (params.x < -0.5) { |
| 319 | return 0.0; |
| 320 | } |
| 321 | let slot = i32(params.x + 0.5); |
| 322 | let dist = length(world_pos - light_pos); |
| 323 | // World size of one texel at this distance for a 90 degree face. |
| 324 | let texel = 2.0 * dist / f32(SHADOW_MAP_SIZE); |
| 325 | // The face is picked from the offset point so the lookup never straddles a cube seam. |
| 326 | let biased = world_pos + n * (texel * 1.5); |
| 327 | let d = biased - light_pos; |
| 328 | let a = abs(d); |
| 329 | var face = 4; |
| 330 | if (a.x >= a.y && a.x >= a.z) { |
| 331 | face = select(1, 0, d.x > 0.0); |
| 332 | } else if (a.y >= a.z) { |
| 333 | face = select(3, 2, d.y > 0.0); |
| 334 | } else { |
| 335 | face = select(5, 4, d.z > 0.0); |
| 336 | } |
| 337 | let clip = shadow.face_vp[slot * 6 + face] * vec4<f32>(biased, 1.0); |
| 338 | if (clip.w <= 1e-5) { |
| 339 | return 0.0; |
| 340 | } |
| 341 | let ndc = clip.xy / clip.w; |
| 342 | let base = vec2<i32>(floor(vec2<f32>(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5) * f32(SHADOW_MAP_SIZE))); |
| 343 | let last = SHADOW_MAP_SIZE - 1; |
| 344 | var occluded = 0.0; |
| 345 | for (var dy: i32 = -1; dy <= 1; dy = dy + 1) { |
| 346 | for (var dx: i32 = -1; dx <= 1; dx = dx + 1) { |
| 347 | let t = clamp(base + vec2<i32>(dx, dy), vec2<i32>(0), vec2<i32>(last)); |
| 348 | let stored = textureLoad(shadow_maps, t, slot * 6 + face, 0).r; |
| 349 | occluded = occluded + select(0.0, 1.0, dist - texel * 3.0 > stored); |
| 350 | } |
| 351 | } |
| 352 | return occluded / 9.0; |
| 353 | } |
| 354 | |
| 355 | struct TangentFrame { |
| 356 | t: vec3<f32>, |
| 357 | b: vec3<f32>, |
| 358 | }; |
| 359 | |
| 360 | fn compute_tangent_frame( |
| 361 | base_n: vec3<f32>, |
| 362 | dpdx_w: vec3<f32>, |
| 363 | dpdy_w: vec3<f32>, |
| 364 | duvdx: vec2<f32>, |
| 365 | duvdy: vec2<f32>, |
| 366 | ) -> TangentFrame { |
| 367 | let det = duvdx.x * duvdy.y - duvdx.y * duvdy.x; |
| 368 | var t_vec: vec3<f32>; |
| 369 | var b_vec: vec3<f32>; |
| 370 | if (abs(det) > 1e-6) { |
| 371 | let inv_det = 1.0 / det; |
| 372 | t_vec = (dpdx_w * duvdy.y - dpdy_w * duvdx.y) * inv_det; |
| 373 | b_vec = (dpdy_w * duvdx.x - dpdx_w * duvdy.x) * inv_det; |
| 374 | t_vec = t_vec - base_n * dot(t_vec, base_n); |
| 375 | let t_len = length(t_vec); |
| 376 | if (t_len > 1e-5) { |
| 377 | t_vec = t_vec / t_len; |
| 378 | let b_sign = select(-1.0, 1.0, dot(cross(base_n, t_vec), b_vec) >= 0.0); |
| 379 | b_vec = cross(base_n, t_vec) * b_sign; |
| 380 | return TangentFrame(t_vec, b_vec); |
| 381 | } |
| 382 | } |
| 383 | let up = select(vec3<f32>(0.0, 1.0, 0.0), vec3<f32>(1.0, 0.0, 0.0), abs(base_n.y) > 0.9); |
| 384 | t_vec = normalize(cross(up, base_n)); |
| 385 | b_vec = cross(base_n, t_vec); |
| 386 | return TangentFrame(t_vec, b_vec); |
| 387 | } |
| 388 | |
| 389 | fn eval_light_contrib( |
| 390 | n: vec3<f32>, |
| 391 | t_vec: vec3<f32>, |
| 392 | b_vec: vec3<f32>, |
| 393 | v: vec3<f32>, |
| 394 | l: vec3<f32>, |
| 395 | light_rgb: vec3<f32>, |
| 396 | albedo: vec3<f32>, |
| 397 | spec_rgb: vec3<f32>, |
| 398 | spec_int: f32, |
| 399 | shininess: f32, |
| 400 | diffuse_vis: f32, |
| 401 | spec_vis: f32, |
| 402 | ) -> vec3<f32> { |
| 403 | let ndotl = max(dot(n, l), 0.0); |
| 404 | if (ndotl <= 0.0) { |
| 405 | return vec3<f32>(0.0); |
| 406 | } |
| 407 | let model = i32(u.mat_fx.w + 0.5); |
| 408 | if (model == 3) { |
| 409 | return albedo * light_rgb * (ndotl * diffuse_vis / 3.14159265); |
| 410 | } |
| 411 | var out = albedo * light_rgb * (ndotl * diffuse_vis); |
| 412 | if (spec_int > 1e-4) { |
| 413 | let h = normalize(l + v); |
| 414 | var lobe: f32; |
| 415 | if (model == 1) { |
| 416 | lobe = pow(max(dot(reflect(-l, n), v), 0.0), shininess); |
| 417 | } else if (model == 2) { |
| 418 | // Cook-Torrance: Beckmann D, Torrance-Sparrow G, exact unpolarized Fresnel F. |
| 419 | // `eval_light_contrib` omits the outer N·L factor on `lobe`, so `(F*D*G)/(4*N·L*N·V) * N·L` |
| 420 | // simplifies to `(F*D*G)/(4*N·V)`. |
| 421 | let nh = max(dot(n, h), 1e-3); |
| 422 | let nv = max(dot(n, v), 1e-3); |
| 423 | let vh = max(dot(v, h), 1e-3); |
| 424 | let m2 = max(shininess * shininess, 1e-3); |
| 425 | let nh2 = nh * nh; |
| 426 | let d_term = exp(-(1.0 - nh2) / (nh2 * m2)) / (3.14159265 * m2 * nh2 * nh2); |
| 427 | let g_term = min(1.0, min(2.0 * nh * nv / vh, 2.0 * nh * ndotl / vh)); |
| 428 | var fresnel = 1.0; |
| 429 | if (u.mtl_params.z > 0.5) { |
| 430 | let eta = max(u.mtl_params.y, 1.0); |
| 431 | let c = vh; |
| 432 | let g = sqrt(max(eta * eta + c * c - 1.0, 0.0)); |
| 433 | let gmc = g - c; |
| 434 | let gpc = max(g + c, 1e-5); |
| 435 | let r_s = gmc / gpc; |
| 436 | let r_p = (c * gpc - 1.0) / max(c * gmc + 1.0, 1e-5); |
| 437 | fresnel = 0.5 * r_s * r_s * (1.0 + r_p * r_p); |
| 438 | } |
| 439 | lobe = (fresnel * d_term * g_term) / (4.0 * nv); |
| 440 | } else if (model == 4) { |
| 441 | // Anisotropic Ward (1992) BRDF * (N·L) using tangent frame (T, B) and SpreadU/SpreadV. |
| 442 | let nh = max(dot(n, h), 1e-3); |
| 443 | let nv = max(dot(n, v), 1e-3); |
| 444 | let su = max(shininess, 1e-3); |
| 445 | let sv = max(u.mtl_params.w, 1e-3); |
| 446 | let ht = dot(h, t_vec) / su; |
| 447 | let hb = dot(h, b_vec) / sv; |
| 448 | let expo = -(ht * ht + hb * hb) / (nh * nh); |
| 449 | let denom = max(4.0 * 3.14159265 * su * sv * sqrt(nv), 1e-4); |
| 450 | lobe = (sqrt(ndotl) / denom) * exp(max(expo, -80.0)); |
| 451 | } else { |
| 452 | lobe = pow(max(dot(n, h), 0.0), shininess); |
| 453 | } |
| 454 | out = out + light_rgb * spec_rgb * (lobe * spec_int * spec_vis); |
| 455 | } |
| 456 | return out; |
| 457 | } |
| 458 | |
| 459 | fn sample_bump_height(uv_in: vec2<f32>, wrap_f: f32, ch: i32) -> f32 { |
| 460 | let uv = wrap_uv_only(uv_in, wrap_f); |
| 461 | let c = textureSampleLevel(bump_tex, mat_samp, uv, 0.0); |
| 462 | if (ch == 1) { |
| 463 | return (c.r + c.g + c.b) / 3.0; |
| 464 | } else if (ch == 2) { |
| 465 | return max(c.r, max(c.g, c.b)); |
| 466 | } else if (ch == 3) { |
| 467 | return min(c.r, min(c.g, c.b)); |
| 468 | } else if (ch == 4) { |
| 469 | return c.r; |
| 470 | } else if (ch == 5) { |
| 471 | return c.g; |
| 472 | } else if (ch == 6) { |
| 473 | return c.b; |
| 474 | } else if (ch == 7) { |
| 475 | return c.a; |
| 476 | } |
| 477 | return dot(c.rgb, vec3<f32>(0.299, 0.587, 0.114)); |
| 478 | } |
| 479 | |
| 480 | fn perturb_bump_normal( |
| 481 | base_n: vec3<f32>, |
| 482 | dpdx_w: vec3<f32>, |
| 483 | dpdy_w: vec3<f32>, |
| 484 | duvdx: vec2<f32>, |
| 485 | duvdy: vec2<f32>, |
| 486 | uv_in: vec2<f32>, |
| 487 | bump_scale: f32, |
| 488 | bump_mode: f32, |
| 489 | bump_wrap: f32, |
| 490 | clamp_z: f32, |
| 491 | bump_ch: i32, |
| 492 | ) -> vec3<f32> { |
| 493 | var uv = uv_in; |
| 494 | if (u.bump_dod.z > 0.5 && u.bump_dod.w > 0.5) { |
| 495 | let bdims = vec2<f32>(textureDimensions(bump_tex)); |
| 496 | let canvas_px = vec2<f32>(uv.x * u.bump_dod.z, uv.y * u.bump_dod.w); |
| 497 | uv = (canvas_px - u.bump_dod.xy) / bdims; |
| 498 | } |
| 499 | let frame = compute_tangent_frame(base_n, dpdx_w, dpdy_w, duvdx, duvdy); |
| 500 | let t_vec = frame.t; |
| 501 | let b_vec = frame.b; |
| 502 | |
| 503 | if (bump_mode > 0.5) { |
| 504 | let b_col = textureSampleLevel(bump_tex, mat_samp, wrap_uv_only(uv, bump_wrap), 0.0); |
| 505 | let b_rgb = select(vec3<f32>(0.5, 0.5, 1.0), b_col.rgb / max(b_col.a, 1e-5), b_col.a > 1e-5); |
| 506 | let tn = b_rgb * 2.0 - vec3<f32>(1.0); |
| 507 | let perturbed = t_vec * tn.x + b_vec * tn.y + base_n * tn.z; |
| 508 | let plen = length(perturbed); |
| 509 | return select(base_n, perturbed / plen, plen > 1e-5); |
| 510 | } |
| 511 | |
| 512 | let inv_dim = vec2<f32>(1.0) / max(vec2<f32>(textureDimensions(bump_tex)), vec2<f32>(1.0)); |
| 513 | let h_l = sample_bump_height(uv - vec2<f32>(inv_dim.x, 0.0), bump_wrap, bump_ch); |
| 514 | let h_r = sample_bump_height(uv + vec2<f32>(inv_dim.x, 0.0), bump_wrap, bump_ch); |
| 515 | let h_d = sample_bump_height(uv - vec2<f32>(0.0, inv_dim.y), bump_wrap, bump_ch); |
| 516 | let h_u = sample_bump_height(uv + vec2<f32>(0.0, inv_dim.y), bump_wrap, bump_ch); |
| 517 | let dhdu = (h_r - h_l) * 0.5 * bump_scale; |
| 518 | let dhdv = (h_u - h_d) * 0.5 * bump_scale; |
| 519 | return normalize(base_n - t_vec * dhdu + b_vec * dhdv); |
| 520 | } |
| 521 | |
| 522 | fn sample_sphere_map(world_dir: vec3<f32>) -> vec4<f32> { |
| 523 | let slot = i32(u.env_rot_2.w + 0.5); |
| 524 | if (slot == 0) { |
| 525 | return vec4<f32>(u.refl_tint.rgb, 1.0); |
| 526 | } |
| 527 | let rot = mat3x3<f32>(u.env_rot_0.xyz, u.env_rot_1.xyz, u.env_rot_2.xyz); |
| 528 | let d = normalize(rot * world_dir); |
| 529 | let map_mode = i32(u.env_rot_1.w + 0.5); |
| 530 | var uv: vec2<f32>; |
| 531 | if (map_mode == 1) { |
| 532 | let r_xy = length(d.xy); |
| 533 | let r = select(0.0, acos(clamp(-d.z, -1.0, 1.0)) / (3.14159265 * r_xy), r_xy > 1e-5); |
| 534 | uv = clamp(vec2<f32>(0.5 + d.x * r * 0.5, 0.5 - d.y * r * 0.5), vec2<f32>(0.0), vec2<f32>(1.0)); |
| 535 | } else { |
| 536 | let theta = atan2(d.x, d.z); |
| 537 | var u_c = fract(theta / 6.2831853 + 0.75); |
| 538 | u_c = fract((u_c - u.env_angles.x) * u.env_angles.y); |
| 539 | let v_c = acos(clamp(d.y, -1.0, 1.0)) / 3.14159265; |
| 540 | uv = vec2<f32>(u_c, v_c); |
| 541 | } |
| 542 | var raw: vec4<f32>; |
| 543 | if (slot == 2) { |
| 544 | if (u.bump_dod.z > 0.5 && u.bump_dod.w > 0.5) { |
| 545 | let dims = vec2<f32>(textureDimensions(bump_tex)); |
| 546 | uv = clamp((vec2<f32>(uv.x * u.bump_dod.z, uv.y * u.bump_dod.w) - u.bump_dod.xy) / dims, vec2<f32>(0.0), vec2<f32>(1.0)); |
| 547 | } |
| 548 | raw = textureSampleLevel(bump_tex, mat_samp, uv, 0.0); |
| 549 | } else { |
| 550 | if (u.dod.z > 0.5 && u.dod.w > 0.5) { |
| 551 | let dims = vec2<f32>(textureDimensions(mat_tex)); |
| 552 | uv = clamp((vec2<f32>(uv.x * u.dod.z, uv.y * u.dod.w) - u.dod.xy) / dims, vec2<f32>(0.0), vec2<f32>(1.0)); |
| 553 | } |
| 554 | raw = textureSampleLevel(mat_tex, mat_samp, uv, 0.0); |
| 555 | } |
| 556 | let straight = select(vec3<f32>(0.0), raw.rgb / max(raw.a, 1e-5), raw.a > 1e-5); |
| 557 | return vec4<f32>(straight * u.refl_tint.rgb, raw.a); |
| 558 | } |
| 559 | |
| 560 | fn cb_hls(c: vec3<f32>) -> vec3<f32> { |
| 561 | let mx = max(c.r, max(c.g, c.b)); |
| 562 | let mn = min(c.r, min(c.g, c.b)); |
| 563 | let l = 0.5 * (mx + mn); |
| 564 | let d = mx - mn; |
| 565 | if (d <= 1e-6) { |
| 566 | return vec3<f32>(0.0, l, 0.0); |
| 567 | } |
| 568 | let s = d / max(1.0 - abs(2.0 * l - 1.0), 1e-6); |
| 569 | var h = 0.0; |
| 570 | if (mx == c.r) { |
| 571 | h = (c.g - c.b) / d + select(0.0, 6.0, c.g < c.b); |
| 572 | } else if (mx == c.g) { |
| 573 | h = (c.b - c.r) / d + 2.0; |
| 574 | } else { |
| 575 | h = (c.r - c.g) / d + 4.0; |
| 576 | } |
| 577 | return vec3<f32>(h / 6.0, l, clamp(s, 0.0, 1.0)); |
| 578 | } |
| 579 | |
| 580 | fn cb_operand( |
| 581 | idx: i32, |
| 582 | bg: vec4<f32>, |
| 583 | fg: vec4<f32>, |
| 584 | pos: vec3<f32>, |
| 585 | uv: vec2<f32>, |
| 586 | env_c: vec3<f32>, |
| 587 | n: vec3<f32>, |
| 588 | ) -> f32 { |
| 589 | switch idx { |
| 590 | case 0: { return fg.r; } |
| 591 | case 1: { return fg.g; } |
| 592 | case 2: { return fg.b; } |
| 593 | case 3: { return fg.a; } |
| 594 | case 4: { return bg.r; } |
| 595 | case 5: { return bg.g; } |
| 596 | case 6: { return bg.b; } |
| 597 | case 7: { return bg.a; } |
| 598 | case 8: { return 0.0; } |
| 599 | case 9: { return 1.0; } |
| 600 | case 10: { return 0.5; } |
| 601 | case 11: { return cb_hls(fg.rgb).x; } |
| 602 | case 12: { return cb_hls(fg.rgb).y; } |
| 603 | case 13: { return cb_hls(fg.rgb).z; } |
| 604 | case 14: { return cb_hls(bg.rgb).x; } |
| 605 | case 15: { return cb_hls(bg.rgb).y; } |
| 606 | case 16: { return cb_hls(bg.rgb).z; } |
| 607 | case 17: { return dot(fg.rgb, vec3<f32>(0.299, 0.587, 0.114)); } |
| 608 | case 18: { return dot(bg.rgb, vec3<f32>(0.299, 0.587, 0.114)); } |
| 609 | case 19: { return pos.x; } |
| 610 | case 20: { return pos.y; } |
| 611 | case 21: { return pos.z; } |
| 612 | case 22: { return uv.x; } |
| 613 | case 23: { return 1.0 - uv.y; } |
| 614 | case 24: { return 0.0; } |
| 615 | case 25: { return env_c.x; } |
| 616 | case 26: { return env_c.y; } |
| 617 | case 27: { return env_c.z; } |
| 618 | case 28: { return n.x; } |
| 619 | case 29: { return n.y; } |
| 620 | case 30: { return n.z; } |
| 621 | default: { return bg.r; } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | fn cb_combine(op: i32, a: f32, b: f32) -> f32 { |
| 626 | switch op { |
| 627 | case 0: { return a; } |
| 628 | case 1: { return b; } |
| 629 | case 2: { return 1.0 - a; } |
| 630 | case 3: { return 1.0 - b; } |
| 631 | case 4: { return a + b; } |
| 632 | case 5: { return a - b; } |
| 633 | case 6: { return a * b; } |
| 634 | case 7: { return select(a / b, 0.0, abs(b) <= 1e-5); } |
| 635 | case 8: { return min(a, b); } |
| 636 | case 9: { return max(a, b); } |
| 637 | case 10: { return 0.5 * (a + b); } |
| 638 | default: { return a; } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | @fragment |
| 643 | fn fs_mesh(in: VsOut) -> @location(0) vec4<f32> { |
| 644 | let uv_xformed = apply_uv_xform(in.uv, u.uv_xform, u.uv_off_wrap); |
| 645 | let uv_bump_xformed = apply_uv_xform(in.uv, u.bump_uv_xform, u.bump_off_wrap); |
| 646 | let dpdx_w = dpdx(in.world_pos); |
| 647 | let dpdy_w = dpdy(in.world_pos); |
| 648 | let duvdx = dpdx(uv_bump_xformed); |
| 649 | let duvdy = dpdy(uv_bump_xformed); |
| 650 | let duvdx_main = dpdx(uv_xformed); |
| 651 | let duvdy_main = dpdy(uv_xformed); |
| 652 | let facing_away = dot(in.world_n, u.cam_pos.xyz - in.world_pos) < 0.0; |
| 653 | if (facing_away != BACK_PASS) { |
| 654 | discard; |
| 655 | } |
| 656 | |
| 657 | let env_mode = i32(u.env_rot_0.w + 0.5); |
| 658 | let cb_on = u.cb_flags.x > 0.5; |
| 659 | let v_dir = normalize(u.cam_pos.xyz - in.world_pos); |
| 660 | var n_surf = normalize(in.world_n); |
| 661 | if (u.bump_off_wrap.w > 0.5 && (u.mat_fx.y > 0.5 || abs(u.mat_fx.x) > 1e-5)) { |
| 662 | n_surf = perturb_bump_normal( |
| 663 | n_surf, |
| 664 | dpdx_w, |
| 665 | dpdy_w, |
| 666 | duvdx, |
| 667 | duvdy, |
| 668 | uv_bump_xformed, |
| 669 | u.mat_fx.x, |
| 670 | u.mat_fx.y, |
| 671 | u.bump_off_wrap.z, |
| 672 | u.bump_params.x, |
| 673 | i32(u.bump_params.y + 0.5), |
| 674 | ); |
| 675 | } |
| 676 | |
| 677 | var base_straight = vec3<f32>(0.0); |
| 678 | var rgb_alpha = u.diffuse_col.a; |
| 679 | var out_alpha = rgb_alpha * u.mtl_params.x; |
| 680 | |
| 681 | if (env_mode == 1) { |
| 682 | // Standalone SphereMap shader: equirectangular/angular lookup along view ray. |
| 683 | let env_c = sample_sphere_map(-v_dir); |
| 684 | base_straight = env_c.rgb * u.diffuse_col.rgb; |
| 685 | rgb_alpha = env_c.a * u.diffuse_col.a; |
| 686 | out_alpha = rgb_alpha * u.mtl_params.x; |
| 687 | } else { |
| 688 | var col = u.diffuse_col; |
| 689 | var tex_col = vec4<f32>(1.0); |
| 690 | var uv_samp = uv_xformed; |
| 691 | var tex_straight = vec3<f32>(1.0); |
| 692 | if (u.flags.x > 0.5) { |
| 693 | if (u.dod.z > 0.5 && u.dod.w > 0.5) { |
| 694 | let tdims = vec2<f32>(textureDimensions(mat_tex)); |
| 695 | let canvas_px = vec2<f32>(uv_samp.x * u.dod.z, uv_samp.y * u.dod.w); |
| 696 | uv_samp = (canvas_px - u.dod.xy) / tdims; |
| 697 | if (uv_samp.x < 0.0 || uv_samp.x > 1.0 || uv_samp.y < 0.0 || uv_samp.y > 1.0) { |
| 698 | discard; |
| 699 | } |
| 700 | } |
| 701 | tex_col = textureSampleLevel(mat_tex, mat_samp, uv_samp, 0.0); |
| 702 | if (u.mat_fx.z <= 0.5) { |
| 703 | tex_straight = select(vec3<f32>(0.0), tex_col.rgb / max(tex_col.a, 1e-5), tex_col.a > 1e-5); |
| 704 | } |
| 705 | col = select(tex_col, vec4<f32>(1.0), u.mat_fx.z > 0.5) * u.diffuse_col; |
| 706 | } |
| 707 | // `rgb_alpha` is `tex_col.a * Diffuse.Opacity`; `Diffuse.Color.Alpha` (`u.mtl_params.x`) |
| 708 | // modulates output alpha without attenuating diffuse/specular RGB. |
| 709 | rgb_alpha = col.a; |
| 710 | out_alpha = rgb_alpha * u.mtl_params.x; |
| 711 | if (env_mode == 0 && !cb_on && out_alpha <= 0.002) { |
| 712 | discard; |
| 713 | } |
| 714 | |
| 715 | let albedo = tex_straight * u.diffuse_col.rgb; |
| 716 | if (env_mode == 2 && u.refl_tint.w < 0.5) { |
| 717 | base_straight = vec3<f32>(0.0); |
| 718 | rgb_alpha = 0.0; |
| 719 | out_alpha = 0.0; |
| 720 | } else if (u.flags.y > 0.5) { |
| 721 | let tframe = compute_tangent_frame(n_surf, dpdx_w, dpdy_w, duvdx_main, duvdy_main); |
| 722 | let spec_rgb = u.specular_col.rgb; |
| 723 | let shininess = select(max(u.specular_col.w, 1.0), u.specular_col.w, u.mat_fx.w > 1.5); |
| 724 | let spec_int = u.flags.z; |
| 725 | |
| 726 | var lit = albedo * u.ambient.rgb; |
| 727 | let n_dir = i32(u.light_counts.z + 0.5); |
| 728 | for (var i: i32 = 0; i < MAX_DIR_LIGHTS; i = i + 1) { |
| 729 | if (i >= n_dir) { break; } |
| 730 | let l = normalize(u.dir_to_light[i].xyz); |
| 731 | lit = lit + eval_light_contrib(n_surf, tframe.t, tframe.b, v_dir, l, u.dir_color[i].rgb, albedo, spec_rgb, spec_int, shininess, 1.0, 1.0); |
| 732 | } |
| 733 | |
| 734 | let n_pt = i32(u.light_counts.x + 0.5); |
| 735 | for (var i: i32 = 0; i < MAX_LIGHTS; i = i + 1) { |
| 736 | if (i >= n_pt) { break; } |
| 737 | let to_l = u.point_pos[i].xyz - in.world_pos; |
| 738 | let dist = max(length(to_l), 1e-4); |
| 739 | let att = light_attenuation(dist, u.point_pos[i].w); |
| 740 | var occ = 0.0; |
| 741 | if (u.light_counts.w > 0.5) { |
| 742 | occ = point_shadow_occlusion(i, in.world_pos, n_surf, u.point_pos[i].xyz); |
| 743 | } |
| 744 | lit = lit + eval_light_contrib( |
| 745 | n_surf, tframe.t, tframe.b, v_dir, to_l / dist, u.point_col[i].rgb * att, albedo, spec_rgb, spec_int, shininess, |
| 746 | 1.0 - shadow.point[i].y * occ, 1.0 - occ, |
| 747 | ); |
| 748 | } |
| 749 | |
| 750 | let n_sp = i32(u.light_counts.y + 0.5); |
| 751 | for (var i: i32 = 0; i < MAX_LIGHTS; i = i + 1) { |
| 752 | if (i >= n_sp) { break; } |
| 753 | let to_l = u.spot_pos[i].xyz - in.world_pos; |
| 754 | let dist = max(length(to_l), 1e-4); |
| 755 | let l_dir = to_l / dist; |
| 756 | let cos_theta = dot(-l_dir, normalize(u.spot_dir[i].xyz)); |
| 757 | let cos_inner = u.spot_pos[i].w; |
| 758 | let cos_outer = u.spot_dir[i].w; |
| 759 | let cone = clamp((cos_theta - cos_outer) / max(cos_inner - cos_outer, 1e-4), 0.0, 1.0); |
| 760 | if (cone > 0.0) { |
| 761 | let att = light_attenuation(dist, u.spot_col[i].w) * cone; |
| 762 | lit = lit + eval_light_contrib(n_surf, tframe.t, tframe.b, v_dir, l_dir, u.spot_col[i].rgb * att, albedo, spec_rgb, spec_int, shininess, 1.0, 1.0); |
| 763 | } |
| 764 | } |
| 765 | base_straight = lit; |
| 766 | } else { |
| 767 | base_straight = albedo * u.uv_off_wrap.w; |
| 768 | } |
| 769 | |
| 770 | if (env_mode == 2) { |
| 771 | let r_dir = reflect(-v_dir, n_surf); |
| 772 | let refl_c = sample_sphere_map(r_dir); |
| 773 | let strength_var = i32(u.env_angles.z + 0.5); |
| 774 | var refl_str = clamp(u.env_angles.w, 0.0, 1.0); |
| 775 | if (strength_var != 0) { |
| 776 | let cos_nv = clamp(dot(n_surf, v_dir), 0.0, 1.0); |
| 777 | let f_ang = pow(1.0 - cos_nv, max(u.refl_params.z, 1e-4)); |
| 778 | refl_str = clamp(mix(u.refl_params.y, u.refl_params.x, f_ang), 0.0, 1.0); |
| 779 | } |
| 780 | var under_rgb = base_straight; |
| 781 | var under_a = rgb_alpha; |
| 782 | if (rgb_alpha < 0.999 && u.refl_params.w > 0.01 && u.env_rot_2.w > 0.5 && u.refl_tint.w > 0.5) { |
| 783 | let eta = 1.0 / max(u.refl_params.w, 0.01); |
| 784 | var refr_dir = refract(-v_dir, n_surf, eta); |
| 785 | if (dot(refr_dir, refr_dir) < 1e-5) { |
| 786 | refr_dir = r_dir; |
| 787 | } |
| 788 | let refr_c = sample_sphere_map(refr_dir); |
| 789 | under_rgb = mix(refr_c.rgb * u.refract_tint.rgb, base_straight, rgb_alpha); |
| 790 | under_a = clamp(max(rgb_alpha, refr_c.a * (1.0 - rgb_alpha)), 0.0, 1.0); |
| 791 | } |
| 792 | let under_premul = under_rgb * under_a; |
| 793 | let refl_a = refl_str * refl_c.a; |
| 794 | let out_premul = under_premul * (1.0 - refl_str) + refl_c.rgb * refl_a; |
| 795 | let final_a = clamp(under_a * (1.0 - refl_str) + refl_a, 0.0, 1.0); |
| 796 | base_straight = select(vec3<f32>(0.0), out_premul / max(final_a, 1e-5), final_a > 1e-5); |
| 797 | rgb_alpha = final_a; |
| 798 | out_alpha = clamp(out_alpha * (1.0 - refl_str) + refl_a, 0.0, 1.0); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | if (cb_on) { |
| 803 | let bg_c = vec4<f32>(base_straight, out_alpha); |
| 804 | var fg_c = u.cb_fg_col; |
| 805 | if (u.cb_flags.z > 1.5) { |
| 806 | let fg_raw = textureSampleLevel(bump_tex, mat_samp, wrap_uv_only(uv_xformed, u.bump_off_wrap.z), 0.0); |
| 807 | let fg_s = select(vec3<f32>(0.0), fg_raw.rgb / max(fg_raw.a, 1e-5), fg_raw.a > 1e-5); |
| 808 | fg_c = vec4<f32>(fg_s * u.cb_fg_col.rgb, fg_raw.a * u.cb_fg_col.a); |
| 809 | } |
| 810 | let cspace = i32(u.cb_flags.y + 0.5); |
| 811 | var c_pos = in.world_pos; |
| 812 | var c_n = n_surf; |
| 813 | if (cspace == 0) { |
| 814 | c_pos = in.local_pos; |
| 815 | let ln_len = length(in.local_n); |
| 816 | c_n = select(vec3<f32>(0.0, 0.0, 1.0), in.local_n / ln_len, ln_len > 1e-5); |
| 817 | } else if (cspace == 2) { |
| 818 | c_pos = (u.view * vec4<f32>(in.world_pos, 1.0)).xyz; |
| 819 | let en = (u.view * vec4<f32>(n_surf, 0.0)).xyz; |
| 820 | let elen = length(en); |
| 821 | c_n = select(vec3<f32>(0.0, 0.0, 1.0), en / elen, elen > 1e-5); |
| 822 | } |
| 823 | let env_c = reflect(-v_dir, n_surf); |
| 824 | let r_out = cb_combine(i32(u.cb_ops.x + 0.5), cb_operand(i32(u.cb_ops_a.x + 0.5), bg_c, fg_c, c_pos, uv_xformed, env_c, c_n), cb_operand(i32(u.cb_ops_b.x + 0.5), bg_c, fg_c, c_pos, uv_xformed, env_c, c_n)); |
| 825 | let g_out = cb_combine(i32(u.cb_ops.y + 0.5), cb_operand(i32(u.cb_ops_a.y + 0.5), bg_c, fg_c, c_pos, uv_xformed, env_c, c_n), cb_operand(i32(u.cb_ops_b.y + 0.5), bg_c, fg_c, c_pos, uv_xformed, env_c, c_n)); |
| 826 | let b_out = cb_combine(i32(u.cb_ops.z + 0.5), cb_operand(i32(u.cb_ops_a.z + 0.5), bg_c, fg_c, c_pos, uv_xformed, env_c, c_n), cb_operand(i32(u.cb_ops_b.z + 0.5), bg_c, fg_c, c_pos, uv_xformed, env_c, c_n)); |
| 827 | let a_out = cb_combine(i32(u.cb_ops.w + 0.5), cb_operand(i32(u.cb_ops_a.w + 0.5), bg_c, fg_c, c_pos, uv_xformed, env_c, c_n), cb_operand(i32(u.cb_ops_b.w + 0.5), bg_c, fg_c, c_pos, uv_xformed, env_c, c_n)); |
| 828 | let clamped_a = clamp(a_out, 0.0, 1.0); |
| 829 | if (clamped_a <= 0.002) { |
| 830 | discard; |
| 831 | } |
| 832 | return vec4<f32>(clamp(vec3<f32>(r_out, g_out, b_out) * clamped_a, vec3<f32>(0.0), vec3<f32>(1.0)), clamped_a); |
| 833 | } |
| 834 | |
| 835 | if (out_alpha <= 0.002) { |
| 836 | discard; |
| 837 | } |
| 838 | return vec4<f32>(clamp(base_straight * rgb_alpha, vec3<f32>(0.0), vec3<f32>(1.0)), out_alpha); |
| 839 | } |
| 840 | |
| 841 | @fragment |
| 842 | fn fs_normal(in: VsOut) -> @location(0) vec4<f32> { |
| 843 | let uv_xformed = apply_uv_xform(in.uv, u.uv_xform, u.uv_off_wrap); |
| 844 | let uv_bump_xformed = apply_uv_xform(in.uv, u.bump_uv_xform, u.bump_off_wrap); |
| 845 | let dpdx_w = dpdx(in.world_pos); |
| 846 | let dpdy_w = dpdy(in.world_pos); |
| 847 | let duvdx = dpdx(uv_bump_xformed); |
| 848 | let duvdy = dpdy(uv_bump_xformed); |
| 849 | let facing_away = dot(in.world_n, u.cam_pos.xyz - in.world_pos) < 0.0; |
| 850 | if (facing_away != BACK_PASS) { |
| 851 | discard; |
| 852 | } |
| 853 | |
| 854 | var col = u.diffuse_col; |
| 855 | if (u.flags.x > 0.5) { |
| 856 | var uv_samp = uv_xformed; |
| 857 | if (u.dod.z > 0.5 && u.dod.w > 0.5) { |
| 858 | let tdims = vec2<f32>(textureDimensions(mat_tex)); |
| 859 | let canvas_px = vec2<f32>(uv_samp.x * u.dod.z, uv_samp.y * u.dod.w); |
| 860 | uv_samp = (canvas_px - u.dod.xy) / tdims; |
| 861 | if (uv_samp.x < 0.0 || uv_samp.x > 1.0 || uv_samp.y < 0.0 || uv_samp.y > 1.0) { |
| 862 | discard; |
| 863 | } |
| 864 | } |
| 865 | let tex_col = textureSampleLevel(mat_tex, mat_samp, uv_samp, 0.0); |
| 866 | col = select(tex_col, vec4<f32>(1.0), u.mat_fx.z > 0.5) * u.diffuse_col; |
| 867 | } |
| 868 | let out_alpha = col.a * u.mtl_params.x; |
| 869 | if (i32(u.env_rot_0.w + 0.5) == 0 && u.cb_flags.x < 0.5 && out_alpha <= 0.002) { |
| 870 | discard; |
| 871 | } |
| 872 | |
| 873 | let nlen = length(in.world_n); |
| 874 | var n = select(vec3<f32>(0.0, 0.0, 1.0), in.world_n / nlen, nlen > 1e-5); |
| 875 | if (u.bump_off_wrap.w > 0.5 && (u.mat_fx.y > 0.5 || abs(u.mat_fx.x) > 1e-5)) { |
| 876 | n = perturb_bump_normal( |
| 877 | n, |
| 878 | dpdx_w, |
| 879 | dpdy_w, |
| 880 | duvdx, |
| 881 | duvdy, |
| 882 | uv_bump_xformed, |
| 883 | u.mat_fx.x, |
| 884 | u.mat_fx.y, |
| 885 | u.bump_off_wrap.z, |
| 886 | u.bump_params.x, |
| 887 | i32(u.bump_params.y + 0.5), |
| 888 | ); |
| 889 | } |
| 890 | return vec4<f32>(n, 0.0); |
| 891 | } |
| 892 | "#; |