| 1 | struct SurfaceFx { |
| 2 | proc_a: vec4<f32>, |
| 3 | proc_b: vec4<f32>, |
| 4 | layer: vec4<f32>, |
| 5 | layer_col: vec4<f32>, |
| 6 | layer_spec: vec4<f32>, |
| 7 | layer_mtl: vec4<f32>, |
| 8 | pbr: vec4<f32>, |
| 9 | pbr_spec: vec4<f32>, |
| 10 | pbr_layers: vec4<f32>, |
| 11 | sheen: vec4<f32>, |
| 12 | emission: vec4<f32>, |
| 13 | fog: vec4<f32>, |
| 14 | fog_col: vec4<f32>, |
| 15 | env: vec4<f32>, |
| 16 | lut: array<vec4<f32>, 16>, |
| 17 | }; |
| 18 | |
| 19 | fn sfx_lut(t: f32) -> vec4<f32> { |
| 20 | let x = clamp(t, 0.0, 1.0) * 15.0; |
| 21 | let i = min(u32(x), 14u); |
| 22 | return mix(u.sfx.lut[i], u.sfx.lut[i + 1u], x - f32(i)); |
| 23 | } |
| 24 | |
| 25 | // `TexGradient` position along the gradient. Types measure from the UVW origin; Linear, Reflect |
| 26 | // and Cross depend on W alone, which every primitive leaves at 0. |
| 27 | fn gradient_t(uv_up: vec2<f32>) -> f32 { |
| 28 | let kind = i32(u.sfx.proc_a.y + 0.5); |
| 29 | var t = 0.5; |
| 30 | if (kind == 1 || kind == 3) { |
| 31 | t = 0.0; |
| 32 | } else if (kind == 2) { |
| 33 | t = max(abs(uv_up.x), abs(uv_up.y)); |
| 34 | } else if (kind == 4) { |
| 35 | t = length(uv_up); |
| 36 | } |
| 37 | t = (t - u.sfx.proc_a.w - 0.5) / u.sfx.proc_a.z + 0.5; |
| 38 | let repeat = i32(u.sfx.proc_b.x + 0.5); |
| 39 | if (repeat == 1) { |
| 40 | t = fract(t); |
| 41 | } else if (repeat == 2) { |
| 42 | t = 1.0 - abs(fract(t * 0.5) * 2.0 - 1.0); |
| 43 | } |
| 44 | return t; |
| 45 | } |
| 46 | |
| 47 | // `FastNoiseTexture3D`: Seethe is the lattice Z for every octave and octave weights 0.5^i are |
| 48 | // normalised to sum to 2 (4 for Discontinuous). Contrast scales about 0 after Inverted. |
| 49 | fn noise_value(uv_up: vec2<f32>) -> f32 { |
| 50 | let detail = clamp(u.sfx.proc_a.y, 0.0, 10.0); |
| 51 | let disc = u.sfx.proc_b.w > 0.5; |
| 52 | let p = uv_up * (2.0 * u.sfx.proc_a.z); |
| 53 | let full = i32(floor(detail)); |
| 54 | let frac_w = detail - f32(full); |
| 55 | var sum = 0.0; |
| 56 | var norm = 0.0; |
| 57 | var amp = 1.0; |
| 58 | var scale = 1.0; |
| 59 | for (var i = 0; i <= full; i = i + 1) { |
| 60 | let w = select(amp, amp * frac_w, i == full); |
| 61 | if (w > 0.0) { |
| 62 | let n = simplex3d(vec3<f32>(p * scale, u.sfx.proc_a.w)); |
| 63 | sum = sum + w * select(n, abs(n), disc); |
| 64 | norm = norm + w; |
| 65 | } |
| 66 | amp = amp * 0.5; |
| 67 | scale = scale * 2.0; |
| 68 | } |
| 69 | let avg = select(0.0, sum / norm, norm > 0.0); |
| 70 | var val = select(0.5 + 2.0 * avg, 4.0 * avg, disc); |
| 71 | if (u.sfx.proc_b.z > 0.5) { |
| 72 | val = 1.0 - val; |
| 73 | } |
| 74 | return clamp(val * u.sfx.proc_b.x + u.sfx.proc_b.y, 0.0, 1.0); |
| 75 | } |
| 76 | |
| 77 | // Premultiplied colour of the procedural source `proc_a.x`; `tex` is the bound texture or white. |
| 78 | fn surface_proc(tex: vec4<f32>, uv_up: vec2<f32>) -> vec4<f32> { |
| 79 | let kind = i32(u.sfx.proc_a.x + 0.5); |
| 80 | if (kind == 1) { |
| 81 | return sfx_lut(gradient_t(uv_up)); |
| 82 | } |
| 83 | return tex * noise_value(uv_up); |
| 84 | } |
| 85 | |
| 86 | fn falloff_t(n: vec3<f32>, v: vec3<f32>, power: f32) -> f32 { |
| 87 | // Back faces see their inner side face-on, as Fusion shades them. |
| 88 | return 1.0 - pow(min(abs(dot(n, v)), 1.0), power); |
| 89 | } |
| 90 | |
| 91 | fn smith_g1(x: f32, a2: f32) -> f32 { |
| 92 | return 2.0 * x / max(x + sqrt(a2 + (1.0 - a2) * x * x), 1e-5); |
| 93 | } |
| 94 | |
| 95 | // GGX distribution times Smith shadowing over 4 N.V (alpha = roughness^2). |
| 96 | fn ggx_spec(nh: f32, nl: f32, nv: f32, rough: f32) -> f32 { |
| 97 | let a = max(rough * rough, 1e-3); |
| 98 | let a2 = a * a; |
| 99 | let dd = nh * nh * (a2 - 1.0) + 1.0; |
| 100 | return a2 / (3.14159265 * dd * dd) * smith_g1(nl, a2) * smith_g1(nv, a2) / (4.0 * nv); |
| 101 | } |
| 102 | |
| 103 | fn schlick(ior: f32, vh: f32) -> f32 { |
| 104 | let r0 = (ior - 1.0) / (ior + 1.0); |
| 105 | return r0 * r0 + (1.0 - r0 * r0) * pow(1.0 - vh, 5.0); |
| 106 | } |
| 107 | |
| 108 | // GGX/Smith directional albedo E(N.V, roughness): rows are roughness i/7, columns N.V j/7. |
| 109 | const GGX_ALBEDO: array<f32, 64> = array<f32, 64>( |
| 110 | 0.9990, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, |
| 111 | 0.8846, 0.9884, 0.9974, 0.9991, 0.9995, 0.9998, 0.9994, 0.9995, |
| 112 | 0.9178, 0.9005, 0.9486, 0.9736, 0.9833, 0.9878, 0.9907, 0.9924, |
| 113 | 0.9113, 0.8517, 0.8629, 0.8934, 0.9187, 0.9364, 0.9482, 0.9564, |
| 114 | 0.8688, 0.8191, 0.7954, 0.7947, 0.8061, 0.8227, 0.8396, 0.8548, |
| 115 | 0.8011, 0.7525, 0.7125, 0.6876, 0.6745, 0.6704, 0.6722, 0.6778, |
| 116 | 0.7090, 0.6530, 0.6019, 0.5623, 0.5317, 0.5080, 0.4897, 0.4758, |
| 117 | 0.6010, 0.5370, 0.4774, 0.4296, 0.3905, 0.3580, 0.3304, 0.3069, |
| 118 | ); |
| 119 | |
| 120 | fn ggx_albedo(nv: f32, rough: f32) -> f32 { |
| 121 | let x = clamp(nv, 0.0, 1.0) * 7.0; |
| 122 | let y = clamp(rough, 0.0, 1.0) * 7.0; |
| 123 | let i = min(u32(x), 6u); |
| 124 | let j = min(u32(y), 6u); |
| 125 | let row0 = mix(GGX_ALBEDO[j * 8u + i], GGX_ALBEDO[j * 8u + i + 1u], x - f32(i)); |
| 126 | let row1 = mix(GGX_ALBEDO[j * 8u + i + 8u], GGX_ALBEDO[j * 8u + i + 9u], x - f32(i)); |
| 127 | return mix(row0, row1, y - f32(j)); |
| 128 | } |
| 129 | |
| 130 | // `MtlPBRUber`: GGX specular with Schlick Fresnel whose F0 blends from the IOR reflectance toward |
| 131 | // the base colour by metalness, scaled by 1 + F0 (1/E - 1) for the energy single scattering |
| 132 | // loses; a Charlie sheen lobe; and a GGX coat whose Fresnel dims everything beneath it. |
| 133 | fn pbr_uber_light( |
| 134 | n: vec3<f32>, |
| 135 | v: vec3<f32>, |
| 136 | l: vec3<f32>, |
| 137 | light_rgb: vec3<f32>, |
| 138 | albedo: vec3<f32>, |
| 139 | diffuse_vis: f32, |
| 140 | spec_vis: f32, |
| 141 | ) -> vec3<f32> { |
| 142 | let nl = max(dot(n, l), 0.0); |
| 143 | let nv = max(dot(n, v), 1e-3); |
| 144 | let h = normalize(l + v); |
| 145 | let nh = max(dot(n, h), 0.0); |
| 146 | let vh = max(dot(v, h), 0.0); |
| 147 | let metal = clamp(u.sfx.pbr.x, 0.0, 1.0); |
| 148 | let r0 = (u.sfx.pbr.z - 1.0) / (u.sfx.pbr.z + 1.0); |
| 149 | let f0 = mix(vec3<f32>(r0 * r0), albedo, metal); |
| 150 | let fres = f0 + (vec3<f32>(1.0) - f0) * pow(1.0 - vh, 5.0); |
| 151 | let diffuse = (1.0 - metal) * (vec3<f32>(1.0) - u.sfx.pbr.w * fres) * albedo * (u.sfx.pbr_spec.w * nl / 3.14159265); |
| 152 | let multi = vec3<f32>(1.0) + f0 * (1.0 / ggx_albedo(nv, u.sfx.pbr.y) - 1.0); |
| 153 | var spec = u.sfx.pbr_spec.rgb * fres * multi * (u.sfx.pbr.w * ggx_spec(nh, nl, nv, u.sfx.pbr.y)); |
| 154 | let inv_sheen = 1.0 / max(u.sfx.sheen.w * u.sfx.sheen.w, 1e-3); |
| 155 | let sin_h = sqrt(max(1.0 - nh * nh, 0.0)); |
| 156 | let charlie = (2.0 + inv_sheen) * pow(sin_h, inv_sheen) / 6.2831853; |
| 157 | spec = spec + u.sfx.sheen.rgb * (u.sfx.pbr_layers.w * charlie * nl / (4.0 * (nl + nv - nl * nv))); |
| 158 | let coat_f = u.sfx.pbr_layers.x * schlick(u.sfx.pbr_layers.z, vh); |
| 159 | let coat = coat_f * ggx_spec(nh, nl, nv, u.sfx.pbr_layers.y); |
| 160 | return light_rgb * ((diffuse * diffuse_vis + spec * spec_vis) * (1.0 - coat_f) + coat * spec_vis); |
| 161 | } |
| 162 | |
| 163 | // `Fog3D` over the lit colour: eye depth, or eye distance when Radial. |
| 164 | fn apply_fog(c: vec3<f32>, world_pos: vec3<f32>) -> vec3<f32> { |
| 165 | let kind = i32(u.sfx.fog.x + 0.5); |
| 166 | if (kind == 0) { |
| 167 | return c; |
| 168 | } |
| 169 | let eye = (u.view * vec4<f32>(world_pos, 1.0)).xyz; |
| 170 | let d = select(-eye.z, length(eye), u.sfx.fog_col.w > 0.5); |
| 171 | var f = exp(-u.sfx.fog.w * d); |
| 172 | if (kind == 1) { |
| 173 | f = (u.sfx.fog.z - d) / max(u.sfx.fog.z - u.sfx.fog.y, 1e-5); |
| 174 | } else if (kind == 3) { |
| 175 | f = exp(-(u.sfx.fog.w * d) * (u.sfx.fog.w * d)); |
| 176 | } |
| 177 | f = clamp(f, 0.0, 1.0); |
| 178 | return c * f + u.sfx.fog_col.rgb * (1.0 - f); |
| 179 | } |
| 180 | |
| 181 | // World direction in the environment's coordinate system (`env.x`: 1 model, 2 camera). |
| 182 | fn env_space(d: vec3<f32>) -> vec3<f32> { |
| 183 | let space = i32(u.sfx.env.x + 0.5); |
| 184 | if (space == 1) { |
| 185 | // The adjugate is the inverse up to a positive scale, which the caller normalises away. |
| 186 | return transpose(normal_matrix(u.model)) * d; |
| 187 | } |
| 188 | if (space == 2) { |
| 189 | return (u.view * vec4<f32>(d, 0.0)).xyz; |
| 190 | } |
| 191 | return d; |
| 192 | } |
| 193 | |
| 194 | // `CubeMap` texture coordinates (V down) of direction `d`: `mode` 2 = vertical cross, 3 = |
| 195 | // horizontal cross, 4 = one face image. Crosses sit at the image's bottom-left corner. |
| 196 | fn cube_uv(d: vec3<f32>, mode: i32, dims: vec2<f32>) -> vec2<f32> { |
| 197 | let a = abs(d); |
| 198 | let horizontal = mode == 3; |
| 199 | let mid = select(2.0, 1.0, horizontal); |
| 200 | var st: vec2<f32>; |
| 201 | var tile: vec2<f32>; |
| 202 | if (a.x >= a.y && a.x >= a.z) { |
| 203 | let s = vec2<f32>(d.z, d.y) / a.x * 0.5 + 0.5; |
| 204 | st = select(vec2<f32>(1.0 - s.x, s.y), s, d.x > 0.0); |
| 205 | tile = vec2<f32>(select(2.0, 0.0, d.x > 0.0), mid); |
| 206 | } else if (a.y >= a.z) { |
| 207 | let s = vec2<f32>(d.x, d.z) / a.y * 0.5 + 0.5; |
| 208 | st = select(vec2<f32>(1.0 - s.x, s.y), vec2<f32>(1.0) - s, d.y > 0.0); |
| 209 | tile = vec2<f32>(1.0, mid + select(-1.0, 1.0, d.y > 0.0)); |
| 210 | } else { |
| 211 | let s = vec2<f32>(d.x, d.y) / a.z * 0.5 + 0.5; |
| 212 | if (d.z > 0.0) { |
| 213 | st = vec2<f32>(1.0 - s.x, s.y); |
| 214 | tile = vec2<f32>(1.0, mid); |
| 215 | } else if (horizontal || mode == 4) { |
| 216 | st = s; |
| 217 | tile = vec2<f32>(3.0, 1.0); |
| 218 | } else { |
| 219 | st = vec2<f32>(1.0) - s; |
| 220 | tile = vec2<f32>(1.0, 0.0); |
| 221 | } |
| 222 | } |
| 223 | if (mode == 4) { |
| 224 | return vec2<f32>(st.x, 1.0 - st.y); |
| 225 | } |
| 226 | let face = select(min(dims.x / 3.0, dims.y / 4.0), min(dims.x / 4.0, dims.y / 3.0), horizontal); |
| 227 | let uv_up = (tile + st) * face / dims; |
| 228 | return vec2<f32>(uv_up.x, 1.0 - uv_up.y); |
| 229 | } |