diff --git a/crates/formats/README.md b/crates/formats/README.md index ca377337..acfe2f53 100644 --- a/crates/formats/README.md +++ b/crates/formats/README.md @@ -137,6 +137,7 @@ Built-in presets (each one lists the public specification its packing follows in | OpenPBR (separate maps) | one map per OpenPBR Surface input the set has: `base_color`, `base_metalness`, `specular_roughness`, `specular_roughness_anisotropy`, `coat_weight`, `coat_roughness`, `fuzz_weight`, `fuzz_color`, `fuzz_roughness`, `transmission_weight`, `subsurface_weight`, `emission_color`, `geometry_opacity`, `geometry_normal` (OpenGL), `height` (16-bit) | | Unity URP Lit (Metallic Smoothness) | `BaseMap`, `MetallicMap` (R metal, A smoothness), `NormalMap` (OpenGL), `OcclusionMap`, `EmissionMap` | | USD Preview Surface (separate maps) | one map per UsdPreviewSurface input the set has, named like it: `diffuseColor`, `emissiveColor`, `metallic`, `roughness`, `normal` (OpenGL), `occlusion`, `opacity`, `clearcoat`, `clearcoatRoughness` | +| Unreal Engine Subsurface Profile (Packed) | `BaseColor` (no alpha), `OcclusionRoughnessMetallic`, `Normal` (DirectX), `SubsurfaceMask` (scatter weight, for the Opacity input) | | Separate maps | one file per channel, gray where scalar, height 16-bit | | Mesh maps | every baked map (`$channel` = map id), 16-bit | diff --git a/crates/formats/src/export/tests.rs b/crates/formats/src/export/tests.rs index 14bb5238..ab398c0f 100644 --- a/crates/formats/src/export/tests.rs +++ b/crates/formats/src/export/tests.rs @@ -1740,3 +1740,48 @@ fn a_map_preview_holds_the_values_its_file_stores() { assert!(small.width < RES && small.pixels.len() == (small.width * small.height) as usize * 4, "a smaller preview samples the same map"); assert!(MapPreview::sample(&fx.input(), &options, options.preset.maps.len(), 8).is_err(), "a map the template doesn't have is an error"); } + +/// The 8-bit value of an image position as RGBA +fn pixel(file: &ExportedFile) -> [i32; 4] { + decode(file).to_rgba8().get_pixel(3, 3).0.map(i32::from) +} + +fn byte(value: f32) -> i32 { + (value * 255.0).round() as i32 +} + +fn assert_close(actual: [i32; 4], expected: [i32; 4], what: &str) { + assert!(actual.iter().zip(expected).all(|(a, e)| (a - e).abs() <= 1), "{what}: {actual:?} instead of {expected:?}"); +} + +/// Tilts every normal of `fx` and returns the green byte an OpenGL normal map stores for it +fn tilt_normals(fx: &mut Fx) -> i32 { + let tilt = Vec3::new(0.3, 0.4, 0.0); + let tilt = Vec3::new(tilt.x, tilt.y, (1.0 - tilt.length_squared()).sqrt()); + fx.set_all(Channel::Normal, |_, _| (tilt * 0.5 + Vec3::splat(0.5)).to_array()); + byte(tilt.y * 0.5 + 0.5) +} + +#[test] +fn the_unreal_subsurface_template_writes_the_scatter_mask_beside_the_packed_maps() { + let mut channels = default_channels(); + channels.extend([Channel::AmbientOcclusion, Channel::Opacity, Channel::ScatterWeight]); + let mut fx = Fx::new(&channels); + fx.set_all(Channel::AmbientOcclusion, |_, _| [0.6; 3]); + fx.set_all(Channel::Roughness, |_, _| [0.4; 3]); + fx.set_all(Channel::Metallic, |_, _| [0.8; 3]); + fx.set_all(Channel::Opacity, |_, _| [0.9; 3]); + fx.set_all(Channel::ScatterWeight, |_, _| [0.7; 3]); + let open_gl_green = tilt_normals(&mut fx); + + let files = fx.render_preset("Unreal Engine Subsurface Profile (Packed)"); + + let mask = find(&files, "_SubsurfaceMask.png"); + assert_eq!(decode(mask).color(), image::ColorType::L8, "the mask is one 8-bit channel"); + assert_close(pixel(mask), [byte(0.7), byte(0.7), byte(0.7), 255], "scatter weight in the mask"); + let [r, g, b, _] = pixel(find(&files, "_OcclusionRoughnessMetallic.png")); + assert_close([r, g, b, 0], [byte(0.6), byte(0.4), byte(0.8), 0], "R occlusion, G roughness, B metallic"); + assert_eq!(decode(find(&files, "_BaseColor.png")).color(), image::ColorType::Rgb8, "opacity feeds the mask, not the base color's alpha"); + let green = pixel(find(&files, "_Normal.png"))[1]; + assert!((green - (255 - open_gl_green)).abs() <= 1, "DirectX normal: green {green}"); +} diff --git a/crates/formats/src/presets.rs b/crates/formats/src/presets.rs index 2f2879a4..5fcf0ef0 100644 --- a/crates/formats/src/presets.rs +++ b/crates/formats/src/presets.rs @@ -219,6 +219,16 @@ pub fn builtin_presets() -> Vec { map("clearcoatRoughness", 8, gray(ch(Channel::CoatRoughness, 0))), ], ), + // Spec: https://dev.epicgames.com/documentation/en-us/unreal-engine/subsurface-profile-shading-model-in-unreal-engine (the Opacity input masks the scattering, so the base color has no alpha) + preset( + "Unreal Engine Subsurface Profile (Packed)", + vec![ + map("BaseColor", 8, rgb(|k| ch(Channel::BaseColor, k))), + map("OcclusionRoughnessMetallic", 8, orm()), + map("Normal", 8, normal(VirtualMap::NormalDirectX)), + map("SubsurfaceMask", 8, gray(ch(Channel::ScatterWeight, 0))), + ], + ), preset("Separate maps", Channel::ALL.into_iter().map(channel_map).collect()), preset( "Mesh maps", diff --git a/docs/features.md b/docs/features.md index 3da74d2d..3d0ff36a 100644 --- a/docs/features.md +++ b/docs/features.md @@ -163,7 +163,7 @@ Everything TextureCraft does today, by area. What's still missing is in [`ROADMA finished; the bake bar follows the newest map, pins a finished one or shows the material. - **Export presets** for glTF PBR Metal Roughness, Unreal Engine (packed), Unity HDRP (mask map), Unity Standard, Unity URP Lit, USD Preview Surface, Blender (Principled BSDF), Godot 4 (ORM), OpenPBR (with the coat, fuzz, - anisotropy, transmission and subsurface maps), separate maps, mesh maps and document channels, in + anisotropy, transmission and subsurface maps), Unreal Engine (subsurface profile), separate maps, mesh maps and document channels, in PNG, JPEG, JPEG 2000, JNG, PGM, TGA, TIFF, BMP, WEBP, PSD, EXR or HDR at 8 bits (optionally dithered), 16 bits or 32-bit float (TIFF, EXR, HDR; 16-bit half float for EXR), with UV padding (dilation, infinite, or dilation + diffusion), size override and OpenGL or DirectX normals.