Add AppearanceProfile (40 material presets) + appearance_profile(); LightingPreset + LightingConfig + lighting_preset(); kelvin_to_rgb, hex_to_rgb, color_palette. test_appearance 5/5.
416 lines
17 KiB
C++
416 lines
17 KiB
C++
#pragma once
|
||
/**
|
||
* @file appearance.h
|
||
* @brief PBR material, texture mapping and environment map for rendering
|
||
*
|
||
* VDE-409: pbr_material + texture_mapping + environment_map
|
||
*
|
||
* @ingroup brep
|
||
*/
|
||
#include "vde/core/point.h"
|
||
#include <string>
|
||
#include <vector>
|
||
#include <memory>
|
||
#include <array>
|
||
#include <cmath>
|
||
#include <cctype>
|
||
#include <algorithm>
|
||
|
||
namespace vde::brep {
|
||
|
||
/// PBR (Physically-Based Rendering) material
|
||
struct PbrMaterial {
|
||
std::string name;
|
||
core::Vector3D base_color{0.8, 0.8, 0.8}; ///< RGB 0-1
|
||
double metallic = 0.0;
|
||
double roughness = 0.5;
|
||
double specular = 0.5;
|
||
double specular_tint = 0.0;
|
||
double sheen = 0.0;
|
||
double sheen_tint = 0.5;
|
||
double clearcoat = 0.0;
|
||
double clearcoat_roughness = 0.03;
|
||
double anisotropy = 0.0;
|
||
double subsurface = 0.0;
|
||
double ior = 1.45; ///< Index of refraction
|
||
double transmission = 0.0;
|
||
|
||
// Texture paths
|
||
std::string base_color_texture;
|
||
std::string normal_texture;
|
||
std::string metallic_roughness_texture;
|
||
std::string emissive_texture;
|
||
std::string ao_texture;
|
||
std::string displacement_texture;
|
||
|
||
core::Vector3D emissive{0, 0, 0};
|
||
bool double_sided = false;
|
||
bool alpha_blend = false;
|
||
};
|
||
|
||
/// Texture mapping
|
||
struct TextureMapping {
|
||
enum class MappingType { UV, Planar, Cylindrical, Spherical, Triplanar, Box };
|
||
|
||
MappingType type = MappingType::UV;
|
||
core::Vector3D offset{0, 0, 0};
|
||
core::Vector3D scale{1, 1, 1};
|
||
double rotation_deg = 0.0;
|
||
bool repeat_u = true;
|
||
bool repeat_v = true;
|
||
bool mirror_u = false;
|
||
bool mirror_v = false;
|
||
|
||
/// UV channel index
|
||
int uv_channel = 0;
|
||
};
|
||
|
||
/// Environment map (IBL - Image-Based Lighting)
|
||
struct EnvironmentMap {
|
||
std::string hdri_path;
|
||
double intensity = 1.0;
|
||
double rotation_deg = 0.0;
|
||
core::Vector3D tint{1, 1, 1};
|
||
bool visible_in_background = true;
|
||
bool affect_lighting = true;
|
||
bool affect_reflections = true;
|
||
|
||
// Generated from HDRI
|
||
std::string diffuse_irradiance; ///< Pre-filtered irradiance map
|
||
std::string specular_radiance; ///< Pre-filtered specular map
|
||
std::string brdf_lut; ///< BRDF integration LUT
|
||
};
|
||
|
||
/// Create a default PBR material
|
||
[[nodiscard]] inline PbrMaterial create_default_pbr() {
|
||
PbrMaterial mat;
|
||
mat.name = "Default PBR";
|
||
return mat;
|
||
}
|
||
|
||
/// Create a metallic PBR material
|
||
[[nodiscard]] inline PbrMaterial create_metal_pbr(const std::string& name,
|
||
double r, double g, double b, double roughness = 0.3) {
|
||
PbrMaterial mat;
|
||
mat.name = name;
|
||
mat.base_color = core::Vector3D(r, g, b);
|
||
mat.metallic = 1.0;
|
||
mat.roughness = roughness;
|
||
return mat;
|
||
}
|
||
|
||
/// Create a plastic PBR material
|
||
[[nodiscard]] inline PbrMaterial create_plastic_pbr(const std::string& name,
|
||
double r, double g, double b, double roughness = 0.4) {
|
||
PbrMaterial mat;
|
||
mat.name = name;
|
||
mat.base_color = core::Vector3D(r, g, b);
|
||
mat.metallic = 0.0;
|
||
mat.roughness = roughness;
|
||
mat.specular = 0.5;
|
||
return mat;
|
||
}
|
||
|
||
/// Apply texture mapping to a material
|
||
[[nodiscard]] inline PbrMaterial texture_mapping(PbrMaterial mat,
|
||
const std::string& base_color_tex,
|
||
const std::string& normal_tex = "",
|
||
const std::string& metallic_roughness_tex = "") {
|
||
mat.base_color_texture = base_color_tex;
|
||
mat.normal_texture = normal_tex;
|
||
mat.metallic_roughness_texture = metallic_roughness_tex;
|
||
return mat;
|
||
}
|
||
|
||
/// Create an environment map from an HDRI file
|
||
[[nodiscard]] inline EnvironmentMap environment_map(const std::string& hdri_path,
|
||
double intensity = 1.0) {
|
||
EnvironmentMap env;
|
||
env.hdri_path = hdri_path;
|
||
env.intensity = intensity;
|
||
return env;
|
||
}
|
||
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
// VDE-409 §1/§2/§4: material presets + lighting presets + color utilities
|
||
// ═══════════════════════════════════════════════════════════════════════════
|
||
|
||
/// 材质预设档(金属/塑料/玻璃/木材/石材/复合材料,40 种)
|
||
enum class AppearanceProfile {
|
||
// ── metals ──
|
||
PolishedSteel, BrushedSteel, StainlessSteel, Aluminum, AnodizedAluminum,
|
||
Copper, Brass, Bronze, Chrome, Gold, Silver, Titanium, CastIron,
|
||
// ── plastics ──
|
||
AbsPlastic, AbsBlack, AbsWhite, Polycarbonate, Nylon, Acrylic, Rubber, Silicone,
|
||
// ── glass ──
|
||
ClearGlass, FrostedGlass, TintedGlass, Diamond,
|
||
// ── wood ──
|
||
Oak, Walnut, Pine, Mahogany, Birch, Cherry,
|
||
// ── stone ──
|
||
Granite, Marble, Concrete, Slate, Sandstone,
|
||
// ── composites ──
|
||
CarbonFiber, Fiberglass, Kevlar, Graphite
|
||
};
|
||
|
||
/// 光照预设档
|
||
enum class LightingPreset {
|
||
Studio, ///< 影棚三点柔光
|
||
ThreePoint, ///< 经典三点布光
|
||
Outdoor, ///< 户外日光
|
||
Product, ///< 产品展示
|
||
Jewelry, ///< 珠宝高亮
|
||
Automotive, ///< 汽车条光
|
||
Night ///< 夜景弱光
|
||
};
|
||
|
||
/// 光照配置
|
||
struct LightingConfig {
|
||
struct Light {
|
||
core::Vector3D dir{0, 0, -1}; ///< 方向(平行光)
|
||
core::Vector3D pos{0, 0, 0}; ///< 位置(点光)
|
||
core::Vector3D color{1, 1, 1}; ///< 颜色
|
||
double intensity = 1.0; ///< 强度
|
||
bool directional = true; ///< 是否平行光
|
||
};
|
||
std::vector<Light> lights;
|
||
core::Vector3D ambient{0.05, 0.05, 0.05};
|
||
double ambient_intensity = 1.0;
|
||
};
|
||
|
||
namespace detail {
|
||
/// HSV → RGB(h∈[0,1) 自动取小数部分),返回分量 0..1
|
||
[[nodiscard]] inline core::Vector3D hsv_to_rgb(double h, double s, double v) {
|
||
h = h - std::floor(h);
|
||
const double c = v * s;
|
||
const double x = c * (1.0 - std::fabs(std::fmod(h * 6.0, 2.0) - 1.0));
|
||
const double m = v - c;
|
||
double r = 0, g = 0, b = 0;
|
||
switch (static_cast<int>(h * 6.0)) {
|
||
case 0: r = c; g = x; b = 0; break;
|
||
case 1: r = x; g = c; b = 0; break;
|
||
case 2: r = 0; g = c; b = x; break;
|
||
case 3: r = 0; g = x; b = c; break;
|
||
case 4: r = x; g = 0; b = c; break;
|
||
default: r = c; g = 0; b = x; break;
|
||
}
|
||
return core::Vector3D(r + m, g + m, b + m);
|
||
}
|
||
} // namespace detail
|
||
|
||
/// 按预设档生成 PBR 材质
|
||
[[nodiscard]] inline PbrMaterial appearance_profile(AppearanceProfile profile) {
|
||
auto metal = [](const char* n, double r, double g, double b, double rough) {
|
||
PbrMaterial m = create_metal_pbr(n, r, g, b, rough);
|
||
return m;
|
||
};
|
||
auto plastic = [](const char* n, double r, double g, double b, double rough) {
|
||
return create_plastic_pbr(n, r, g, b, rough);
|
||
};
|
||
switch (profile) {
|
||
case AppearanceProfile::PolishedSteel: return metal("Polished Steel", 0.56, 0.57, 0.58, 0.12);
|
||
case AppearanceProfile::BrushedSteel: return metal("Brushed Steel", 0.55, 0.56, 0.57, 0.35);
|
||
case AppearanceProfile::StainlessSteel: return metal("Stainless Steel", 0.62, 0.63, 0.64, 0.22);
|
||
case AppearanceProfile::Aluminum: return metal("Aluminum", 0.77, 0.78, 0.79, 0.28);
|
||
case AppearanceProfile::AnodizedAluminum: return metal("Anodized Aluminum", 0.70, 0.71, 0.73, 0.30);
|
||
case AppearanceProfile::Copper: return metal("Copper", 0.95, 0.64, 0.54, 0.25);
|
||
case AppearanceProfile::Brass: return metal("Brass", 0.89, 0.79, 0.36, 0.28);
|
||
case AppearanceProfile::Bronze: return metal("Bronze", 0.71, 0.43, 0.24, 0.32);
|
||
case AppearanceProfile::Chrome: return metal("Chrome", 0.82, 0.83, 0.85, 0.06);
|
||
case AppearanceProfile::Gold: return metal("Gold", 1.00, 0.77, 0.34, 0.15);
|
||
case AppearanceProfile::Silver: return metal("Silver", 0.97, 0.96, 0.92, 0.10);
|
||
case AppearanceProfile::Titanium: return metal("Titanium", 0.53, 0.51, 0.49, 0.35);
|
||
case AppearanceProfile::CastIron: return metal("Cast Iron", 0.28, 0.28, 0.29, 0.55);
|
||
case AppearanceProfile::AbsPlastic: return plastic("ABS Plastic", 0.85, 0.85, 0.85, 0.45);
|
||
case AppearanceProfile::AbsBlack: return plastic("ABS Black", 0.05, 0.05, 0.05, 0.40);
|
||
case AppearanceProfile::AbsWhite: return plastic("ABS White", 0.93, 0.93, 0.93, 0.40);
|
||
case AppearanceProfile::Polycarbonate: return plastic("Polycarbonate", 0.80, 0.84, 0.88, 0.25);
|
||
case AppearanceProfile::Nylon: return plastic("Nylon", 0.90, 0.88, 0.82, 0.55);
|
||
case AppearanceProfile::Acrylic: return plastic("Acrylic", 0.92, 0.94, 0.96, 0.10);
|
||
case AppearanceProfile::Rubber: {
|
||
PbrMaterial m = plastic("Rubber", 0.10, 0.10, 0.10, 0.90);
|
||
m.specular = 0.15;
|
||
return m;
|
||
}
|
||
case AppearanceProfile::Silicone: {
|
||
PbrMaterial m = plastic("Silicone", 0.85, 0.83, 0.80, 0.70);
|
||
m.subsurface = 0.4;
|
||
return m;
|
||
}
|
||
case AppearanceProfile::ClearGlass: {
|
||
PbrMaterial m;
|
||
m.name = "Clear Glass";
|
||
m.base_color = core::Vector3D(0.92, 0.95, 0.96);
|
||
m.metallic = 0.0; m.roughness = 0.02;
|
||
m.ior = 1.52; m.transmission = 0.98;
|
||
m.alpha_blend = true;
|
||
return m;
|
||
}
|
||
case AppearanceProfile::FrostedGlass: {
|
||
PbrMaterial m;
|
||
m.name = "Frosted Glass";
|
||
m.base_color = core::Vector3D(0.90, 0.93, 0.94);
|
||
m.metallic = 0.0; m.roughness = 0.55;
|
||
m.ior = 1.50; m.transmission = 0.85;
|
||
m.alpha_blend = true;
|
||
return m;
|
||
}
|
||
case AppearanceProfile::TintedGlass: {
|
||
PbrMaterial m;
|
||
m.name = "Tinted Glass";
|
||
m.base_color = core::Vector3D(0.35, 0.42, 0.50);
|
||
m.metallic = 0.0; m.roughness = 0.03;
|
||
m.ior = 1.52; m.transmission = 0.80;
|
||
m.alpha_blend = true;
|
||
return m;
|
||
}
|
||
case AppearanceProfile::Diamond: {
|
||
PbrMaterial m;
|
||
m.name = "Diamond";
|
||
m.base_color = core::Vector3D(0.98, 0.99, 1.00);
|
||
m.metallic = 0.0; m.roughness = 0.01;
|
||
m.ior = 2.42; m.transmission = 0.99;
|
||
m.alpha_blend = true;
|
||
return m;
|
||
}
|
||
case AppearanceProfile::Oak: return plastic("Oak", 0.62, 0.45, 0.27, 0.75);
|
||
case AppearanceProfile::Walnut: return plastic("Walnut", 0.42, 0.28, 0.18, 0.72);
|
||
case AppearanceProfile::Pine: return plastic("Pine", 0.85, 0.70, 0.45, 0.78);
|
||
case AppearanceProfile::Mahogany: return plastic("Mahogany", 0.45, 0.20, 0.14, 0.70);
|
||
case AppearanceProfile::Birch: return plastic("Birch", 0.88, 0.78, 0.60, 0.76);
|
||
case AppearanceProfile::Cherry: return plastic("Cherry", 0.60, 0.30, 0.22, 0.70);
|
||
case AppearanceProfile::Granite: return plastic("Granite", 0.50, 0.49, 0.47, 0.85);
|
||
case AppearanceProfile::Marble: return plastic("Marble", 0.90, 0.90, 0.88, 0.40);
|
||
case AppearanceProfile::Concrete: return plastic("Concrete", 0.60, 0.60, 0.58, 0.90);
|
||
case AppearanceProfile::Slate: return plastic("Slate", 0.35, 0.37, 0.38, 0.82);
|
||
case AppearanceProfile::Sandstone:return plastic("Sandstone", 0.76, 0.68, 0.50, 0.88);
|
||
case AppearanceProfile::CarbonFiber: {
|
||
PbrMaterial m = metal("Carbon Fiber", 0.08, 0.08, 0.09, 0.30);
|
||
m.anisotropy = 0.8;
|
||
return m;
|
||
}
|
||
case AppearanceProfile::Fiberglass: return plastic("Fiberglass", 0.85, 0.87, 0.88, 0.35);
|
||
case AppearanceProfile::Kevlar: return plastic("Kevlar", 0.85, 0.75, 0.15, 0.60);
|
||
case AppearanceProfile::Graphite: return metal("Graphite", 0.20, 0.21, 0.22, 0.50);
|
||
}
|
||
return create_default_pbr();
|
||
}
|
||
|
||
/// 按预设档生成光照配置
|
||
[[nodiscard]] inline LightingConfig lighting_preset(LightingPreset preset) {
|
||
LightingConfig cfg;
|
||
auto add = [&cfg](core::Vector3D dir, core::Vector3D pos, core::Vector3D color,
|
||
double intensity, bool directional) {
|
||
LightingConfig::Light l;
|
||
l.dir = dir; l.pos = pos; l.color = color;
|
||
l.intensity = intensity; l.directional = directional;
|
||
cfg.lights.push_back(l);
|
||
};
|
||
const core::Vector3D white(1.0, 1.0, 1.0);
|
||
switch (preset) {
|
||
case LightingPreset::Studio:
|
||
add(core::Vector3D(-0.4, -0.6, -0.7), core::Vector3D(-3, 4, 4), white, 1.6, false);
|
||
add(core::Vector3D(0.5, -0.4, -0.6), core::Vector3D(3, 3, 4), white, 1.1, false);
|
||
cfg.ambient = core::Vector3D(0.16, 0.16, 0.17);
|
||
cfg.ambient_intensity = 1.0;
|
||
break;
|
||
case LightingPreset::ThreePoint:
|
||
add(core::Vector3D(-0.3, -0.6, -0.7), core::Vector3D(), white, 1.5, true); // key
|
||
add(core::Vector3D(0.6, -0.3, -0.4), core::Vector3D(), white, 0.6, true); // fill
|
||
add(core::Vector3D(0.2, 0.7, 0.6), core::Vector3D(), white, 0.9, true); // rim
|
||
cfg.ambient = core::Vector3D(0.08, 0.08, 0.09);
|
||
break;
|
||
case LightingPreset::Outdoor:
|
||
add(core::Vector3D(0.3, -0.8, -0.5), core::Vector3D(), core::Vector3D(1.0, 0.96, 0.88), 3.0, true);
|
||
cfg.ambient = core::Vector3D(0.28, 0.32, 0.40);
|
||
cfg.ambient_intensity = 1.0;
|
||
break;
|
||
case LightingPreset::Product:
|
||
add(core::Vector3D(-0.5, -0.5, -0.7), core::Vector3D(-2, 3, 3), white, 1.4, false);
|
||
add(core::Vector3D(0.5, -0.5, -0.7), core::Vector3D(2, 3, 3), white, 1.4, false);
|
||
cfg.ambient = core::Vector3D(0.10, 0.10, 0.10);
|
||
break;
|
||
case LightingPreset::Jewelry:
|
||
add(core::Vector3D(-0.5, -0.6, -0.6), core::Vector3D(-2, 3, 3), white, 2.2, false);
|
||
add(core::Vector3D(0.5, -0.6, -0.6), core::Vector3D(2, 3, 3), white, 2.2, false);
|
||
add(core::Vector3D(0.0, 0.9, 0.4), core::Vector3D(0, 4, -3), white, 1.5, false);
|
||
cfg.ambient = core::Vector3D(0.05, 0.05, 0.06);
|
||
break;
|
||
case LightingPreset::Automotive:
|
||
add(core::Vector3D(-0.2, -0.9, -0.3), core::Vector3D(-2, 4, 2), white, 2.0, false);
|
||
add(core::Vector3D(0.2, -0.9, -0.3), core::Vector3D(2, 4, 2), white, 2.0, false);
|
||
cfg.ambient = core::Vector3D(0.12, 0.12, 0.13);
|
||
break;
|
||
case LightingPreset::Night:
|
||
add(core::Vector3D(0.4, -0.5, -0.7), core::Vector3D(), core::Vector3D(0.7, 0.8, 1.0), 0.35, true);
|
||
cfg.ambient = core::Vector3D(0.02, 0.02, 0.04);
|
||
cfg.ambient_intensity = 0.3;
|
||
break;
|
||
}
|
||
return cfg;
|
||
}
|
||
|
||
/// 色温(开尔文,1000~40000 clamp)→ 归一化 RGB(Tanner Helland 近似)
|
||
[[nodiscard]] inline core::Vector3D kelvin_to_rgb(double kelvin) {
|
||
const double t = std::clamp(kelvin, 1000.0, 40000.0) / 100.0;
|
||
double r, g, b;
|
||
if (t <= 66.0) {
|
||
r = 255.0;
|
||
} else {
|
||
r = 329.698727446 * std::pow(t - 60.0, -0.1332047592);
|
||
}
|
||
if (t <= 66.0) {
|
||
g = 99.4708025861 * std::log(t) - 161.1195681661;
|
||
} else {
|
||
g = 288.1221695283 * std::pow(t - 60.0, -0.0755148492);
|
||
}
|
||
if (t >= 66.0) {
|
||
b = 255.0;
|
||
} else if (t <= 19.0) {
|
||
b = 0.0;
|
||
} else {
|
||
b = 138.5177312231 * std::log(t - 10.0) - 305.0447927307;
|
||
}
|
||
auto norm = [](double c) { return std::clamp(c, 0.0, 255.0) / 255.0; };
|
||
return core::Vector3D(norm(r), norm(g), norm(b));
|
||
}
|
||
|
||
/// 十六进制颜色("#RRGGBB"/"RRGGBB"/"#RGB")→ 归一化 RGB;非法输入返回黑
|
||
[[nodiscard]] inline core::Vector3D hex_to_rgb(const std::string& hex) {
|
||
std::string s;
|
||
for (char c : hex) {
|
||
if (c == '#') continue;
|
||
s.push_back(static_cast<char>(std::toupper(static_cast<unsigned char>(c))));
|
||
}
|
||
auto hexval = [](char c) -> int {
|
||
if (c >= '0' && c <= '9') return c - '0';
|
||
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
|
||
return -1;
|
||
};
|
||
if (s.size() == 3) {
|
||
int v[3];
|
||
for (int i = 0; i < 3; ++i) { v[i] = hexval(s[i]); if (v[i] < 0) return core::Vector3D(0, 0, 0); }
|
||
return core::Vector3D(v[0] * 17 / 255.0, v[1] * 17 / 255.0, v[2] * 17 / 255.0);
|
||
}
|
||
if (s.size() == 6) {
|
||
int v[6];
|
||
for (int i = 0; i < 6; ++i) { v[i] = hexval(s[i]); if (v[i] < 0) return core::Vector3D(0, 0, 0); }
|
||
auto byte = [&](int i) { return (v[i] * 16 + v[i + 1]) / 255.0; };
|
||
return core::Vector3D(byte(0), byte(2), byte(4));
|
||
}
|
||
return core::Vector3D(0, 0, 0);
|
||
}
|
||
|
||
/// 生成 count 个等亮度/饱和度的黄金角调色板(分量 0..1)
|
||
[[nodiscard]] inline std::vector<core::Vector3D> color_palette(int count) {
|
||
std::vector<core::Vector3D> out;
|
||
if (count <= 0) return out;
|
||
out.reserve(static_cast<size_t>(count));
|
||
constexpr double kGolden = 0.6180339887498949;
|
||
for (int i = 0; i < count; ++i) {
|
||
out.push_back(detail::hsv_to_rgb(i * kGolden, 0.65, 0.9));
|
||
}
|
||
return out;
|
||
}
|
||
|
||
} // namespace vde::brep
|