This translation is community contributed and may not be up to date. We only maintain the English version of the documentation. Read this manual in English
Renderowanie oparte na zasadach fizycznych (Physically Based Rendering, PBR) to podejście do cieniowania, które modeluje interakcję światła z powierzchniami na podstawie rzeczywistych zasad fizyki. Zapewnia spójne, realistyczne oświetlenie w różnych środowiskach i pozwala zasobom wyglądać poprawnie w szerokim zakresie warunków oświetleniowych.
Implementacja PBR w Defold opiera się na specyfikacji materiałów glTF 2.0 oraz powiązanych rozszerzeniach Khronos. Gdy importujesz zasoby glTF do Defold, właściwości materiału są automatycznie analizowane i zapisywane jako uporządkowane dane materiału, do których można odwołać się w shaderach w czasie działania programu.
Materiały PBR mogą obejmować efekty takie jak metaliczne odbicia, chropowatość powierzchni, transmisję, clearcoat, rozpraszanie podpowierzchniowe, irydescencję i wiele innych.
Defold obecnie udostępnia shaderom dane materiału PBR, ale nie zapewnia wbudowanego modelu oświetlenia PBR. Możesz użyć tych danych we własnych shaderach oświetlenia i odbić, aby uzyskać renderowanie oparte na zasadach fizycznych. Domyślny model oświetlenia PBR zostanie dodany do Defold na późniejszym etapie.
Osadzone tekstury z plików glTF nie są obecnie automatycznie przypisywane w Defold. Shaderom udostępniane są tylko parametry materiału. Nadal możesz ręcznie przypisać tekstury do komponentów modeli i pobierać je w shaderze.
Właściwości materiału są odczytywane z plików źródłowych glTF 2.0 przypisanych do komponentu modelu. Nie wszystkie właściwości są standardowe. Niektóre są dostarczane przez opcjonalne rozszerzenia glTF, które mogą, ale nie muszą, zostać dołączone przez narzędzie użyte do eksportu pliku glTF. Odpowiednie rozszerzenie jest oznaczone w nawiasie po nazwie właściwości poniżej.
Niektóre z tych właściwości dostarczają wskazówek, jak materiał powinien być renderowany. Dane dla właściwości (alpha cutoff, alpha mode, double sided i unlit) są dostępne w shaderach, ale nie wpływają na to, jak materiał jest renderowany w Defold.
Dane materiału PBR są udostępniane shaderom na podstawie typów i konwencji nazewnictwa. System materiałów PBR przekazuje wszystkie odczytane parametry materiału do shaderów przez uporządkowany blok uniformów o nazwie PbrMaterial. Każde obsługiwane rozszerzenie glTF odpowiada strukturze w tym bloku, którą można kompilować warunkowo za pomocą znaczników #define.
uniform PbrMaterial
{
// Właściwości materiału
};
Różne cechy materiału są w shaderze opisane jako stałe struktury. Dane zostały spakowane do vec4 w maksymalnym możliwym stopniu, ponieważ w ten sposób stałe są wewnętrznie ustawiane w Defold. W przypadkach, w których dane zostały spakowane, zostało to oznaczone komentarzami w przykładach shaderów dla każdej funkcji poniżej:
struct PbrMetallicRoughness
{
vec4 baseColorFactor;
// R: metallic (domyślnie=1.0), G: roughness (domyślnie=1.0)
vec4 metallicAndRoughnessFactor;
// R: użyj baseColorTexture, G: użyj metallicRoughnessTexture
vec4 metallicRoughnessTextures;
};
struct PbrSpecularGlossiness
{
vec4 diffuseFactor;
// RGB: specular (domyślnie=1.0), A: glossiness (domyślnie=1.0)
vec4 specularAndSpecularGlossinessFactor;
// R: użyj diffuseTexture, G: użyj specularGlossinessTexture
vec4 specularGlossinessTextures;
};
struct PbrClearCoat
{
// R: clearCoat (domyślnie=0.0), G: clearCoatRoughness (domyślnie=0.0)
vec4 clearCoatAndClearCoatRoughnessFactor;
// R: użyj clearCoatTexture, G: użyj clearCoatRoughnessTexture, B: użyj clearCoatNormalTexture
vec4 clearCoatTextures;
};
struct PbrTransmission
{
// R: transmission (domyślnie=0.0)
vec4 transmissionFactor;
// R: użyj transmissionTexture
vec4 transmissionTextures;
};
struct PbrIor
{
// R: ior (domyślnie=0.0)
vec4 ior;
};
struct PbrSpecular
{
// RGB: specularColor, A: specularFactor (domyślnie=1.0);
vec4 specularColorAndSpecularFactor;
// R: użyj specularTexture, G: użyj specularColorTexture
vec4 specularTextures;
};
struct PbrVolume
{
// R: thicknessFactor (domyślnie=0.0), RGB: attenuationColor
vec4 thicknessFactorAndAttenuationColor;
// R: attentuationDistance (domyślnie=-1.0)
vec4 attenuationDistance;
// R: użyj thicknessTexture
vec4 volumeTextures;
};
struct PbrSheen
{
// RGB: sheenColor, A: sheenRoughnessFactor (domyślnie=0.0)
vec4 sheenColorAndRoughnessFactor;
// R: użyj sheenColorTexture, G: użyj sheenRoughnessTexture
vec4 sheenTextures;
};
struct PbrEmissiveStrength
{
// R: emissiveStrength (domyślnie=1.0)
vec4 emissiveStrength;
};
struct PbrIridescence
{
// R: iridescenceFactor (domyślnie=0.0), G: iridescenceIor (domyślnie=1.3), B: iridescenceThicknessMin (domyślnie=100.0), A: iridescenceThicknessMax (domyślnie=400.0)
vec4 iridescenceFactorAndIorAndThicknessMinMax;
// R: użyj iridescenceTexture, G: użyj iridescenceThicknessTexture
vec4 iridescenceTextures;
};
Wspólne właściwości są ustawiane w samym uniformie materiału (i ponownie warto zwrócić uwagę na pakowanie danych do vec4).
// Wspólne tekstury
uniform sampler2D PbrMaterial_normalTexture;
uniform sampler2D PbrMaterial_occlusionTexture;
uniform sampler2D PbrMaterial_emissiveTexture;
uniform PbrMaterial
{
// Wspólne właściwości:
// R: alphaCutoff (domyślnie=0.5), G: doubleSided (domyślnie=false), B: unlit (domyślnie=false)
vec4 pbrAlphaCutoffAndDoubleSidedAndIsUnlit;
// R: użyj normalTexture, G: użyj occlusionTexture, B: użyj emissiveTexture
vec4 pbrCommonTextures;
// Pozostałe właściwości...
};
Poniżej znajduje się przykład shadera zawierającego wszystkie funkcje oraz proponowany schemat nazewnictwa dla powiązań tekstur (tę część również trzeba obsłużyć ręcznie). Zwróć uwagę, że możesz wyłączyć poszczególne funkcje, używając #define wokół każdego elementu PbrMaterial, tak jak pokazano w przykładzie poniżej:
// Flagi funkcji, zakomentuj je lub usuń, aby odchudzić shader.
#define PBR_METALLIC_ROUGHNESS
#define PBR_SPECULAR_GLOSSINESS
#define PBR_CLEARCOAT
#define PBR_TRANSMISSION
#define PBR_IOR
#define PBR_SPECULAR
#define PBR_VOLUME
#define PBR_SHEEN
#define PBR_EMISSIVE_STRENGTH
#define PBR_IRIDESCENCE
// Wspólne
uniform sampler2D PbrMaterial_normalTexture;
uniform sampler2D PbrMaterial_occlusionTexture;
uniform sampler2D PbrMaterial_emissiveTexture;
// PbrMetallicRoughness
uniform sampler2D PbrMetallicRoughness_baseColorTexture;
uniform sampler2D PbrMetallicRoughness_metallicRoughnessTexture;
struct PbrMetallicRoughness
{
vec4 baseColorFactor;
// R: metallic (domyślnie=1.0), G: roughness (domyślnie=1.0)
vec4 metallicAndRoughnessFactor;
// R: użyj baseColorTexture, G: użyj metallicRoughnessTexture
vec4 metallicRoughnessTextures;
};
// PbrSpecularGlossiness
uniform sampler2D PbrSpecularGlossiness_diffuseTexture;
uniform sampler2D PbrSpecularGlossiness_specularGlossinessTexture;
struct PbrSpecularGlossiness
{
vec4 diffuseFactor;
// RGB: specular (domyślnie=1.0), A: glossiness (domyślnie=1.0)
vec4 specularAndSpecularGlossinessFactor;
// R: użyj diffuseTexture, G: użyj specularGlossinessTexture
vec4 specularGlossinessTextures;
};
// PbrClearCoat
uniform sampler2D PbrClearCoat_clearCoatTexture;
uniform sampler2D PbrClearCoat_clearCoatRoughnessTexture;
uniform sampler2D PbrClearCoat_clearCoatNormalTexture;
struct PbrClearCoat
{
// R: clearCoat (domyślnie=0.0), G: clearCoatRoughness (domyślnie=0.0)
vec4 clearCoatAndClearCoatRoughnessFactor;
// R: użyj clearCoatTexture, G: użyj clearCoatRoughnessTexture, B: użyj clearCoatNormalTexture
vec4 clearCoatTextures;
};
// PbrTransmission
uniform sampler2D PbrTransmission_transmissionTexture;
struct PbrTransmission
{
// R: transmission (domyślnie=0.0)
vec4 transmissionFactor;
// R: użyj transmissionTexture
vec4 transmissionTextures;
};
struct PbrIor
{
// R: ior (domyślnie=0.0)
vec4 ior;
};
// PbrSpecular
uniform sampler2D PbrSpecular_specularTexture;
uniform sampler2D PbrSpecular_specularColorTexture;
struct PbrSpecular
{
// RGB: specularColor, A: specularFactor (domyślnie=1.0);
vec4 specularColorAndSpecularFactor;
// R: użyj specularTexture, G: użyj specularColorTexture
vec4 specularTextures;
};
// PbrVolume
uniform sampler2D PbrVolume_thicknessTexture;
struct PbrVolume
{
// R: thicknessFactor (domyślnie=0.0), RGB: attenuationColor
vec4 thicknessFactorAndAttenuationColor;
// R: attentuationDistance (domyślnie=-1.0)
vec4 attenuationDistance;
// R: użyj thicknessTexture
vec4 volumeTextures;
};
// PbrSheen
uniform sampler2D PbrSheen_sheenColorTexture;
uniform sampler2D PbrSheen_sheenRoughnessTexture;
struct PbrSheen
{
// RGB: sheenColor, A: sheenRoughnessFactor (domyślnie=0.0)
vec4 sheenColorAndRoughnessFactor;
// R: użyj sheenColorTexture, G: użyj sheenRoughnessTexture
vec4 sheenTextures;
};
struct PbrEmissiveStrength
{
// R: emissiveStrength (domyślnie=1.0)
vec4 emissiveStrength;
};
// PbrIridescence
uniform sampler2D PbrEmissive_iridescenceTexture;
uniform sampler2D PbrEmissive_iridescenceThicknessTexture;
struct PbrIridescence
{
// R: iridescenceFactor (domyślnie=0.0), G: iridescenceIor (domyślnie=1.3), B: iridescenceThicknessMin (domyślnie=100.0), A: iridescenceThicknessMax (domyślnie=400.0)
vec4 iridescenceFactorAndIorAndThicknessMinMax;
// R: użyj iridescenceTexture, G: użyj iridescenceThicknessTexture
vec4 iridescenceTextures;
};
uniform PbrMaterial
{
// Wspólne właściwości
// R: alphaCutoff (domyślnie=0.5), G: doubleSided (domyślnie=false), B: unlit (domyślnie=false)
vec4 pbrAlphaCutoffAndDoubleSidedAndIsUnlit;
// R: użyj normalTexture, G: użyj occlusionTexture, B: użyj emissiveTexture
vec4 pbrCommonTextures;
// Funkcje
#ifdef PBR_METALLIC_ROUGHNESS
PbrMetallicRoughness pbrMetallicRoughness;
#endif
#ifdef PBR_SPECULAR_GLOSSINESS
PbrSpecularGlossiness pbrSpecularGlossiness;
#endif
#ifdef PBR_CLEARCOAT
PbrClearCoat pbrClearCoat;
#endif
#ifdef PBR_TRANSMISSION
PbrTransmission pbrTransmission;
#endif
#ifdef PBR_IOR
PbrIor pbrIor;
#endif
#ifdef PBR_SPECULAR
PbrSpecular pbrSpecular;
#endif
#ifdef PBR_VOLUME
PbrVolume pbrVolume;
#endif
#ifdef PBR_SHEEN
PbrSheen pbrSheen;
#endif
#ifdef PBR_EMISSIVE_STRENGTH
PbrEmissiveStrength pbrEmissiveStrength;
#endif
#ifdef PBR_IRIDESCENCE
PbrIridescence pbrIridescence;
#endif
};
Jeśli w strukturze materiału nie zostaną znalezione określone punkty danych, dane dla tych funkcji nie zostaną ustawione. Na przykład jeśli w strukturze materiału nie ma pbrClearCoat, dane clearcoat nie zostaną ustawione. Jeśli blok uniformów nie zostanie znaleziony, podczas renderowania nie zostaną ustawione żadne dane.
Każda właściwość materiału odpowiada wewnętrznej stałej renderowania w Defold. Możesz nadpisać wartości domyślne, definiując stałe w samym zasobie materiału, zgodnie ze wzorcem nazewnictwa pbrFeature.structMember. Te wartości zostaną zastosowane automatycznie, jeśli odpowiadające im dane będą nieobecne w materiale glTF.

Aby użyć danych materiału do renderowania opartego na zasadach fizycznych, zaimplementuj BRDF w shaderze fragmentu, korzystając z parametrów udostępnionych w bloku PbrMaterial.
Zobacz także: