Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.
C++ 3d3d-sculptingcppdear-imguidigital-sculptingfbxgame-developmentgaussian-splattinggltfimage-to-3dmcpopenglopenusdsculptingtext-to-3dunreal-engine
PBR viewport (phase 1B): Lit Sphere | PBR shading with image-based lighting
islandmed248c9ee89616
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 25ce1993..5762d696 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -6,6 +6,18 @@ All notable changes to SculptForge are listed here. The format follows## [Unreleased]+### Added+- PBR viewport: **Material > Viewport Shading** switches between **Lit Sphere** (the default) and **PBR**, which+ shows the texture's material channels as a game engine or renderer does: GGX reflections from a prefiltered+ environment, diffuse light from its spherical harmonics, multiple-scattering energy for rough metals, exposure+ and an ACES, AgX or Reinhard tone map. Painted Normal and Height channels bend the shading through MikkTSpace+ tangents. Four environments: **Studio** (generated) and three CC0 HDRIs from Poly Haven (**Photo Studio**,+ **Small Studio**, **Outdoor Sky**), plus `.hdr` / `.exr` files from `SCULPTFORGE_HDRI_PATH`; rotation, the+ environment behind the model and a cavity term that keeps sculpted detail readable. Parts without a material+ texture use a roughness and metallic set there. A new environment is prepared on a worker thread, and a+ 20.9-million-triangle mesh draws in about 8 ms in PBR next to about 6 ms with Lit Sphere (Apple M1 Pro).+ Saved with the project (`viewport_shading mode=pbr environment=outdoor_sky exposure=0.5 tone_map=agx`).+## [0.10.4] - 2026-11-01### Addeddiff --git a/CMakeLists.txt b/CMakeLists.txtindex 3046f86f..49b3df1d 100644--- a/CMakeLists.txt+++ b/CMakeLists.txt@@ -73,6 +73,7 @@ add_library(sculptforge_core STATICsrc/core/Texture.cppsrc/core/TextureCommands.cppsrc/core/TileImage.cpp+ src/core/EnvironmentMath.cppsrc/core/TextureChannels.cppsrc/core/TextureChannelCommands.cppsrc/core/LayerStackIO.cpp@@ -137,6 +138,13 @@ embed_files(${CMAKE_CURRENT_BINARY_DIR}/EmbeddedFace.cpp sf::assetsfaceTemplateData ${CMAKE_CURRENT_SOURCE_DIR}/assets/face/ict-arkit.sfface)target_sources(sculptforge_core PRIVATE src/core/Face.cpp src/core/FaceCommands.cpp${CMAKE_CURRENT_BINARY_DIR}/EmbeddedFace.cpp)+# The PBR viewport's bundled environments (core/EnvironmentMath.h): CC0 HDRIs from Poly Haven, 512 x 256+# (THIRD_PARTY_NOTICES.md).+embed_files(${CMAKE_CURRENT_BINARY_DIR}/EmbeddedHdri.cpp sf::assets+ hdriPhotoStudio ${CMAKE_CURRENT_SOURCE_DIR}/assets/hdri/brown_photostudio_02.hdr+ hdriSmallStudio ${CMAKE_CURRENT_SOURCE_DIR}/assets/hdri/studio_small_09.hdr+ hdriOutdoorSky ${CMAKE_CURRENT_SOURCE_DIR}/assets/hdri/kloofendal_48d_partly_cloudy_puresky.hdr)+target_sources(sculptforge_core PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/EmbeddedHdri.cpp)target_include_directories(sculptforge_core PUBLIC src PRIVATE third_party/stb third_party/ufbx third_party/xatlas third_party/mikktspace third_party/tinyexr)if(WIN32)target_link_libraries(sculptforge_core PRIVATE shell32) # core/Trash.cpp: the Recycle Bindiff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.mdindex cc45bdfe..5d3d20c1 100644--- a/THIRD_PARTY_NOTICES.md+++ b/THIRD_PARTY_NOTICES.md@@ -18,6 +18,7 @@ licenses require.| [Instant Meshes](https://github.com/wjakob/instant-meshes) | formulas only, nothing vendored | BSD 3-Clause License | QRemesh (Tool > Geometry): the field formulas in src/core/QRemesh.cpp (extrinsic 4-RoSy orientation / position compatibility, the middle point of two tangent planes, lattice rounding) follow its reference code. The rest of QRemesh is our own. || QRemesh 2 (published methods, no third-party code) | — | — | The default QRemesh method (src/core/QRemesh2*.cpp) is our own implementation of published techniques: integer-grid maps / mixed-integer quadrangulation (Bommes, Zimmer & Kobbelt 2009; Bommes, Campen, Ebke, Alliez & Kobbelt 2013), N-RoSy fields as complex powers and the smoothest field by inverse power iteration (Ray, Vallet, Li & Lévy 2008; Knöppel, Crane, Pinkall & Schröder 2013), the dihedral curvature tensor (Cohen-Steiner & Morvan 2003), isotropic remeshing (Botsch & Kobbelt 2004) and smoothed aggregation multigrid. No code from QuadWild (GPL-3), libigl (MPL-2.0), CoMISo or Instant Meshes is included. || [Inter typeface](https://github.com/rsms/inter) | 4.1 | SIL Open Font License 1.1 | User interface font, embedded in the desktop app. Vendored in assets/fonts. |+| [Poly Haven](https://polyhaven.com) HDRIs: Brown Photostudio 02, Studio Small 09 (Sergej Majboroda), Kloofendal 48d Partly Cloudy (Pure Sky) (Greg Zaal, sky edits by Jarod Guest) | 1K files, downsampled to 512 x 256 | CC0 1.0 (public domain) | Image-based lighting of the PBR viewport (Material > Viewport Shading). Vendored in assets/hdri and embedded. || [ICT-FaceKit](https://github.com/ICT-VGL/ICT-FaceKit) (ICT Face Model Light) | commit on master, 2020 | MIT License | The template head of Generate Facial Shapes (Tool > Facial Shapes): its neutral skin, mouth and eye sockets and its 53 ARKit-style expression shapes, converted by tools/face/build_ict_template.py into assets/face/ict-arkit.sfface (embedded). Not the separately licensed full ICT Face Model. || [libcurl](https://curl.se) | system library | curl License (MIT/X derivate) | HTTPS for optional AI features on Linux and macOS. Linked dynamically from the operating system; Windows uses the built-in WinHTTP instead. || [QR Code generator](https://github.com/nayuki/QR-Code-generator) | 1.8.0 | MIT License | The phone link's pairing QR code (File > Capture from phone). Vendored in third_party/qrcodegen. |@@ -613,6 +614,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.---+## Poly Haven HDRIs++https://polyhaven.com — CC0 1.0 Universal (public domain dedication), https://polyhaven.com/license.+assets/hdri/brown_photostudio_02.hdr and assets/hdri/studio_small_09.hdr (by Sergej Majboroda) and+assets/hdri/kloofendal_48d_partly_cloudy_puresky.hdr (by Greg Zaal, sky edits by Jarod Guest) are the 1K Radiance files from Poly Haven,+downsampled to 512 x 256. CC0 needs no attribution; it is given here as thanks. No other environment is bundled:+the "Studio" environment is generated in code (src/core/EnvironmentMath.cpp).+## ICT-FaceKithttps://github.com/ICT-VGL/ICT-FaceKit — MIT License. The "ICT Face Model Light" (the repository's FaceXModeldiff --git a/assets/hdri/brown_photostudio_02.hdr b/assets/hdri/brown_photostudio_02.hdrnew file mode 100644index 00000000..0ab53817Binary files /dev/null and b/assets/hdri/brown_photostudio_02.hdr differdiff --git a/assets/hdri/kloofendal_48d_partly_cloudy_puresky.hdr b/assets/hdri/kloofendal_48d_partly_cloudy_puresky.hdrnew file mode 100644index 00000000..e16d10e2Binary files /dev/null and b/assets/hdri/kloofendal_48d_partly_cloudy_puresky.hdr differdiff --git a/assets/hdri/studio_small_09.hdr b/assets/hdri/studio_small_09.hdrnew file mode 100644index 00000000..14d002f3Binary files /dev/null and b/assets/hdri/studio_small_09.hdr differdiff --git a/src/app/App.cpp b/src/app/App.cppindex b4ba1440..28411b29 100644--- a/src/app/App.cpp+++ b/src/app/App.cpp@@ -169,6 +169,7 @@ bool App::init(const AppOptions& options) {SDL_Log("%s", err.c_str());return false;}+ renderer_.setEnvironmentWake([] { wakeMainLoop(); }); // (PBR view: a new environment's lighting is ready)IMGUI_CHECKVERSION();ImGui::CreateContext();@@ -643,7 +644,7 @@ bool App::backgroundBusy() const {return running(pendingBrush_) || running(pendingGenerator_) || running(pendingModels_) || running(pendingUnreal_) ||running(pendingComfyCheck_) || galleryBusy() || thumbs_.busy() || libraryScanner_.busy() || autoUv_ ||bakeBusy() || qremesh_ || voxelRemesh_ || updateJob_ || splatTrainBusy() || sculptableBusy() || phoneBusy() ||- supportCardLoading();+ supportCardLoading() || renderer_.environmentPending();}void App::redrawIn(double seconds) {@@ -1757,6 +1758,7 @@ void App::runCommand(const std::string& name, const Args& args) {name != "export_unreal" && name.rfind("unreal_", 0) != 0 && name != "save_obj" && name != "save_project" &&name != "mesh_info" && name.rfind("brush_", 0) != 0 && name.rfind("splat_", 0) != 0 && name != "mesh_to_splats" &&name != "set_color" && name != "set_material" && name != "load_matcap" && name != "set_shading" && name != "subtool_shading" &&+ name != "viewport_shading" &&name != "export_ply" && name != "face_weights" &&name != "face_preset" && name.rfind("face_take", 0) != 0 && name != "face_landmarks_detect") {clearLods();@@ -1954,7 +1956,8 @@ void App::syncScene() {// Solo channel view (channel_view): the texture's shown texels follow before the renderer uploads them.if (mesh->texture) syncChannelView(*mesh->texture, doc_.channelView);const bool flat = s.shading == Shading::Flat || (s.shading == Shading::Default && renderSettings_.flatShading);- scene.push_back({s.id, mesh, active, s.visible, bakeNormalMap(s.id, *mesh), renderSettings_.wireframe, flat});+ scene.push_back({s.id, mesh, active, s.visible, bakeNormalMap(s.id, *mesh), renderSettings_.wireframe, flat,+ renderSettings_.pbr()});}renderer_.syncScene(scene);renderer_.syncSplats(doc_.splats, doc_.splatsVersion);diff --git a/src/app/App.h b/src/app/App.hindex 71a2d9b0..16d7e94b 100644--- a/src/app/App.h+++ b/src/app/App.h@@ -644,6 +644,7 @@ private:void setBackground(bool gradient, const glm::vec3& color);void drawBackgroundControls(const char* idSuffix); // Preferences > Display and the Render palettevoid drawShadingControls(); // Material palette and Tool > Display: Smooth | Flat, and the SubTool's own+ void drawViewportShadingControls(); // Material palette: Lit Sphere | PBR and the environmentvoid paintAt(const DabParams& p);void pickColorUnderCursor();// First paint stroke on a mesh without polypaint: undo swaps this paint stack (no colours) back in, so the@@ -679,6 +680,7 @@ private:// The channel chips of texture layer `layer` (a row under its name).Args drawLayerChannelChips(int layer); // the texture_layer_channel arguments to run (empty: nothing clicked)void registerPbrUiActions();+ double benchmarkDrawMs(int frames); // PanelPbr.cpp: UI tests' draw_msdouble pbrTestValue(const std::string& name) const; // UI test values (UiTest.cpp), NaN when not one of oursint exportPresetChoice_ = 0;// Dense textured meshes: while the Paint brush hovers, the texture painting caches for the area under it arediff --git a/src/app/PanelPbr.cpp b/src/app/PanelPbr.cppindex c9358c61..d2625288 100644--- a/src/app/PanelPbr.cpp+++ b/src/app/PanelPbr.cpp@@ -360,6 +360,23 @@ void App::prewarmTexturePaint() {prewarmTexturePainting(m, &doc_.bvh, centers, r * 1.25f); // a little wider: the pen moves before it lands}+// Benchmarks (UI tests' print draw_ms): the 3D view drawn `frames` times into the window's framebuffer, waiting+// for the GPU each time, in milliseconds per frame.+double App::benchmarkDrawMs(int frames) {+ int w = 0, h = 0;+ SDL_GetWindowSizeInPixels(window_, &w, &h);+ if (w <= 0 || h <= 0) return 0.0;+ syncScene();+ renderer_.draw(camera_, w, h, renderSettings_, canvasViewport(w, h));+ gl::Finish();+ const Uint64 start = SDL_GetTicksNS();+ for (int i = 0; i < frames; ++i) {+ renderer_.draw(camera_, w, h, renderSettings_, canvasViewport(w, h));+ gl::Finish();+ }+ return double(SDL_GetTicksNS() - start) * 1e-6 / double(std::max(frames, 1));+}+double App::pbrTestValue(const std::string& name) const {const Mesh& m = doc_.mesh;const bool tex = hasTexture(m);@@ -370,6 +387,12 @@ double App::pbrTestValue(const std::string& name) const {return n;}if (name == "channel_view") return double(int(doc_.channelView));+ if (name == "frame_ms") return frameSeconds_ * 1000.0; // (smoothed frame interval; fixed in UI tests)+ if (name == "draw_ms") return const_cast<App*>(this)->benchmarkDrawMs(20); // the 3D view drawn 20 times, GPU included+ if (name == "view_pbr") return renderSettings_.pbr() ? 1.0 : 0.0;+ if (name == "environment") return double(renderSettings_.environment);+ if (name == "environment_pending") return renderer_.environmentPending() ? 1.0 : 0.0;+ if (name == "pbr_planes") return double(renderer_.pbrPlanes(doc_.activeSubtoolInfo().id));if (name == "brush_channels") return double(doc_.paintMaterial.channels);if (name == "roughness_painted") {if (!tex || !channelEnabled(*m.texture, Channel::Roughness)) return 0.0;diff --git a/src/app/PanelPolypaint.cpp b/src/app/PanelPolypaint.cppindex cfe94300..05ec9a27 100644--- a/src/app/PanelPolypaint.cpp+++ b/src/app/PanelPolypaint.cpp@@ -166,6 +166,57 @@ void App::registerPaintUiCommands() {return {true, "Removed " + shown + " for this session; delete " + path +" to remove it for good (" + err + ")"};}});+ // Viewport shading: Lit Sphere or PBR (render/Renderer.h RenderSettings, core/EnvironmentMath.h).+ commands_.add({"viewport_shading",+ "Viewport shading: lit_sphere (the material sphere or studio lights, the default) or pbr (the "+ "texture's material channels lit by an environment: GGX reflections, diffuse light from the "+ "environment, exposure and a tone map). Only changes what the view shows.",+ {{"mode", ParamSpec::Type::String, "lit_sphere or pbr (default: unchanged).", false},+ {"environment", ParamSpec::Type::String,+ "studio, photo_studio, small_studio, outdoor_sky, or a file name from SCULPTFORGE_HDRI_PATH.", false},+ {"rotation", ParamSpec::Type::Number, "Environment rotation in degrees about the up axis.", false},+ {"exposure", ParamSpec::Type::Number, "Exposure in stops (-6 to 6; default 0).", false},+ {"tone_map", ParamSpec::Type::String, "aces (default), agx, reinhard or none.", false},+ {"background", ParamSpec::Type::Bool, "Show the environment behind the model.", false},+ {"blur", ParamSpec::Type::Number, "Background blur, 0 (sharp) to 1.", false},+ {"roughness", ParamSpec::Type::Number, "Roughness of meshes without a material texture (0-1).", false},+ {"metallic", ParamSpec::Type::Number, "Metallic of meshes without a material texture (0-1).", false},+ {"cavity", ParamSpec::Type::Number, "How much creases darken under PBR, 0-1 (default 0.5).", false}},+ [this](Document&, const Args& a) -> CommandResult {+ RenderSettings v = renderSettings_;+ if (a.contains("mode")) {+ const std::string m = argString(a, "mode");+ if (m == "pbr") v.shading = RenderSettings::Shading::Pbr;+ else if (m == "lit_sphere" || m == "litsphere" || m == "lit" || m == "matcap")+ v.shading = RenderSettings::Shading::LitSphere;+ else return {false, "mode must be lit_sphere or pbr"};+ }+ if (a.contains("environment")) {+ const int e = findEnvironment(argString(a, "environment"));+ if (e < 0) {+ std::string names;+ for (const EnvironmentInfo& i : environments()) names += (names.empty() ? "" : ", ") + i.id;+ return {false, "Unknown environment (" + names + ")"};+ }+ v.environment = e;+ }+ if (a.contains("tone_map") && !parseToneMap(argString(a, "tone_map"), v.toneMap))+ return {false, "tone_map must be aces, agx, reinhard or none"};+ v.environmentRotation = float(std::fmod(argNumber(a, "rotation", v.environmentRotation), 360.0));+ v.exposure = std::clamp(float(argNumber(a, "exposure", v.exposure)), -6.0f, 6.0f);+ v.environmentBackground = argBool(a, "background", v.environmentBackground);+ v.backgroundBlur = std::clamp(float(argNumber(a, "blur", v.backgroundBlur)), 0.0f, 1.0f);+ v.pbrRoughness = std::clamp(float(argNumber(a, "roughness", v.pbrRoughness)), 0.0f, 1.0f);+ v.pbrMetallic = std::clamp(float(argNumber(a, "metallic", v.pbrMetallic)), 0.0f, 1.0f);+ v.pbrCavity = std::clamp(float(argNumber(a, "cavity", v.pbrCavity)), 0.0f, 1.0f);+ renderSettings_ = v;+ if (!v.pbr()) return {true, "Viewport shading: Lit Sphere"};+ char buf[200];+ std::snprintf(buf, sizeof buf, "Viewport shading: PBR, %s, %+.1f EV, %s%s",+ environments()[size_t(v.environment)].label.c_str(), double(v.exposure),+ toneMapLabel(v.toneMap), v.environmentBackground ? ", background on" : "");+ return {true, buf};+ }});// The view's background (#21). Also the preference for new sessions.commands_.add({"set_background","The viewport background: gradient (the default charcoal radial gradient) or flat, one solid "@@ -398,6 +449,67 @@ void App::drawPalette_Material() {ImGui::ColorEdit3(tr("Base colour"), &renderSettings_.baseColor.x, ImGuiColorEditFlags_NoInputs);ui::HelpText("Base colour", nullptr, "Colour of the Studio material (the clay colour).", "Material");drawShadingControls();+ drawViewportShadingControls();+}++// Material > Viewport Shading: Lit Sphere or PBR, and the PBR view's environment. Every change runs+// viewport_shading, so scripts and the console see the same thing.+void App::drawViewportShadingControls() {+ ImGui::Spacing();+ ImGui::TextDisabled("%s", tr("Viewport Shading"));+ ui::Segmented(*this, {"view.shading_lit_sphere", "view.shading_pbr"});+ if (!renderSettings_.pbr()) return;+ RenderSettings& v = renderSettings_;+ const auto& envs = environments();+ const int current = std::clamp(v.environment, 0, int(envs.size()) - 1);+ ImGui::SetNextItemWidth(-1);+ if (ImGui::BeginCombo("##environment", envs[size_t(current)].label.c_str())) {+ for (int i = 0; i < int(envs.size()); ++i) {+ if (ImGui::Selectable(envs[size_t(i)].label.c_str(), i == current))+ runCommand("viewport_shading", {{"environment", envs[size_t(i)].id}});+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("%s", envs[size_t(i)].credit.c_str());+ }+ ImGui::EndCombo();+ }+ ui::HelpText("Environment", nullptr,+ "The light around the model: Studio (generated), Photo Studio, Small Studio and Outdoor Sky. HDR "+ "files (.hdr, .exr) in the folders of SCULPTFORGE_HDRI_PATH are listed after them.",+ "Material > Viewport Shading");+ // Sliders change the view live and run the command once when let go (one console line per change).+ auto slider = [&](const char* label, const char* key, float* value, float lo, float hi, const char* fmt, const char* help) {+ ImGui::SetNextItemWidth(-1);+ ImGui::SliderFloat(label, value, lo, hi, fmt);+ if (ImGui::IsItemDeactivatedAfterEdit()) runCommand("viewport_shading", {{key, double(*value)}});+ ui::HelpText(label + 2, nullptr, help, "Material > Viewport Shading");+ };+ slider("##Exposure", "exposure", &v.exposure, -6.0f, 6.0f, tr("Exposure %+.1f EV"),+ "Brightness of the light in stops: +1 doubles it.");+ slider("##Rotation", "rotation", &v.environmentRotation, -180.0f, 180.0f, tr("Rotation %.0f deg"),+ "Turns the environment about the up axis, moving the highlights.");+ ImGui::SetNextItemWidth(-1);+ if (ImGui::BeginCombo("##tonemap", (std::string(tr("Tone map")) + ": " + toneMapLabel(v.toneMap)).c_str())) {+ for (int i = 0; i < kToneMapCount; ++i)+ if (ImGui::Selectable(toneMapLabel(ToneMap(i)), int(v.toneMap) == i))+ runCommand("viewport_shading", {{"tone_map", std::string(toneMapId(ToneMap(i)))}});+ ImGui::EndCombo();+ }+ ui::HelpText("Tone map", nullptr,+ "How bright light maps to the screen: ACES (filmic contrast), AgX (softer, keeps bright colours from "+ "going flat), Reinhard (gentle) or None.",+ "Material > Viewport Shading");+ bool background = v.environmentBackground;+ if (ui::Checkbox("Show environment##envbg", &background))+ runCommand("viewport_shading", {{"background", background}});+ ui::HelpText("Show environment", nullptr, "Show the environment behind the model instead of the background.",+ "Material > Viewport Shading");+ if (v.environmentBackground)+ slider("##Blur", "blur", &v.backgroundBlur, 0.0f, 1.0f, tr("Blur %.2f"), "How soft the environment behind the model is.");+ slider("##Roughness", "roughness", &v.pbrRoughness, 0.0f, 1.0f, tr("Roughness %.2f"),+ "Roughness of Parts without a material texture (their colour comes from polypaint or the base colour).");+ slider("##Metallic", "metallic", &v.pbrMetallic, 0.0f, 1.0f, tr("Metallic %.2f"),+ "Metallic of Parts without a material texture.");+ slider("##Cavity", "cavity", &v.pbrCavity, 0.0f, 1.0f, tr("Cavity %.2f"),+ "How much the sculpt's creases darken, so detail reads under even light. 0 is plain PBR.");}void App::drawShadingControls() {diff --git a/src/app/PanelProject.cpp b/src/app/PanelProject.cppindex cc68f8e4..217cffff 100644--- a/src/app/PanelProject.cpp+++ b/src/app/PanelProject.cpp@@ -262,6 +262,22 @@ void App::storeProjectSettings() {render.set("background", std::string(renderSettings_.gradientBackground ? "gradient" : "flat"));render.set("background_color", vec3Json(renderSettings_.backgroundColor));render.set("matcap", renderSettings_.matcap < 0 ? std::string() : std::string(matcapName(renderSettings_.matcap)));+ // Viewport shading (Material > Viewport Shading): Lit Sphere or PBR with its environment.+ {+ const RenderSettings& v = renderSettings_;+ Json pbr = Json::object();+ pbr.set("shading", std::string(v.pbr() ? "pbr" : "lit_sphere"));+ pbr.set("environment", environments()[size_t(std::clamp(v.environment, 0, environmentCount() - 1))].id);+ pbr.set("rotation", double(v.environmentRotation));+ pbr.set("exposure", double(v.exposure));+ pbr.set("tone_map", std::string(toneMapId(v.toneMap)));+ pbr.set("background", v.environmentBackground);+ pbr.set("background_blur", double(v.backgroundBlur));+ pbr.set("roughness", double(v.pbrRoughness));+ pbr.set("metallic", double(v.pbrMetallic));+ pbr.set("cavity", double(v.pbrCavity));+ render.set("viewport", std::move(pbr));+ }// A custom MatCap travels inside the project (a PNG attachment), so the project opens with it on any machine.for (auto it = doc_.projectFiles.begin(); it != doc_.projectFiles.end();)it = it->first.rfind("matcaps/", 0) == 0 ? doc_.projectFiles.erase(it) : std::next(it);@@ -333,6 +349,29 @@ void App::applyProjectSettings() {readFloat(r, "splat_opacity", renderSettings_.splatOpacity);// Projects from before the setting: the preferred shading.renderSettings_.flatShading = r["flat_shading"].asBool(uiPrefs_.flatShading);+ // Viewport shading; projects from before it: Lit Sphere.+ {+ RenderSettings& v = renderSettings_;+ const RenderSettings defaults;+ const Json& pbr = r["viewport"];+ auto num = [&](const char* key, float def, float lo, float hi) {+ return pbr.isObject() && pbr[key].isNumber() ? std::clamp(float(pbr[key].asNumber()), lo, hi) : def;+ };+ v.shading = pbr.isObject() && pbr["shading"].isString() && pbr["shading"].asString() == "pbr"+ ? RenderSettings::Shading::Pbr+ : RenderSettings::Shading::LitSphere;+ const int env = pbr.isObject() && pbr["environment"].isString() ? findEnvironment(pbr["environment"].asString()) : -1;+ v.environment = env >= 0 ? env : 0;+ v.environmentRotation = num("rotation", defaults.environmentRotation, -360.0f, 360.0f);+ v.exposure = num("exposure", defaults.exposure, -10.0f, 10.0f);+ v.toneMap = defaults.toneMap;+ if (pbr.isObject() && pbr["tone_map"].isString()) parseToneMap(pbr["tone_map"].asString(), v.toneMap);+ v.environmentBackground = pbr.isObject() && pbr["background"].isBool() ? pbr["background"].asBool() : defaults.environmentBackground;+ v.backgroundBlur = num("background_blur", defaults.backgroundBlur, 0.0f, 1.0f);+ v.pbrRoughness = num("roughness", defaults.pbrRoughness, 0.0f, 1.0f);+ v.pbrMetallic = num("metallic", defaults.pbrMetallic, 0.0f, 1.0f);+ v.pbrCavity = num("cavity", defaults.pbrCavity, 0.0f, 1.0f);+ }// The background (#21); projects from before it: the preferred one.renderSettings_.gradientBackground =r["background"].isString() ? r["background"].asString() != "flat" : uiPrefs_.gradientBackground;diff --git a/src/app/Shell.cpp b/src/app/Shell.cppindex 59481238..089a05c5 100644--- a/src/app/Shell.cpp+++ b/src/app/Shell.cpp@@ -422,6 +422,20 @@ void App::registerShellActions() {flag("view.polypaint", "Paint", ICON_LC_PALETTE, "Render", "", &renderSettings_.showPolypaint,"Show polypaint colours (off: plain material).");// Shading (Material palette, Tool > Display): the scene's, and the active SubTool's own.+ // Viewport shading (Material > Viewport Shading): Lit Sphere or PBR.+ add({"view.shading_lit_sphere", "Lit Sphere", nullptr,+ "Lit Sphere shading (the default): the chosen material sphere or the studio lights, made for reading forms "+ "while sculpting.",+ "Material > Viewport Shading", "", "viewport_shading",+ [this] { runCommand("viewport_shading", {{"mode", std::string("lit_sphere")}}); },+ [this] { return !renderSettings_.pbr(); }, nullptr});+ add({"view.shading_pbr", "PBR", nullptr,+ "PBR shading: the texture's material channels (base colour, roughness, metallic, normal, height, AO, emissive) "+ "lit by an environment, as a game engine or renderer shows them. Meshes without a material use the roughness "+ "and metallic set here.",+ "Material > Viewport Shading", "", "viewport_shading",+ [this] { runCommand("viewport_shading", {{"mode", std::string("pbr")}}); },+ [this] { return renderSettings_.pbr(); }, nullptr});add({"material.smooth", "Smooth", nullptr,"Smooth shading (the default): normals blend across polygons, so the surface reads as one form.", "Material", "","", [this] { runCommand("set_shading", {{"mode", std::string("smooth")}}); },diff --git a/src/app/Ui.cpp b/src/app/Ui.cppindex 1f674c17..3d22adfc 100644--- a/src/app/Ui.cpp+++ b/src/app/Ui.cpp@@ -118,7 +118,8 @@ bool IsModeAction(std::string_view id) {bool IsChoiceAction(std::string_view id) {return id == "draw.zadd" || id == "draw.zsub" || id == "material.smooth" || id == "material.flat" ||- id == "view.background_gradient" || id == "view.background_flat" ||+ id == "view.background_gradient" || id == "view.background_flat" || id == "view.shading_lit_sphere" ||+ id == "view.shading_pbr" ||id.substr(0, 16) == "subtool.shading_";}diff --git a/src/app/UiTest.cpp b/src/app/UiTest.cppindex d57c1394..757a2869 100644--- a/src/app/UiTest.cpp+++ b/src/app/UiTest.cpp@@ -140,6 +140,8 @@// (mesh values are the active SubTool's)// snapshot remember the active mesh's vertices (for the moved* values)// remember VALUE NAME keep VALUE's current value as $NAME+// run_fails COMMAND ... run a command that must be refused (bad arguments)+// print VALUE write VALUE's current value to the log (benchmarks: frame_ms)// shot NAME save a screenshot NAME.png (to --ui-out)// expect_guide FILE the User Guide shows FILE ("masking.md", "index.md")// dump_shortcuts NAME write every default shortcut as a Markdown table to --ui-out/NAME@@ -1371,6 +1373,19 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& e}}});+ } else if (verb == "run_fails") { // run_fails COMMAND ARGS...: the command must be refused+ if (!need(1)) return false;+ std::vector<ScriptStatement> statements;+ if (!parseScript(restOf(lineText_, 1), statements, error)) return false;+ run([this, line, statements] {+ for (const auto& st : statements) {+ app_.runCommand(st.command, st.args);+ if (app_.lastCommandOk_) {+ fail(line, st.command + " was expected to fail: " + app_.status_);+ return;+ }+ }+ });} else if (verb == "support_card") {// The launch decision for the support card (Support.h), as a start would make it: `first_run`, `updated` (a// new version since it last showed), `days=N` (the clock N days after it last showed) and `test_mode` (how@@ -1549,6 +1564,20 @@ bool Demo::parseStep(int line, const std::vector<std::string>& w, std::string& ebasePositions_ = app_.doc_.mesh.positions;baseNormals_ = app_.doc_.mesh.normals;});+ } else if (verb == "print") { // print NAME: writes a value to the log (benchmarks: frame_ms, ...)+ if (!need(1)) return false;+ bool known = false;+ query(w[1], known);+ if (!known) {+ error = "unknown value '" + w[1] + "'";+ return false;+ }+ run([this, name = w[1]] {+ bool k = false;+ const double v = query(name, k);+ std::printf("%s = %g\n", name.c_str(), v);+ std::fflush(stdout);+ });} else if (verb == "remember") {if (!need(2)) return false;bool known = false;diff --git a/src/core/EnvironmentMath.cpp b/src/core/EnvironmentMath.cppnew file mode 100644index 00000000..20ac452c--- /dev/null+++ b/src/core/EnvironmentMath.cpp@@ -0,0 +1,529 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+#include "core/EnvironmentMath.h"++#include "core/BakeImage.h"+#include "core/Parallel.h"++#define STBI_NO_STDIO+#include <stb_image.h>++#include <algorithm>+#include <chrono>+#include <cmath>+#include <cstdlib>+#include <filesystem>+#include <fstream>+#include <iterator>+#include <mutex>++namespace sf {++namespace assets {+extern const unsigned char hdriPhotoStudio[];+extern const unsigned long long hdriPhotoStudio_size;+extern const unsigned char hdriSmallStudio[];+extern const unsigned long long hdriSmallStudio_size;+extern const unsigned char hdriOutdoorSky[];+extern const unsigned long long hdriOutdoorSky_size;+} // namespace assets++namespace {++constexpr float kPi = 3.14159265358979f;++float radicalInverse(uint32_t b) {+ b = (b << 16u) | (b >> 16u);+ b = ((b & 0x55555555u) << 1u) | ((b & 0xAAAAAAAAu) >> 1u);+ b = ((b & 0x33333333u) << 2u) | ((b & 0xCCCCCCCCu) >> 2u);+ b = ((b & 0x0F0F0F0Fu) << 4u) | ((b & 0xF0F0F0F0u) >> 4u);+ b = ((b & 0x00FF00FFu) << 8u) | ((b & 0xFF00FF00u) >> 8u);+ return float(b) * 2.3283064365386963e-10f;+}++// A GGX half vector around +Z for roughness alpha (= roughness^2).+glm::vec3 sampleGgx(float u1, float u2, float alpha) {+ const float phi = 2.0f * kPi * u1;+ const float a2 = alpha * alpha;+ const float cosT = std::sqrt((1.0f - u2) / (1.0f + (a2 - 1.0f) * u2));+ const float sinT = std::sqrt(std::max(0.0f, 1.0f - cosT * cosT));+ return {sinT * std::cos(phi), sinT * std::sin(phi), cosT};+}++void basis(const glm::vec3& n, glm::vec3& t, glm::vec3& b) {+ const float s = n.z >= 0.0f ? 1.0f : -1.0f;+ const float a = -1.0f / (s + n.z), c = n.x * n.y * a;+ t = glm::vec3(1.0f + s * n.x * n.x * a, s * c, -s * n.x);+ b = glm::vec3(c, s + n.y * n.y * a, -n.y);+}++float ggxD(float NdotH, float alpha) {+ const float a2 = alpha * alpha;+ const float d = NdotH * NdotH * (a2 - 1.0f) + 1.0f;+ return a2 / (kPi * d * d);+}++std::filesystem::path fsPath(const std::string& s) { return std::filesystem::path(std::u8string(s.begin(), s.end())); }++bool decodeHdr(const unsigned char* data, size_t size, EnvImage& out, std::string& error) {+ int w = 0, h = 0, n = 0;+ float* px = stbi_loadf_from_memory(data, int(size), &w, &h, &n, 3);+ if (!px) {+ error = std::string("Can't read the environment image: ") + (stbi_failure_reason() ? stbi_failure_reason() : "unknown format");+ return false;+ }+ out.width = w;+ out.height = h;+ out.texels.resize(size_t(w) * size_t(h));+ for (size_t i = 0; i < out.texels.size(); ++i)+ out.texels[i] = glm::max(glm::vec3(px[i * 3], px[i * 3 + 1], px[i * 3 + 2]), glm::vec3(0.0f));+ stbi_image_free(px);+ return true;+}++} // namespace++// ---------------------------------------------------------------------------+// Images and directions+// ---------------------------------------------------------------------------++glm::vec3 equirectDirection(float u, float v) {+ const float phi = (u - 0.5f) * 2.0f * kPi, theta = v * kPi;+ const float st = std::sin(theta);+ return {st * std::sin(phi), std::cos(theta), -st * std::cos(phi)};+}++glm::vec2 equirectCoords(const glm::vec3& d) {+ const float phi = std::atan2(d.x, -d.z);+ return {phi / (2.0f * kPi) + 0.5f, std::acos(std::clamp(d.y, -1.0f, 1.0f)) / kPi};+}++glm::vec3 EnvImage::sample(float u, float v) const {+ if (!valid()) return glm::vec3(0.0f);+ const float fx = u * float(width) - 0.5f, fy = std::clamp(v * float(height) - 0.5f, 0.0f, float(height - 1));+ const float x0f = std::floor(fx), y0f = std::floor(fy);+ const float ax = fx - x0f, ay = fy - y0f;+ auto wrap = [&](int x) { return ((x % width) + width) % width; };+ const int x0 = wrap(int(x0f)), x1 = wrap(int(x0f) + 1);+ const int y0 = int(y0f), y1 = std::min(y0 + 1, height - 1);+ auto at = [&](int x, int y) { return texels[size_t(y) * size_t(width) + size_t(x)]; };+ return glm::mix(glm::mix(at(x0, y0), at(x1, y0), ax), glm::mix(at(x0, y1), at(x1, y1), ax), ay);+}++glm::vec3 EnvImage::sample(const glm::vec3& dir) const {+ const glm::vec2 uv = equirectCoords(dir);+ return sample(uv.x, uv.y);+}++EnvImage downsample(const EnvImage& img) {+ EnvImage out;+ out.width = std::max(1, img.width / 2);+ out.height = std::max(1, img.height / 2);+ out.texels.resize(size_t(out.width) * size_t(out.height));+ for (int y = 0; y < out.height; ++y)+ for (int x = 0; x < out.width; ++x) {+ glm::vec3 s(0.0f);+ int n = 0;+ for (int dy = 0; dy < 2; ++dy)+ for (int dx = 0; dx < 2; ++dx) {+ const int sx = std::min(x * 2 + dx, img.width - 1), sy = std::min(y * 2 + dy, img.height - 1);+ s += img.texels[size_t(sy) * size_t(img.width) + size_t(sx)];+ ++n;+ }+ out.texels[size_t(y) * size_t(out.width) + size_t(x)] = s / float(n);+ }+ return out;+}++// ---------------------------------------------------------------------------+// Diffuse: SH9+// ---------------------------------------------------------------------------++std::array<float, 9> shBasis(const glm::vec3& n) {+ return {0.282095f,+ 0.488603f * n.y,+ 0.488603f * n.z,+ 0.488603f * n.x,+ 1.092548f * n.x * n.y,+ 1.092548f * n.y * n.z,+ 0.315392f * (3.0f * n.z * n.z - 1.0f),+ 1.092548f * n.x * n.z,+ 0.546274f * (n.x * n.x - n.y * n.y)};+}++glm::vec3 SH9::eval(const glm::vec3& n) const {+ const std::array<float, 9> b = shBasis(n);+ glm::vec3 s(0.0f);+ for (int i = 0; i < 9; ++i) s += c[size_t(i)] * b[size_t(i)];+ return glm::max(s, glm::vec3(0.0f));+}++SH9 irradianceSH9(const EnvImage& env) {+ SH9 sh;+ if (!env.valid()) return sh;+ const float dPhi = 2.0f * kPi / float(env.width), dTheta = kPi / float(env.height);+ for (int y = 0; y < env.height; ++y) {+ const float v = (float(y) + 0.5f) / float(env.height);+ const float dOmega = dPhi * dTheta * std::sin(v * kPi);+ for (int x = 0; x < env.width; ++x) {+ const glm::vec3 d = equirectDirection((float(x) + 0.5f) / float(env.width), v);+ const std::array<float, 9> b = shBasis(d);+ const glm::vec3 L = env.texels[size_t(y) * size_t(env.width) + size_t(x)] * dOmega;+ for (int i = 0; i < 9; ++i) sh.c[size_t(i)] += L * b[size_t(i)];+ }+ }+ // Cosine-lobe convolution (Ramamoorthi & Hanrahan 2001): A0 = pi, A1 = 2 pi / 3, A2 = pi / 4; then / pi.+ const float band[9] = {1.0f, 2.0f / 3.0f, 2.0f / 3.0f, 2.0f / 3.0f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f};+ for (int i = 0; i < 9; ++i) sh.c[size_t(i)] *= band[i];+ return sh;+}++glm::vec3 irradianceBruteForce(const EnvImage& env, const glm::vec3& n) {+ glm::vec3 s(0.0f);+ const float dPhi = 2.0f * kPi / float(env.width), dTheta = kPi / float(env.height);+ for (int y = 0; y < env.height; ++y) {+ const float v = (float(y) + 0.5f) / float(env.height);+ const float dOmega = dPhi * dTheta * std::sin(v * kPi);+ for (int x = 0; x < env.width; ++x) {+ const glm::vec3 d = equirectDirection((float(x) + 0.5f) / float(env.width), v);+ const float c = glm::dot(d, n);+ if (c > 0.0f) s += env.texels[size_t(y) * size_t(env.width) + size_t(x)] * (c * dOmega);+ }+ }+ return s / kPi;+}++// ---------------------------------------------------------------------------+// Specular: BRDF table and prefiltered levels+// ---------------------------------------------------------------------------++glm::vec2 integrateBrdf(float NdotV, float roughness, int samples) {+ NdotV = std::clamp(NdotV, 1e-4f, 1.0f);+ const glm::vec3 V(std::sqrt(1.0f - NdotV * NdotV), 0.0f, NdotV);+ const float alpha = std::max(roughness * roughness, 1e-4f);+ const float k = alpha / 2.0f; // Smith-Schlick for image-based lighting (Karis 2013)+ float A = 0.0f, B = 0.0f;+ for (int i = 0; i < samples; ++i) {+ const glm::vec3 H = sampleGgx((float(i) + 0.5f) / float(samples), radicalInverse(uint32_t(i)), alpha);+ const glm::vec3 L = 2.0f * glm::dot(V, H) * H - V;+ const float NdotL = L.z, NdotH = std::max(H.z, 0.0f), VdotH = std::max(glm::dot(V, H), 0.0f);+ if (NdotL <= 0.0f) continue;+ const float gv = NdotV / (NdotV * (1.0f - k) + k), gl = NdotL / (NdotL * (1.0f - k) + k);+ const float gVis = gv * gl * VdotH / std::max(NdotH * NdotV, 1e-6f);+ const float fc = std::pow(1.0f - VdotH, 5.0f);+ A += (1.0f - fc) * gVis;+ B += fc * gVis;+ }+ return glm::vec2(A, B) / float(samples);+}++std::vector<glm::vec2> brdfLut(int size) {+ std::vector<glm::vec2> lut(size_t(size) * size_t(size));+ parallelFor(size_t(size), [&](size_t b, size_t e) {+ for (size_t y = b; y < e; ++y)+ for (int x = 0; x < size; ++x)+ lut[y * size_t(size) + size_t(x)] =+ integrateBrdf((float(x) + 0.5f) / float(size), (float(y) + 0.5f) / float(size), 256);+ }, 1);+ return lut;+}++std::vector<EnvImage> prefilterEnvironment(const EnvImage& envIn, int samples) {+ std::vector<EnvImage> levels;+ if (!envIn.valid()) return levels;+ // Source pyramid from at least the base size, for filtered importance sampling (Krivanek & Colbert 2008:+ // each sample reads the mip whose texels cover its share of the lobe, so few samples come out smooth).+ std::vector<EnvImage> pyramid{envIn};+ while (pyramid.back().width > kEnvBaseWidth * 2) pyramid.push_back(downsample(pyramid.back()));+ while (pyramid.back().width > 4) pyramid.push_back(downsample(pyramid.back()));+ auto trilinear = [&](const glm::vec3& d, float mip) {+ mip = std::clamp(mip, 0.0f, float(pyramid.size() - 1));+ const int m0 = int(mip), m1 = std::min(m0 + 1, int(pyramid.size()) - 1);+ const glm::vec2 uv = equirectCoords(d);+ return glm::mix(pyramid[size_t(m0)].sample(uv.x, uv.y), pyramid[size_t(m1)].sample(uv.x, uv.y), mip - float(m0));+ };+ const float texelSolid = 4.0f * kPi / (float(pyramid[0].width) * float(pyramid[0].height));+ for (int k = 0; k < kEnvLevels; ++k) {+ EnvImage out;+ out.width = std::max(4, kEnvBaseWidth >> k);+ out.height = std::max(2, out.width / 2);+ out.texels.resize(size_t(out.width) * size_t(out.height));+ const float roughness = float(k) / float(kEnvLevels - 1);+ const float alpha = std::max(roughness * roughness, 1e-3f);+ // The level-0 image: the source at this size (the pyramid level that is at least as big, resampled).+ parallelFor(size_t(out.height), [&](size_t b, size_t e) {+ for (size_t y = b; y < e; ++y)+ for (int x = 0; x < out.width; ++x) {+ const glm::vec3 N = equirectDirection((float(x) + 0.5f) / float(out.width), (float(y) + 0.5f) / float(out.height));+ glm::vec3& o = out.texels[y * size_t(out.width) + size_t(x)];+ if (k == 0) {+ const float mip = std::log2(float(pyramid[0].width) / float(out.width));+ o = trilinear(N, mip);+ continue;+ }+ glm::vec3 t, bt;+ basis(N, t, bt);+ glm::vec3 sum(0.0f);+ float weight = 0.0f;+ for (int i = 0; i < samples; ++i) {+ const glm::vec3 h = sampleGgx((float(i) + 0.5f) / float(samples), radicalInverse(uint32_t(i)), alpha);+ const glm::vec3 H = t * h.x + bt * h.y + N * h.z;+ const glm::vec3 L = 2.0f * glm::dot(N, H) * H - N; // V = N+ const float NdotL = glm::dot(N, L);+ if (NdotL <= 0.0f) continue;+ const float pdf = ggxD(h.z, alpha) / 4.0f; // D * NdotH / (4 VdotH) with V = N+ const float sampleSolid = 1.0f / (float(samples) * std::max(pdf, 1e-6f));+ const float mip = 0.5f * std::log2(sampleSolid / texelSolid) + 1.0f;+ sum += trilinear(L, mip) * NdotL;+ weight += NdotL;+ }+ o = weight > 0.0f ? sum / weight : glm::vec3(0.0f);+ }+ }, 1);+ levels.push_back(std::move(out));+ }+ return levels;+}++// ---------------------------------------------------------------------------+// Tone mapping+// ---------------------------------------------------------------------------++const char* toneMapId(ToneMap t) {+ switch (t) {+ case ToneMap::Aces: return "aces";+ case ToneMap::AgX: return "agx";+ case ToneMap::Reinhard: return "reinhard";+ case ToneMap::None: return "none";+ }+ return "aces";+}++const char* toneMapLabel(ToneMap t) {+ switch (t) {+ case ToneMap::Aces: return "ACES";+ case ToneMap::AgX: return "AgX";+ case ToneMap::Reinhard: return "Reinhard";+ case ToneMap::None: return "None";+ }+ return "ACES";+}++bool parseToneMap(const std::string& text, ToneMap& out) {+ std::string s;+ for (char c : text) s += char(std::tolower(static_cast<unsigned char>(c)));+ for (int i = 0; i < kToneMapCount; ++i)+ if (s == toneMapId(ToneMap(i))) {+ out = ToneMap(i);+ return true;+ }+ if (s == "linear" || s == "off") {+ out = ToneMap::None;+ return true;+ }+ return false;+}++glm::vec3 toneMap(ToneMap t, const glm::vec3& cIn) {+ const glm::vec3 c = glm::max(cIn, glm::vec3(0.0f));+ switch (t) {+ case ToneMap::Aces: { // Narkowicz's fit of the ACES film curve+ const glm::vec3 r = (c * (2.51f * c + 0.03f)) / (c * (2.43f * c + 0.59f) + 0.14f);+ return glm::clamp(r, 0.0f, 1.0f);+ }+ case ToneMap::AgX: {+ // AgX-style: into a log2 space of 16.5 stops around mid grey, through a smooth S curve, then back to+ // display-linear. The inset matrix (rows) desaturates bright colours toward white as film does.+ const glm::mat3 inset(glm::vec3(0.842479f, 0.0423282f, 0.0423756f), glm::vec3(0.0784336f, 0.878468f, 0.0784336f),+ glm::vec3(0.0792237f, 0.0791661f, 0.879142f));+ const glm::mat3 outset(glm::vec3(1.19688f, -0.0528968f, -0.0529716f), glm::vec3(-0.0980209f, 1.15190f, -0.0980435f),+ glm::vec3(-0.0990297f, -0.0989612f, 1.15107f));+ glm::vec3 x = inset * c;+ const float minEv = -12.47393f, maxEv = 4.026069f;+ for (int i = 0; i < 3; ++i) {+ float v = std::log2(std::max(x[i], 1e-10f));+ v = std::clamp((v - minEv) / (maxEv - minEv), 0.0f, 1.0f);+ // A smooth S curve through (0, 0), (0.606, 0.5), (1, 1): mid grey lands at about 0.18 display-linear.+ const float p = 0.6060606f;+ const float s = v < p ? 0.5f * std::pow(v / p, 2.2f) : 1.0f - 0.5f * std::pow((1.0f - v) / (1.0f - p), 1.6f);+ x[i] = s;+ }+ x = outset * x;+ return glm::clamp(glm::pow(glm::max(x, glm::vec3(0.0f)), glm::vec3(2.2f)), 0.0f, 1.0f);+ }+ case ToneMap::Reinhard: { // extended Reinhard on luminance, white at 6+ const float l = glm::dot(c, glm::vec3(0.2126f, 0.7152f, 0.0722f));+ if (l <= 0.0f) return glm::vec3(0.0f);+ const float w2 = 36.0f;+ const float lo = l * (1.0f + l / w2) / (1.0f + l);+ return glm::clamp(c * (lo / l), 0.0f, 1.0f);+ }+ case ToneMap::None: return glm::clamp(c, 0.0f, 1.0f);+ }+ return glm::clamp(c, 0.0f, 1.0f);+}++// ---------------------------------------------------------------------------+// Environments+// ---------------------------------------------------------------------------++EnvImage studioEnvironment(int width) {+ EnvImage img;+ img.width = std::max(8, width);+ img.height = img.width / 2;+ img.texels.resize(size_t(img.width) * size_t(img.height));+ struct Box {+ float azimuth, elevation; // degrees: azimuth 0 = -Z (in front of the model), 90 = +X+ float halfW, halfH; // half size on the box's plane at distance 1 (tangent of the half angle)+ glm::vec3 color;+ };+ const Box boxes[] = {+ {55.0f, 38.0f, 0.45f, 0.32f, glm::vec3(9.0f, 8.7f, 8.2f)}, // key: big, warm-white, high to the front right+ {-110.0f, 10.0f, 0.55f, 0.35f, glm::vec3(0.8f, 0.9f, 1.1f)}, // fill: broad and cool, to the left+ {170.0f, 22.0f, 0.10f, 0.75f, glm::vec3(6.0f, 6.0f, 6.3f)}, // rim: a tall strip behind+ {0.0f, 85.0f, 0.5f, 0.5f, glm::vec3(0.9f)}, // overhead+ };+ for (int y = 0; y < img.height; ++y)+ for (int x = 0; x < img.width; ++x) {+ const glm::vec3 d = equirectDirection((float(x) + 0.5f) / float(img.width), (float(y) + 0.5f) / float(img.height));+ // Dome: a soft grey that darkens toward the horizon, then a warm dim floor.+ glm::vec3 c = d.y >= 0.0f ? glm::mix(glm::vec3(0.10f, 0.105f, 0.11f), glm::vec3(0.22f, 0.225f, 0.24f), std::sqrt(d.y))+ : glm::mix(glm::vec3(0.12f, 0.11f, 0.10f), glm::vec3(0.05f, 0.045f, 0.04f), std::sqrt(-d.y));+ for (const Box& b : boxes) {+ const float az = glm::radians(b.azimuth), el = glm::radians(b.elevation);+ const glm::vec3 dir(std::cos(el) * std::sin(az), std::sin(el), -std::cos(el) * std::cos(az));+ const float facing = glm::dot(d, dir);+ if (facing <= 0.05f) continue;+ glm::vec3 right = glm::normalize(glm::cross(dir, glm::vec3(0, 1, 0)) + glm::vec3(1e-6f, 0, 0));+ const glm::vec3 up = glm::cross(right, dir);+ const float px = glm::dot(d, right) / facing, py = glm::dot(d, up) / facing;+ const float soft = 0.06f;+ const float wx = 1.0f - glm::smoothstep(b.halfW - soft, b.halfW + soft, std::abs(px));+ const float wy = 1.0f - glm::smoothstep(b.halfH - soft, b.halfH + soft, std::abs(py));+ c += b.color * (wx * wy);+ }+ img.texels[size_t(y) * size_t(img.width) + size_t(x)] = c;+ }+ return img;+}++const std::vector<EnvironmentInfo>& environments() {+ static std::vector<EnvironmentInfo> list;+ static std::once_flag once;+ std::call_once(once, [] {+ list.push_back({"studio", "Studio", "", "Generated by SculptForge"});+ list.push_back({"photo_studio", "Photo Studio", "", "Brown Photostudio 02 by Sergej Majboroda, Poly Haven, CC0"});+ list.push_back({"small_studio", "Small Studio", "", "Studio Small 09 by Sergej Majboroda, Poly Haven, CC0"});+ list.push_back({"outdoor_sky", "Outdoor Sky", "",+ "Kloofendal 48d Partly Cloudy (Pure Sky) by Greg Zaal and Jarod Guest, Poly Haven, CC0"});+ const char* env = std::getenv(kHdriPathVariable);+ if (!env) return;+#ifdef _WIN32+ const char sep = ';';+#else+ const char sep = ':';+#endif+ std::string all = env;+ size_t start = 0;+ while (start <= all.size()) {+ const size_t end = std::min(all.find(sep, start), all.size());+ const std::string dir = all.substr(start, end - start);+ start = end + 1;+ if (dir.empty()) continue;+ std::error_code ec;+ std::vector<std::filesystem::path> files;+ for (auto it = std::filesystem::directory_iterator(fsPath(dir), ec); !ec && it != std::filesystem::directory_iterator();+ it.increment(ec)) {+ std::string ext = it->path().extension().string();+ for (char& c : ext) c = char(std::tolower(static_cast<unsigned char>(c)));+ if (ext == ".hdr" || ext == ".exr") files.push_back(it->path());+ }+ std::sort(files.begin(), files.end());+ for (const auto& f : files) {+ const std::u8string stem = f.stem().u8string(), full = f.u8string();+ const std::string s(stem.begin(), stem.end());+ list.push_back({s, s, std::string(full.begin(), full.end()), "From " + std::string(kHdriPathVariable)});+ }+ }+ });+ return list;+}++int environmentCount() { return int(environments().size()); }++int findEnvironment(const std::string& text) {+ std::string s;+ for (char c : text) s += char(std::tolower(static_cast<unsigned char>(c)));+ const auto& list = environments();+ for (size_t i = 0; i < list.size(); ++i) {+ std::string id = list[i].id, label = list[i].label;+ for (char& c : id) c = char(std::tolower(static_cast<unsigned char>(c)));+ for (char& c : label) c = char(std::tolower(static_cast<unsigned char>(c)));+ if (s == id || s == label) return int(i);+ }+ if (!s.empty() && std::all_of(s.begin(), s.end(), [](char c) { return c >= '0' && c <= '9'; })) {+ const int i = std::atoi(s.c_str());+ if (i >= 0 && i < int(list.size())) return i;+ }+ return -1;+}++bool loadEnvironment(int index, EnvImage& out, std::string& error) {+ const auto& list = environments();+ if (index < 0 || index >= int(list.size())) {+ error = "No such environment";+ return false;+ }+ switch (index) {+ case 0: out = studioEnvironment(512); return true;+ case 1: return decodeHdr(assets::hdriPhotoStudio, size_t(assets::hdriPhotoStudio_size), out, error);+ case 2: return decodeHdr(assets::hdriSmallStudio, size_t(assets::hdriSmallStudio_size), out, error);+ case 3: return decodeHdr(assets::hdriOutdoorSky, size_t(assets::hdriOutdoorSky_size), out, error);+ default: break;+ }+ const std::string& path = list[size_t(index)].path;+ std::string ext = fsPath(path).extension().string();+ for (char& c : ext) c = char(std::tolower(static_cast<unsigned char>(c)));+ if (ext == ".exr") {+ int w = 0, h = 0, ch = 0;+ std::vector<float> px;+ if (!loadExr(path, w, h, ch, px, error)) return false;+ out.width = w;+ out.height = h;+ out.texels.resize(size_t(w) * size_t(h));+ for (size_t i = 0; i < out.texels.size(); ++i)+ for (int c = 0; c < 3; ++c) out.texels[i][c] = std::max(0.0f, px[i * size_t(ch) + size_t(std::min(c, ch - 1))]);+ return true;+ }+ std::ifstream f(fsPath(path), std::ios::binary);+ if (!f) {+ error = "Can't open " + path;+ return false;+ }+ const std::vector<unsigned char> bytes((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());+ if (bytes.size() > (size_t(512) << 20)) {+ error = "The environment file is too large";+ return false;+ }+ return decodeHdr(bytes.data(), bytes.size(), out, error);+}++bool buildEnvironmentLighting(int index, EnvironmentLighting& out, std::string& error) {+ const auto start = std::chrono::steady_clock::now();+ EnvImage env;+ if (!loadEnvironment(index, env, error)) return false;+ if (!env.valid() || env.width < 8 || env.height < 4) {+ error = "The environment image is too small";+ return false;+ }+ out.index = index;+ out.levels = prefilterEnvironment(env);+ // SH from a small copy: the low bands don't need the detail.+ EnvImage small = env;+ while (small.width > 128) small = downsample(small);+ out.sh = irradianceSH9(small);+ out.seconds = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();+ return true;+}++} // namespace sfdiff --git a/src/core/EnvironmentMath.h b/src/core/EnvironmentMath.hindex 305c1fcd..f1e929b0 100644--- a/src/core/EnvironmentMath.h+++ b/src/core/EnvironmentMath.h@@ -1,15 +1,104 @@// SculptForge - open source digital sculpting// SPDX-License-Identifier: GPL-3.0-or-later//-// Environment maths for the PBR viewport (PBR texturing phase 1B; scaffolding only). Will hold the CPU side:-// SH9 irradiance, a split-sum BRDF lookup table, procedural environments and the SCULPTFORGE_HDRI_PATH library.-// The GPU prefilter (render/Environment) and the renderer's PBR shader bit wait for the renderer's upload-// rewrite; until then the viewport shows material channels one at a time (channel_view).+// Environment maths for the PBR viewport (render/Renderer.cpp, RenderSettings::shading = Pbr): the CPU side of+// image-based lighting, kept free of GL so it is tested on its own.+//+// - Environments are equirectangular linear-RGB images (EnvImage, row 0 at the top, +Y up, u = 0.5 looking+// down -Z). The built-in list is "Studio" (generated in code) and three CC0 HDRIs bundled in assets/hdri+// (THIRD_PARTY_NOTICES.md); .hdr / .exr files in the folders of SCULPTFORGE_HDRI_PATH are added after them.+// - Diffuse light is nine spherical-harmonic coefficients (SH9) of the environment, already convolved with the+// cosine lobe and divided by pi: evaluating them at a normal gives the colour a white diffuse surface shows.+// - Specular light uses the split-sum approximation: the environment prefiltered with the GGX lobe at a few+// roughness levels (one image per level, half the size each, uploaded as the mip levels of one texture), and+// a lookup table of the BRDF's scale and bias for F0 per (n.v, roughness).+// - Tone maps (ACES fit, AgX-style, Reinhard) take the exposed linear colour to display range; the shader has+// the same formulas, these are their CPU reference.#pragma once+#include <glm/glm.hpp>++#include <array>+#include <cstdint>+#include <string>+#include <vector>+namespace sf {constexpr int kBrdfLutSize = 64;constexpr const char* kHdriPathVariable = "SCULPTFORGE_HDRI_PATH";+// Prefiltered levels: roughness 0, 0.2, ..., 1 (level k at roughness k / (kEnvLevels - 1)).+constexpr int kEnvLevels = 6;+constexpr int kEnvBaseWidth = 256; // level 0 is 256 x 128++struct EnvImage {+ int width = 0, height = 0;+ std::vector<glm::vec3> texels; // linear RGB, rows top-down+ bool valid() const { return width > 0 && height > 0 && texels.size() == size_t(width) * size_t(height); }+ // Bilinear lookup at equirect coordinates (u wraps, v clamps).+ glm::vec3 sample(float u, float v) const;+ glm::vec3 sample(const glm::vec3& dir) const;+};++// Equirect coordinates <-> unit direction.+glm::vec3 equirectDirection(float u, float v);+glm::vec2 equirectCoords(const glm::vec3& dir);+// Half the size (box filter), at least 1 x 1.+EnvImage downsample(const EnvImage& img);++// --- Diffuse ---+struct SH9 {+ std::array<glm::vec3, 9> c{}; // multiply by the basis below at a normal and add up+ glm::vec3 eval(const glm::vec3& n) const;+};+// Real SH basis, band 0..2, at a unit direction.+std::array<float, 9> shBasis(const glm::vec3& n);+// The environment's irradiance divided by pi, as SH9 (cosine lobe applied).+SH9 irradianceSH9(const EnvImage& env);+// The same by brute force (tests): the cosine-weighted mean radiance over the hemisphere around n.+glm::vec3 irradianceBruteForce(const EnvImage& env, const glm::vec3& n);++// --- Specular ---+// Split-sum BRDF integral for GGX with Smith-Schlick visibility: F0 * x + y is the directional albedo.+glm::vec2 integrateBrdf(float NdotV, float roughness, int samples = 512);+// kBrdfLutSize^2 entries, row = roughness (0..1), column = n.v (0..1), texel centres.+std::vector<glm::vec2> brdfLut(int size = kBrdfLutSize);+// GGX-prefiltered levels of `env` (level 0 = the environment itself at kEnvBaseWidth), kEnvLevels of them.+std::vector<EnvImage> prefilterEnvironment(const EnvImage& env, int samples = 96);++// --- Tone mapping ---+enum class ToneMap : uint8_t { Aces, AgX, Reinhard, None };+constexpr int kToneMapCount = 4;+const char* toneMapId(ToneMap t); // "aces", "agx", "reinhard", "none"+const char* toneMapLabel(ToneMap t); // "ACES", "AgX", "Reinhard", "None"+bool parseToneMap(const std::string& text, ToneMap& out);+// Exposed linear colour -> display-linear 0..1 (the caller sRGB-encodes).+glm::vec3 toneMap(ToneMap t, const glm::vec3& c);++// --- Environments ---+struct EnvironmentInfo {+ std::string id; // "studio", "photo_studio", ... or the file's stem for SCULPTFORGE_HDRI_PATH files+ std::string label; // "Studio", ...+ std::string path; // a file from SCULPTFORGE_HDRI_PATH; empty for built-ins+ std::string credit; // who made it and its licence, for the UI's tooltip+};+// The built-ins, then the files found in SCULPTFORGE_HDRI_PATH (searched once, at the first call).+const std::vector<EnvironmentInfo>& environments();+int environmentCount();+// Accepts the id, the label (any case) or an index.+int findEnvironment(const std::string& text);+// Loads environment `index` (generated, decoded from the embedded file, or read from disk).+bool loadEnvironment(int index, EnvImage& out, std::string& error);+// The generated "Studio": two soft boxes, a rim strip and a warm floor bounce over a dark grey dome.+EnvImage studioEnvironment(int width = 512);++// Everything the renderer uploads for one environment.+struct EnvironmentLighting {+ int index = -1;+ std::vector<EnvImage> levels; // prefiltered, kEnvLevels+ SH9 sh;+ double seconds = 0.0;+};+bool buildEnvironmentLighting(int index, EnvironmentLighting& out, std::string& error);} // namespace sfdiff --git a/src/core/Texture.cpp b/src/core/Texture.cppindex 1f1ce28c..8b6f14cb 100644--- a/src/core/Texture.cpp+++ b/src/core/Texture.cpp@@ -1997,4 +1997,6 @@ void clearTextureLayer(PaintTexture& t, int layer) {compositeTextureAll(t);}+uint64_t meshUvFingerprint(const Mesh& mesh) { return uvFingerprint(mesh); }+} // namespace sfdiff --git a/src/core/Texture.h b/src/core/Texture.hindex 8e7e4c9c..9de11a31 100644--- a/src/core/Texture.h+++ b/src/core/Texture.h@@ -125,6 +125,8 @@ struct PaintTexture {size_t colourBytes() const;};+// A fingerprint of the mesh's UVs (sampled; changes when the layout does), for caches keyed by UV layout.+uint64_t meshUvFingerprint(const Mesh& mesh);// Bytes of the texture-painting caches shared by every texture on the same UVs (UV tile bins, coverage, texel// maps of dense meshes): Preferences > Performance.size_t texturePaintCacheBytes();diff --git a/src/core/TextureChannels.cpp b/src/core/TextureChannels.cppindex 4f5233ee..107c8911 100644--- a/src/core/TextureChannels.cpp+++ b/src/core/TextureChannels.cpp@@ -420,6 +420,57 @@ void channelViewTexels(const PaintTexture& t, Channel c, int x0, int y0, int x1,}, 16, 4);}+uint32_t pbrViewPlanes(const PaintTexture& t) {+ uint32_t planes = 0;+ for (Channel c : {Channel::AO, Channel::Roughness, Channel::Metallic, Channel::Opacity})+ if (channelEnabled(t, c)) planes |= 1u;+ if (channelEnabled(t, Channel::Normal) || channelEnabled(t, Channel::Height)) planes |= 2u;+ if (channelEnabled(t, Channel::Emissive)) planes |= 4u;+ return planes;+}++void pbrViewTexels(const PaintTexture& t, int x0, int y0, int x1, int y1, uint8_t* orm, uint8_t* normal, uint8_t* emissive) {+ const int rw = x1 - x0, rx = x0, ry = y0; // the caller's rectangle+ x0 = std::max(x0, 0), y0 = std::max(y0, 0);+ const int cx1 = std::min(x1, t.width), cy1 = std::min(y1, t.height);+ if (x0 >= cx1 || y0 >= cy1) return;+ const TileImage* img[4] = {};+ const Channel ormCh[4] = {Channel::AO, Channel::Roughness, Channel::Metallic, Channel::Opacity};+ float def[4];+ for (int k = 0; k < 4; ++k) {+ img[k] = channelEnabled(t, ormCh[k]) ? &channelImage(t, ormCh[k]) : nullptr;+ def[k] = channelFormat(ormCh[k]).defaultValue[0];+ }+ const TileImage* em = channelEnabled(t, Channel::Emissive) ? &channelImage(t, Channel::Emissive) : nullptr;+ const bool nrm = normal && (channelEnabled(t, Channel::Normal) || channelEnabled(t, Channel::Height));+ auto u8 = [](float v) { return uint8_t(std::lround(std::clamp(v, 0.0f, 1.0f) * 255.0f)); };+ poolFor(size_t(cy1 - y0), [&](size_t b, size_t e) {+ for (int y = y0 + int(b); y < y0 + int(e); ++y) {+ const size_t row = size_t(y - ry) * size_t(rw);+ for (int x = x0; x < cx1; ++x) {+ const size_t i = (row + size_t(x - rx)) * 4;+ if (orm) {+ for (int k = 0; k < 4; ++k) orm[i + size_t(k)] = u8(img[k] ? img[k]->value(x, y) : def[k]);+ }+ if (normal) {+ glm::vec3 n(0, 0, 1);+ if (nrm) n = effectiveNormalAt(t, x, y);+ normal[i] = u8(n.x * 0.5f + 0.5f);+ normal[i + 1] = u8(n.y * 0.5f + 0.5f);+ normal[i + 2] = u8(n.z * 0.5f + 0.5f);+ normal[i + 3] = 255;+ }+ if (emissive) {+ float v[4] = {0, 0, 0, 0};+ if (em) em->values(x, y, v);+ for (int k = 0; k < 3; ++k) emissive[i + size_t(k)] = u8(v[k]);+ emissive[i + 3] = 255;+ }+ }+ }+ }, 16, 4);+}+void syncChannelView(PaintTexture& t, Channel c) {if (c == Channel::BaseColor || !channelEnabled(t, c)) {if (t.viewChannel != Channel::BaseColor) {diff --git a/src/core/TextureChannels.h b/src/core/TextureChannels.hindex 366de1d8..98286ac5 100644--- a/src/core/TextureChannels.h+++ b/src/core/TextureChannels.h@@ -151,6 +151,16 @@ void channelViewTexels(const PaintTexture& tex, Channel c, int x0, int y0, int x// a change of channel redoes everything and marks the texture dirty.void syncChannelView(PaintTexture& tex, Channel c);+// --- PBR viewport ---+// Which packed planes the PBR viewport needs for this texture: bit 0 = ORM (AO, roughness, metallic, opacity;+// any of them enabled), bit 1 = normal (Normal or Height enabled), bit 2 = emissive.+uint32_t pbrViewPlanes(const PaintTexture& tex);+// The packed RGBA8 texels of [x0, x1) x [y0, y1) (clamped to the texture) for the renderer, each plane into its+// own rectangle-sized buffer (rows bottom-up, (x1 - x0) * 4 bytes per row; null = skip): ORM in linear units,+// the effective tangent-space normal (Normal with Height's normal combined), emissive as stored (sRGB).+void pbrViewTexels(const PaintTexture& tex, int x0, int y0, int x1, int y1, uint8_t* orm, uint8_t* normal,+ uint8_t* emissive);+// --- Memory ---// Bytes of material channel images (bases, composites and layer channels). Walks the tiles: call it only where// the texture is edited (the main thread), never while a dab may run elsewhere; TileImage::liveBytes is thediff --git a/src/render/GL.h b/src/render/GL.hindex 23ec83dc..fc45adb9 100644--- a/src/render/GL.h+++ b/src/render/GL.h@@ -147,6 +147,7 @@ constexpr GLbitfield GL_COLOR_BUFFER_BIT = 0x4000;X(void, UseProgram, GLuint) \X(GLint, GetUniformLocation, GLuint, const GLchar*) \X(void, UniformMatrix4fv, GLint, GLsizei, GLboolean, const GLfloat*) \+ X(void, UniformMatrix3fv, GLint, GLsizei, GLboolean, const GLfloat*) \X(void, Uniform3fv, GLint, GLsizei, const GLfloat*) \X(void, Uniform4fv, GLint, GLsizei, const GLfloat*) \X(void, DrawElements, GLenum, GLsizei, GLenum, const void*) \diff --git a/src/render/Renderer.cpp b/src/render/Renderer.cppindex 083f4c58..36297e79 100644--- a/src/render/Renderer.cpp+++ b/src/render/Renderer.cpp@@ -4,7 +4,9 @@#include "render/MicroRenderer.h"#include "core/Matcap.h"+#include "core/Tangents.h"#include "core/Texture.h"+#include "core/TextureChannels.h"#include "core/ThreadPool.h"#include "core/TierConfig.h"#include "core/UploadOrder.h"@@ -13,6 +15,9 @@#include <algorithm>#include <atomic>+#include <bit>+#include <chrono>+#include <thread>#include <cstdlib>#include <limits>#include <vector>@@ -267,6 +272,12 @@ in FragData {flat int vPrimBase;};uniform sampler2D uPaintTex; // sRGB texture: samples come back linear+#ifdef PBR+uniform sampler2D uOrmTex; // PBR view: AO, roughness, metallic, opacity (linear)+uniform sampler2D uEmissiveTex; // PBR view: emissive (sRGB)+uniform int uHasOrm;+uniform int uHasEmissive;+#endif#ifdef NORMALMAPuniform sampler2D uNormalTex; // baked tangent-space normal map (linear RGB)uniform int uHasPaint; // 1: a painted texture is bound to uPaintTex too@@ -376,6 +387,70 @@ vec3 viewRay() {vec2 ndc = (gl_FragCoord.xy - uViewport.xy) / uViewport.zw * 2.0 - 1.0;return normalize(vec3(-ndc / uProjScale, 1.0));}+#ifdef PBR+// PBR viewport (core/EnvironmentMath.h): Cook-Torrance GGX through the split sum (the environment prefiltered per+// roughness level in the mips of uEnvTex, the BRDF's scale and bias in uBrdfLut), diffuse from the environment's+// SH9 (already cosine-convolved and divided by pi), exposure, then a tone map. Directions are turned from view+// space into the environment's with uViewToEnv (the inverse view rotation and the environment's yaw).+uniform sampler2D uEnvTex;+uniform sampler2D uBrdfLut;+uniform vec3 uSH[9];+uniform mat3 uViewToEnv;+uniform float uEnvLevels; // the roughest level's mip+uniform float uExposure; // stops+uniform int uToneMap; // sf::ToneMap: 0 ACES, 1 AgX, 2 Reinhard, 3 none+uniform vec2 uMaterial; // roughness, metallic of a mesh without a PBR texture+uniform float uPbrCavity; // how much creases darken (the sculpt's curvature), 0 = none+vec3 envSample(vec3 dirView, float lod) {+ vec3 d = uViewToEnv * dirView;+ vec2 uv = vec2(atan(d.x, -d.z) / 6.2831853 + 0.5, acos(clamp(d.y, -1.0, 1.0)) / 3.14159265);+ return textureLod(uEnvTex, uv, lod).rgb;+}+vec3 shIrradiance(vec3 dirView) {+ vec3 d = uViewToEnv * dirView;+ vec3 r = uSH[0] * 0.282095 + uSH[1] * (0.488603 * d.y) + uSH[2] * (0.488603 * d.z) + uSH[3] * (0.488603 * d.x) ++ uSH[4] * (1.092548 * d.x * d.y) + uSH[5] * (1.092548 * d.y * d.z) + uSH[6] * (0.315392 * (3.0 * d.z * d.z - 1.0)) ++ uSH[7] * (1.092548 * d.x * d.z) + uSH[8] * (0.546274 * (d.x * d.x - d.y * d.y));+ return max(r, vec3(0.0));+}+// Keep in sync with sf::toneMap (core/EnvironmentMath.cpp).+vec3 toneMapped(vec3 c) {+ c = max(c, vec3(0.0));+ if (uToneMap == 0) return clamp((c * (2.51 * c + 0.03)) / (c * (2.43 * c + 0.59) + 0.14), 0.0, 1.0);+ if (uToneMap == 1) {+ mat3 inset = mat3(0.842479, 0.0423282, 0.0423756, 0.0784336, 0.878468, 0.0784336, 0.0792237, 0.0791661, 0.879142);+ mat3 outset = mat3(1.19688, -0.0528968, -0.0529716, -0.0980209, 1.15190, -0.0980435, -0.0990297, -0.0989612, 1.15107);+ vec3 x = inset * c;+ x = clamp((log2(max(x, vec3(1e-10))) + 12.47393) / 16.5, 0.0, 1.0);+ const float p = 0.6060606;+ vec3 lo = 0.5 * pow(x / p, vec3(2.2)), hi = 1.0 - 0.5 * pow((1.0 - x) / (1.0 - p), vec3(1.6));+ x = mix(lo, hi, step(vec3(p), x));+ return clamp(pow(max(outset * x, vec3(0.0)), vec3(2.2)), 0.0, 1.0);+ }+ if (uToneMap == 2) {+ float l = dot(c, vec3(0.2126, 0.7152, 0.0722));+ return l > 0.0 ? clamp(c * (l * (1.0 + l / 36.0) / (1.0 + l) / l), 0.0, 1.0) : vec3(0.0);+ }+ return clamp(c, 0.0, 1.0);+}+vec3 pbrShade(vec3 albedo, vec3 n, vec3 v, float rough, float metal, float ao, float curvature, vec3 emissive) {+ rough = clamp(rough, 0.03, 1.0);+ metal = clamp(metal, 0.0, 1.0);+ float NdotV = max(dot(n, v), 1e-4);+ vec3 F0 = mix(vec3(0.04), albedo, metal);+ vec3 F = F0 + (max(vec3(1.0 - rough), F0) - F0) * pow(1.0 - NdotV, 5.0);+ vec2 ab = texture(uBrdfLut, vec2(NdotV, rough)).rg;+ vec3 irr = shIrradiance(n);+ vec3 spec = envSample(reflect(-v, n), rough * uEnvLevels) * (F0 * ab.x + ab.y);+ // Multiple scattering (Fdez-Aguera 2019): rough metals keep the energy single scattering loses.+ float ess = ab.x + ab.y;+ spec += (F0 * ab.x + ab.y) * (1.0 - ess) / max(ess, 1e-3) * F0 * irr;+ vec3 diffuse = (1.0 - F) * (1.0 - metal) * albedo * irr;+ float specOcc = clamp(pow(NdotV + ao, exp2(-16.0 * rough - 1.0)) - 1.0 + ao, 0.0, 1.0); // Lagarde & de Rousiers+ float cavity = 1.0 - uPbrCavity * 0.6 * smoothstep(0.0, 0.16, curvature);+ return ((diffuse * ao + spec * specOcc) * cavity + emissive) * exp2(uExposure);+}+#endifvoid main() {#ifdef MICROmicroFetch();@@ -419,6 +494,25 @@ void main() {if (!gl_FrontFacing && uTwoSided == 1) n = -n;#endifvec3 v = viewRay();+#ifdef PBR+ {+ float rough = uMaterial.x, metal = uMaterial.y, ao = 1.0;+ vec3 emissive = vec3(0.0);+#ifdef TEXTURED+ if (uHasOrm == 1) {+ vec4 orm = texture(uOrmTex, vUv);+ ao = orm.r;+ rough = orm.g;+ metal = orm.b;+ }+ if (uHasEmissive == 1) emissive = texture(uEmissiveTex, vUv).rgb;+#endif+ vec3 c = toneMapped(pbrShade(albedo, n, v, rough, metal, ao, curvature, emissive));+ if (uShowMask == 1) c *= 1.0 - 0.75 * clamp(V_MASK, 0.0, 1.0);+ fragColor = finish(c);+ return;+ }+#endifif (uMatcap == 1) {// View-aligned basis so the material stays put towards the edges of a perspective view.vec3 bx = normalize(vec3(v.z, 0.0, -v.x));@@ -516,6 +610,51 @@ void main() {})";+// The PBR view's environment behind the model: the view ray of each pixel into the prefiltered environment at+// the background's blur, exposed and tone mapped like the shading.+const char* kEnvBgFS = R"(#version 330 core+in vec2 vUv;+out vec4 fragColor;+uniform mat4 uInvProj;+uniform float uLod;+uniform sampler2D uEnvTex;+uniform mat3 uViewToEnv;+uniform float uExposure;+uniform int uToneMap;+vec3 envSample(vec3 dirView, float lod) {+ vec3 d = uViewToEnv * dirView;+ vec2 uv = vec2(atan(d.x, -d.z) / 6.2831853 + 0.5, acos(clamp(d.y, -1.0, 1.0)) / 3.14159265);+ return textureLod(uEnvTex, uv, lod).rgb;+}+// Keep in sync with sf::toneMap (core/EnvironmentMath.cpp).+vec3 toneMapped(vec3 c) {+ c = max(c, vec3(0.0));+ if (uToneMap == 0) return clamp((c * (2.51 * c + 0.03)) / (c * (2.43 * c + 0.59) + 0.14), 0.0, 1.0);+ if (uToneMap == 1) {+ mat3 inset = mat3(0.842479, 0.0423282, 0.0423756, 0.0784336, 0.878468, 0.0784336, 0.0792237, 0.0791661, 0.879142);+ mat3 outset = mat3(1.19688, -0.0528968, -0.0529716, -0.0980209, 1.15190, -0.0980435, -0.0990297, -0.0989612, 1.15107);+ vec3 x = inset * c;+ x = clamp((log2(max(x, vec3(1e-10))) + 12.47393) / 16.5, 0.0, 1.0);+ const float p = 0.6060606;+ vec3 lo = 0.5 * pow(x / p, vec3(2.2)), hi = 1.0 - 0.5 * pow((1.0 - x) / (1.0 - p), vec3(1.6));+ x = mix(lo, hi, step(vec3(p), x));+ return clamp(pow(max(outset * x, vec3(0.0)), vec3(2.2)), 0.0, 1.0);+ }+ if (uToneMap == 2) {+ float l = dot(c, vec3(0.2126, 0.7152, 0.0722));+ return l > 0.0 ? clamp(c * (l * (1.0 + l / 36.0) / (1.0 + l) / l), 0.0, 1.0) : vec3(0.0);+ }+ return clamp(c, 0.0, 1.0);+}+void main() {+ vec4 p = uInvProj * vec4(vUv * 2.0 - 1.0, 1.0, 1.0);+ vec3 dir = abs(p.w) > 1e-6 ? normalize(p.xyz / p.w) : vec3(0.0, 0.0, -1.0);+ vec3 c = toneMapped(envSample(dir, uLod) * exp2(uExposure));+ float noise = fract(sin(dot(gl_FragCoord.xy, vec2(12.9898, 78.233))) * 43758.5453);+ fragColor = vec4(pow(c, vec3(1.0 / 2.2)) + (noise - 0.5) / 255.0, 1.0);+}+)";+// 3D Gaussian splats (EWA projection of each 3D covariance to a screen-space ellipse).const char* kSplatVS = R"(#version 330 corelayout(location = 0) in uint aIndex;@@ -577,6 +716,9 @@ constexpr GLenum kTexture2 = 0x84C2; // GL_TEXTURE2 (matcap)constexpr GLenum kTexture5 = 0x84C5; // kTexture5 (baked normal map)constexpr GLenum kTexture6 = 0x84C6; // kTexture6 (its tangents)constexpr GLenum kRgb = 0x1907, kRgb8 = 0x8051; // GL_RGB, GL_RGB8+// PBR view: ORM 8, emissive 9, environment 10, BRDF table 11.+constexpr GLenum kTexture8 = 0x84C8, kTexture9 = 0x84C9, kTexture10 = 0x84CA, kTexture11 = 0x84CB;+constexpr GLenum kRgb16f = 0x881B, kRg16f = 0x822F, kRg = 0x8227, kRepeat = 0x2901, kMaxLevel = 0x813D;GLuint compile(GLenum type, const char* src, std::string& error, const char* prefix = nullptr) {GLuint s = gl::CreateShader(type);@@ -689,8 +831,8 @@ bool Renderer::init(std::string& error) {const Renderer::MeshProgram* Renderer::meshProgram(int features) {if (features & (kTextured | kNormalMap)) features |= kTextured | kPrim;// (micro levels: no per-vertex paint, per-triangle data or predictive preview)- if (features & kMicro) features &= kMicro | kFlat;- MeshProgram& p = programs_[features & 127];+ if (features & kMicro) features &= kMicro | kFlat | kPbr;+ MeshProgram& p = programs_[features & 255];if (p.id) return &p;std::string prefix = "#version 330 core\n";if (features & kMicro) prefix += "#define MICRO\n";@@ -700,6 +842,7 @@ const Renderer::MeshProgram* Renderer::meshProgram(int features) {if (features & kNormalMap) prefix += "#define NORMALMAP\n";if (features & kFlat) prefix += "#define FLAT\n";if (features & kGhost) prefix += "#define GHOST\n";+ if (features & kPbr) prefix += "#define PBR\n";const std::string vs = prefix + ((features & kMicro) ? microVertexShaderSource() : kMeshVS), fs = prefix + kMeshFS,gs = prefix + kMeshGS;p.id = link(vs.c_str(), fs.c_str(), programError_, nullptr, (features & kTextured) ? gs.c_str() : nullptr);@@ -731,6 +874,15 @@ const Renderer::MeshProgram* Renderer::meshProgram(int features) {p.ghostBound = at("uGhostBound");p.ghostKind = at("uGhostKind");p.ghostFocal = at("uGhostFocal");+ p.hasOrm = at("uHasOrm");+ p.hasEmissive = at("uHasEmissive");+ p.envLevels = at("uEnvLevels");+ p.exposure = at("uExposure");+ p.toneMap = at("uToneMap");+ p.material = at("uMaterial");+ p.pbrCavity = at("uPbrCavity");+ p.viewToEnv = at("uViewToEnv");+ p.sh = at("uSH");// Texture units: groups 1, matcap 2, UVs 3, painted texture 4, normal map 5, tangents 6, cluster table 7.gl::UseProgram(p.id);gl::Uniform1i(at("uGroupIds"), 1);@@ -740,6 +892,10 @@ const Renderer::MeshProgram* Renderer::meshProgram(int features) {gl::Uniform1i(at("uNormalTex"), 5);gl::Uniform1i(at("uTangents"), 6);gl::Uniform1i(at("uClusterPrims"), 7);+ gl::Uniform1i(at("uOrmTex"), 8);+ gl::Uniform1i(at("uEmissiveTex"), 9);+ gl::Uniform1i(at("uEnvTex"), 10);+ gl::Uniform1i(at("uBrdfLut"), 11);gl::UseProgram(0);return &p;}@@ -871,6 +1027,8 @@ void Renderer::freeSlot(MeshSlot& s) {gl::DeleteTextures(1, &s.uvTexture);if (s.paintTexture) gl::DeleteTextures(1, &s.paintTexture);if (s.normalTexture) gl::DeleteTextures(1, &s.normalTexture);+ for (GLuint* t : {&s.ormTexture, &s.matNormalTexture, &s.emissiveTexture})+ if (*t) gl::DeleteTextures(1, t);if (s.tangentData) gl::DeleteBuffers(1, &s.tangentData);if (s.tangentTexture) gl::DeleteTextures(1, &s.tangentTexture);gl::DeleteVertexArrays(1, &s.vao);@@ -894,6 +1052,7 @@ size_t Renderer::gpuBytes() const {total += s.patchBytes;if (s.proxyAllocated) total += (s.order.triangleCount() / 4) * 3 * sizeof(uint16_t);if (s.paintTexture) total += size_t(s.textureW) * size_t(s.textureH) * 4 * 4 / 3; // with mipmaps+ total += size_t(std::popcount(s.pbrPlanes)) * size_t(s.textureW) * size_t(s.textureH) * 4 * 4 / 3;if (s.micro) total += s.micro->gpuBytes();}return total;@@ -907,6 +1066,11 @@ void Renderer::shutdown() {matcapTextures_.clear();matcapHashes_.clear();matcapRevision_ = 0;+ for (GLuint* t : {&envTexture_, &brdfTexture_})+ if (*t) gl::DeleteTextures(1, t);+ envIndex_ = envFailedIndex_ = -1;+ if (envBgProgram_) gl::DeleteProgram(envBgProgram_);+ envBgProgram_ = 0;gl::DeleteVertexArrays(1, &emptyVao_);for (MeshProgram& p : programs_) {if (p.id) gl::DeleteProgram(p.id);@@ -1122,6 +1286,15 @@ int hiddenDiagonal(const Mesh& mesh, uint32_t t) {} // namespace+// The lighting of an environment is built on a worker thread (decode, prefilter, SH9: a few hundred ms); the view+// keeps the environment it has until the new one is ready (the very first one is waited for).+struct Renderer::EnvJob {+ int index = -1;+ std::atomic<bool> done{false};+ bool ok = false;+ EnvironmentLighting light;+};+bool Renderer::outOfMemory(uint32_t id) const {for (const MeshSlot& s : slots_)if (s.id == id) return s.outOfMemory;@@ -1130,6 +1303,7 @@ bool Renderer::outOfMemory(uint32_t id) const {void Renderer::syncScene(const std::vector<SceneMesh>& scene) {syncMatcaps();+ if (envJob_ && envJob_->done) ensureEnvironment(envJob_->index); // (a new environment is ready: upload it)// Free the slots of SubTools that are gone (deleted, merged, a new scene).for (auto it = slots_.begin(); it != slots_.end();) {const bool live = std::any_of(scene.begin(), scene.end(), [&](const SceneMesh& m) { return m.id == it->id; });@@ -1143,7 +1317,7 @@ void Renderer::syncScene(const std::vector<SceneMesh>& scene) {drawList_.clear();for (const SceneMesh& m : scene) {if (!m.visible || !m.mesh) continue; // hidden: keeps its dirty state until shown- sync(slot(m.id), *m.mesh, m.normalMap, m.wireframe && m.active);+ sync(slot(m.id), *m.mesh, m.normalMap, m.wireframe && m.active, m.pbr);drawList_.push_back({m.id, m.active, m.flat});}}@@ -1813,7 +1987,7 @@ void Renderer::drawPatches(const MeshSlot& s, GLenum mode, Part part, GLint primif (primBaseLocation >= 0) gl::Uniform1i(primBaseLocation, -1);}-void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, bool wireframe) {+void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, bool wireframe, bool pbr) {if (mesh.micro && microMatches(mesh)) { // a micro level draws its own way (render/MicroRenderer.h)if (!s.micro) s.micro = std::make_shared<MicroRenderer>();s.outOfMemory = !s.micro->sync(mesh);@@ -1923,7 +2097,7 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo// Small meshes draw in full while the view moves too (their proxy would save little).constexpr size_t kProxyMinTriangles = size_t(1) << 18;if (proxyUsed_ && !s.outOfMemory && s.drawnTriangles >= kProxyMinTriangles) buildProxies(s, mesh);- syncTexture(s, mesh);+ syncTexture(s, mesh, pbr);syncNormalMap(s, mesh, normalMap);// Optional per-vertex attributes: a full upload when they appear (or the@@ -1965,71 +2139,160 @@ void Renderer::sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap, boo}// The painted texture (core/Texture.h): a full upload when it is new or replaced, then only the rectangle-// painted since this slot's last upload (PaintTexture::revision / dirtyFrom), with fresh mipmaps.-void Renderer::syncTexture(MeshSlot& s, Mesh& mesh) {+// painted since this slot's last upload (PaintTexture::revision / dirtyFrom), with fresh mipmaps. In the PBR view+// the material channels go up the same way as packed planes (pbrViewTexels): ORM, normal and emissive.+void Renderer::syncTexture(MeshSlot& s, Mesh& mesh, bool pbr) {PaintTexture* tex = s.hasUvs ? mesh.texture.get() : nullptr;+ GLuint* planeTex[3] = {&s.ormTexture, &s.matNormalTexture, &s.emissiveTexture};+ const uint32_t want = tex && pbr ? pbrViewPlanes(*tex) : 0u;+ for (int b = 0; b < 3; ++b)+ if (!(want & (1u << b)) && *planeTex[b]) {+ gl::DeleteTextures(1, planeTex[b]);+ *planeTex[b] = 0;+ }if (!tex) {if (s.paintTexture) gl::DeleteTextures(1, &s.paintTexture);s.paintTexture = 0;s.textureSerial = 0;+ s.pbrPlanes = 0;return;}const bool fresh = !s.paintTexture || s.textureSerial != tex->serial || s.textureW != tex->width || s.textureH != tex->height;- if (!fresh && s.textureRevision == tex->revision) return;+ // Planes to send whole: new ones, or all of them with a new texture.+ const uint32_t wholePlanes = fresh || s.pbrSerial != tex->serial ? want : want & ~s.pbrPlanes;+ s.pbrPlanes &= want;+ if (!fresh && s.textureRevision == tex->revision && !wholePlanes) return;gl::PixelStorei(GL_UNPACK_ALIGNMENT, 1);+ const bool rect = !fresh && s.textureRevision >= tex->dirtyFrom && tex->dirtyMin.x < tex->dirtyMax.x && tex->dirtyMin.y < tex->dirtyMax.y;+ const bool changed = fresh || s.textureRevision != tex->revision;+ // Calls fn(x0, y0, x1, y1) for the runs of dirty tiles along each tile row of the dirty rectangle.+ auto dirtyRuns = [&](auto&& fn) {+ const int tx0 = tex->dirtyMin.x / kTextureTile, tx1 = (tex->dirtyMax.x - 1) / kTextureTile;+ for (int ty = tex->dirtyMin.y / kTextureTile; ty <= (tex->dirtyMax.y - 1) / kTextureTile; ++ty)+ for (int tx = tx0; tx <= tx1;) {+ if (!tex->tileDirty(tx, ty)) {+ ++tx;+ continue;+ }+ const int start = tx;+ while (tx <= tx1 && tex->tileDirty(tx, ty)) ++tx;+ fn(start * kTextureTile, ty * kTextureTile, std::min(tx * kTextureTile, tex->width),+ std::min((ty + 1) * kTextureTile, tex->height));+ }+ };+ auto setParams = [] {+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GLint(GL_LINEAR_MIPMAP_LINEAR));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GLint(GL_LINEAR));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GLint(GL_CLAMP_TO_EDGE));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GLint(GL_CLAMP_TO_EDGE));+ };if (fresh) {if (!s.paintTexture) gl::GenTextures(1, &s.paintTexture);gl::BindTexture(GL_TEXTURE_2D, s.paintTexture);gl::TexImage2D(GL_TEXTURE_2D, 0, GLint(GL_SRGB8_ALPHA8), tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,tex->shown().data());- gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GLint(GL_LINEAR_MIPMAP_LINEAR));- gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GLint(GL_LINEAR));- gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GLint(GL_CLAMP_TO_EDGE));- gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GLint(GL_CLAMP_TO_EDGE));+ setParams();s.textureSerial = tex->serial;s.textureW = tex->width;s.textureH = tex->height;- } else {+ gl::GenerateMipmap(GL_TEXTURE_2D);+ } else if (changed) {gl::BindTexture(GL_TEXTURE_2D, s.paintTexture);- const bool rect = s.textureRevision >= tex->dirtyFrom && tex->dirtyMin.x < tex->dirtyMax.x && tex->dirtyMin.y < tex->dirtyMax.y;gl::PixelStorei(GL_UNPACK_ROW_LENGTH, tex->width);auto upload = [&](int x0, int y0, int x1, int y1) {x1 = std::min(x1, tex->width), y1 = std::min(y1, tex->height);gl::TexSubImage2D(GL_TEXTURE_2D, 0, x0, y0, x1 - x0, y1 - y0, GL_RGBA, GL_UNSIGNED_BYTE,tex->shown().data() + (size_t(y0) * size_t(tex->width) + size_t(x0)) * 4);};- if (!rect) {- upload(0, 0, tex->width, tex->height);+ if (!rect) upload(0, 0, tex->width, tex->height);+ else dirtyRuns(upload); // only the dirty tiles of the rectangle, as runs along each tile row+ gl::PixelStorei(GL_UNPACK_ROW_LENGTH, 0);+ gl::GenerateMipmap(GL_TEXTURE_2D);+ }+ // PBR planes: packed rectangle by rectangle (a staging buffer the size of what is sent).+ std::vector<uint8_t> staging;+ for (int b = 0; b < 3; ++b) {+ const uint32_t bit = 1u << b;+ if (!(want & bit)) continue;+ const bool whole = (wholePlanes & bit) || !rect;+ if (!whole && !changed) continue;+ GLuint& t = *planeTex[b];+ const GLint format = b == 2 ? GLint(GL_SRGB8_ALPHA8) : GLint(GL_RGBA8);+ auto pack = [&](int x0, int y0, int x1, int y1) {+ staging.assign(size_t(x1 - x0) * size_t(y1 - y0) * 4, 0);+ pbrViewTexels(*tex, x0, y0, x1, y1, b == 0 ? staging.data() : nullptr, b == 1 ? staging.data() : nullptr,+ b == 2 ? staging.data() : nullptr);+ };+ if (whole) {+ if (!t) gl::GenTextures(1, &t);+ gl::BindTexture(GL_TEXTURE_2D, t);+ pack(0, 0, tex->width, tex->height);+ gl::TexImage2D(GL_TEXTURE_2D, 0, format, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, staging.data());+ setParams();} else {- // Only the dirty tiles of the rectangle, as runs along each tile row.- const int tx0 = tex->dirtyMin.x / kTextureTile, tx1 = (tex->dirtyMax.x - 1) / kTextureTile;- for (int ty = tex->dirtyMin.y / kTextureTile; ty <= (tex->dirtyMax.y - 1) / kTextureTile; ++ty)- for (int tx = tx0; tx <= tx1;) {- if (!tex->tileDirty(tx, ty)) {- ++tx;- continue;- }- const int start = tx;- while (tx <= tx1 && tex->tileDirty(tx, ty)) ++tx;- upload(start * kTextureTile, ty * kTextureTile, tx * kTextureTile, (ty + 1) * kTextureTile);- }+ gl::BindTexture(GL_TEXTURE_2D, t);+ // (one texel wider for the normal: the height's normal reads its neighbours)+ const int pad = b == 1 ? 1 : 0;+ dirtyRuns([&](int x0, int y0, int x1, int y1) {+ x0 = std::max(0, x0 - pad), y0 = std::max(0, y0 - pad);+ x1 = std::min(tex->width, x1 + pad), y1 = std::min(tex->height, y1 + pad);+ pack(x0, y0, x1, y1);+ gl::TexSubImage2D(GL_TEXTURE_2D, 0, x0, y0, x1 - x0, y1 - y0, GL_RGBA, GL_UNSIGNED_BYTE, staging.data());+ });}- gl::PixelStorei(GL_UNPACK_ROW_LENGTH, 0);+ gl::GenerateMipmap(GL_TEXTURE_2D);+ s.pbrPlanes |= bit;}- gl::GenerateMipmap(GL_TEXTURE_2D);+ s.pbrSerial = tex->serial;gl::BindTexture(GL_TEXTURE_2D, 0);s.textureRevision = tex->revision;tex->clearDirty();}+// Tangents (MikkTSpace, per mesh corner) into the slot's texture buffer, in draw order (the clusters' order,+// without hidden triangles), like the UVs.+void Renderer::uploadTangents(MeshSlot& s, const std::vector<glm::vec4>& all) {+ if (!s.tangentData) gl::GenBuffers(1, &s.tangentData);+ if (!s.tangentTexture) gl::GenTextures(1, &s.tangentTexture);+ std::vector<glm::vec4> tans(s.drawnOrder.size() * 3);+ for (size_t i = 0; i < s.drawnOrder.size(); ++i)+ if (size_t(s.drawnOrder[i]) * 3 + 3 <= all.size()) std::copy_n(all.data() + size_t(s.drawnOrder[i]) * 3, 3, tans.data() + i * 3);+ gl::BindBuffer(GL_TEXTURE_BUFFER, s.tangentData);+ gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(tans.size() * sizeof(glm::vec4)), tans.data(), GL_STATIC_DRAW);+ gl::BindTexture(GL_TEXTURE_BUFFER, s.tangentTexture);+ gl::TexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, s.tangentData);+ gl::BindTexture(GL_TEXTURE_BUFFER, 0);+ gl::BindBuffer(GL_TEXTURE_BUFFER, 0);+ s.hasTangents = true;+ s.tangentsStale = false;+ s.bytes[7] = s.drawnOrder.size() * 3 * (sizeof(glm::vec2) + sizeof(glm::vec4));+}+// The baked normal map preview: the map uploads when its serial changes, the tangents (in draw order, without-// hidden triangles, like the UVs) whenever the faces were re-uploaded too.+// hidden triangles, like the UVs) whenever the faces were re-uploaded too. Without a preview, the PBR view's+// material normal (syncTexture) gets tangents computed here, once per UV layout.void Renderer::syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView* nm) {if (!nm || !s.hasUvs) {if (s.normalTexture) gl::DeleteTextures(1, &s.normalTexture);s.normalTexture = 0;s.normalSerial = 0;- s.hasTangents = false;+ if (!s.hasUvs || !(s.pbrPlanes & 2u)) {+ s.hasTangents = false;+ s.tangentsFromMaterial = false;+ std::vector<glm::vec4>().swap(s.matTangents);+ s.matTangentKey = 0;+ return;+ }+ const uint64_t key = meshUvFingerprint(mesh) ^ (uint64_t(mesh.vertexCount()) << 32) ^ uint64_t(mesh.triangleCount());+ if (s.matTangentKey != key || s.matTangents.size() != mesh.uvs.size()) {+ s.matTangents = computeTangents(mesh);+ s.matTangentKey = key;+ s.tangentsStale = true;+ }+ if (!s.tangentsFromMaterial) s.tangentsStale = true;+ s.tangentsFromMaterial = true;+ s.normalFlipGreen = false;+ if (s.tangentsStale || !s.hasTangents) uploadTangents(s, s.matTangents);return;}if (!s.normalTexture || s.normalSerial != nm->serial) {@@ -2046,23 +2309,100 @@ void Renderer::syncNormalMap(MeshSlot& s, const Mesh& mesh, const NormalMapView*s.normalSerial = nm->serial;s.tangentsStale = true;}+ if (s.tangentsFromMaterial) s.tangentsStale = true;+ s.tangentsFromMaterial = false;s.normalFlipGreen = nm->flipGreen;if (!s.tangentsStale && s.hasTangents) return;- if (!s.tangentData) gl::GenBuffers(1, &s.tangentData);- if (!s.tangentTexture) gl::GenTextures(1, &s.tangentTexture);- // In draw order (the clusters' order, without hidden triangles), like the UVs.- const std::vector<glm::vec4>& all = *nm->tangents;- std::vector<glm::vec4> tans(s.drawnOrder.size() * 3);- for (size_t i = 0; i < s.drawnOrder.size(); ++i) std::copy_n(all.data() + size_t(s.drawnOrder[i]) * 3, 3, tans.data() + i * 3);- gl::BindBuffer(GL_TEXTURE_BUFFER, s.tangentData);- gl::BufferData(GL_TEXTURE_BUFFER, GLsizeiptr(tans.size() * sizeof(glm::vec4)), tans.data(), GL_STATIC_DRAW);- gl::BindTexture(GL_TEXTURE_BUFFER, s.tangentTexture);- gl::TexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, s.tangentData);- gl::BindTexture(GL_TEXTURE_BUFFER, 0);- gl::BindBuffer(GL_TEXTURE_BUFFER, 0);- s.hasTangents = true;- s.tangentsStale = false;- s.bytes[7] = s.drawnOrder.size() * 3 * (sizeof(glm::vec2) + sizeof(glm::vec4));+ uploadTangents(s, *nm->tangents);+}++// ---------------------------------------------------------------------------+// PBR view: image-based lighting+// ---------------------------------------------------------------------------++bool Renderer::ensureEnvironment(int index) {+ if (index < 0 || index >= environmentCount()) index = 0;+ if (envFailedIndex_ == index) index = 0; // (a file that failed once: the built-in studio instead)+ if (!brdfTexture_) {+ const std::vector<glm::vec2> lut = brdfLut(kBrdfLutSize);+ gl::GenTextures(1, &brdfTexture_);+ gl::BindTexture(GL_TEXTURE_2D, brdfTexture_);+ gl::PixelStorei(GL_UNPACK_ALIGNMENT, 4);+ gl::TexImage2D(GL_TEXTURE_2D, 0, GLint(kRg16f), kBrdfLutSize, kBrdfLutSize, 0, kRg, GL_FLOAT, lut.data());+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GLint(GL_LINEAR));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GLint(GL_LINEAR));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GLint(GL_CLAMP_TO_EDGE));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GLint(GL_CLAMP_TO_EDGE));+ }+ if (envIndex_ == index && envTexture_ && !envJob_) return true;+ if (!envJob_ || envJob_->index != index) {+ auto job = std::make_shared<EnvJob>();+ job->index = index;+ envJob_ = job;+ std::thread([job, wake = envWake_] {+ std::string error;+ job->ok = buildEnvironmentLighting(job->index, job->light, error);+ job->done = true;+ if (wake) wake();+ }).detach();+ }+ if (!envJob_->done) {+ if (envTexture_) return true; // the old one meanwhile+ while (!envJob_->done) std::this_thread::sleep_for(std::chrono::milliseconds(2));+ }+ std::shared_ptr<EnvJob> job = std::move(envJob_);+ if (!job->ok) {+ envFailedIndex_ = job->index;+ if (job->index == 0) return envTexture_ != 0;+ return ensureEnvironment(0);+ }+ const EnvironmentLighting& light = job->light;+ if (!envTexture_) gl::GenTextures(1, &envTexture_);+ gl::BindTexture(GL_TEXTURE_2D, envTexture_);+ gl::PixelStorei(GL_UNPACK_ALIGNMENT, 4);+ for (size_t k = 0; k < light.levels.size(); ++k) {+ const EnvImage& l = light.levels[k];+ gl::TexImage2D(GL_TEXTURE_2D, GLint(k), GLint(kRgb16f), l.width, l.height, 0, kRgb, GL_FLOAT, l.texels.data());+ }+ gl::TexParameteri(GL_TEXTURE_2D, kMaxLevel, GLint(light.levels.size()) - 1);+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GLint(GL_LINEAR_MIPMAP_LINEAR));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GLint(GL_LINEAR));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GLint(kRepeat));+ gl::TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GLint(GL_CLAMP_TO_EDGE));+ gl::BindTexture(GL_TEXTURE_2D, 0);+ envSh_ = light.sh;+ envIndex_ = job->index;+ return true;+}++bool Renderer::environmentPending() const { return envJob_ != nullptr; }++uint32_t Renderer::pbrPlanes(uint32_t id) const {+ for (const auto& s : slots_)+ if (s.id == id) return s.pbrPlanes;+ return 0;+}++void Renderer::setPbrUniforms(const MeshProgram& p, const RenderSettings& settings, bool textured, const MeshSlot& s) {+ if (p.envLevels < 0 && p.material < 0) return; // (not a PBR program)+ gl::ActiveTexture(kTexture10);+ gl::BindTexture(GL_TEXTURE_2D, envTexture_);+ gl::ActiveTexture(kTexture11);+ gl::BindTexture(GL_TEXTURE_2D, brdfTexture_);+ gl::ActiveTexture(kTexture8);+ gl::BindTexture(GL_TEXTURE_2D, textured ? s.ormTexture : 0);+ gl::ActiveTexture(kTexture9);+ gl::BindTexture(GL_TEXTURE_2D, textured ? s.emissiveTexture : 0);+ gl::ActiveTexture(GL_TEXTURE0);+ gl::Uniform1i(p.hasOrm, textured && s.ormTexture ? 1 : 0);+ gl::Uniform1i(p.hasEmissive, textured && s.emissiveTexture ? 1 : 0);+ gl::Uniform1f(p.envLevels, float(kEnvLevels - 1));+ gl::Uniform1f(p.exposure, settings.exposure);+ gl::Uniform1i(p.toneMap, int(settings.toneMap));+ gl::Uniform2f(p.material, std::clamp(settings.pbrRoughness, 0.0f, 1.0f), std::clamp(settings.pbrMetallic, 0.0f, 1.0f));+ gl::Uniform1f(p.pbrCavity, std::clamp(settings.pbrCavity, 0.0f, 1.0f));+ gl::UniformMatrix3fv(p.viewToEnv, 1, GL_FALSE, glm::value_ptr(frameViewToEnv_));+ gl::Uniform3fv(p.sh, 9, glm::value_ptr(envSh_.c[0]));}GLuint Renderer::paintTexture(uint32_t id) const {@@ -2254,13 +2594,46 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, con}} scissorOff{inset};- // Background: charcoal with a radial vignette (or the flat colour cleared above).+ const glm::mat4 view = camera.view(), proj = camera.projection();+ // PBR view: the environment's lighting (built when it first shows), and the rotation from view space into it.+ // (Thumbnails use whatever environment the view has: their settings must not start another build.)+ const bool pbr = settings.pbr() && (thumbnailPass_ ? envTexture_ != 0 : ensureEnvironment(settings.environment));+ if (pbr) {+ const float yaw = glm::radians(settings.environmentRotation);+ const glm::mat3 spin(glm::vec3(std::cos(yaw), 0.0f, -std::sin(yaw)), glm::vec3(0.0f, 1.0f, 0.0f),+ glm::vec3(std::sin(yaw), 0.0f, std::cos(yaw)));+ frameViewToEnv_ = spin * glm::transpose(glm::mat3(view));+ }++ // Background: charcoal with a radial vignette (or the flat colour cleared above); in the PBR view optionally+ // the environment itself, blurred.gl::Disable(GL_DEPTH_TEST);- gl::UseProgram(bgProgram_);gl::BindVertexArray(emptyVao_);- if (!thumbnailPass_ && settings.gradientBackground) gl::DrawArrays(GL_TRIANGLES, 0, 3);+ if (!thumbnailPass_ && pbr && settings.environmentBackground) {+ if (!envBgProgram_) {+ std::string error;+ envBgProgram_ = link(kBgVS, kEnvBgFS, error);+ }+ if (envBgProgram_) {+ gl::UseProgram(envBgProgram_);+ auto at = [&](const char* n) { return gl::GetUniformLocation(envBgProgram_, n); };+ const glm::mat4 invProj = glm::inverse(proj);+ gl::UniformMatrix4fv(at("uInvProj"), 1, GL_FALSE, glm::value_ptr(invProj));+ gl::UniformMatrix3fv(at("uViewToEnv"), 1, GL_FALSE, glm::value_ptr(frameViewToEnv_));+ gl::Uniform1f(at("uLod"), std::clamp(settings.backgroundBlur, 0.0f, 1.0f) * float(kEnvLevels - 1));+ gl::Uniform1f(at("uExposure"), settings.exposure);+ gl::Uniform1i(at("uToneMap"), int(settings.toneMap));+ gl::Uniform1i(at("uEnvTex"), 10);+ gl::ActiveTexture(kTexture10);+ gl::BindTexture(GL_TEXTURE_2D, envTexture_);+ gl::ActiveTexture(GL_TEXTURE0);+ gl::DrawArrays(GL_TRIANGLES, 0, 3);+ }+ } else if (!thumbnailPass_ && settings.gradientBackground) {+ gl::UseProgram(bgProgram_);+ gl::DrawArrays(GL_TRIANGLES, 0, 3);+ }- const glm::mat4 view = camera.view(), proj = camera.projection();frameView_ = view;frameProj_ = proj;frameViewport_ = inset ? glm::vec4(float(viewport.x), float(viewport.y), float(viewport.w), float(viewport.h))@@ -2294,7 +2667,7 @@ void Renderer::draw(const Camera& camera, int targetWidth, int targetHeight, congl::ActiveTexture(unit);gl::BindTexture(GL_TEXTURE_BUFFER, 0);}- for (GLenum unit : {kTexture5, GL_TEXTURE4, kTexture2}) {+ for (GLenum unit : {kTexture11, kTexture10, kTexture9, kTexture8, kTexture5, GL_TEXTURE4, kTexture2}) {gl::ActiveTexture(unit);gl::BindTexture(GL_TEXTURE_2D, 0);}@@ -2432,9 +2805,10 @@ void Renderer::drawClusters(const MeshSlot& s, const std::vector<uint32_t>& clus// One mesh. Mask and polygroup colours are for the SubTool being edited; polypaint shows on all of them.void Renderer::drawMicro(const MeshSlot& s, bool active, bool flat, const RenderSettings& settings) {- const MeshProgram* p = meshProgram(kMicro | (flat ? kFlat : 0));+ const MeshProgram* p = meshProgram(kMicro | (flat ? kFlat : 0) | (settings.pbr() && envTexture_ ? kPbr : 0));if (!p) return;gl::UseProgram(p->id);+ setPbrUniforms(*p, settings, false, s);gl::UniformMatrix4fv(p->view, 1, GL_FALSE, glm::value_ptr(frameView_));gl::UniformMatrix4fv(p->proj, 1, GL_FALSE, glm::value_ptr(frameProj_));gl::Uniform4fv(p->viewport, 1, glm::value_ptr(frameViewport_));@@ -2466,13 +2840,16 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS}// A painted texture shows through the textured program (polypaint still multiplies it).const bool textured = s.hasUvs && s.paintTexture && settings.showPolypaint && settings.showTexture;- // A baked normal map preview bends the shading normal (core/Bake.h).- const bool normalMapped = s.hasUvs && s.normalTexture && s.hasTangents;+ const bool pbr = settings.pbr() && envTexture_;+ // A baked normal map preview bends the shading normal (core/Bake.h), and in the PBR view the texture's+ // Normal / Height channels do (syncTexture's material normal, with tangents from syncNormalMap).+ const GLuint normalTex = s.normalTexture ? s.normalTexture : (pbr && textured ? s.matNormalTexture : 0);+ const bool normalMapped = s.hasUvs && normalTex && s.hasTangents;const int groups = active && settings.showPolygroups ? (s.hasGroups ? 1 : 2) : 0;// (Not through the textured program's geometry shader: those meshes are sculpted without the preview.)const bool ghost = active && s.id == ghostId_ && !ghost_.empty() && !settings.motionProxy && !textured && !normalMapped;const int features = (s.hasColors || s.hasMask ? kPaint : 0) | (groups == 1 ? kPrim : 0) | (textured ? kTextured : 0) |- (normalMapped ? kNormalMap : 0) | (flat ? kFlat : 0);+ (normalMapped ? kNormalMap : 0) | (flat ? kFlat : 0) | (pbr ? kPbr : 0);const MeshProgram* p = meshProgram(features);if (!p) return;const auto bind = [&](const MeshProgram* p) {@@ -2499,12 +2876,13 @@ void Renderer::drawSlot(const MeshSlot& s, bool active, bool flat, const RenderS}if (normalMapped) {gl::ActiveTexture(kTexture5);- gl::BindTexture(GL_TEXTURE_2D, s.normalTexture);+ gl::BindTexture(GL_TEXTURE_2D, normalTex);gl::ActiveTexture(kTexture6);gl::BindTexture(GL_TEXTURE_BUFFER, s.tangentTexture);gl::Uniform1i(p->hasPaint, textured ? 1 : 0);- gl::Uniform1i(p->normalFlipY, s.normalFlipGreen ? 1 : 0);+ gl::Uniform1i(p->normalFlipY, s.normalTexture && s.normalFlipGreen ? 1 : 0);}+ if (pbr) setPbrUniforms(*p, settings, textured, s);gl::ActiveTexture(kTexture7);gl::BindTexture(GL_TEXTURE_BUFFER, s.primTexture);gl::ActiveTexture(GL_TEXTURE1);diff --git a/src/render/Renderer.h b/src/render/Renderer.hindex 2ee0a7c0..a12a317d 100644--- a/src/render/Renderer.h+++ b/src/render/Renderer.h@@ -2,6 +2,7 @@// SPDX-License-Identifier: GPL-3.0-or-later#pragma once+#include "core/EnvironmentMath.h"#include "core/Matcap.h"#include "core/Mesh.h"#include "core/Splats.h"@@ -12,6 +13,7 @@#include "render/Ghost.h"#include <cstdint>+#include <functional>#include <list>#include <memory>#include <string>@@ -42,6 +44,21 @@ struct RenderSettings {// of detail drops to fit; a hardware profile may set it), and the micro edge length to aim for on screen.size_t renderTriangleBudget = 0; // 0 = TierConfig::activeTriangleBudget (core/TierConfig.h)float microTargetPx = 2.0f;+ // Viewport shading: Lit Sphere (the matcap or studio lights above) or PBR, which shades the material channels+ // (core/TextureChannels.h) with image-based lighting from an environment (core/EnvironmentMath.h): GGX+ // specular through a prefiltered environment and a BRDF table, diffuse from its SH9, exposure and a tone map.+ // Meshes without a PBR texture use pbrRoughness / pbrMetallic with their colour.+ enum class Shading : uint8_t { LitSphere, Pbr };+ Shading shading = Shading::LitSphere;+ int environment = 0; // index into environments()+ float environmentRotation = 0.0f; // degrees about the up axis+ float exposure = 0.0f; // stops+ ToneMap toneMap = ToneMap::Aces;+ bool environmentBackground = false; // show the environment behind the model (blurred by backgroundBlur)+ float backgroundBlur = 0.4f; // 0 = sharp .. 1 = the roughest level+ float pbrRoughness = 0.45f, pbrMetallic = 0.0f;+ float pbrCavity = 0.5f; // how much the sculpt's creases darken under PBR (0 = physically plain)+ bool pbr() const { return shading == Shading::Pbr; }};// A baked tangent-space normal map to preview on a mesh (core/Bake.h): RGB8 rows bottom-up, with the@@ -64,6 +81,7 @@ struct SceneMesh {bool wireframe = false; // keep the wireframe's edges on the GPU (built on first need; draw() falls back to// outlining the triangles, quad diagonals included, until they are)bool flat = false; // flat shading: each triangle shows its own face normal (faceted)+ bool pbr = false; // the view shades PBR: upload the texture's material channels and their tangents};// Part of the target the 3D view is drawn into (framebuffer pixels, origin bottom-left); the rest is@@ -111,6 +129,13 @@ public:bool beginCapture(int width, int height, std::string& error);void endCapture(std::vector<uint8_t>& rgba); // top-down RGBA rows+ // PBR view: an environment's lighting is being built (the view shows the previous one meanwhile). `wake` is+ // called from the worker thread when it is ready (the app redraws then).+ bool environmentPending() const;+ // The PBR planes SubTool `id` has on the GPU (pbrViewPlanes bits: 1 ORM, 2 normal, 4 emissive), for tests.+ uint32_t pbrPlanes(uint32_t id) const;+ void setEnvironmentWake(std::function<void()> wake) { envWake_ = std::move(wake); }+// GL texture of the painted texture of SubTool `id` as last synced (0 if none), for UI previews (rows bottom-up).GLuint paintTexture(uint32_t id) const;@@ -220,6 +245,15 @@ private:// Baked normal map preview (NormalMapView): the map, and the tangents per drawn corner (texture buffer).GLuint normalTexture = 0, tangentData = 0, tangentTexture = 0;uint64_t normalSerial = 0;+ // PBR view (RenderSettings::Shading::Pbr): the texture's material channels packed (pbrViewTexels) as+ // ORM, normal and emissive textures; `pbrPlanes` says which are uploaded (pbrViewPlanes bits). A material+ // normal uses tangents computed here (MikkTSpace) unless a baked normal map preview brings its own.+ GLuint ormTexture = 0, matNormalTexture = 0, emissiveTexture = 0;+ uint32_t pbrPlanes = 0;+ uint64_t pbrSerial = 0;+ std::vector<glm::vec4> matTangents; // per mesh corner+ uint64_t matTangentKey = 0; // UV fingerprint + counts they were computed for+ bool tangentsFromMaterial = false; // tangentTexture holds matTangents (not the baked preview's)bool hasTangents = false, tangentsStale = true, normalFlipGreen = false;bool hasColors = false, hasMask = false, hasGroups = false, hasUvs = false;bool fresh = true; // nothing uploaded yet@@ -284,7 +318,7 @@ private:};MeshSlot& slot(uint32_t id); // created on first usestatic void freeSlot(MeshSlot& s);- void sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap = nullptr, bool wireframe = false);+ void sync(MeshSlot& s, Mesh& mesh, const NormalMapView* normalMap = nullptr, bool wireframe = false, bool pbr = false);void uploadVertices(MeshSlot& s, const Mesh& mesh, bool full);// `computed`: the bounds are already current (only the totals are left).void updateBounds(MeshSlot& s, const Mesh& mesh, bool all, bool computed = false);@@ -314,7 +348,19 @@ private:std::vector<uint8_t> staging_; // scratch for sync: gathered vertex datastd::vector<uint8_t> blockChanged_, blockMoved_; // scratch for uploadVertices, per blockstd::vector<std::pair<uint32_t, uint32_t>> movedRuns_; // the dirty runs whose positions moved- void syncTexture(MeshSlot& s, Mesh& mesh);+ void syncTexture(MeshSlot& s, Mesh& mesh, bool pbr);+ void uploadTangents(MeshSlot& s, const std::vector<glm::vec4>& all);+ // Image-based lighting (core/EnvironmentMath.h): the prefiltered environment (RGB16F, a mip per roughness+ // level), the BRDF table (RG16F) and the SH9, built when the environment first shows or changes.+ bool ensureEnvironment(int index);+ void setPbrUniforms(const MeshProgram& p, const RenderSettings& settings, bool textured, const MeshSlot& s);+ GLuint envTexture_ = 0, brdfTexture_ = 0, envBgProgram_ = 0;+ struct EnvJob;+ std::shared_ptr<EnvJob> envJob_;+ std::function<void()> envWake_;+ int envIndex_ = -1, envFailedIndex_ = -1;+ SH9 envSh_;+ glm::mat3 frameViewToEnv_{1.0f};// The clusters of `s` to draw this frame: in full (drawClusters_), and with `proxy` as their motion proxy// where it is built (proxyClusters_).void cull(const MeshSlot& s, const CullView& view, bool proxy = false);@@ -336,16 +382,19 @@ private:glm::mat4 frameView_{1.0f}, frameProj_{1.0f};glm::vec4 frameViewport_{0.0f};CullView frameCull_;- // Mesh shader variants by feature bits (kPaint, kPrim, kTextured, kNormalMap, kFlat), compiled on first use.+ // Mesh shader variants by feature bits (kPaint, kPrim, kTextured, kNormalMap, kFlat, kPbr), compiled on first use.struct MeshProgram {GLuint id = 0;GLint view = -1, proj = -1, gridOrigin = -1, gridStep = -1, viewport = -1, projScale = -1, baseColor = -1, paint = -1,showMask = -1, groups = -1, matcap = -1, matcapCavity = -1, dim = -1, primBase = -1, hasPaint = -1,normalFlipY = -1, twoSided = -1;+ GLint hasOrm = -1, hasEmissive = -1, hasMatNormal = -1, envLevels = -1, exposure = -1, toneMap = -1, material = -1,+ pbrCavity = -1, viewToEnv = -1, sh = -1;GLint ghostGroups = -1, ghostRange = -1, ghostEdge = -1, ghostA = -1, ghostB = -1, ghostC = -1, ghostBound = -1, ghostKind = -1, ghostFocal = -1;};- static constexpr int kPaint = 1, kPrim = 2, kTextured = 4, kNormalMap = 8, kFlat = 16, kMicro = 32, kGhost = 64;- MeshProgram programs_[128];+ static constexpr int kPaint = 1, kPrim = 2, kTextured = 4, kNormalMap = 8, kFlat = 16, kMicro = 32, kGhost = 64,+ kPbr = 128;+ MeshProgram programs_[256];void drawMicro(const MeshSlot& s, bool active, bool flat, const RenderSettings& settings);std::string programError_;const MeshProgram* meshProgram(int features);diff --git a/tests/test_environment.cpp b/tests/test_environment.cppnew file mode 100644index 00000000..2ef20d73--- /dev/null+++ b/tests/test_environment.cpp@@ -0,0 +1,193 @@+// SculptForge - open source digital sculpting+// SPDX-License-Identifier: GPL-3.0-or-later+//+// The PBR viewport's CPU side (core/EnvironmentMath.h): directions, SH9 irradiance against brute force, the split-sum+// BRDF table, GGX prefiltering, tone maps, the bundled environments, and the packed material planes the renderer+// uploads (pbrViewTexels).+#include "TestHarness.h"++#include "core/EnvironmentMath.h"+#include "core/Primitives.h"+#include "core/Texture.h"+#include "core/TextureChannels.h"++#include <cmath>++using namespace sf;++namespace {++EnvImage uniformEnv(glm::vec3 c, int w = 64) {+ EnvImage e;+ e.width = w;+ e.height = w / 2;+ e.texels.assign(size_t(e.width) * size_t(e.height), c);+ return e;+}++// Bright sky above, dark ground below, and a hot spot: something with real low-frequency structure.+EnvImage skyEnv(int w = 128) {+ EnvImage e;+ e.width = w;+ e.height = w / 2;+ e.texels.resize(size_t(e.width) * size_t(e.height));+ for (int y = 0; y < e.height; ++y)+ for (int x = 0; x < e.width; ++x) {+ const glm::vec3 d = equirectDirection((x + 0.5f) / e.width, (y + 0.5f) / e.height);+ glm::vec3 c = d.y > 0 ? glm::vec3(0.4f, 0.6f, 1.0f) * (0.5f + d.y) : glm::vec3(0.15f, 0.12f, 0.1f);+ c += glm::vec3(3.0f) * std::pow(std::max(0.0f, glm::dot(d, glm::normalize(glm::vec3(0.5f, 0.6f, -0.6f)))), 8.0f);+ e.texels[size_t(y) * size_t(e.width) + size_t(x)] = c;+ }+ return e;+}++} // namespace++TEST(environment_directions_round_trip) {+ for (float u : {0.1f, 0.37f, 0.5f, 0.75f, 0.93f})+ for (float v : {0.05f, 0.3f, 0.5f, 0.8f}) {+ const glm::vec3 d = equirectDirection(u, v);+ CHECK(std::abs(glm::length(d) - 1.0f) < 1e-5f);+ const glm::vec2 uv = equirectCoords(d);+ CHECK(std::abs(uv.x - u) < 1e-4f && std::abs(uv.y - v) < 1e-4f);+ }+ // u = 0.5 looks down -Z, the top row is +Y, u = 0.75 is +X.+ CHECK(glm::distance(equirectDirection(0.5f, 0.5f), glm::vec3(0, 0, -1)) < 1e-5f);+ CHECK(equirectDirection(0.3f, 0.0f).y > 0.999f);+ CHECK(glm::distance(equirectDirection(0.75f, 0.5f), glm::vec3(1, 0, 0)) < 1e-5f);+}++TEST(environment_sh9_matches_brute_force_irradiance) {+ // A uniform environment of radiance L lights a white diffuse surface to L from every side.+ const SH9 flat = irradianceSH9(uniformEnv(glm::vec3(0.7f, 1.0f, 2.0f)));+ for (const glm::vec3& n : {glm::vec3(0, 1, 0), glm::vec3(1, 0, 0), glm::normalize(glm::vec3(-1, -2, 0.5f))}) {+ const glm::vec3 e = flat.eval(n);+ CHECK(std::abs(e.x - 0.7f) < 0.01f && std::abs(e.y - 1.0f) < 0.01f && std::abs(e.z - 2.0f) < 0.02f);+ }+ // With structure, nine coefficients stay within a few percent of the exact cosine integral (the known bound+ // for the order-2 approximation of the clamped cosine).+ const EnvImage sky = skyEnv();+ const SH9 sh = irradianceSH9(sky);+ for (const glm::vec3& n : {glm::vec3(0, 1, 0), glm::vec3(0, -1, 0), glm::normalize(glm::vec3(0.5f, 0.6f, -0.6f)),+ glm::normalize(glm::vec3(-1, 0.2f, 0.3f)), glm::vec3(0, 0, 1)}) {+ const glm::vec3 exact = irradianceBruteForce(sky, n), approx = sh.eval(n);+ const float scale = std::max(glm::length(exact), 0.05f);+ CHECK(glm::length(exact - approx) / scale < 0.1f);+ }+}++TEST(environment_brdf_table_is_the_split_sum) {+ // Smooth and head-on, a mirror reflects all of F0: scale ~1, bias ~0.+ const glm::vec2 mirror = integrateBrdf(1.0f, 0.0f);+ CHECK(mirror.x > 0.95f && mirror.y < 0.02f);+ // Grazing on a smooth surface, Fresnel takes over: the bias grows.+ CHECK(integrateBrdf(0.05f, 0.05f).y > integrateBrdf(0.9f, 0.05f).y);+ // Directional albedo never exceeds 1, and roughness only loses energy (single scattering).+ for (float r : {0.1f, 0.4f, 0.7f, 1.0f})+ for (float nv : {0.1f, 0.5f, 1.0f}) {+ const glm::vec2 ab = integrateBrdf(nv, r);+ CHECK(ab.x >= 0.0f && ab.y >= 0.0f && ab.x + ab.y <= 1.001f);+ }+ CHECK(integrateBrdf(0.8f, 1.0f).x + integrateBrdf(0.8f, 1.0f).y < integrateBrdf(0.8f, 0.2f).x + integrateBrdf(0.8f, 0.2f).y);+ const std::vector<glm::vec2> lut = brdfLut(16);+ CHECK(lut.size() == 256);+ // Row 0 is smooth, the last column head-on.+ CHECK(lut[15].x > 0.9f);+}++TEST(environment_prefilter_keeps_energy_and_blurs) {+ // A uniform environment stays uniform at every roughness (the lobe is normalised).+ const std::vector<EnvImage> flat = prefilterEnvironment(uniformEnv(glm::vec3(1.5f), 128), 64);+ CHECK(int(flat.size()) == kEnvLevels);+ for (size_t k = 0; k < flat.size(); ++k) {+ CHECK(flat[k].width == std::max(4, kEnvBaseWidth >> k));+ for (const glm::vec3& t : flat[k].texels) CHECK(std::abs(t.x - 1.5f) < 0.02f);+ }+ // A small bright spot spreads out as roughness grows: the peak drops level by level.+ EnvImage spot = uniformEnv(glm::vec3(0.0f), 256);+ for (int y = 60; y < 68; ++y)+ for (int x = 124; x < 132; ++x) spot.texels[size_t(y) * 256 + size_t(x)] = glm::vec3(100.0f);+ const std::vector<EnvImage> blurred = prefilterEnvironment(spot, 64);+ float lastPeak = 1e30f;+ for (size_t k = 1; k < blurred.size(); ++k) {+ float peak = 0.0f;+ for (const glm::vec3& t : blurred[k].texels) peak = std::max(peak, t.x);+ CHECK(peak < lastPeak);+ lastPeak = peak;+ }+}++TEST(environment_tone_maps_are_monotonic_and_bounded) {+ for (int t = 0; t < kToneMapCount; ++t) {+ float last = -1.0f;+ for (float x = 0.0f; x < 40.0f; x = x * 1.3f + 0.01f) {+ const float y = toneMap(ToneMap(t), glm::vec3(x)).y;+ CHECK(y >= last - 1e-5f && y >= 0.0f && y <= 1.0f);+ last = y;+ }+ ToneMap parsed;+ CHECK(parseToneMap(toneMapId(ToneMap(t)), parsed) && parsed == ToneMap(t));+ }+ // Mid grey lands near the middle of the display range for the filmic curves.+ CHECK(toneMap(ToneMap::Aces, glm::vec3(0.18f)).y > 0.1f && toneMap(ToneMap::Aces, glm::vec3(0.18f)).y < 0.3f);+ CHECK(toneMap(ToneMap::AgX, glm::vec3(0.18f)).y > 0.1f && toneMap(ToneMap::AgX, glm::vec3(0.18f)).y < 0.35f);+}++TEST(environment_builtins_load_and_light) {+ CHECK(environmentCount() >= 4);+ CHECK(findEnvironment("studio") == 0 && findEnvironment("Outdoor Sky") == 3 && findEnvironment("2") == 2);+ CHECK(findEnvironment("nope") == -1);+ for (int i = 0; i < 4; ++i) {+ EnvImage e;+ std::string err;+ CHECK(loadEnvironment(i, e, err));+ CHECK(e.valid() && e.width == 2 * e.height);+ float peak = 0.0f;+ for (const glm::vec3& t : e.texels) peak = std::max(peak, std::max(t.x, std::max(t.y, t.z)));+ CHECK(peak > 1.0f); // high dynamic range: the light sources are brighter than white+ }+ EnvironmentLighting light;+ std::string err;+ for (int i = 3; i >= 0; --i) CHECK(buildEnvironmentLighting(i, light, err));+ CHECK(int(light.levels.size()) == kEnvLevels && light.levels[0].width == kEnvBaseWidth);+ // Lit from above more than from below (the generated studio).+ CHECK(glm::length(light.sh.eval(glm::vec3(0, 1, 0))) > glm::length(light.sh.eval(glm::vec3(0, -1, 0))));+ std::printf(" environment lighting built in %.0f ms\n", light.seconds * 1000.0);+}++TEST(pbr_view_packs_the_material_channels) {+ Mesh m = makePlane(8);+ m.uvs.resize(m.triangleCount() * 3);+ for (size_t t = 0; t < m.triangleCount(); ++t)+ for (int c = 0; c < 3; ++c) {+ const glm::vec3 p = m.positions[m.triangles[t][c]];+ m.uvs[t * 3 + size_t(c)] = glm::vec2(p.x * 0.5f + 0.5f, p.z * 0.5f + 0.5f);+ }+ m.texture = makeTexture(128, 128);+ PaintTexture& t = *m.texture;+ CHECK(pbrViewPlanes(t) == 0u);+ setChannelEnabled(t, Channel::Roughness, true);+ setChannelEnabled(t, Channel::Metallic, true);+ CHECK(pbrViewPlanes(t) == 1u);+ setChannelEnabled(t, Channel::Height, true);+ setChannelEnabled(t, Channel::Emissive, true);+ CHECK(pbrViewPlanes(t) == 7u);+ // Paint a metallic square and a height bump.+ for (int y = 40; y < 60; ++y)+ for (int x = 40; x < 60; ++x) {+ const float one = 1.0f, high = 0.9f;+ t.channels[size_t(Channel::Metallic)].base.setValues(x, y, &one);+ t.channels[size_t(Channel::Height)].base.setValues(x, y, &high);+ }+ std::vector<uint8_t> orm(32 * 32 * 4), nrm(32 * 32 * 4), em(32 * 32 * 4);+ pbrViewTexels(t, 32, 32, 64, 64, orm.data(), nrm.data(), em.data());+ auto at = [&](std::vector<uint8_t>& v, int x, int y, int c) { return int(v[(size_t(y - 32) * 32 + size_t(x - 32)) * 4 + size_t(c)]); };+ CHECK(at(orm, 50, 50, 2) == 255); // metallic inside the square+ CHECK(at(orm, 35, 35, 2) == 0); // and not outside+ CHECK(std::abs(at(orm, 35, 35, 1) - int(std::lround(channelFormat(Channel::Roughness).defaultValue[0] * 255))) <= 1);+ CHECK(at(orm, 35, 35, 0) == 255); // AO off: unoccluded+ CHECK(at(nrm, 50, 50, 2) > 250); // flat on top of the bump+ CHECK(at(nrm, 40, 50, 0) < 120); // its left edge leans to -X+ CHECK(at(nrm, 59, 50, 0) > 136); // its right edge to +X+ CHECK(at(em, 50, 50, 0) == 0 && at(em, 50, 50, 3) == 255);+}diff --git a/tests/ui/pbr_viewport.sfui b/tests/ui/pbr_viewport.sfuinew file mode 100644index 00000000..73a1c09a--- /dev/null+++ b/tests/ui/pbr_viewport.sfui@@ -0,0 +1,73 @@+# PBR viewport (Material > Viewport Shading): Lit Sphere | PBR, the environment (built-in Studio and three bundled+# CC0 HDRIs), exposure, tone map and the environment behind the model. The texture's material channels are uploaded+# as ORM, normal and emissive planes; a painted height shows through tangent-space normals. Settings are saved with+# the project. Files are written to the working directory (the build folder).+run new_sphere subdivisions=5+run uv_auto resolution=1024+run texture_create size=1024+run texture_channels enable=roughness,metallic,height+run paint_material color=d4af37 roughness=0.2 metallic=1 height=0.5+key p+stroke 0.15 0.5 0.85 0.5+run paint_material color=8a2a1a roughness=0.85 metallic=0 height=0.95 channels=base_color,roughness,metallic,height+stroke 0.5 0.15 0.5 0.85+key escape+expect_value view_pbr == 0+expect_value pbr_planes == 0++# The Material palette: Lit Sphere | PBR.+click "Material##palette.material"+expect "Lit Sphere##view.shading_lit_sphere"+click "PBR##view.shading_pbr"+expect_status "Viewport shading: PBR"+expect_value view_pbr == 1+wait_value environment_pending == 0+wait 0.3+# ORM (roughness, metallic) and the normal from Height are on the GPU; no emissive channel.+expect_value pbr_planes == 3+expect "Polypaint & Materials/##environment"+shot material-palette-pbr+key escape++run viewport_shading environment=photo_studio exposure=0.3+wait_value environment_pending == 0+expect_value environment == 1+camera 25 10+wait 0.3+shot pbr-photo-studio++run viewport_shading environment=outdoor_sky background=true blur=0.3 tone_map=agx+wait_value environment_pending == 0+wait 0.3+expect_status "background on"+shot pbr-outdoor-background++# An emissive channel adds the third plane.+run texture_channels enable=emissive+wait 0.2+expect_value pbr_planes == 7+run texture_channels disable=emissive+wait 0.2+expect_value pbr_planes == 3++# Unknown values are refused.+run_fails viewport_shading environment=nowhere+run_fails viewport_shading tone_map=sepia+run_fails viewport_shading mode=raytraced++# Saved with the project; Lit Sphere frees the planes.+run save_project path=sf_ui_pbr_viewport.sfproj+run viewport_shading mode=lit_sphere+expect_value view_pbr == 0+wait 0.2+expect_value pbr_planes == 0+run open path=sf_ui_pbr_viewport.sfproj+expect_value view_pbr == 1+expect_value environment == 3++# Ctrl-hover help.+click "Material##palette.material"+hold ctrl+hover "PBR##view.shading_pbr"+expect_help "environment"+hold none