| 1 | struct MeshUniform { |
| 2 | mvp: mat4x4<f32>, |
| 3 | model: mat4x4<f32>, |
| 4 | view: mat4x4<f32>, |
| 5 | dod: vec4<f32>, |
| 6 | flags: vec4<f32>, |
| 7 | diffuse_col: vec4<f32>, |
| 8 | specular_col: vec4<f32>, |
| 9 | ambient: vec4<f32>, |
| 10 | dir_to_light: array<vec4<f32>, MAX_DIR_LIGHTS>, |
| 11 | dir_color: array<vec4<f32>, MAX_DIR_LIGHTS>, |
| 12 | cam_pos: vec4<f32>, |
| 13 | light_counts: vec4<f32>, |
| 14 | uv_xform: vec4<f32>, |
| 15 | uv_off_wrap: vec4<f32>, |
| 16 | mat_fx: vec4<f32>, |
| 17 | bump_uv_xform: vec4<f32>, |
| 18 | bump_off_wrap: vec4<f32>, |
| 19 | bump_dod: vec4<f32>, |
| 20 | bump_params: vec4<f32>, |
| 21 | mtl_params: vec4<f32>, |
| 22 | env_rot_0: vec4<f32>, |
| 23 | env_rot_1: vec4<f32>, |
| 24 | env_rot_2: vec4<f32>, |
| 25 | refl_strength: vec4<f32>, |
| 26 | refl_params: vec4<f32>, |
| 27 | refl_tint: vec4<f32>, |
| 28 | refract_tint: vec4<f32>, |
| 29 | cb_ops_a: vec4<f32>, |
| 30 | cb_ops: vec4<f32>, |
| 31 | cb_ops_b: vec4<f32>, |
| 32 | cb_flags: vec4<f32>, |
| 33 | point_pos: array<vec4<f32>, MAX_LIGHTS>, |
| 34 | point_col: array<vec4<f32>, MAX_LIGHTS>, |
| 35 | spot_pos: array<vec4<f32>, MAX_LIGHTS>, |
| 36 | spot_dir: array<vec4<f32>, MAX_LIGHTS>, |
| 37 | spot_col: array<vec4<f32>, MAX_LIGHTS>, |
| 38 | sfx: SurfaceFx, |
| 39 | }; |
| 40 | |
| 41 | struct ShadowUniform { |
| 42 | face_vp: array<mat4x4<f32>, 6 * MAX_SHADOW_LIGHTS>, |
| 43 | point: array<vec4<f32>, MAX_LIGHTS>, |
| 44 | }; |
| 45 | |
| 46 | override DRAW_BACK: bool = true; |
| 47 | override DRAW_FRONT: bool = true; |
| 48 | |
| 49 | @group(0) @binding(0) var<uniform> u: MeshUniform; |
| 50 | @group(0) @binding(1) var mat_tex: texture_2d<f32>; |
| 51 | @group(0) @binding(2) var mat_samp: sampler; |
| 52 | @group(0) @binding(3) var<uniform> shadow: ShadowUniform; |
| 53 | @group(0) @binding(4) var shadow_maps: texture_2d_array<f32>; |
| 54 | @group(0) @binding(5) var bump_tex: texture_2d<f32>; |
| 55 | |
| 56 | struct VsIn { |
| 57 | @location(0) pos: vec3<f32>, |
| 58 | @location(1) normal: vec3<f32>, |
| 59 | @location(2) uv: vec2<f32>, |
| 60 | }; |
| 61 | |
| 62 | struct VsOut { |
| 63 | @builtin(position) clip: vec4<f32>, |
| 64 | @location(0) uv: vec2<f32>, |
| 65 | @location(1) world_pos: vec3<f32>, |
| 66 | @location(2) world_n: vec3<f32>, |
| 67 | @location(3) local_pos: vec3<f32>, |
| 68 | @location(4) local_n: vec3<f32>, |
| 69 | }; |
| 70 | |
| 71 | // Cofactor matrix of the upper-left 3x3 of `m` (inverse-transpose up to scale), so normals stay |
| 72 | // perpendicular under non-uniform scale. Dividing by |det| instead of det keeps mirrored |
| 73 | // transforms pointing outward. |
| 74 | fn normal_matrix(m: mat4x4<f32>) -> mat3x3<f32> { |
| 75 | let c0 = m[0].xyz; |
| 76 | let c1 = m[1].xyz; |
| 77 | let c2 = m[2].xyz; |
| 78 | let sign = select(-1.0, 1.0, dot(c0, cross(c1, c2)) >= 0.0); |
| 79 | return mat3x3<f32>(cross(c1, c2), cross(c2, c0), cross(c0, c1)) * sign; |
| 80 | } |
| 81 | |
| 82 | @vertex |
| 83 | fn vs_mesh(v: VsIn) -> VsOut { |
| 84 | var out: VsOut; |
| 85 | let wp = u.model * vec4<f32>(v.pos, 1.0); |
| 86 | out.clip = u.mvp * vec4<f32>(v.pos, 1.0); |
| 87 | out.uv = v.uv; |
| 88 | out.world_pos = wp.xyz; |
| 89 | let wn = normal_matrix(u.model) * v.normal; |
| 90 | let nlen = length(wn); |
| 91 | var n_norm = select(vec3<f32>(0.0, 0.0, 1.0), wn / nlen, nlen > 1e-5); |
| 92 | var ln = v.normal; |
| 93 | if (u.flags.w > 0.5) { |
| 94 | n_norm = -n_norm; |
| 95 | ln = -ln; |
| 96 | } |
| 97 | out.world_n = n_norm; |
| 98 | out.local_pos = v.pos; |
| 99 | out.local_n = ln; |
| 100 | return out; |
| 101 | } |
| 102 | |
| 103 | fn light_attenuation(dist: f32, decay_type: f32) -> f32 { |
| 104 | let dt = i32(decay_type + 0.5); |
| 105 | let d = max(dist, 1e-3); |
| 106 | if (dt == 1) { |
| 107 | return 1.0 / d; |
| 108 | } else if (dt == 2) { |
| 109 | return 1.0 / (d * d); |
| 110 | } |
| 111 | return 1.0; |
| 112 | } |
| 113 | |
| 114 | /// Fraction of PCF taps where the cube map of point light `i` records a closer occluder. |
| 115 | fn point_shadow_occlusion(i: i32, world_pos: vec3<f32>, n: vec3<f32>, light_pos: vec3<f32>) -> f32 { |
| 116 | let params = shadow.point[i]; |
| 117 | if (params.x < -0.5) { |
| 118 | return 0.0; |
| 119 | } |
| 120 | let slot = i32(params.x + 0.5); |
| 121 | let dist = length(world_pos - light_pos); |
| 122 | // World size of one texel at this distance for a 90 degree face. |
| 123 | let texel = 2.0 * dist / f32(SHADOW_MAP_SIZE); |
| 124 | // The face is picked from the offset point so the lookup never straddles a cube seam. |
| 125 | let biased = world_pos + n * (texel * 1.5); |
| 126 | let d = biased - light_pos; |
| 127 | let a = abs(d); |
| 128 | var face = 4; |
| 129 | if (a.x >= a.y && a.x >= a.z) { |
| 130 | face = select(1, 0, d.x > 0.0); |
| 131 | } else if (a.y >= a.z) { |
| 132 | face = select(3, 2, d.y > 0.0); |
| 133 | } else { |
| 134 | face = select(5, 4, d.z > 0.0); |
| 135 | } |
| 136 | let clip = shadow.face_vp[slot * 6 + face] * vec4<f32>(biased, 1.0); |
| 137 | if (clip.w <= 1e-5) { |
| 138 | return 0.0; |
| 139 | } |
| 140 | let ndc = clip.xy / clip.w; |
| 141 | let base = vec2<i32>(floor(vec2<f32>(ndc.x * 0.5 + 0.5, 0.5 - ndc.y * 0.5) * f32(SHADOW_MAP_SIZE))); |
| 142 | let last = SHADOW_MAP_SIZE - 1; |
| 143 | var occluded = 0.0; |
| 144 | for (var dy: i32 = -1; dy <= 1; dy = dy + 1) { |
| 145 | for (var dx: i32 = -1; dx <= 1; dx = dx + 1) { |
| 146 | let t = clamp(base + vec2<i32>(dx, dy), vec2<i32>(0), vec2<i32>(last)); |
| 147 | let stored = textureLoad(shadow_maps, t, slot * 6 + face, 0).r; |
| 148 | occluded = occluded + select(0.0, 1.0, dist - texel * 3.0 > stored); |
| 149 | } |
| 150 | } |
| 151 | return occluded / 9.0; |
| 152 | } |
| 153 | |
| 154 | struct TangentFrame { |
| 155 | t: vec3<f32>, |
| 156 | b: vec3<f32>, |
| 157 | }; |
| 158 | |
| 159 | fn compute_tangent_frame( |
| 160 | base_n: vec3<f32>, |
| 161 | dpdx_w: vec3<f32>, |
| 162 | dpdy_w: vec3<f32>, |
| 163 | duvdx: vec2<f32>, |
| 164 | duvdy: vec2<f32>, |
| 165 | ) -> TangentFrame { |
| 166 | let det = duvdx.x * duvdy.y - duvdx.y * duvdy.x; |
| 167 | var t_vec: vec3<f32>; |
| 168 | var b_vec: vec3<f32>; |
| 169 | if (abs(det) > 1e-6) { |
| 170 | let inv_det = 1.0 / det; |
| 171 | t_vec = (dpdx_w * duvdy.y - dpdy_w * duvdx.y) * inv_det; |
| 172 | b_vec = (dpdy_w * duvdx.x - dpdx_w * duvdy.x) * inv_det; |
| 173 | t_vec = t_vec - base_n * dot(t_vec, base_n); |
| 174 | let t_len = length(t_vec); |
| 175 | if (t_len > 1e-5) { |
| 176 | t_vec = t_vec / t_len; |
| 177 | let b_sign = select(-1.0, 1.0, dot(cross(base_n, t_vec), b_vec) >= 0.0); |
| 178 | b_vec = cross(base_n, t_vec) * b_sign; |
| 179 | return TangentFrame(t_vec, b_vec); |
| 180 | } |
| 181 | } |
| 182 | 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); |
| 183 | t_vec = normalize(cross(up, base_n)); |
| 184 | b_vec = cross(base_n, t_vec); |
| 185 | return TangentFrame(t_vec, b_vec); |
| 186 | } |
| 187 | |
| 188 | fn eval_light_contrib( |
| 189 | n: vec3<f32>, |
| 190 | t_vec: vec3<f32>, |
| 191 | b_vec: vec3<f32>, |
| 192 | v: vec3<f32>, |
| 193 | l: vec3<f32>, |
| 194 | light_rgb: vec3<f32>, |
| 195 | albedo: vec3<f32>, |
| 196 | spec_rgb: vec3<f32>, |
| 197 | spec_int: f32, |
| 198 | shininess: f32, |
| 199 | diffuse_vis: f32, |
| 200 | spec_vis: f32, |
| 201 | model: i32, |
| 202 | ) -> vec3<f32> { |
| 203 | let ndotl = max(dot(n, l), 0.0); |
| 204 | if (ndotl <= 0.0) { |
| 205 | return vec3<f32>(0.0); |
| 206 | } |
| 207 | if (model == 5) { |
| 208 | return pbr_uber_light(n, v, l, light_rgb, albedo, diffuse_vis, spec_vis); |
| 209 | } |
| 210 | if (model == 3) { |
| 211 | return albedo * light_rgb * (ndotl * diffuse_vis / 3.14159265); |
| 212 | } |
| 213 | var out = albedo * light_rgb * (ndotl * diffuse_vis); |
| 214 | if (spec_int > 1e-4) { |
| 215 | let h = normalize(l + v); |
| 216 | var lobe: f32; |
| 217 | if (model == 1) { |
| 218 | lobe = pow(max(dot(reflect(-l, n), v), 0.0), shininess); |
| 219 | } else if (model == 2) { |
| 220 | // Cook-Torrance: Beckmann D, Torrance-Sparrow G, exact unpolarized Fresnel F. |
| 221 | // `eval_light_contrib` omits the outer N·L factor on `lobe`, so `(F*D*G)/(4*N·L*N·V) * N·L` |
| 222 | // simplifies to `(F*D*G)/(4*N·V)`. |
| 223 | let nh = max(dot(n, h), 1e-3); |
| 224 | let nv = max(dot(n, v), 1e-3); |
| 225 | let vh = max(dot(v, h), 1e-3); |
| 226 | let m2 = max(shininess * shininess, 1e-3); |
| 227 | let nh2 = nh * nh; |
| 228 | let d_term = exp(-(1.0 - nh2) / (nh2 * m2)) / (3.14159265 * m2 * nh2 * nh2); |
| 229 | let g_term = min(1.0, min(2.0 * nh * nv / vh, 2.0 * nh * ndotl / vh)); |
| 230 | var fresnel = 1.0; |
| 231 | if (u.mtl_params.z > 0.5) { |
| 232 | let eta = max(u.mtl_params.y, 1.0); |
| 233 | let c = vh; |
| 234 | let g = sqrt(max(eta * eta + c * c - 1.0, 0.0)); |
| 235 | let gmc = g - c; |
| 236 | let gpc = max(g + c, 1e-5); |
| 237 | let r_s = gmc / gpc; |
| 238 | let r_p = (c * gpc - 1.0) / max(c * gmc + 1.0, 1e-5); |
| 239 | fresnel = 0.5 * r_s * r_s * (1.0 + r_p * r_p); |
| 240 | } |
| 241 | lobe = (fresnel * d_term * g_term) / (4.0 * nv); |
| 242 | } else if (model == 4) { |
| 243 | // Anisotropic Ward (1992) BRDF * (N·L) using tangent frame (T, B) and SpreadU/SpreadV. |
| 244 | let nh = max(dot(n, h), 1e-3); |
| 245 | let nv = max(dot(n, v), 1e-3); |
| 246 | let su = max(shininess, 1e-3); |
| 247 | let sv = max(u.mtl_params.w, 1e-3); |
| 248 | let ht = dot(h, t_vec) / su; |
| 249 | let hb = dot(h, b_vec) / sv; |
| 250 | let expo = -(ht * ht + hb * hb) / (nh * nh); |
| 251 | let denom = max(4.0 * 3.14159265 * su * sv * sqrt(nv), 1e-4); |
| 252 | lobe = (sqrt(ndotl) / denom) * exp(max(expo, -80.0)); |
| 253 | } else { |
| 254 | lobe = pow(max(dot(n, h), 0.0), shininess); |
| 255 | } |
| 256 | out = out + light_rgb * spec_rgb * (lobe * spec_int * spec_vis); |
| 257 | } |
| 258 | return out; |
| 259 | } |
| 260 | |
| 261 | fn sample_bump_height(uv_in: vec2<f32>, wrap_f: f32, ch: i32) -> f32 { |
| 262 | let uv = wrap_uv_only(uv_in, wrap_f); |
| 263 | let c = textureSampleLevel(bump_tex, mat_samp, uv, 0.0); |
| 264 | if (ch == 1) { |
| 265 | return (c.r + c.g + c.b) / 3.0; |
| 266 | } else if (ch == 2) { |
| 267 | return max(c.r, max(c.g, c.b)); |
| 268 | } else if (ch == 3) { |
| 269 | return min(c.r, min(c.g, c.b)); |
| 270 | } else if (ch == 4) { |
| 271 | return c.r; |
| 272 | } else if (ch == 5) { |
| 273 | return c.g; |
| 274 | } else if (ch == 6) { |
| 275 | return c.b; |
| 276 | } else if (ch == 7) { |
| 277 | return c.a; |
| 278 | } |
| 279 | return dot(c.rgb, vec3<f32>(0.299, 0.587, 0.114)); |
| 280 | } |
| 281 | |
| 282 | fn perturb_bump_normal( |
| 283 | base_n: vec3<f32>, |
| 284 | dpdx_w: vec3<f32>, |
| 285 | dpdy_w: vec3<f32>, |
| 286 | duvdx: vec2<f32>, |
| 287 | duvdy: vec2<f32>, |
| 288 | uv_in: vec2<f32>, |
| 289 | bump_scale: f32, |
| 290 | bump_mode: f32, |
| 291 | bump_wrap: f32, |
| 292 | clamp_z: f32, |
| 293 | bump_ch: i32, |
| 294 | ) -> vec3<f32> { |
| 295 | var uv = uv_in; |
| 296 | if (u.bump_dod.z > 0.5 && u.bump_dod.w > 0.5) { |
| 297 | let bdims = vec2<f32>(textureDimensions(bump_tex)); |
| 298 | let canvas_px = vec2<f32>(uv.x * u.bump_dod.z, uv.y * u.bump_dod.w); |
| 299 | uv = (canvas_px - u.bump_dod.xy) / bdims; |
| 300 | } |
| 301 | let frame = compute_tangent_frame(base_n, dpdx_w, dpdy_w, duvdx, duvdy); |
| 302 | let t_vec = frame.t; |
| 303 | let b_vec = frame.b; |
| 304 | |
| 305 | if (bump_mode > 0.5) { |
| 306 | let b_col = textureSampleLevel(bump_tex, mat_samp, wrap_uv_only(uv, bump_wrap), 0.0); |
| 307 | 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); |
| 308 | let tn = b_rgb * 2.0 - vec3<f32>(1.0); |
| 309 | let perturbed = t_vec * tn.x + b_vec * tn.y + base_n * tn.z; |
| 310 | let plen = length(perturbed); |
| 311 | return select(base_n, perturbed / plen, plen > 1e-5); |
| 312 | } |
| 313 | |
| 314 | let inv_dim = vec2<f32>(1.0) / max(vec2<f32>(textureDimensions(bump_tex)), vec2<f32>(1.0)); |
| 315 | let h_l = sample_bump_height(uv - vec2<f32>(inv_dim.x, 0.0), bump_wrap, bump_ch); |
| 316 | let h_r = sample_bump_height(uv + vec2<f32>(inv_dim.x, 0.0), bump_wrap, bump_ch); |
| 317 | let h_d = sample_bump_height(uv - vec2<f32>(0.0, inv_dim.y), bump_wrap, bump_ch); |
| 318 | let h_u = sample_bump_height(uv + vec2<f32>(0.0, inv_dim.y), bump_wrap, bump_ch); |
| 319 | let dhdu = (h_r - h_l) * 0.5 * bump_scale; |
| 320 | let dhdv = (h_u - h_d) * 0.5 * bump_scale; |
| 321 | return normalize(base_n - t_vec * dhdu + b_vec * dhdv); |
| 322 | } |
| 323 | |
| 324 | fn unpremultiply(c: vec4<f32>) -> vec3<f32> { |
| 325 | return select(vec3<f32>(0.0), c.rgb / max(c.a, 1e-5), c.a > 1e-5); |
| 326 | } |
| 327 | |
| 328 | // Ambient plus every light's contribution for one material. |
| 329 | fn shade( |
| 330 | pos: vec3<f32>, |
| 331 | n: vec3<f32>, |
| 332 | v: vec3<f32>, |
| 333 | tf: TangentFrame, |
| 334 | albedo: vec3<f32>, |
| 335 | spec_rgb: vec3<f32>, |
| 336 | spec_int: f32, |
| 337 | exponent: f32, |
| 338 | model: i32, |
| 339 | ) -> vec3<f32> { |
| 340 | let shininess = select(max(exponent, 1.0), exponent, model > 1); |
| 341 | var lit = albedo * u.ambient.rgb; |
| 342 | let n_dir = i32(u.light_counts.z + 0.5); |
| 343 | for (var i: i32 = 0; i < MAX_DIR_LIGHTS; i = i + 1) { |
| 344 | if (i >= n_dir) { break; } |
| 345 | let l = normalize(u.dir_to_light[i].xyz); |
| 346 | lit = lit + eval_light_contrib(n, tf.t, tf.b, v, l, u.dir_color[i].rgb, albedo, spec_rgb, spec_int, shininess, 1.0, 1.0, model); |
| 347 | } |
| 348 | |
| 349 | let n_pt = i32(u.light_counts.x + 0.5); |
| 350 | for (var i: i32 = 0; i < MAX_LIGHTS; i = i + 1) { |
| 351 | if (i >= n_pt) { break; } |
| 352 | let to_l = u.point_pos[i].xyz - pos; |
| 353 | let dist = max(length(to_l), 1e-4); |
| 354 | let att = light_attenuation(dist, u.point_pos[i].w); |
| 355 | var occ = 0.0; |
| 356 | if (u.light_counts.w > 0.5) { |
| 357 | occ = point_shadow_occlusion(i, pos, n, u.point_pos[i].xyz); |
| 358 | } |
| 359 | lit = lit + eval_light_contrib( |
| 360 | n, tf.t, tf.b, v, to_l / dist, u.point_col[i].rgb * att, albedo, spec_rgb, spec_int, shininess, |
| 361 | 1.0 - shadow.point[i].y * occ, 1.0 - occ, model, |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | let n_sp = i32(u.light_counts.y + 0.5); |
| 366 | for (var i: i32 = 0; i < MAX_LIGHTS; i = i + 1) { |
| 367 | if (i >= n_sp) { break; } |
| 368 | let to_l = u.spot_pos[i].xyz - pos; |
| 369 | let dist = max(length(to_l), 1e-4); |
| 370 | let l_dir = to_l / dist; |
| 371 | let cos_theta = dot(-l_dir, normalize(u.spot_dir[i].xyz)); |
| 372 | let cos_inner = u.spot_pos[i].w; |
| 373 | let cos_outer = u.spot_dir[i].w; |
| 374 | let cone = clamp((cos_theta - cos_outer) / max(cos_inner - cos_outer, 1e-4), 0.0, 1.0); |
| 375 | if (cone > 0.0) { |
| 376 | let att = light_attenuation(dist, u.spot_col[i].w) * cone; |
| 377 | lit = lit + eval_light_contrib(n, tf.t, tf.b, v, l_dir, u.spot_col[i].rgb * att, albedo, spec_rgb, spec_int, shininess, 1.0, 1.0, model); |
| 378 | } |
| 379 | } |
| 380 | return lit; |
| 381 | } |
| 382 | |
| 383 | // Secondary material of `MtlChanBool`, `MtlMerge3D` and `FalloffOperator`: straight RGB, alpha. |
| 384 | fn layer_color(pos: vec3<f32>, n: vec3<f32>, v: vec3<f32>, tf: TangentFrame, uv: vec2<f32>) -> vec4<f32> { |
| 385 | var c = u.sfx.layer_col; |
| 386 | if (u.sfx.layer.w > 1.5) { |
| 387 | let t = textureSampleLevel(bump_tex, mat_samp, wrap_uv_only(uv, u.bump_off_wrap.z), 0.0); |
| 388 | c = vec4<f32>(unpremultiply(t) * c.rgb, t.a * c.a); |
| 389 | } |
| 390 | if (u.sfx.layer.z > 0.5) { |
| 391 | let model = i32(u.sfx.layer_mtl.y + 0.5); |
| 392 | c = vec4<f32>(shade(pos, n, v, tf, c.rgb, u.sfx.layer_spec.rgb, u.sfx.layer_mtl.x, u.sfx.layer_spec.w, model), c.a); |
| 393 | } |
| 394 | return c; |
| 395 | } |
| 396 | |
| 397 | // Environment colour along `world_dir` (`env_map_mode`: 0 equirect, 1 angular, 2-4 cube map, |
| 398 | // 5 the image at `mesh_uv`), tinted, from the texture in `env_tex_slot`. |
| 399 | fn sample_sphere_map(world_dir: vec3<f32>, mesh_uv: vec2<f32>) -> vec4<f32> { |
| 400 | let slot = i32(u.env_rot_2.w + 0.5); |
| 401 | if (slot == 0) { |
| 402 | return vec4<f32>(u.refl_tint.rgb, 1.0); |
| 403 | } |
| 404 | let rot = mat3x3<f32>(u.env_rot_0.xyz, u.env_rot_1.xyz, u.env_rot_2.xyz); |
| 405 | let d = normalize(rot * env_space(world_dir)); |
| 406 | let map_mode = i32(u.env_rot_1.w + 0.5); |
| 407 | let dod = select(u.dod, u.bump_dod, slot == 2); |
| 408 | let has_dod = dod.z > 0.5 && dod.w > 0.5; |
| 409 | let tex_dims = vec2<f32>(select(textureDimensions(mat_tex), textureDimensions(bump_tex), slot == 2)); |
| 410 | var uv: vec2<f32>; |
| 411 | if (map_mode == 5) { |
| 412 | uv = mesh_uv; |
| 413 | } else if (map_mode >= 2) { |
| 414 | uv = cube_uv(d, map_mode, select(tex_dims, dod.zw, has_dod)); |
| 415 | } else { |
| 416 | // Fusion's longitude runs against atan2(x, z); Angular maps V linearly by height. |
| 417 | let u_c = 1.0 - fract(atan2(d.x, d.z) / 6.2831853 + 0.75); |
| 418 | let v_c = select(acos(clamp(d.y, -1.0, 1.0)) / 3.14159265, 0.5 - 0.5 * d.y, map_mode == 1); |
| 419 | uv = vec2<f32>(u_c, v_c); |
| 420 | } |
| 421 | if (has_dod) { |
| 422 | uv = clamp((uv * dod.zw - dod.xy) / tex_dims, vec2<f32>(0.0), vec2<f32>(1.0)); |
| 423 | } |
| 424 | let raw = select(textureSampleLevel(mat_tex, mat_samp, uv, 0.0), textureSampleLevel(bump_tex, mat_samp, uv, 0.0), slot == 2); |
| 425 | return vec4<f32>(unpremultiply(raw) * u.refl_tint.rgb, raw.a); |
| 426 | } |
| 427 | |
| 428 | // Fusion evaluates the HLS operands (11-16) as 0 and EnvCoords as the eye-space normal's XY. |
| 429 | fn cb_operand( |
| 430 | idx: i32, |
| 431 | bg: vec4<f32>, |
| 432 | fg: vec4<f32>, |
| 433 | pos: vec3<f32>, |
| 434 | uv: vec2<f32>, |
| 435 | env_c: vec2<f32>, |
| 436 | n: vec3<f32>, |
| 437 | ) -> f32 { |
| 438 | switch idx { |
| 439 | case 0: { return fg.r; } |
| 440 | case 1: { return fg.g; } |
| 441 | case 2: { return fg.b; } |
| 442 | case 3: { return fg.a; } |
| 443 | case 4: { return bg.r; } |
| 444 | case 5: { return bg.g; } |
| 445 | case 6: { return bg.b; } |
| 446 | case 7: { return bg.a; } |
| 447 | case 9: { return 1.0; } |
| 448 | case 10: { return 0.5; } |
| 449 | case 17: { return dot(fg.rgb, vec3<f32>(0.299, 0.587, 0.114)); } |
| 450 | case 18: { return dot(bg.rgb, vec3<f32>(0.299, 0.587, 0.114)); } |
| 451 | case 19: { return pos.x; } |
| 452 | case 20: { return pos.y; } |
| 453 | case 21: { return pos.z; } |
| 454 | case 22: { return uv.x; } |
| 455 | case 23: { return 1.0 - uv.y; } |
| 456 | case 25: { return env_c.x; } |
| 457 | case 26: { return env_c.y; } |
| 458 | case 28: { return n.x; } |
| 459 | case 29: { return n.y; } |
| 460 | case 30: { return n.z; } |
| 461 | default: { return 0.0; } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | fn cb_combine(op: i32, a: f32, b: f32) -> f32 { |
| 466 | switch op { |
| 467 | case 0: { return a; } |
| 468 | case 1: { return b; } |
| 469 | case 2: { return 1.0 - a; } |
| 470 | case 3: { return 1.0 - b; } |
| 471 | case 4: { return a + b; } |
| 472 | case 5: { return a - b; } |
| 473 | case 6: { return a * b; } |
| 474 | case 7: { return select(a / b, 0.0, abs(b) <= 1e-5); } |
| 475 | case 8: { return min(a, b); } |
| 476 | case 9: { return max(a, b); } |
| 477 | case 10: { return 0.5 * (a + b); } |
| 478 | default: { return a; } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | @fragment |
| 483 | fn fs_mesh(in: VsOut) -> @location(0) vec4<f32> { |
| 484 | let uv_xformed = apply_uv_xform(in.uv, u.uv_xform, u.uv_off_wrap); |
| 485 | let uv_bump_xformed = apply_uv_xform(in.uv, u.bump_uv_xform, u.bump_off_wrap); |
| 486 | let dpdx_w = dpdx(in.world_pos); |
| 487 | let dpdy_w = dpdy(in.world_pos); |
| 488 | let duvdx = dpdx(uv_bump_xformed); |
| 489 | let duvdy = dpdy(uv_bump_xformed); |
| 490 | let duvdx_main = dpdx(uv_xformed); |
| 491 | let duvdy_main = dpdy(uv_xformed); |
| 492 | let facing_away = dot(in.world_n, u.cam_pos.xyz - in.world_pos) < 0.0; |
| 493 | if (!select(DRAW_FRONT, DRAW_BACK, facing_away)) { |
| 494 | discard; |
| 495 | } |
| 496 | |
| 497 | let env_mode = i32(u.env_rot_0.w + 0.5); |
| 498 | let cb_on = u.cb_flags.x > 0.5; |
| 499 | let layer_mode = i32(u.sfx.layer.x + 0.5); |
| 500 | let proc_on = u.sfx.proc_a.x > 0.5; |
| 501 | let v_dir = normalize(u.cam_pos.xyz - in.world_pos); |
| 502 | var n_surf = normalize(in.world_n); |
| 503 | if (u.bump_off_wrap.w > 0.5 && (u.mat_fx.y > 0.5 || abs(u.mat_fx.x) > 1e-5)) { |
| 504 | n_surf = perturb_bump_normal( |
| 505 | n_surf, |
| 506 | dpdx_w, |
| 507 | dpdy_w, |
| 508 | duvdx, |
| 509 | duvdy, |
| 510 | uv_bump_xformed, |
| 511 | u.mat_fx.x, |
| 512 | u.mat_fx.y, |
| 513 | u.bump_off_wrap.z, |
| 514 | u.bump_params.x, |
| 515 | i32(u.bump_params.y + 0.5), |
| 516 | ); |
| 517 | } |
| 518 | let tframe = compute_tangent_frame(n_surf, dpdx_w, dpdy_w, duvdx_main, duvdy_main); |
| 519 | |
| 520 | var base_straight = vec3<f32>(0.0); |
| 521 | var rgb_alpha = u.diffuse_col.a; |
| 522 | var out_alpha = rgb_alpha * u.mtl_params.x; |
| 523 | |
| 524 | if (env_mode == 1) { |
| 525 | // Standalone SphereMap / CubeMap shader: lookup along the view ray. |
| 526 | let env_c = sample_sphere_map(-v_dir, in.uv); |
| 527 | base_straight = env_c.rgb * u.diffuse_col.rgb; |
| 528 | rgb_alpha = env_c.a * u.diffuse_col.a; |
| 529 | out_alpha = rgb_alpha * u.mtl_params.x; |
| 530 | } else { |
| 531 | var col = u.diffuse_col; |
| 532 | var tex_col = vec4<f32>(1.0); |
| 533 | var tex_straight = vec3<f32>(1.0); |
| 534 | if (u.flags.x > 0.5) { |
| 535 | var uv_samp = uv_xformed; |
| 536 | if (u.dod.z > 0.5 && u.dod.w > 0.5) { |
| 537 | let tdims = vec2<f32>(textureDimensions(mat_tex)); |
| 538 | let canvas_px = vec2<f32>(uv_samp.x * u.dod.z, uv_samp.y * u.dod.w); |
| 539 | uv_samp = (canvas_px - u.dod.xy) / tdims; |
| 540 | if (uv_samp.x < 0.0 || uv_samp.x > 1.0 || uv_samp.y < 0.0 || uv_samp.y > 1.0) { |
| 541 | discard; |
| 542 | } |
| 543 | } |
| 544 | tex_col = textureSampleLevel(mat_tex, mat_samp, uv_samp, 0.0); |
| 545 | } |
| 546 | if (proc_on) { |
| 547 | tex_col = surface_proc(tex_col, vec2<f32>(in.uv.x, 1.0 - in.uv.y)); |
| 548 | } |
| 549 | if (u.flags.x > 0.5 || proc_on) { |
| 550 | if (u.mat_fx.z <= 0.5) { |
| 551 | tex_straight = unpremultiply(tex_col); |
| 552 | } |
| 553 | col = select(tex_col, vec4<f32>(1.0), u.mat_fx.z > 0.5) * u.diffuse_col; |
| 554 | } |
| 555 | // `rgb_alpha` is `tex_col.a * Diffuse.Opacity`; `Diffuse.Color.Alpha` (`u.mtl_params.x`) |
| 556 | // modulates output alpha without attenuating diffuse/specular RGB. |
| 557 | rgb_alpha = col.a; |
| 558 | out_alpha = rgb_alpha * u.mtl_params.x; |
| 559 | if (env_mode == 0 && !cb_on && layer_mode == 0 && out_alpha <= 0.002) { |
| 560 | discard; |
| 561 | } |
| 562 | |
| 563 | let albedo = tex_straight * u.diffuse_col.rgb; |
| 564 | if (env_mode == 2 && u.refl_tint.w < 0.5) { |
| 565 | base_straight = vec3<f32>(0.0); |
| 566 | rgb_alpha = 0.0; |
| 567 | out_alpha = 0.0; |
| 568 | } else if (u.flags.y > 0.5) { |
| 569 | base_straight = shade(in.world_pos, n_surf, v_dir, tframe, albedo, u.specular_col.rgb, u.flags.z, u.specular_col.w, i32(u.mat_fx.w + 0.5)) |
| 570 | + u.sfx.emission.rgb; |
| 571 | } else { |
| 572 | base_straight = albedo * u.uv_off_wrap.w; |
| 573 | } |
| 574 | |
| 575 | if (env_mode == 2) { |
| 576 | let r_dir = reflect(-v_dir, n_surf); |
| 577 | let refl_c = sample_sphere_map(r_dir, in.uv); |
| 578 | var refl_str = clamp(u.refl_strength.y, 0.0, 1.0); |
| 579 | if (u.refl_strength.x > 0.5) { |
| 580 | let cos_nv = clamp(dot(n_surf, v_dir), 0.0, 1.0); |
| 581 | let f_ang = pow(1.0 - cos_nv, max(u.refl_params.z, 1e-4)); |
| 582 | refl_str = clamp(mix(u.refl_params.y, u.refl_params.x, f_ang), 0.0, 1.0); |
| 583 | } |
| 584 | var under_rgb = base_straight; |
| 585 | var under_a = rgb_alpha; |
| 586 | if (rgb_alpha < 0.999 && u.refl_params.w > 0.01 && u.env_rot_2.w > 0.5 && u.refl_tint.w > 0.5) { |
| 587 | let eta = 1.0 / max(u.refl_params.w, 0.01); |
| 588 | var refr_dir = refract(-v_dir, n_surf, eta); |
| 589 | if (dot(refr_dir, refr_dir) < 1e-5) { |
| 590 | refr_dir = r_dir; |
| 591 | } |
| 592 | let refr_c = sample_sphere_map(refr_dir, in.uv); |
| 593 | under_rgb = mix(refr_c.rgb * u.refract_tint.rgb, base_straight, rgb_alpha); |
| 594 | under_a = clamp(max(rgb_alpha, refr_c.a * (1.0 - rgb_alpha)), 0.0, 1.0); |
| 595 | } |
| 596 | let under_premul = under_rgb * under_a; |
| 597 | let refl_a = refl_str * refl_c.a; |
| 598 | let out_premul = under_premul * (1.0 - refl_str) + refl_c.rgb * refl_a; |
| 599 | let final_a = clamp(under_a * (1.0 - refl_str) + refl_a, 0.0, 1.0); |
| 600 | base_straight = select(vec3<f32>(0.0), out_premul / max(final_a, 1e-5), final_a > 1e-5); |
| 601 | rgb_alpha = final_a; |
| 602 | out_alpha = clamp(out_alpha * (1.0 - refl_str) + refl_a, 0.0, 1.0); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if (layer_mode >= 2) { |
| 607 | let fg = layer_color(in.world_pos, n_surf, v_dir, tframe, uv_xformed); |
| 608 | let bg_p = vec4<f32>(base_straight * rgb_alpha, out_alpha); |
| 609 | let fg_p = vec4<f32>(fg.rgb * fg.a, fg.a); |
| 610 | var o: vec4<f32>; |
| 611 | if (layer_mode == 2) { |
| 612 | o = fg_p * u.sfx.layer.y + bg_p * (1.0 - fg.a * u.sfx.layer.y); |
| 613 | } else { |
| 614 | let t = falloff_t(n_surf, v_dir, u.sfx.layer.y); |
| 615 | o = mix(bg_p, vec4<f32>(fg_p.rgb, fg_p.a * u.sfx.layer_mtl.z), t); |
| 616 | if (layer_mode == 4) { |
| 617 | o = o * sfx_lut(t); |
| 618 | } |
| 619 | } |
| 620 | base_straight = unpremultiply(o); |
| 621 | rgb_alpha = o.a; |
| 622 | out_alpha = o.a; |
| 623 | } |
| 624 | base_straight = apply_fog(base_straight, in.world_pos); |
| 625 | |
| 626 | if (cb_on) { |
| 627 | let bg_c = vec4<f32>(base_straight, out_alpha); |
| 628 | let fg_c = layer_color(in.world_pos, n_surf, v_dir, tframe, uv_xformed); |
| 629 | let cspace = i32(u.cb_flags.y + 0.5); |
| 630 | var c_pos = in.world_pos; |
| 631 | var c_n = n_surf; |
| 632 | if (cspace == 0) { |
| 633 | c_pos = in.local_pos; |
| 634 | let ln_len = length(in.local_n); |
| 635 | c_n = select(vec3<f32>(0.0, 0.0, 1.0), in.local_n / ln_len, ln_len > 1e-5); |
| 636 | } else if (cspace == 2) { |
| 637 | c_pos = (u.view * vec4<f32>(in.world_pos, 1.0)).xyz; |
| 638 | let en = (u.view * vec4<f32>(n_surf, 0.0)).xyz; |
| 639 | let elen = length(en); |
| 640 | c_n = select(vec3<f32>(0.0, 0.0, 1.0), en / elen, elen > 1e-5); |
| 641 | } |
| 642 | let env_c = normalize((u.view * vec4<f32>(n_surf, 0.0)).xyz).xy; |
| 643 | 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)); |
| 644 | 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)); |
| 645 | 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)); |
| 646 | 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)); |
| 647 | let clamped_a = clamp(a_out, 0.0, 1.0); |
| 648 | if (clamped_a <= 0.002) { |
| 649 | discard; |
| 650 | } |
| 651 | // Fusion does not premultiply the channel results by the computed alpha. |
| 652 | return vec4<f32>(clamp(vec3<f32>(r_out, g_out, b_out), vec3<f32>(0.0), vec3<f32>(1.0)), clamped_a); |
| 653 | } |
| 654 | |
| 655 | if (out_alpha <= 0.002) { |
| 656 | discard; |
| 657 | } |
| 658 | return vec4<f32>(clamp(base_straight * rgb_alpha, vec3<f32>(0.0), vec3<f32>(1.0)), out_alpha); |
| 659 | } |
| 660 | |
| 661 | @fragment |
| 662 | fn fs_normal(in: VsOut) -> @location(0) vec4<f32> { |
| 663 | let uv_xformed = apply_uv_xform(in.uv, u.uv_xform, u.uv_off_wrap); |
| 664 | let uv_bump_xformed = apply_uv_xform(in.uv, u.bump_uv_xform, u.bump_off_wrap); |
| 665 | let dpdx_w = dpdx(in.world_pos); |
| 666 | let dpdy_w = dpdy(in.world_pos); |
| 667 | let duvdx = dpdx(uv_bump_xformed); |
| 668 | let duvdy = dpdy(uv_bump_xformed); |
| 669 | let facing_away = dot(in.world_n, u.cam_pos.xyz - in.world_pos) < 0.0; |
| 670 | if (!select(DRAW_FRONT, DRAW_BACK, facing_away)) { |
| 671 | discard; |
| 672 | } |
| 673 | |
| 674 | var col = u.diffuse_col; |
| 675 | if (u.flags.x > 0.5) { |
| 676 | var uv_samp = uv_xformed; |
| 677 | if (u.dod.z > 0.5 && u.dod.w > 0.5) { |
| 678 | let tdims = vec2<f32>(textureDimensions(mat_tex)); |
| 679 | let canvas_px = vec2<f32>(uv_samp.x * u.dod.z, uv_samp.y * u.dod.w); |
| 680 | uv_samp = (canvas_px - u.dod.xy) / tdims; |
| 681 | if (uv_samp.x < 0.0 || uv_samp.x > 1.0 || uv_samp.y < 0.0 || uv_samp.y > 1.0) { |
| 682 | discard; |
| 683 | } |
| 684 | } |
| 685 | let tex_col = textureSampleLevel(mat_tex, mat_samp, uv_samp, 0.0); |
| 686 | col = select(tex_col, vec4<f32>(1.0), u.mat_fx.z > 0.5) * u.diffuse_col; |
| 687 | } |
| 688 | let out_alpha = col.a * u.mtl_params.x; |
| 689 | if (i32(u.env_rot_0.w + 0.5) == 0 && u.cb_flags.x < 0.5 && u.sfx.layer.x < 0.5 && out_alpha <= 0.002) { |
| 690 | discard; |
| 691 | } |
| 692 | |
| 693 | let nlen = length(in.world_n); |
| 694 | var n = select(vec3<f32>(0.0, 0.0, 1.0), in.world_n / nlen, nlen > 1e-5); |
| 695 | if (u.bump_off_wrap.w > 0.5 && (u.mat_fx.y > 0.5 || abs(u.mat_fx.x) > 1e-5)) { |
| 696 | n = perturb_bump_normal( |
| 697 | n, |
| 698 | dpdx_w, |
| 699 | dpdy_w, |
| 700 | duvdx, |
| 701 | duvdy, |
| 702 | uv_bump_xformed, |
| 703 | u.mat_fx.x, |
| 704 | u.mat_fx.y, |
| 705 | u.bump_off_wrap.z, |
| 706 | u.bump_params.x, |
| 707 | i32(u.bump_params.y + 0.5), |
| 708 | ); |
| 709 | } |
| 710 | return vec4<f32>(n, 0.0); |
| 711 | } |