fuckingtech/public/assets/glsl/lygia/lighting/toMetallic.hlsl
Elias Rhouzlane bf76c6b086 fix: code
2025-04-06 17:23:52 +02:00

35 lines
No EOL
1.5 KiB
HLSL

/*
contributors: Patricio Gonzalez Vivo
description: Convert diffuse/specular/glossiness workflow to PBR metallic factor
use: <float> toMetallic(<float3> diffuse, <float3> specular, <float> maxSpecular)
license:
- Copyright (c) 2021 Patricio Gonzalez Vivo under Prosperity License - https://prosperitylicense.com/versions/3.0.0
- Copyright (c) 2021 Patricio Gonzalez Vivo under Patron License - https://lygia.xyz/license
*/
#ifndef TOMETALLIC_MIN_REFLECTANCE
#define TOMETALLIC_MIN_REFLECTANCE 0.04
#endif
#ifndef FNC_TOMETALLIC
#define FNC_TOMETTALIC
float toMetallic(float3 diffuse, float3 specular, float maxSpecular) {
float perceivedDiffuse = sqrt(0.299 * diffuse.r * diffuse.r + 0.587 * diffuse.g * diffuse.g + 0.114 * diffuse.b * diffuse.b);
float perceivedSpecular = sqrt(0.299 * specular.r * specular.r + 0.587 * specular.g * specular.g + 0.114 * specular.b * specular.b);
if (perceivedSpecular < TOMETALLIC_MIN_REFLECTANCE) {
return 0.0;
}
float a = TOMETALLIC_MIN_REFLECTANCE;
float b = perceivedDiffuse * (1.0 - maxSpecular) / (1.0 - TOMETALLIC_MIN_REFLECTANCE) + perceivedSpecular - 2.0 * TOMETALLIC_MIN_REFLECTANCE;
float c = TOMETALLIC_MIN_REFLECTANCE - perceivedSpecular;
float D = max(b * b - 4.0 * a * c, 0.0);
return saturate((-b + sqrt(D)) / (2.0 * a));
}
float toMetallic(float3 diffuse, float3 specular) {
float maxSpecula = max(max(specular.r, specular.g), specular.b);
return toMetallic(diffuse, specular, maxSpecula);
}
#endif