Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lyuma/8268047e10ebca9436a2cdcd960e0c6f to your computer and use it in GitHub Desktop.
Save lyuma/8268047e10ebca9436a2cdcd960e0c6f to your computer and use it in GitHub Desktop.
/*************************************************************************/
/* default_godot_light_function.gdshader */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
// Mostly compatible with Godot 3 and Godot 4.
float SchlickFresnel(float u) {
float m = 1.0 - u;
float m2 = m * m;
return m2 * m2 * m;
}
float G_GGX_2cos(float cos_theta_m, float alpha) {
float k = 0.5 * alpha;
return 0.5 / (cos_theta_m * (1.0 - k) + k);
}
float D_GGX(float cos_theta_m, float alpha) {
float alpha2 = alpha * alpha;
float d = 1.0 + (alpha2 - 1.0) * cos_theta_m * cos_theta_m;
return alpha2 / (3.1415926 * d * d);
}
vec3 F0(float xmetallic, float xspecular, vec3 xalbedo) {
float dielectric = 0.16 * xspecular * xspecular;
return mix(vec3(dielectric), xalbedo, vec3(xmetallic));
}
void light() {
// TODO: RIM, RIM_TINT and SPECULAR must be assigned using varying
float xxRIM = 1.0; //rim;
float xxRIM_TINT = 1.0; //rim_tint;
float xxSPECULAR = 0.5; //specular;
// Default Godot BRDF.
float NdotL = dot(NORMAL, LIGHT);
float cNdotL = max(NdotL, 0.0); // clamped NdotL
float NdotV = dot(NORMAL, VIEW);
float cNdotV = max(NdotV, 0.0);
vec3 H = normalize(VIEW + LIGHT);
float cNdotH = max(dot(NORMAL, H), 0.0);
float cLdotH = max(dot(LIGHT, H), 0.0);
float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance
{
float FD90_minus_1 = 2.0 * cLdotH * cLdotH * ROUGHNESS - 0.5;
float FdV = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotV);
float FdL = 1.0 + FD90_minus_1 * SchlickFresnel(cNdotL);
diffuse_brdf_NL = (1.0 / 3.1415926) * FdV * FdL * cNdotL;
}
DIFFUSE_LIGHT += 1.0 * LIGHT_COLOR * ALBEDO * diffuse_brdf_NL * ATTENUATION;
// gd 3.0 only: DIFFUSE_LIGHT += LIGHT_COLOR * ALBEDO * (vec3(1.0 / 3.1415926) - diffuse_brdf_NL) * TRANSMISSION * ATTENUATION;
float rim_light = pow(max(0.0, 1.0 - cNdotV), max(0.0, (1.0 - ROUGHNESS) * 16.0));
DIFFUSE_LIGHT += rim_light * xxRIM * mix(vec3(1.0), ALBEDO, xxRIM_TINT) * LIGHT_COLOR;
if (ROUGHNESS > 0.0) {
float alpha_ggx = roughness * roughness;
float D = D_GGX(cNdotH, alpha_ggx);
float G = G_GGX_2cos(cNdotL, alpha_ggx) * G_GGX_2cos(cNdotV, alpha_ggx);
// F
vec3 f0 = F0(METALLIC, xxSPECULAR, ALBEDO);
float cLdotH5 = SchlickFresnel(cLdotH);
vec3 F = mix(vec3(cLdotH5), vec3(1.0), f0);
vec3 specular_brdf_NL = cNdotL * D * F * G;
SPECULAR_LIGHT += specular_brdf_NL * LIGHT_COLOR * ATTENUATION;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment