Godot Engine v4.1.rc2.official.46424488e - https://godotengine.org TextServer: Added interface "Dummy" TextServer: Added interface "ICU / HarfBuzz / Graphite (Built-in)" Using "winink" pen tablet driver... OpenGL debugging not supported! Shader 'CanvasSdfShaderGLES3' SHA256: 72cda41890bce2f09217c172023d31e735a3fee605bad83c8e63f1a981a36a77 Shader 'SkeletonShaderGLES3' SHA256: add3f03e6eebdffdf0073fae42195857307eddada333b82998642329511917a2 Shader 'ParticlesShaderGLES3' SHA256: 9bf432d37b0b4792d67aa9f577258845d4047e3f98e990aeca6fd20a868c4db2 Shader 'ParticlesCopyShaderGLES3' SHA256: c778843b0cd1c7ce4c621fa2b924f703ac4001faef0555114b81a76c264a6908 Shader 'CopyShaderGLES3' SHA256: b1bc5a355ed9bda23f291956f121e0535b4e81b54cdac398412d200e6133161f Shader 'CanvasShaderGLES3' SHA256: 007cddfcf35f7d57b219cffec53f6a7d9fe7a1979be4db64c6450cfac6000053 Shader 'CanvasOcclusionShaderGLES3' SHA256: 968915a469db9a7d505be274e8060c8288c15a5865baeb8362b3b0e2cacb16f3 Shader 'SceneShaderGLES3' SHA256: 7757391c487ac94cfa6418166bd5fdacce470f09394c1a2a875e878c8c5d8596 #version 330 #define USE_GLES_OVER_GL #define USE_RADIANCE_MAP #define MAX_GLOBAL_SHADER_UNIFORMS 256 #define MAX_LIGHT_DATA_STRUCTS 32 #define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS 8 #define MAX_FORWARD_LIGHTS uint(8) #define BASE_PASS #ifdef USE_MULTIVIEW #if defined(GL_OVR_multiview2) #extension GL_OVR_multiview2 : require #elif defined(GL_OVR_multiview) #extension GL_OVR_multiview : require #endif #define ViewIndex gl_ViewID_OVR #define MAX_VIEWS 2 #else #define ViewIndex uint(0) #define MAX_VIEWS 1 #endif precision highp float; precision highp int; // Default to SPECULAR_SCHLICK_GGX. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON) #define SPECULAR_SCHLICK_GGX #endif #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED) #ifndef NORMAL_USED #define NORMAL_USED #endif #endif #ifndef MODE_RENDER_DEPTH #ifdef USE_BCS uniform vec3 bcs; #endif #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT uniform sampler2D source_color_correction; //texunit:-1 #else uniform sampler3D source_color_correction; //texunit:-1 #endif #endif layout(std140) uniform TonemapData { //ubo:0 float exposure; float white; int tonemapper; int pad; }; vec3 apply_bcs(vec3 color, vec3 bcs) { color = mix(vec3(0.0), color, bcs.x); color = mix(vec3(0.5), color, bcs.y); color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z); return color; } #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT vec3 apply_color_correction(vec3 color) { color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r; color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g; color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b; return color; } #else vec3 apply_color_correction(vec3 color) { return textureLod(source_color_correction, color, 0.0).rgb; } #endif #endif vec3 tonemap_filmic(vec3 color, float p_white) { // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) // has no effect on the curve's general shape or visual properties const float exposure_bias = 2.0f; const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance const float B = 0.30f * exposure_bias; const float C = 0.10f; const float D = 0.20f; const float E = 0.01f; const float F = 0.30f; vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F; return color_tonemapped / p_white_tonemapped; } // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl // (MIT License). vec3 tonemap_aces(vec3 color, float p_white) { const float exposure_bias = 1.8f; const float A = 0.0245786f; const float B = 0.000090537f; const float C = 0.983729f; const float D = 0.432951f; const float E = 0.238081f; // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias` const mat3 rgb_to_rrt = mat3( vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias), vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias), vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias)); const mat3 odt_to_rgb = mat3( vec3(1.60475f, -0.53108f, -0.07367f), vec3(-0.10208f, 1.10813f, -0.00605f), vec3(-0.00327f, -0.07276f, 1.07602f)); color *= rgb_to_rrt; vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E); color_tonemapped *= odt_to_rgb; p_white *= exposure_bias; float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E); return color_tonemapped / p_white_tonemapped; } vec3 tonemap_reinhard(vec3 color, float p_white) { return (p_white * color + color) / (color * p_white + p_white); } // This expects 0-1 range input. vec3 linear_to_srgb(vec3 color) { //color = clamp(color, vec3(0.0), vec3(1.0)); //const vec3 a = vec3(0.055f); //return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0)); } // This expects 0-1 range input, outside that range it behaves poorly. vec3 srgb_to_linear(vec3 color) { // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878); } #define TONEMAPPER_LINEAR 0 #define TONEMAPPER_REINHARD 1 #define TONEMAPPER_FILMIC 2 #define TONEMAPPER_ACES 3 vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color // Ensure color values passed to tonemappers are positive. // They can be negative in the case of negative lights, which leads to undesired behavior. if (tonemapper == TONEMAPPER_LINEAR) { return color; } else if (tonemapper == TONEMAPPER_REINHARD) { return tonemap_reinhard(max(vec3(0.0f), color), p_white); } else if (tonemapper == TONEMAPPER_FILMIC) { return tonemap_filmic(max(vec3(0.0f), color), p_white); } else { // TONEMAPPER_ACES return tonemap_aces(max(vec3(0.0f), color), p_white); } } #endif #ifdef USE_GLES_OVER_GL // Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile. uint float2half(uint f) { uint e = f & uint(0x7f800000); if (e <= uint(0x38000000)) { return uint(0); } else { return ((f >> uint(16)) & uint(0x8000)) | (((e - uint(0x38000000)) >> uint(13)) & uint(0x7c00)) | ((f >> uint(13)) & uint(0x03ff)); } } uint half2float(uint h) { uint h_e = h & uint(0x7c00); return ((h & uint(0x8000)) << uint(16)) | uint((h_e >> uint(10)) != uint(0)) * (((h_e + uint(0x1c000)) << uint(13)) | ((h & uint(0x03ff)) << uint(13))); } uint packHalf2x16(vec2 v) { return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16); } vec2 unpackHalf2x16(uint v) { return vec2(uintBitsToFloat(half2float(v & uint(0xffff))), uintBitsToFloat(half2float(v >> uint(16)))); } uint packUnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0)); return uv.x | uv.y << uint(16); } vec2 unpackUnorm2x16(uint p) { return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization } uint packSnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0); return uv.x | uv.y << uint(16); } vec2 unpackSnorm2x16(uint p) { vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16))); return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0)); } #endif // Compatibility renames. These are exposed with the "godot_" prefix // to work around an Adreno bug which was exposing these ES310 functions // in ES300 shaders. Internally, we must use the "godot_" prefix, but user shaders // will be mapped automatically. uint godot_packUnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0)); return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24)); } vec4 godot_unpackUnorm4x8(uint p) { return vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))) * 0.00392156862; // 1.0 / 255.0 } uint godot_packSnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0); return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24); } vec4 godot_unpackSnorm4x8(uint p) { vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))); return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0)); } #define packUnorm4x8 godot_packUnorm4x8 #define unpackUnorm4x8 godot_unpackUnorm4x8 #define packSnorm4x8 godot_packSnorm4x8 #define unpackSnorm4x8 godot_unpackSnorm4x8 /* texture unit usage, N is max_texture_unity-N 1-color correction // In tonemap_inc.glsl 2-radiance 3-directional_shadow 4-positional_shadow 5-screen 6-depth */ #define M_PI 3.14159265359 /* clang-format on */ #define SHADER_IS_SRGB true /* Varyings */ #if defined(COLOR_USED) in vec4 color_interp; #endif #if defined(UV_USED) in vec2 uv_interp; #endif #if defined(UV2_USED) in vec2 uv2_interp; #else #ifdef USE_LIGHTMAP in vec2 uv2_interp; #endif #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) in vec3 tangent_interp; in vec3 binormal_interp; #endif #ifdef NORMAL_USED in vec3 normal_interp; #endif in highp vec3 vertex_interp; #ifdef USE_RADIANCE_MAP #define RADIANCE_MAX_LOD 5.0 uniform samplerCube radiance_map; // texunit:-2 #endif layout(std140) uniform GlobalShaderUniformData { //ubo:1 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS]; }; /* Material Uniforms */ #ifdef MATERIAL_UNIFORMS_USED /* clang-format off */ layout(std140) uniform MaterialUniforms { // ubo:3 }; /* clang-format on */ #endif layout(std140) uniform SceneData { // ubo:2 highp mat4 projection_matrix; highp mat4 inv_projection_matrix; highp mat4 inv_view_matrix; highp mat4 view_matrix; vec2 viewport_size; vec2 screen_pixel_size; mediump vec4 ambient_light_color_energy; mediump float ambient_color_sky_mix; bool material_uv2_mode; float emissive_exposure_normalization; bool use_ambient_light; bool use_ambient_cubemap; bool use_reflection_cubemap; float fog_aerial_perspective; float time; mat3 radiance_inverse_xform; uint directional_light_count; float z_far; float z_near; float IBL_exposure_normalization; bool fog_enabled; float fog_density; float fog_height; float fog_height_density; vec3 fog_light_color; float fog_sun_scatter; uint camera_visible_layers; uint pad3; uint pad4; uint pad5; } scene_data; #ifdef USE_MULTIVIEW layout(std140) uniform MultiviewData { // ubo:8 highp mat4 projection_matrix_view[MAX_VIEWS]; highp mat4 inv_projection_matrix_view[MAX_VIEWS]; highp vec4 eye_offset[MAX_VIEWS]; } multiview_data; #endif /* clang-format off */ /* clang-format on */ // Directional light data. #ifndef DISABLE_LIGHT_DIRECTIONAL struct DirectionalLightData { mediump vec3 direction; mediump float energy; mediump vec3 color; mediump float size; mediump vec3 pad; mediump float specular; }; layout(std140) uniform DirectionalLights { // ubo:7 DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; }; #endif // !DISABLE_LIGHT_DIRECTIONAL // Omni and spot light data. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) struct LightData { // This structure needs to be as packed as possible. highp vec3 position; highp float inv_radius; mediump vec3 direction; highp float size; mediump vec3 color; mediump float attenuation; mediump float cone_attenuation; mediump float cone_angle; mediump float specular_amount; mediump float shadow_opacity; }; #ifndef DISABLE_LIGHT_OMNI layout(std140) uniform OmniLightData { // ubo:5 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint omni_light_indices[MAX_FORWARD_LIGHTS]; uniform uint omni_light_count; #endif #ifndef DISABLE_LIGHT_SPOT layout(std140) uniform SpotLightData { // ubo:6 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint spot_light_indices[MAX_FORWARD_LIGHTS]; uniform uint spot_light_count; #endif #ifdef USE_ADDITIVE_LIGHTING uniform highp samplerCubeShadow positional_shadow; // texunit:-4 #endif #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifdef USE_MULTIVIEW uniform highp sampler2DArray depth_buffer; // texunit:-6 uniform highp sampler2DArray color_buffer; // texunit:-5 vec3 multiview_uv(vec2 uv) { return vec3(uv, ViewIndex); } #else uniform highp sampler2D depth_buffer; // texunit:-6 uniform highp sampler2D color_buffer; // texunit:-5 vec2 multiview_uv(vec2 uv) { return uv; } #endif uniform highp mat4 world_transform; uniform mediump float opaque_prepass_threshold; layout(location = 0) out vec4 frag_color; vec3 F0(float metallic, float specular, vec3 albedo) { float dielectric = 0.16 * specular * specular; // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials; // see https://google.github.io/filament/Filament.md.html return mix(vec3(dielectric), albedo, vec3(metallic)); } #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) float D_GGX(float cos_theta_m, float alpha) { float a = cos_theta_m * alpha; float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a); return k * k * (1.0 / M_PI); } // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX float V_GGX(float NdotL, float NdotV, float alpha) { return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha); } float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { float alpha2 = alpha_x * alpha_y; highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m); highp float v2 = dot(v, v); float w2 = alpha2 / v2; float D = alpha2 * w2 * w2 * (1.0 / M_PI); return D; } float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) { float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV)); float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL)); return 0.5 / (Lambda_V + Lambda_L); } float SchlickFresnel(float u) { float m = 1.0 - u; float m2 = m * m; return m2 * m2 * m; // pow(m,5) } void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_directional, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 B, vec3 T, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { #if defined(USE_LIGHT_SHADER_CODE) // light is written by the light shader highp mat4 model_matrix = world_transform; mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; vec3 normal = N; vec3 light = L; vec3 view = V; /* clang-format off */ /* clang-format on */ #else float NdotL = min(A + dot(N, L), 1.0); float cNdotL = max(NdotL, 0.0); // clamped NdotL float NdotV = dot(N, V); float cNdotV = max(NdotV, 1e-4); #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) vec3 H = normalize(V + L); #endif #if defined(SPECULAR_SCHLICK_GGX) float cNdotH = clamp(A + dot(N, H), 0.0, 1.0); #endif #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) float cLdotH = clamp(A + dot(L, H), 0.0, 1.0); #endif if (metallic < 1.0) { float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance #if defined(DIFFUSE_LAMBERT_WRAP) // Energy conserving lambert wrap shader. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/ diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI); #elif defined(DIFFUSE_TOON) diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI); #elif defined(DIFFUSE_BURLEY) { 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 / M_PI) * FdV * FdL * cNdotL; } #else // Lambert diffuse_brdf_NL = cNdotL * (1.0 / M_PI); #endif diffuse_light += light_color * diffuse_brdf_NL * attenuation; #if defined(LIGHT_BACKLIGHT_USED) diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation; #endif #if defined(LIGHT_RIM_USED) // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0)); diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color; #endif } if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely // D #if defined(SPECULAR_TOON) vec3 R = normalize(-reflect(L, N)); float RdotV = dot(R, V); float mid = 1.0 - roughness; mid *= mid; float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid; diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection #elif defined(SPECULAR_DISABLED) // none.. #elif defined(SPECULAR_SCHLICK_GGX) // shlick+ggx as default float alpha_ggx = roughness * roughness; #if defined(LIGHT_ANISOTROPY_USED) float aspect = sqrt(1.0 - anisotropy * 0.9); float ax = alpha_ggx / aspect; float ay = alpha_ggx * aspect; float XdotH = dot(T, H); float YdotH = dot(B, H); float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH); float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL); #else float D = D_GGX(cNdotH, alpha_ggx); float G = V_GGX(cNdotL, cNdotV, alpha_ggx); #endif // LIGHT_ANISOTROPY_USED // F float cLdotH5 = SchlickFresnel(cLdotH); // Calculate Fresnel using cheap approximate specular occlusion term from Filament: // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion float f90 = clamp(50.0 * f0.g, 0.0, 1.0); vec3 F = f0 + (f90 - f0) * cLdotH5; vec3 specular_brdf_NL = cNdotL * D * F * G; specular_light += specular_brdf_NL * light_color * attenuation * specular_amount; #endif #if defined(LIGHT_CLEARCOAT_USED) // Clearcoat ignores normal_map, use vertex normal instead float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0); float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0); float ccNdotV = max(dot(vertex_normal, V), 1e-4); #if !defined(SPECULAR_SCHLICK_GGX) float cLdotH5 = SchlickFresnel(cLdotH); #endif float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness)); float Gr = 0.25 / (cLdotH * cLdotH); float Fr = mix(.04, 1.0, cLdotH5); float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL; specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount; // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR) // but to do so we need to rearrange this entire function #endif // LIGHT_CLEARCOAT_USED } #ifdef USE_SHADOW_TO_OPACITY alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0)); #endif #endif // USE_LIGHT_SHADER_CODE } float get_omni_spot_attenuation(float distance, float inv_range, float decay) { float nd = distance * inv_range; nd *= nd; nd *= nd; // nd^4 nd = max(1.0 - nd, 0.0); nd *= nd; // nd^2 return nd * pow(max(distance, 0.0001), -decay); } #ifndef DISABLE_LIGHT_OMNI void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = omni_lights[idx].position - vertex; float light_length = length(light_rel_vec); float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation); vec3 color = omni_lights[idx].color; float size_A = 0.0; if (omni_lights[idx].size > 0.0) { float t = omni_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * omni_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = spot_lights[idx].position - vertex; float light_length = length(light_rel_vec); float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation); vec3 spot_dir = spot_lights[idx].direction; float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle); float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle)); spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].cone_attenuation); vec3 color = spot_lights[idx].color; float size_A = 0.0; if (spot_lights[idx].size > 0.0) { float t = spot_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * spot_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifndef MODE_RENDER_DEPTH vec4 fog_process(vec3 vertex) { vec3 fog_color = scene_data.fog_light_color; #ifdef USE_RADIANCE_MAP /* if (scene_data.fog_aerial_perspective > 0.0) { vec3 sky_fog_color = vec3(0.0); vec3 cube_view = scene_data.radiance_inverse_xform * vertex; // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near)); sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb; fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective); } */ #endif #ifndef DISABLE_LIGHT_DIRECTIONAL if (scene_data.fog_sun_scatter > 0.001) { vec4 sun_scatter = vec4(0.0); float sun_total = 0.0; vec3 view = normalize(vertex); for (uint i = uint(0); i < scene_data.directional_light_count; i++) { vec3 light_color = directional_lights[i].color * directional_lights[i].energy; float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); fog_color += light_color * light_amount * scene_data.fog_sun_scatter; } } #endif // !DISABLE_LIGHT_DIRECTIONAL float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density)); if (abs(scene_data.fog_height_density) >= 0.0001) { float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y; float y_dist = y - scene_data.fog_height; float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density)); fog_amount = max(vfog_amount, fog_amount); } return vec4(fog_color, fog_amount); } #endif // !MODE_RENDER_DEPTH void main() { //lay out everything, whatever is unused is optimized away anyway vec3 vertex = vertex_interp; #ifdef USE_MULTIVIEW vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz; vec3 view = -normalize(vertex_interp - eye_offset); mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex]; mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex]; #else vec3 eye_offset = vec3(0.0, 0.0, 0.0); vec3 view = -normalize(vertex_interp); mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; #endif highp mat4 model_matrix = world_transform; vec3 albedo = vec3(1.0); vec3 backlight = vec3(0.0); vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0); float transmittance_depth = 0.0; float transmittance_boost = 0.0; float metallic = 0.0; float specular = 0.5; vec3 emission = vec3(0.0); float roughness = 1.0; float rim = 0.0; float rim_tint = 0.0; float clearcoat = 0.0; float clearcoat_roughness = 0.0; float anisotropy = 0.0; vec2 anisotropy_flow = vec2(1.0, 0.0); vec4 fog = vec4(0.0); #if defined(CUSTOM_RADIANCE_USED) vec4 custom_radiance = vec4(0.0); #endif #if defined(CUSTOM_IRRADIANCE_USED) vec4 custom_irradiance = vec4(0.0); #endif float ao = 1.0; float ao_light_affect = 0.0; float alpha = 1.0; #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) vec3 binormal = normalize(binormal_interp); vec3 tangent = normalize(tangent_interp); #else vec3 binormal = vec3(0.0); vec3 tangent = vec3(0.0); #endif #ifdef NORMAL_USED vec3 normal = normalize(normal_interp); #if defined(DO_SIDE_CHECK) if (!gl_FrontFacing) { normal = -normal; } #endif #endif //NORMAL_USED #ifdef UV_USED vec2 uv = uv_interp; #endif #if defined(UV2_USED) || defined(USE_LIGHTMAP) vec2 uv2 = uv2_interp; #endif #if defined(COLOR_USED) vec4 color = color_interp; #endif #if defined(NORMAL_MAP_USED) vec3 normal_map = vec3(0.5); #endif float normal_map_depth = 1.0; vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size; float sss_strength = 0.0; #ifdef ALPHA_SCISSOR_USED float alpha_scissor_threshold = 1.0; #endif // ALPHA_SCISSOR_USED #ifdef ALPHA_HASH_USED float alpha_hash_scale = 1.0; #endif // ALPHA_HASH_USED #ifdef ALPHA_ANTIALIASING_EDGE_USED float alpha_antialiasing_edge = 0.0; vec2 alpha_texture_coordinate = vec2(0.0, 0.0); #endif // ALPHA_ANTIALIASING_EDGE_USED { } #ifndef USE_SHADOW_TO_OPACITY #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor_threshold) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // !USE_SHADOW_TO_OPACITY #ifdef NORMAL_MAP_USED normal_map.xy = normal_map.xy * 2.0 - 1.0; normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth)); #endif #ifdef LIGHT_ANISOTROPY_USED if (anisotropy > 0.01) { //rotation matrix mat3 rot = mat3(tangent, binormal, normal); //make local to space tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0)); binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0)); } #endif #ifndef MODE_RENDER_DEPTH #ifndef CUSTOM_FOG_USED #ifndef DISABLE_FOG // fog must be processed as early as possible and then packed. // to maximize VGPR usage if (scene_data.fog_enabled) { fog = fog_process(vertex); } #endif // !DISABLE_FOG #endif // !CUSTOM_FOG_USED uint fog_rg = packHalf2x16(fog.rg); uint fog_ba = packHalf2x16(fog.ba); // Convert colors to linear albedo = srgb_to_linear(albedo); emission = srgb_to_linear(emission); // TODO Backlight and transmittance when used #ifndef MODE_UNSHADED vec3 f0 = F0(metallic, specular, albedo); vec3 specular_light = vec3(0.0, 0.0, 0.0); vec3 diffuse_light = vec3(0.0, 0.0, 0.0); vec3 ambient_light = vec3(0.0, 0.0, 0.0); #ifdef BASE_PASS /////////////////////// LIGHTING ////////////////////////////// // IBL precalculations float ndotv = clamp(dot(normal, view), 0.0, 1.0); vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0); #ifdef USE_RADIANCE_MAP if (scene_data.use_reflection_cubemap) { #ifdef LIGHT_ANISOTROPY_USED // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent; vec3 anisotropic_tangent = cross(anisotropic_direction, view); vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction); vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0))); vec3 ref_vec = reflect(-view, bent_normal); #else vec3 ref_vec = reflect(-view, normal); #endif ref_vec = mix(ref_vec, normal, roughness * roughness); float horizon = min(1.0 + dot(ref_vec, normal), 1.0); ref_vec = scene_data.radiance_inverse_xform * ref_vec; specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb; specular_light = srgb_to_linear(specular_light); specular_light *= horizon * horizon; specular_light *= scene_data.ambient_light_color_energy.a; } #endif // Calculate Reflection probes // Calculate Lightmaps #if defined(CUSTOM_RADIANCE_USED) specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a); #endif // CUSTOM_RADIANCE_USED #ifndef USE_LIGHTMAP //lightmap overrides everything if (scene_data.use_ambient_light) { ambient_light = scene_data.ambient_light_color_energy.rgb; #ifdef USE_RADIANCE_MAP if (scene_data.use_ambient_cubemap) { vec3 ambient_dir = scene_data.radiance_inverse_xform * normal; vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb; cubemap_ambient = srgb_to_linear(cubemap_ambient); ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix); } #endif } #endif // USE_LIGHTMAP #if defined(CUSTOM_IRRADIANCE_USED) ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a); #endif // CUSTOM_IRRADIANCE_USED { #if defined(AMBIENT_LIGHT_DISABLED) ambient_light = vec3(0.0, 0.0, 0.0); #else ambient_light *= albedo.rgb; ambient_light *= ao; #endif // AMBIENT_LIGHT_DISABLED } // convert ao to direct light ao ao = mix(1.0, ao, ao_light_affect); { #if defined(DIFFUSE_TOON) //simplify for toon, as specular_light *= specular * metallic * albedo * 2.0; #else // scales the specular reflections, needs to be be computed before lighting happens, // but after environment, GI, and reflection probes are added // Environment brdf approximation (Lazarov 2013) // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); vec4 r = roughness * c0 + c1; float ndotv = clamp(dot(normal, view), 0.0, 1.0); float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y; vec2 env = vec2(-1.04, 1.04) * a004 + r.zw; specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0); #endif } #endif // BASE_PASS #ifndef DISABLE_LIGHT_DIRECTIONAL //diffuse_light = normal; // for (uint i = uint(0); i < scene_data.directional_light_count; i++) { light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, true, 1.0, f0, roughness, metallic, 1.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_DIRECTIONAL #ifndef DISABLE_LIGHT_OMNI for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= omni_light_count) { break; } light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= spot_light_count) { break; } light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED tangent, binormal, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !MODE_UNSHADED #endif // !MODE_RENDER_DEPTH #if defined(USE_SHADOW_TO_OPACITY) alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0)); #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // USE_SHADOW_TO_OPACITY #ifdef MODE_RENDER_DEPTH //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) #else // !MODE_RENDER_DEPTH #ifdef MODE_UNSHADED frag_color = vec4(albedo, alpha); #else diffuse_light *= albedo; diffuse_light *= 1.0 - metallic; ambient_light *= 1.0 - metallic; frag_color = vec4(diffuse_light + specular_light, alpha); #ifdef BASE_PASS frag_color.rgb += emission + ambient_light; #endif #endif //MODE_UNSHADED fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba)); #ifndef DISABLE_FOG if (scene_data.fog_enabled) { #ifdef BASE_PASS frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a); #else frag_color.rgb *= (1.0 - fog.a); #endif // BASE_PASS } #endif // Tonemap before writing as we are writing to an sRGB framebuffer frag_color.rgb *= exposure; frag_color.rgb = apply_tonemapping(frag_color.rgb, white); frag_color.rgb = linear_to_srgb(frag_color.rgb); #ifdef USE_BCS frag_color.rgb = apply_bcs(frag_color.rgb, bcs); #endif #ifdef USE_COLOR_CORRECTION frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction); #endif #endif //!MODE_RENDER_DEPTH } #version 330 #define USE_GLES_OVER_GL #define USE_RADIANCE_MAP #define MAX_GLOBAL_SHADER_UNIFORMS 256 #define MAX_LIGHT_DATA_STRUCTS 32 #define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS 8 #define MAX_FORWARD_LIGHTS uint(8) #define BASE_PASS #define USE_INSTANCING #ifdef USE_MULTIVIEW #if defined(GL_OVR_multiview2) #extension GL_OVR_multiview2 : require #elif defined(GL_OVR_multiview) #extension GL_OVR_multiview : require #endif #define ViewIndex gl_ViewID_OVR #define MAX_VIEWS 2 #else #define ViewIndex uint(0) #define MAX_VIEWS 1 #endif precision highp float; precision highp int; // Default to SPECULAR_SCHLICK_GGX. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON) #define SPECULAR_SCHLICK_GGX #endif #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED) #ifndef NORMAL_USED #define NORMAL_USED #endif #endif #ifndef MODE_RENDER_DEPTH #ifdef USE_BCS uniform vec3 bcs; #endif #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT uniform sampler2D source_color_correction; //texunit:-1 #else uniform sampler3D source_color_correction; //texunit:-1 #endif #endif layout(std140) uniform TonemapData { //ubo:0 float exposure; float white; int tonemapper; int pad; }; vec3 apply_bcs(vec3 color, vec3 bcs) { color = mix(vec3(0.0), color, bcs.x); color = mix(vec3(0.5), color, bcs.y); color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z); return color; } #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT vec3 apply_color_correction(vec3 color) { color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r; color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g; color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b; return color; } #else vec3 apply_color_correction(vec3 color) { return textureLod(source_color_correction, color, 0.0).rgb; } #endif #endif vec3 tonemap_filmic(vec3 color, float p_white) { // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) // has no effect on the curve's general shape or visual properties const float exposure_bias = 2.0f; const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance const float B = 0.30f * exposure_bias; const float C = 0.10f; const float D = 0.20f; const float E = 0.01f; const float F = 0.30f; vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F; return color_tonemapped / p_white_tonemapped; } // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl // (MIT License). vec3 tonemap_aces(vec3 color, float p_white) { const float exposure_bias = 1.8f; const float A = 0.0245786f; const float B = 0.000090537f; const float C = 0.983729f; const float D = 0.432951f; const float E = 0.238081f; // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias` const mat3 rgb_to_rrt = mat3( vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias), vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias), vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias)); const mat3 odt_to_rgb = mat3( vec3(1.60475f, -0.53108f, -0.07367f), vec3(-0.10208f, 1.10813f, -0.00605f), vec3(-0.00327f, -0.07276f, 1.07602f)); color *= rgb_to_rrt; vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E); color_tonemapped *= odt_to_rgb; p_white *= exposure_bias; float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E); return color_tonemapped / p_white_tonemapped; } vec3 tonemap_reinhard(vec3 color, float p_white) { return (p_white * color + color) / (color * p_white + p_white); } // This expects 0-1 range input. vec3 linear_to_srgb(vec3 color) { //color = clamp(color, vec3(0.0), vec3(1.0)); //const vec3 a = vec3(0.055f); //return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0)); } // This expects 0-1 range input, outside that range it behaves poorly. vec3 srgb_to_linear(vec3 color) { // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878); } #define TONEMAPPER_LINEAR 0 #define TONEMAPPER_REINHARD 1 #define TONEMAPPER_FILMIC 2 #define TONEMAPPER_ACES 3 vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color // Ensure color values passed to tonemappers are positive. // They can be negative in the case of negative lights, which leads to undesired behavior. if (tonemapper == TONEMAPPER_LINEAR) { return color; } else if (tonemapper == TONEMAPPER_REINHARD) { return tonemap_reinhard(max(vec3(0.0f), color), p_white); } else if (tonemapper == TONEMAPPER_FILMIC) { return tonemap_filmic(max(vec3(0.0f), color), p_white); } else { // TONEMAPPER_ACES return tonemap_aces(max(vec3(0.0f), color), p_white); } } #endif #ifdef USE_GLES_OVER_GL // Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile. uint float2half(uint f) { uint e = f & uint(0x7f800000); if (e <= uint(0x38000000)) { return uint(0); } else { return ((f >> uint(16)) & uint(0x8000)) | (((e - uint(0x38000000)) >> uint(13)) & uint(0x7c00)) | ((f >> uint(13)) & uint(0x03ff)); } } uint half2float(uint h) { uint h_e = h & uint(0x7c00); return ((h & uint(0x8000)) << uint(16)) | uint((h_e >> uint(10)) != uint(0)) * (((h_e + uint(0x1c000)) << uint(13)) | ((h & uint(0x03ff)) << uint(13))); } uint packHalf2x16(vec2 v) { return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16); } vec2 unpackHalf2x16(uint v) { return vec2(uintBitsToFloat(half2float(v & uint(0xffff))), uintBitsToFloat(half2float(v >> uint(16)))); } uint packUnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0)); return uv.x | uv.y << uint(16); } vec2 unpackUnorm2x16(uint p) { return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization } uint packSnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0); return uv.x | uv.y << uint(16); } vec2 unpackSnorm2x16(uint p) { vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16))); return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0)); } #endif // Compatibility renames. These are exposed with the "godot_" prefix // to work around an Adreno bug which was exposing these ES310 functions // in ES300 shaders. Internally, we must use the "godot_" prefix, but user shaders // will be mapped automatically. uint godot_packUnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0)); return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24)); } vec4 godot_unpackUnorm4x8(uint p) { return vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))) * 0.00392156862; // 1.0 / 255.0 } uint godot_packSnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0); return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24); } vec4 godot_unpackSnorm4x8(uint p) { vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))); return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0)); } #define packUnorm4x8 godot_packUnorm4x8 #define unpackUnorm4x8 godot_unpackUnorm4x8 #define packSnorm4x8 godot_packSnorm4x8 #define unpackSnorm4x8 godot_unpackSnorm4x8 /* texture unit usage, N is max_texture_unity-N 1-color correction // In tonemap_inc.glsl 2-radiance 3-directional_shadow 4-positional_shadow 5-screen 6-depth */ #define M_PI 3.14159265359 /* clang-format on */ #define SHADER_IS_SRGB true /* Varyings */ #if defined(COLOR_USED) in vec4 color_interp; #endif #if defined(UV_USED) in vec2 uv_interp; #endif #if defined(UV2_USED) in vec2 uv2_interp; #else #ifdef USE_LIGHTMAP in vec2 uv2_interp; #endif #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) in vec3 tangent_interp; in vec3 binormal_interp; #endif #ifdef NORMAL_USED in vec3 normal_interp; #endif in highp vec3 vertex_interp; #ifdef USE_RADIANCE_MAP #define RADIANCE_MAX_LOD 5.0 uniform samplerCube radiance_map; // texunit:-2 #endif layout(std140) uniform GlobalShaderUniformData { //ubo:1 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS]; }; /* Material Uniforms */ #ifdef MATERIAL_UNIFORMS_USED /* clang-format off */ layout(std140) uniform MaterialUniforms { // ubo:3 }; /* clang-format on */ #endif layout(std140) uniform SceneData { // ubo:2 highp mat4 projection_matrix; highp mat4 inv_projection_matrix; highp mat4 inv_view_matrix; highp mat4 view_matrix; vec2 viewport_size; vec2 screen_pixel_size; mediump vec4 ambient_light_color_energy; mediump float ambient_color_sky_mix; bool material_uv2_mode; float emissive_exposure_normalization; bool use_ambient_light; bool use_ambient_cubemap; bool use_reflection_cubemap; float fog_aerial_perspective; float time; mat3 radiance_inverse_xform; uint directional_light_count; float z_far; float z_near; float IBL_exposure_normalization; bool fog_enabled; float fog_density; float fog_height; float fog_height_density; vec3 fog_light_color; float fog_sun_scatter; uint camera_visible_layers; uint pad3; uint pad4; uint pad5; } scene_data; #ifdef USE_MULTIVIEW layout(std140) uniform MultiviewData { // ubo:8 highp mat4 projection_matrix_view[MAX_VIEWS]; highp mat4 inv_projection_matrix_view[MAX_VIEWS]; highp vec4 eye_offset[MAX_VIEWS]; } multiview_data; #endif /* clang-format off */ /* clang-format on */ // Directional light data. #ifndef DISABLE_LIGHT_DIRECTIONAL struct DirectionalLightData { mediump vec3 direction; mediump float energy; mediump vec3 color; mediump float size; mediump vec3 pad; mediump float specular; }; layout(std140) uniform DirectionalLights { // ubo:7 DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; }; #endif // !DISABLE_LIGHT_DIRECTIONAL // Omni and spot light data. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) struct LightData { // This structure needs to be as packed as possible. highp vec3 position; highp float inv_radius; mediump vec3 direction; highp float size; mediump vec3 color; mediump float attenuation; mediump float cone_attenuation; mediump float cone_angle; mediump float specular_amount; mediump float shadow_opacity; }; #ifndef DISABLE_LIGHT_OMNI layout(std140) uniform OmniLightData { // ubo:5 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint omni_light_indices[MAX_FORWARD_LIGHTS]; uniform uint omni_light_count; #endif #ifndef DISABLE_LIGHT_SPOT layout(std140) uniform SpotLightData { // ubo:6 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint spot_light_indices[MAX_FORWARD_LIGHTS]; uniform uint spot_light_count; #endif #ifdef USE_ADDITIVE_LIGHTING uniform highp samplerCubeShadow positional_shadow; // texunit:-4 #endif #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifdef USE_MULTIVIEW uniform highp sampler2DArray depth_buffer; // texunit:-6 uniform highp sampler2DArray color_buffer; // texunit:-5 vec3 multiview_uv(vec2 uv) { return vec3(uv, ViewIndex); } #else uniform highp sampler2D depth_buffer; // texunit:-6 uniform highp sampler2D color_buffer; // texunit:-5 vec2 multiview_uv(vec2 uv) { return uv; } #endif uniform highp mat4 world_transform; uniform mediump float opaque_prepass_threshold; layout(location = 0) out vec4 frag_color; vec3 F0(float metallic, float specular, vec3 albedo) { float dielectric = 0.16 * specular * specular; // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials; // see https://google.github.io/filament/Filament.md.html return mix(vec3(dielectric), albedo, vec3(metallic)); } #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) float D_GGX(float cos_theta_m, float alpha) { float a = cos_theta_m * alpha; float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a); return k * k * (1.0 / M_PI); } // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX float V_GGX(float NdotL, float NdotV, float alpha) { return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha); } float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { float alpha2 = alpha_x * alpha_y; highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m); highp float v2 = dot(v, v); float w2 = alpha2 / v2; float D = alpha2 * w2 * w2 * (1.0 / M_PI); return D; } float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) { float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV)); float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL)); return 0.5 / (Lambda_V + Lambda_L); } float SchlickFresnel(float u) { float m = 1.0 - u; float m2 = m * m; return m2 * m2 * m; // pow(m,5) } void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_directional, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 B, vec3 T, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { #if defined(USE_LIGHT_SHADER_CODE) // light is written by the light shader highp mat4 model_matrix = world_transform; mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; vec3 normal = N; vec3 light = L; vec3 view = V; /* clang-format off */ /* clang-format on */ #else float NdotL = min(A + dot(N, L), 1.0); float cNdotL = max(NdotL, 0.0); // clamped NdotL float NdotV = dot(N, V); float cNdotV = max(NdotV, 1e-4); #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) vec3 H = normalize(V + L); #endif #if defined(SPECULAR_SCHLICK_GGX) float cNdotH = clamp(A + dot(N, H), 0.0, 1.0); #endif #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) float cLdotH = clamp(A + dot(L, H), 0.0, 1.0); #endif if (metallic < 1.0) { float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance #if defined(DIFFUSE_LAMBERT_WRAP) // Energy conserving lambert wrap shader. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/ diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI); #elif defined(DIFFUSE_TOON) diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI); #elif defined(DIFFUSE_BURLEY) { 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 / M_PI) * FdV * FdL * cNdotL; } #else // Lambert diffuse_brdf_NL = cNdotL * (1.0 / M_PI); #endif diffuse_light += light_color * diffuse_brdf_NL * attenuation; #if defined(LIGHT_BACKLIGHT_USED) diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation; #endif #if defined(LIGHT_RIM_USED) // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0)); diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color; #endif } if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely // D #if defined(SPECULAR_TOON) vec3 R = normalize(-reflect(L, N)); float RdotV = dot(R, V); float mid = 1.0 - roughness; mid *= mid; float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid; diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection #elif defined(SPECULAR_DISABLED) // none.. #elif defined(SPECULAR_SCHLICK_GGX) // shlick+ggx as default float alpha_ggx = roughness * roughness; #if defined(LIGHT_ANISOTROPY_USED) float aspect = sqrt(1.0 - anisotropy * 0.9); float ax = alpha_ggx / aspect; float ay = alpha_ggx * aspect; float XdotH = dot(T, H); float YdotH = dot(B, H); float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH); float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL); #else float D = D_GGX(cNdotH, alpha_ggx); float G = V_GGX(cNdotL, cNdotV, alpha_ggx); #endif // LIGHT_ANISOTROPY_USED // F float cLdotH5 = SchlickFresnel(cLdotH); // Calculate Fresnel using cheap approximate specular occlusion term from Filament: // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion float f90 = clamp(50.0 * f0.g, 0.0, 1.0); vec3 F = f0 + (f90 - f0) * cLdotH5; vec3 specular_brdf_NL = cNdotL * D * F * G; specular_light += specular_brdf_NL * light_color * attenuation * specular_amount; #endif #if defined(LIGHT_CLEARCOAT_USED) // Clearcoat ignores normal_map, use vertex normal instead float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0); float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0); float ccNdotV = max(dot(vertex_normal, V), 1e-4); #if !defined(SPECULAR_SCHLICK_GGX) float cLdotH5 = SchlickFresnel(cLdotH); #endif float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness)); float Gr = 0.25 / (cLdotH * cLdotH); float Fr = mix(.04, 1.0, cLdotH5); float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL; specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount; // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR) // but to do so we need to rearrange this entire function #endif // LIGHT_CLEARCOAT_USED } #ifdef USE_SHADOW_TO_OPACITY alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0)); #endif #endif // USE_LIGHT_SHADER_CODE } float get_omni_spot_attenuation(float distance, float inv_range, float decay) { float nd = distance * inv_range; nd *= nd; nd *= nd; // nd^4 nd = max(1.0 - nd, 0.0); nd *= nd; // nd^2 return nd * pow(max(distance, 0.0001), -decay); } #ifndef DISABLE_LIGHT_OMNI void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = omni_lights[idx].position - vertex; float light_length = length(light_rel_vec); float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation); vec3 color = omni_lights[idx].color; float size_A = 0.0; if (omni_lights[idx].size > 0.0) { float t = omni_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * omni_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = spot_lights[idx].position - vertex; float light_length = length(light_rel_vec); float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation); vec3 spot_dir = spot_lights[idx].direction; float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle); float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle)); spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].cone_attenuation); vec3 color = spot_lights[idx].color; float size_A = 0.0; if (spot_lights[idx].size > 0.0) { float t = spot_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * spot_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifndef MODE_RENDER_DEPTH vec4 fog_process(vec3 vertex) { vec3 fog_color = scene_data.fog_light_color; #ifdef USE_RADIANCE_MAP /* if (scene_data.fog_aerial_perspective > 0.0) { vec3 sky_fog_color = vec3(0.0); vec3 cube_view = scene_data.radiance_inverse_xform * vertex; // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near)); sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb; fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective); } */ #endif #ifndef DISABLE_LIGHT_DIRECTIONAL if (scene_data.fog_sun_scatter > 0.001) { vec4 sun_scatter = vec4(0.0); float sun_total = 0.0; vec3 view = normalize(vertex); for (uint i = uint(0); i < scene_data.directional_light_count; i++) { vec3 light_color = directional_lights[i].color * directional_lights[i].energy; float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); fog_color += light_color * light_amount * scene_data.fog_sun_scatter; } } #endif // !DISABLE_LIGHT_DIRECTIONAL float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density)); if (abs(scene_data.fog_height_density) >= 0.0001) { float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y; float y_dist = y - scene_data.fog_height; float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density)); fog_amount = max(vfog_amount, fog_amount); } return vec4(fog_color, fog_amount); } #endif // !MODE_RENDER_DEPTH void main() { //lay out everything, whatever is unused is optimized away anyway vec3 vertex = vertex_interp; #ifdef USE_MULTIVIEW vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz; vec3 view = -normalize(vertex_interp - eye_offset); mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex]; mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex]; #else vec3 eye_offset = vec3(0.0, 0.0, 0.0); vec3 view = -normalize(vertex_interp); mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; #endif highp mat4 model_matrix = world_transform; vec3 albedo = vec3(1.0); vec3 backlight = vec3(0.0); vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0); float transmittance_depth = 0.0; float transmittance_boost = 0.0; float metallic = 0.0; float specular = 0.5; vec3 emission = vec3(0.0); float roughness = 1.0; float rim = 0.0; float rim_tint = 0.0; float clearcoat = 0.0; float clearcoat_roughness = 0.0; float anisotropy = 0.0; vec2 anisotropy_flow = vec2(1.0, 0.0); vec4 fog = vec4(0.0); #if defined(CUSTOM_RADIANCE_USED) vec4 custom_radiance = vec4(0.0); #endif #if defined(CUSTOM_IRRADIANCE_USED) vec4 custom_irradiance = vec4(0.0); #endif float ao = 1.0; float ao_light_affect = 0.0; float alpha = 1.0; #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) vec3 binormal = normalize(binormal_interp); vec3 tangent = normalize(tangent_interp); #else vec3 binormal = vec3(0.0); vec3 tangent = vec3(0.0); #endif #ifdef NORMAL_USED vec3 normal = normalize(normal_interp); #if defined(DO_SIDE_CHECK) if (!gl_FrontFacing) { normal = -normal; } #endif #endif //NORMAL_USED #ifdef UV_USED vec2 uv = uv_interp; #endif #if defined(UV2_USED) || defined(USE_LIGHTMAP) vec2 uv2 = uv2_interp; #endif #if defined(COLOR_USED) vec4 color = color_interp; #endif #if defined(NORMAL_MAP_USED) vec3 normal_map = vec3(0.5); #endif float normal_map_depth = 1.0; vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size; float sss_strength = 0.0; #ifdef ALPHA_SCISSOR_USED float alpha_scissor_threshold = 1.0; #endif // ALPHA_SCISSOR_USED #ifdef ALPHA_HASH_USED float alpha_hash_scale = 1.0; #endif // ALPHA_HASH_USED #ifdef ALPHA_ANTIALIASING_EDGE_USED float alpha_antialiasing_edge = 0.0; vec2 alpha_texture_coordinate = vec2(0.0, 0.0); #endif // ALPHA_ANTIALIASING_EDGE_USED { } #ifndef USE_SHADOW_TO_OPACITY #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor_threshold) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // !USE_SHADOW_TO_OPACITY #ifdef NORMAL_MAP_USED normal_map.xy = normal_map.xy * 2.0 - 1.0; normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth)); #endif #ifdef LIGHT_ANISOTROPY_USED if (anisotropy > 0.01) { //rotation matrix mat3 rot = mat3(tangent, binormal, normal); //make local to space tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0)); binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0)); } #endif #ifndef MODE_RENDER_DEPTH #ifndef CUSTOM_FOG_USED #ifndef DISABLE_FOG // fog must be processed as early as possible and then packed. // to maximize VGPR usage if (scene_data.fog_enabled) { fog = fog_process(vertex); } #endif // !DISABLE_FOG #endif // !CUSTOM_FOG_USED uint fog_rg = packHalf2x16(fog.rg); uint fog_ba = packHalf2x16(fog.ba); // Convert colors to linear albedo = srgb_to_linear(albedo); emission = srgb_to_linear(emission); // TODO Backlight and transmittance when used #ifndef MODE_UNSHADED vec3 f0 = F0(metallic, specular, albedo); vec3 specular_light = vec3(0.0, 0.0, 0.0); vec3 diffuse_light = vec3(0.0, 0.0, 0.0); vec3 ambient_light = vec3(0.0, 0.0, 0.0); #ifdef BASE_PASS /////////////////////// LIGHTING ////////////////////////////// // IBL precalculations float ndotv = clamp(dot(normal, view), 0.0, 1.0); vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0); #ifdef USE_RADIANCE_MAP if (scene_data.use_reflection_cubemap) { #ifdef LIGHT_ANISOTROPY_USED // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent; vec3 anisotropic_tangent = cross(anisotropic_direction, view); vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction); vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0))); vec3 ref_vec = reflect(-view, bent_normal); #else vec3 ref_vec = reflect(-view, normal); #endif ref_vec = mix(ref_vec, normal, roughness * roughness); float horizon = min(1.0 + dot(ref_vec, normal), 1.0); ref_vec = scene_data.radiance_inverse_xform * ref_vec; specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb; specular_light = srgb_to_linear(specular_light); specular_light *= horizon * horizon; specular_light *= scene_data.ambient_light_color_energy.a; } #endif // Calculate Reflection probes // Calculate Lightmaps #if defined(CUSTOM_RADIANCE_USED) specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a); #endif // CUSTOM_RADIANCE_USED #ifndef USE_LIGHTMAP //lightmap overrides everything if (scene_data.use_ambient_light) { ambient_light = scene_data.ambient_light_color_energy.rgb; #ifdef USE_RADIANCE_MAP if (scene_data.use_ambient_cubemap) { vec3 ambient_dir = scene_data.radiance_inverse_xform * normal; vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb; cubemap_ambient = srgb_to_linear(cubemap_ambient); ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix); } #endif } #endif // USE_LIGHTMAP #if defined(CUSTOM_IRRADIANCE_USED) ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a); #endif // CUSTOM_IRRADIANCE_USED { #if defined(AMBIENT_LIGHT_DISABLED) ambient_light = vec3(0.0, 0.0, 0.0); #else ambient_light *= albedo.rgb; ambient_light *= ao; #endif // AMBIENT_LIGHT_DISABLED } // convert ao to direct light ao ao = mix(1.0, ao, ao_light_affect); { #if defined(DIFFUSE_TOON) //simplify for toon, as specular_light *= specular * metallic * albedo * 2.0; #else // scales the specular reflections, needs to be be computed before lighting happens, // but after environment, GI, and reflection probes are added // Environment brdf approximation (Lazarov 2013) // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); vec4 r = roughness * c0 + c1; float ndotv = clamp(dot(normal, view), 0.0, 1.0); float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y; vec2 env = vec2(-1.04, 1.04) * a004 + r.zw; specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0); #endif } #endif // BASE_PASS #ifndef DISABLE_LIGHT_DIRECTIONAL //diffuse_light = normal; // for (uint i = uint(0); i < scene_data.directional_light_count; i++) { light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, true, 1.0, f0, roughness, metallic, 1.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_DIRECTIONAL #ifndef DISABLE_LIGHT_OMNI for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= omni_light_count) { break; } light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= spot_light_count) { break; } light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED tangent, binormal, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !MODE_UNSHADED #endif // !MODE_RENDER_DEPTH #if defined(USE_SHADOW_TO_OPACITY) alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0)); #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // USE_SHADOW_TO_OPACITY #ifdef MODE_RENDER_DEPTH //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) #else // !MODE_RENDER_DEPTH #ifdef MODE_UNSHADED frag_color = vec4(albedo, alpha); #else diffuse_light *= albedo; diffuse_light *= 1.0 - metallic; ambient_light *= 1.0 - metallic; frag_color = vec4(diffuse_light + specular_light, alpha); #ifdef BASE_PASS frag_color.rgb += emission + ambient_light; #endif #endif //MODE_UNSHADED fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba)); #ifndef DISABLE_FOG if (scene_data.fog_enabled) { #ifdef BASE_PASS frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a); #else frag_color.rgb *= (1.0 - fog.a); #endif // BASE_PASS } #endif // Tonemap before writing as we are writing to an sRGB framebuffer frag_color.rgb *= exposure; frag_color.rgb = apply_tonemapping(frag_color.rgb, white); frag_color.rgb = linear_to_srgb(frag_color.rgb); #ifdef USE_BCS frag_color.rgb = apply_bcs(frag_color.rgb, bcs); #endif #ifdef USE_COLOR_CORRECTION frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction); #endif #endif //!MODE_RENDER_DEPTH } #version 330 #define USE_GLES_OVER_GL #define USE_RADIANCE_MAP #define MAX_GLOBAL_SHADER_UNIFORMS 256 #define MAX_LIGHT_DATA_STRUCTS 32 #define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS 8 #define MAX_FORWARD_LIGHTS uint(8) #define USE_ADDITIVE_LIGHTING #ifdef USE_MULTIVIEW #if defined(GL_OVR_multiview2) #extension GL_OVR_multiview2 : require #elif defined(GL_OVR_multiview) #extension GL_OVR_multiview : require #endif #define ViewIndex gl_ViewID_OVR #define MAX_VIEWS 2 #else #define ViewIndex uint(0) #define MAX_VIEWS 1 #endif precision highp float; precision highp int; // Default to SPECULAR_SCHLICK_GGX. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON) #define SPECULAR_SCHLICK_GGX #endif #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED) #ifndef NORMAL_USED #define NORMAL_USED #endif #endif #ifndef MODE_RENDER_DEPTH #ifdef USE_BCS uniform vec3 bcs; #endif #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT uniform sampler2D source_color_correction; //texunit:-1 #else uniform sampler3D source_color_correction; //texunit:-1 #endif #endif layout(std140) uniform TonemapData { //ubo:0 float exposure; float white; int tonemapper; int pad; }; vec3 apply_bcs(vec3 color, vec3 bcs) { color = mix(vec3(0.0), color, bcs.x); color = mix(vec3(0.5), color, bcs.y); color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z); return color; } #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT vec3 apply_color_correction(vec3 color) { color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r; color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g; color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b; return color; } #else vec3 apply_color_correction(vec3 color) { return textureLod(source_color_correction, color, 0.0).rgb; } #endif #endif vec3 tonemap_filmic(vec3 color, float p_white) { // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) // has no effect on the curve's general shape or visual properties const float exposure_bias = 2.0f; const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance const float B = 0.30f * exposure_bias; const float C = 0.10f; const float D = 0.20f; const float E = 0.01f; const float F = 0.30f; vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F; return color_tonemapped / p_white_tonemapped; } // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl // (MIT License). vec3 tonemap_aces(vec3 color, float p_white) { const float exposure_bias = 1.8f; const float A = 0.0245786f; const float B = 0.000090537f; const float C = 0.983729f; const float D = 0.432951f; const float E = 0.238081f; // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias` const mat3 rgb_to_rrt = mat3( vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias), vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias), vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias)); const mat3 odt_to_rgb = mat3( vec3(1.60475f, -0.53108f, -0.07367f), vec3(-0.10208f, 1.10813f, -0.00605f), vec3(-0.00327f, -0.07276f, 1.07602f)); color *= rgb_to_rrt; vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E); color_tonemapped *= odt_to_rgb; p_white *= exposure_bias; float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E); return color_tonemapped / p_white_tonemapped; } vec3 tonemap_reinhard(vec3 color, float p_white) { return (p_white * color + color) / (color * p_white + p_white); } // This expects 0-1 range input. vec3 linear_to_srgb(vec3 color) { //color = clamp(color, vec3(0.0), vec3(1.0)); //const vec3 a = vec3(0.055f); //return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0)); } // This expects 0-1 range input, outside that range it behaves poorly. vec3 srgb_to_linear(vec3 color) { // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878); } #define TONEMAPPER_LINEAR 0 #define TONEMAPPER_REINHARD 1 #define TONEMAPPER_FILMIC 2 #define TONEMAPPER_ACES 3 vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color // Ensure color values passed to tonemappers are positive. // They can be negative in the case of negative lights, which leads to undesired behavior. if (tonemapper == TONEMAPPER_LINEAR) { return color; } else if (tonemapper == TONEMAPPER_REINHARD) { return tonemap_reinhard(max(vec3(0.0f), color), p_white); } else if (tonemapper == TONEMAPPER_FILMIC) { return tonemap_filmic(max(vec3(0.0f), color), p_white); } else { // TONEMAPPER_ACES return tonemap_aces(max(vec3(0.0f), color), p_white); } } #endif #ifdef USE_GLES_OVER_GL // Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile. uint float2half(uint f) { uint e = f & uint(0x7f800000); if (e <= uint(0x38000000)) { return uint(0); } else { return ((f >> uint(16)) & uint(0x8000)) | (((e - uint(0x38000000)) >> uint(13)) & uint(0x7c00)) | ((f >> uint(13)) & uint(0x03ff)); } } uint half2float(uint h) { uint h_e = h & uint(0x7c00); return ((h & uint(0x8000)) << uint(16)) | uint((h_e >> uint(10)) != uint(0)) * (((h_e + uint(0x1c000)) << uint(13)) | ((h & uint(0x03ff)) << uint(13))); } uint packHalf2x16(vec2 v) { return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16); } vec2 unpackHalf2x16(uint v) { return vec2(uintBitsToFloat(half2float(v & uint(0xffff))), uintBitsToFloat(half2float(v >> uint(16)))); } uint packUnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0)); return uv.x | uv.y << uint(16); } vec2 unpackUnorm2x16(uint p) { return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization } uint packSnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0); return uv.x | uv.y << uint(16); } vec2 unpackSnorm2x16(uint p) { vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16))); return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0)); } #endif // Compatibility renames. These are exposed with the "godot_" prefix // to work around an Adreno bug which was exposing these ES310 functions // in ES300 shaders. Internally, we must use the "godot_" prefix, but user shaders // will be mapped automatically. uint godot_packUnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0)); return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24)); } vec4 godot_unpackUnorm4x8(uint p) { return vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))) * 0.00392156862; // 1.0 / 255.0 } uint godot_packSnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0); return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24); } vec4 godot_unpackSnorm4x8(uint p) { vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))); return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0)); } #define packUnorm4x8 godot_packUnorm4x8 #define unpackUnorm4x8 godot_unpackUnorm4x8 #define packSnorm4x8 godot_packSnorm4x8 #define unpackSnorm4x8 godot_unpackSnorm4x8 /* texture unit usage, N is max_texture_unity-N 1-color correction // In tonemap_inc.glsl 2-radiance 3-directional_shadow 4-positional_shadow 5-screen 6-depth */ #define M_PI 3.14159265359 /* clang-format on */ #define SHADER_IS_SRGB true /* Varyings */ #if defined(COLOR_USED) in vec4 color_interp; #endif #if defined(UV_USED) in vec2 uv_interp; #endif #if defined(UV2_USED) in vec2 uv2_interp; #else #ifdef USE_LIGHTMAP in vec2 uv2_interp; #endif #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) in vec3 tangent_interp; in vec3 binormal_interp; #endif #ifdef NORMAL_USED in vec3 normal_interp; #endif in highp vec3 vertex_interp; #ifdef USE_RADIANCE_MAP #define RADIANCE_MAX_LOD 5.0 uniform samplerCube radiance_map; // texunit:-2 #endif layout(std140) uniform GlobalShaderUniformData { //ubo:1 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS]; }; /* Material Uniforms */ #ifdef MATERIAL_UNIFORMS_USED /* clang-format off */ layout(std140) uniform MaterialUniforms { // ubo:3 }; /* clang-format on */ #endif layout(std140) uniform SceneData { // ubo:2 highp mat4 projection_matrix; highp mat4 inv_projection_matrix; highp mat4 inv_view_matrix; highp mat4 view_matrix; vec2 viewport_size; vec2 screen_pixel_size; mediump vec4 ambient_light_color_energy; mediump float ambient_color_sky_mix; bool material_uv2_mode; float emissive_exposure_normalization; bool use_ambient_light; bool use_ambient_cubemap; bool use_reflection_cubemap; float fog_aerial_perspective; float time; mat3 radiance_inverse_xform; uint directional_light_count; float z_far; float z_near; float IBL_exposure_normalization; bool fog_enabled; float fog_density; float fog_height; float fog_height_density; vec3 fog_light_color; float fog_sun_scatter; uint camera_visible_layers; uint pad3; uint pad4; uint pad5; } scene_data; #ifdef USE_MULTIVIEW layout(std140) uniform MultiviewData { // ubo:8 highp mat4 projection_matrix_view[MAX_VIEWS]; highp mat4 inv_projection_matrix_view[MAX_VIEWS]; highp vec4 eye_offset[MAX_VIEWS]; } multiview_data; #endif /* clang-format off */ /* clang-format on */ // Directional light data. #ifndef DISABLE_LIGHT_DIRECTIONAL struct DirectionalLightData { mediump vec3 direction; mediump float energy; mediump vec3 color; mediump float size; mediump vec3 pad; mediump float specular; }; layout(std140) uniform DirectionalLights { // ubo:7 DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; }; #endif // !DISABLE_LIGHT_DIRECTIONAL // Omni and spot light data. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) struct LightData { // This structure needs to be as packed as possible. highp vec3 position; highp float inv_radius; mediump vec3 direction; highp float size; mediump vec3 color; mediump float attenuation; mediump float cone_attenuation; mediump float cone_angle; mediump float specular_amount; mediump float shadow_opacity; }; #ifndef DISABLE_LIGHT_OMNI layout(std140) uniform OmniLightData { // ubo:5 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint omni_light_indices[MAX_FORWARD_LIGHTS]; uniform uint omni_light_count; #endif #ifndef DISABLE_LIGHT_SPOT layout(std140) uniform SpotLightData { // ubo:6 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint spot_light_indices[MAX_FORWARD_LIGHTS]; uniform uint spot_light_count; #endif #ifdef USE_ADDITIVE_LIGHTING uniform highp samplerCubeShadow positional_shadow; // texunit:-4 #endif #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifdef USE_MULTIVIEW uniform highp sampler2DArray depth_buffer; // texunit:-6 uniform highp sampler2DArray color_buffer; // texunit:-5 vec3 multiview_uv(vec2 uv) { return vec3(uv, ViewIndex); } #else uniform highp sampler2D depth_buffer; // texunit:-6 uniform highp sampler2D color_buffer; // texunit:-5 vec2 multiview_uv(vec2 uv) { return uv; } #endif uniform highp mat4 world_transform; uniform mediump float opaque_prepass_threshold; layout(location = 0) out vec4 frag_color; vec3 F0(float metallic, float specular, vec3 albedo) { float dielectric = 0.16 * specular * specular; // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials; // see https://google.github.io/filament/Filament.md.html return mix(vec3(dielectric), albedo, vec3(metallic)); } #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) float D_GGX(float cos_theta_m, float alpha) { float a = cos_theta_m * alpha; float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a); return k * k * (1.0 / M_PI); } // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX float V_GGX(float NdotL, float NdotV, float alpha) { return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha); } float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { float alpha2 = alpha_x * alpha_y; highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m); highp float v2 = dot(v, v); float w2 = alpha2 / v2; float D = alpha2 * w2 * w2 * (1.0 / M_PI); return D; } float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) { float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV)); float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL)); return 0.5 / (Lambda_V + Lambda_L); } float SchlickFresnel(float u) { float m = 1.0 - u; float m2 = m * m; return m2 * m2 * m; // pow(m,5) } void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_directional, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 B, vec3 T, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { #if defined(USE_LIGHT_SHADER_CODE) // light is written by the light shader highp mat4 model_matrix = world_transform; mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; vec3 normal = N; vec3 light = L; vec3 view = V; /* clang-format off */ /* clang-format on */ #else float NdotL = min(A + dot(N, L), 1.0); float cNdotL = max(NdotL, 0.0); // clamped NdotL float NdotV = dot(N, V); float cNdotV = max(NdotV, 1e-4); #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) vec3 H = normalize(V + L); #endif #if defined(SPECULAR_SCHLICK_GGX) float cNdotH = clamp(A + dot(N, H), 0.0, 1.0); #endif #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) float cLdotH = clamp(A + dot(L, H), 0.0, 1.0); #endif if (metallic < 1.0) { float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance #if defined(DIFFUSE_LAMBERT_WRAP) // Energy conserving lambert wrap shader. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/ diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI); #elif defined(DIFFUSE_TOON) diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI); #elif defined(DIFFUSE_BURLEY) { 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 / M_PI) * FdV * FdL * cNdotL; } #else // Lambert diffuse_brdf_NL = cNdotL * (1.0 / M_PI); #endif diffuse_light += light_color * diffuse_brdf_NL * attenuation; #if defined(LIGHT_BACKLIGHT_USED) diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation; #endif #if defined(LIGHT_RIM_USED) // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0)); diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color; #endif } if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely // D #if defined(SPECULAR_TOON) vec3 R = normalize(-reflect(L, N)); float RdotV = dot(R, V); float mid = 1.0 - roughness; mid *= mid; float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid; diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection #elif defined(SPECULAR_DISABLED) // none.. #elif defined(SPECULAR_SCHLICK_GGX) // shlick+ggx as default float alpha_ggx = roughness * roughness; #if defined(LIGHT_ANISOTROPY_USED) float aspect = sqrt(1.0 - anisotropy * 0.9); float ax = alpha_ggx / aspect; float ay = alpha_ggx * aspect; float XdotH = dot(T, H); float YdotH = dot(B, H); float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH); float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL); #else float D = D_GGX(cNdotH, alpha_ggx); float G = V_GGX(cNdotL, cNdotV, alpha_ggx); #endif // LIGHT_ANISOTROPY_USED // F float cLdotH5 = SchlickFresnel(cLdotH); // Calculate Fresnel using cheap approximate specular occlusion term from Filament: // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion float f90 = clamp(50.0 * f0.g, 0.0, 1.0); vec3 F = f0 + (f90 - f0) * cLdotH5; vec3 specular_brdf_NL = cNdotL * D * F * G; specular_light += specular_brdf_NL * light_color * attenuation * specular_amount; #endif #if defined(LIGHT_CLEARCOAT_USED) // Clearcoat ignores normal_map, use vertex normal instead float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0); float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0); float ccNdotV = max(dot(vertex_normal, V), 1e-4); #if !defined(SPECULAR_SCHLICK_GGX) float cLdotH5 = SchlickFresnel(cLdotH); #endif float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness)); float Gr = 0.25 / (cLdotH * cLdotH); float Fr = mix(.04, 1.0, cLdotH5); float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL; specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount; // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR) // but to do so we need to rearrange this entire function #endif // LIGHT_CLEARCOAT_USED } #ifdef USE_SHADOW_TO_OPACITY alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0)); #endif #endif // USE_LIGHT_SHADER_CODE } float get_omni_spot_attenuation(float distance, float inv_range, float decay) { float nd = distance * inv_range; nd *= nd; nd *= nd; // nd^4 nd = max(1.0 - nd, 0.0); nd *= nd; // nd^2 return nd * pow(max(distance, 0.0001), -decay); } #ifndef DISABLE_LIGHT_OMNI void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = omni_lights[idx].position - vertex; float light_length = length(light_rel_vec); float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation); vec3 color = omni_lights[idx].color; float size_A = 0.0; if (omni_lights[idx].size > 0.0) { float t = omni_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * omni_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = spot_lights[idx].position - vertex; float light_length = length(light_rel_vec); float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation); vec3 spot_dir = spot_lights[idx].direction; float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle); float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle)); spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].cone_attenuation); vec3 color = spot_lights[idx].color; float size_A = 0.0; if (spot_lights[idx].size > 0.0) { float t = spot_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * spot_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifndef MODE_RENDER_DEPTH vec4 fog_process(vec3 vertex) { vec3 fog_color = scene_data.fog_light_color; #ifdef USE_RADIANCE_MAP /* if (scene_data.fog_aerial_perspective > 0.0) { vec3 sky_fog_color = vec3(0.0); vec3 cube_view = scene_data.radiance_inverse_xform * vertex; // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near)); sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb; fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective); } */ #endif #ifndef DISABLE_LIGHT_DIRECTIONAL if (scene_data.fog_sun_scatter > 0.001) { vec4 sun_scatter = vec4(0.0); float sun_total = 0.0; vec3 view = normalize(vertex); for (uint i = uint(0); i < scene_data.directional_light_count; i++) { vec3 light_color = directional_lights[i].color * directional_lights[i].energy; float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); fog_color += light_color * light_amount * scene_data.fog_sun_scatter; } } #endif // !DISABLE_LIGHT_DIRECTIONAL float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density)); if (abs(scene_data.fog_height_density) >= 0.0001) { float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y; float y_dist = y - scene_data.fog_height; float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density)); fog_amount = max(vfog_amount, fog_amount); } return vec4(fog_color, fog_amount); } #endif // !MODE_RENDER_DEPTH void main() { //lay out everything, whatever is unused is optimized away anyway vec3 vertex = vertex_interp; #ifdef USE_MULTIVIEW vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz; vec3 view = -normalize(vertex_interp - eye_offset); mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex]; mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex]; #else vec3 eye_offset = vec3(0.0, 0.0, 0.0); vec3 view = -normalize(vertex_interp); mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; #endif highp mat4 model_matrix = world_transform; vec3 albedo = vec3(1.0); vec3 backlight = vec3(0.0); vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0); float transmittance_depth = 0.0; float transmittance_boost = 0.0; float metallic = 0.0; float specular = 0.5; vec3 emission = vec3(0.0); float roughness = 1.0; float rim = 0.0; float rim_tint = 0.0; float clearcoat = 0.0; float clearcoat_roughness = 0.0; float anisotropy = 0.0; vec2 anisotropy_flow = vec2(1.0, 0.0); vec4 fog = vec4(0.0); #if defined(CUSTOM_RADIANCE_USED) vec4 custom_radiance = vec4(0.0); #endif #if defined(CUSTOM_IRRADIANCE_USED) vec4 custom_irradiance = vec4(0.0); #endif float ao = 1.0; float ao_light_affect = 0.0; float alpha = 1.0; #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) vec3 binormal = normalize(binormal_interp); vec3 tangent = normalize(tangent_interp); #else vec3 binormal = vec3(0.0); vec3 tangent = vec3(0.0); #endif #ifdef NORMAL_USED vec3 normal = normalize(normal_interp); #if defined(DO_SIDE_CHECK) if (!gl_FrontFacing) { normal = -normal; } #endif #endif //NORMAL_USED #ifdef UV_USED vec2 uv = uv_interp; #endif #if defined(UV2_USED) || defined(USE_LIGHTMAP) vec2 uv2 = uv2_interp; #endif #if defined(COLOR_USED) vec4 color = color_interp; #endif #if defined(NORMAL_MAP_USED) vec3 normal_map = vec3(0.5); #endif float normal_map_depth = 1.0; vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size; float sss_strength = 0.0; #ifdef ALPHA_SCISSOR_USED float alpha_scissor_threshold = 1.0; #endif // ALPHA_SCISSOR_USED #ifdef ALPHA_HASH_USED float alpha_hash_scale = 1.0; #endif // ALPHA_HASH_USED #ifdef ALPHA_ANTIALIASING_EDGE_USED float alpha_antialiasing_edge = 0.0; vec2 alpha_texture_coordinate = vec2(0.0, 0.0); #endif // ALPHA_ANTIALIASING_EDGE_USED { } #ifndef USE_SHADOW_TO_OPACITY #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor_threshold) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // !USE_SHADOW_TO_OPACITY #ifdef NORMAL_MAP_USED normal_map.xy = normal_map.xy * 2.0 - 1.0; normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth)); #endif #ifdef LIGHT_ANISOTROPY_USED if (anisotropy > 0.01) { //rotation matrix mat3 rot = mat3(tangent, binormal, normal); //make local to space tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0)); binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0)); } #endif #ifndef MODE_RENDER_DEPTH #ifndef CUSTOM_FOG_USED #ifndef DISABLE_FOG // fog must be processed as early as possible and then packed. // to maximize VGPR usage if (scene_data.fog_enabled) { fog = fog_process(vertex); } #endif // !DISABLE_FOG #endif // !CUSTOM_FOG_USED uint fog_rg = packHalf2x16(fog.rg); uint fog_ba = packHalf2x16(fog.ba); // Convert colors to linear albedo = srgb_to_linear(albedo); emission = srgb_to_linear(emission); // TODO Backlight and transmittance when used #ifndef MODE_UNSHADED vec3 f0 = F0(metallic, specular, albedo); vec3 specular_light = vec3(0.0, 0.0, 0.0); vec3 diffuse_light = vec3(0.0, 0.0, 0.0); vec3 ambient_light = vec3(0.0, 0.0, 0.0); #ifdef BASE_PASS /////////////////////// LIGHTING ////////////////////////////// // IBL precalculations float ndotv = clamp(dot(normal, view), 0.0, 1.0); vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0); #ifdef USE_RADIANCE_MAP if (scene_data.use_reflection_cubemap) { #ifdef LIGHT_ANISOTROPY_USED // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent; vec3 anisotropic_tangent = cross(anisotropic_direction, view); vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction); vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0))); vec3 ref_vec = reflect(-view, bent_normal); #else vec3 ref_vec = reflect(-view, normal); #endif ref_vec = mix(ref_vec, normal, roughness * roughness); float horizon = min(1.0 + dot(ref_vec, normal), 1.0); ref_vec = scene_data.radiance_inverse_xform * ref_vec; specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb; specular_light = srgb_to_linear(specular_light); specular_light *= horizon * horizon; specular_light *= scene_data.ambient_light_color_energy.a; } #endif // Calculate Reflection probes // Calculate Lightmaps #if defined(CUSTOM_RADIANCE_USED) specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a); #endif // CUSTOM_RADIANCE_USED #ifndef USE_LIGHTMAP //lightmap overrides everything if (scene_data.use_ambient_light) { ambient_light = scene_data.ambient_light_color_energy.rgb; #ifdef USE_RADIANCE_MAP if (scene_data.use_ambient_cubemap) { vec3 ambient_dir = scene_data.radiance_inverse_xform * normal; vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb; cubemap_ambient = srgb_to_linear(cubemap_ambient); ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix); } #endif } #endif // USE_LIGHTMAP #if defined(CUSTOM_IRRADIANCE_USED) ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a); #endif // CUSTOM_IRRADIANCE_USED { #if defined(AMBIENT_LIGHT_DISABLED) ambient_light = vec3(0.0, 0.0, 0.0); #else ambient_light *= albedo.rgb; ambient_light *= ao; #endif // AMBIENT_LIGHT_DISABLED } // convert ao to direct light ao ao = mix(1.0, ao, ao_light_affect); { #if defined(DIFFUSE_TOON) //simplify for toon, as specular_light *= specular * metallic * albedo * 2.0; #else // scales the specular reflections, needs to be be computed before lighting happens, // but after environment, GI, and reflection probes are added // Environment brdf approximation (Lazarov 2013) // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); vec4 r = roughness * c0 + c1; float ndotv = clamp(dot(normal, view), 0.0, 1.0); float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y; vec2 env = vec2(-1.04, 1.04) * a004 + r.zw; specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0); #endif } #endif // BASE_PASS #ifndef DISABLE_LIGHT_DIRECTIONAL //diffuse_light = normal; // for (uint i = uint(0); i < scene_data.directional_light_count; i++) { light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, true, 1.0, f0, roughness, metallic, 1.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_DIRECTIONAL #ifndef DISABLE_LIGHT_OMNI for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= omni_light_count) { break; } light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= spot_light_count) { break; } light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED tangent, binormal, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !MODE_UNSHADED #endif // !MODE_RENDER_DEPTH #if defined(USE_SHADOW_TO_OPACITY) alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0)); #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // USE_SHADOW_TO_OPACITY #ifdef MODE_RENDER_DEPTH //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) #else // !MODE_RENDER_DEPTH #ifdef MODE_UNSHADED frag_color = vec4(albedo, alpha); #else diffuse_light *= albedo; diffuse_light *= 1.0 - metallic; ambient_light *= 1.0 - metallic; frag_color = vec4(diffuse_light + specular_light, alpha); #ifdef BASE_PASS frag_color.rgb += emission + ambient_light; #endif #endif //MODE_UNSHADED fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba)); #ifndef DISABLE_FOG if (scene_data.fog_enabled) { #ifdef BASE_PASS frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a); #else frag_color.rgb *= (1.0 - fog.a); #endif // BASE_PASS } #endif // Tonemap before writing as we are writing to an sRGB framebuffer frag_color.rgb *= exposure; frag_color.rgb = apply_tonemapping(frag_color.rgb, white); frag_color.rgb = linear_to_srgb(frag_color.rgb); #ifdef USE_BCS frag_color.rgb = apply_bcs(frag_color.rgb, bcs); #endif #ifdef USE_COLOR_CORRECTION frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction); #endif #endif //!MODE_RENDER_DEPTH } #version 330 #define USE_GLES_OVER_GL #define USE_RADIANCE_MAP #define MAX_GLOBAL_SHADER_UNIFORMS 256 #define MAX_LIGHT_DATA_STRUCTS 32 #define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS 8 #define MAX_FORWARD_LIGHTS uint(8) #define USE_ADDITIVE_LIGHTING #define USE_INSTANCING #ifdef USE_MULTIVIEW #if defined(GL_OVR_multiview2) #extension GL_OVR_multiview2 : require #elif defined(GL_OVR_multiview) #extension GL_OVR_multiview : require #endif #define ViewIndex gl_ViewID_OVR #define MAX_VIEWS 2 #else #define ViewIndex uint(0) #define MAX_VIEWS 1 #endif precision highp float; precision highp int; // Default to SPECULAR_SCHLICK_GGX. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON) #define SPECULAR_SCHLICK_GGX #endif #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED) #ifndef NORMAL_USED #define NORMAL_USED #endif #endif #ifndef MODE_RENDER_DEPTH #ifdef USE_BCS uniform vec3 bcs; #endif #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT uniform sampler2D source_color_correction; //texunit:-1 #else uniform sampler3D source_color_correction; //texunit:-1 #endif #endif layout(std140) uniform TonemapData { //ubo:0 float exposure; float white; int tonemapper; int pad; }; vec3 apply_bcs(vec3 color, vec3 bcs) { color = mix(vec3(0.0), color, bcs.x); color = mix(vec3(0.5), color, bcs.y); color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z); return color; } #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT vec3 apply_color_correction(vec3 color) { color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r; color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g; color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b; return color; } #else vec3 apply_color_correction(vec3 color) { return textureLod(source_color_correction, color, 0.0).rgb; } #endif #endif vec3 tonemap_filmic(vec3 color, float p_white) { // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) // has no effect on the curve's general shape or visual properties const float exposure_bias = 2.0f; const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance const float B = 0.30f * exposure_bias; const float C = 0.10f; const float D = 0.20f; const float E = 0.01f; const float F = 0.30f; vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F; return color_tonemapped / p_white_tonemapped; } // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl // (MIT License). vec3 tonemap_aces(vec3 color, float p_white) { const float exposure_bias = 1.8f; const float A = 0.0245786f; const float B = 0.000090537f; const float C = 0.983729f; const float D = 0.432951f; const float E = 0.238081f; // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias` const mat3 rgb_to_rrt = mat3( vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias), vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias), vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias)); const mat3 odt_to_rgb = mat3( vec3(1.60475f, -0.53108f, -0.07367f), vec3(-0.10208f, 1.10813f, -0.00605f), vec3(-0.00327f, -0.07276f, 1.07602f)); color *= rgb_to_rrt; vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E); color_tonemapped *= odt_to_rgb; p_white *= exposure_bias; float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E); return color_tonemapped / p_white_tonemapped; } vec3 tonemap_reinhard(vec3 color, float p_white) { return (p_white * color + color) / (color * p_white + p_white); } // This expects 0-1 range input. vec3 linear_to_srgb(vec3 color) { //color = clamp(color, vec3(0.0), vec3(1.0)); //const vec3 a = vec3(0.055f); //return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0)); } // This expects 0-1 range input, outside that range it behaves poorly. vec3 srgb_to_linear(vec3 color) { // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878); } #define TONEMAPPER_LINEAR 0 #define TONEMAPPER_REINHARD 1 #define TONEMAPPER_FILMIC 2 #define TONEMAPPER_ACES 3 vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color // Ensure color values passed to tonemappers are positive. // They can be negative in the case of negative lights, which leads to undesired behavior. if (tonemapper == TONEMAPPER_LINEAR) { return color; } else if (tonemapper == TONEMAPPER_REINHARD) { return tonemap_reinhard(max(vec3(0.0f), color), p_white); } else if (tonemapper == TONEMAPPER_FILMIC) { return tonemap_filmic(max(vec3(0.0f), color), p_white); } else { // TONEMAPPER_ACES return tonemap_aces(max(vec3(0.0f), color), p_white); } } #endif #ifdef USE_GLES_OVER_GL // Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile. uint float2half(uint f) { uint e = f & uint(0x7f800000); if (e <= uint(0x38000000)) { return uint(0); } else { return ((f >> uint(16)) & uint(0x8000)) | (((e - uint(0x38000000)) >> uint(13)) & uint(0x7c00)) | ((f >> uint(13)) & uint(0x03ff)); } } uint half2float(uint h) { uint h_e = h & uint(0x7c00); return ((h & uint(0x8000)) << uint(16)) | uint((h_e >> uint(10)) != uint(0)) * (((h_e + uint(0x1c000)) << uint(13)) | ((h & uint(0x03ff)) << uint(13))); } uint packHalf2x16(vec2 v) { return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16); } vec2 unpackHalf2x16(uint v) { return vec2(uintBitsToFloat(half2float(v & uint(0xffff))), uintBitsToFloat(half2float(v >> uint(16)))); } uint packUnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0)); return uv.x | uv.y << uint(16); } vec2 unpackUnorm2x16(uint p) { return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization } uint packSnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0); return uv.x | uv.y << uint(16); } vec2 unpackSnorm2x16(uint p) { vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16))); return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0)); } #endif // Compatibility renames. These are exposed with the "godot_" prefix // to work around an Adreno bug which was exposing these ES310 functions // in ES300 shaders. Internally, we must use the "godot_" prefix, but user shaders // will be mapped automatically. uint godot_packUnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0)); return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24)); } vec4 godot_unpackUnorm4x8(uint p) { return vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))) * 0.00392156862; // 1.0 / 255.0 } uint godot_packSnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0); return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24); } vec4 godot_unpackSnorm4x8(uint p) { vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))); return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0)); } #define packUnorm4x8 godot_packUnorm4x8 #define unpackUnorm4x8 godot_unpackUnorm4x8 #define packSnorm4x8 godot_packSnorm4x8 #define unpackSnorm4x8 godot_unpackSnorm4x8 /* texture unit usage, N is max_texture_unity-N 1-color correction // In tonemap_inc.glsl 2-radiance 3-directional_shadow 4-positional_shadow 5-screen 6-depth */ #define M_PI 3.14159265359 /* clang-format on */ #define SHADER_IS_SRGB true /* Varyings */ #if defined(COLOR_USED) in vec4 color_interp; #endif #if defined(UV_USED) in vec2 uv_interp; #endif #if defined(UV2_USED) in vec2 uv2_interp; #else #ifdef USE_LIGHTMAP in vec2 uv2_interp; #endif #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) in vec3 tangent_interp; in vec3 binormal_interp; #endif #ifdef NORMAL_USED in vec3 normal_interp; #endif in highp vec3 vertex_interp; #ifdef USE_RADIANCE_MAP #define RADIANCE_MAX_LOD 5.0 uniform samplerCube radiance_map; // texunit:-2 #endif layout(std140) uniform GlobalShaderUniformData { //ubo:1 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS]; }; /* Material Uniforms */ #ifdef MATERIAL_UNIFORMS_USED /* clang-format off */ layout(std140) uniform MaterialUniforms { // ubo:3 }; /* clang-format on */ #endif layout(std140) uniform SceneData { // ubo:2 highp mat4 projection_matrix; highp mat4 inv_projection_matrix; highp mat4 inv_view_matrix; highp mat4 view_matrix; vec2 viewport_size; vec2 screen_pixel_size; mediump vec4 ambient_light_color_energy; mediump float ambient_color_sky_mix; bool material_uv2_mode; float emissive_exposure_normalization; bool use_ambient_light; bool use_ambient_cubemap; bool use_reflection_cubemap; float fog_aerial_perspective; float time; mat3 radiance_inverse_xform; uint directional_light_count; float z_far; float z_near; float IBL_exposure_normalization; bool fog_enabled; float fog_density; float fog_height; float fog_height_density; vec3 fog_light_color; float fog_sun_scatter; uint camera_visible_layers; uint pad3; uint pad4; uint pad5; } scene_data; #ifdef USE_MULTIVIEW layout(std140) uniform MultiviewData { // ubo:8 highp mat4 projection_matrix_view[MAX_VIEWS]; highp mat4 inv_projection_matrix_view[MAX_VIEWS]; highp vec4 eye_offset[MAX_VIEWS]; } multiview_data; #endif /* clang-format off */ /* clang-format on */ // Directional light data. #ifndef DISABLE_LIGHT_DIRECTIONAL struct DirectionalLightData { mediump vec3 direction; mediump float energy; mediump vec3 color; mediump float size; mediump vec3 pad; mediump float specular; }; layout(std140) uniform DirectionalLights { // ubo:7 DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; }; #endif // !DISABLE_LIGHT_DIRECTIONAL // Omni and spot light data. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) struct LightData { // This structure needs to be as packed as possible. highp vec3 position; highp float inv_radius; mediump vec3 direction; highp float size; mediump vec3 color; mediump float attenuation; mediump float cone_attenuation; mediump float cone_angle; mediump float specular_amount; mediump float shadow_opacity; }; #ifndef DISABLE_LIGHT_OMNI layout(std140) uniform OmniLightData { // ubo:5 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint omni_light_indices[MAX_FORWARD_LIGHTS]; uniform uint omni_light_count; #endif #ifndef DISABLE_LIGHT_SPOT layout(std140) uniform SpotLightData { // ubo:6 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint spot_light_indices[MAX_FORWARD_LIGHTS]; uniform uint spot_light_count; #endif #ifdef USE_ADDITIVE_LIGHTING uniform highp samplerCubeShadow positional_shadow; // texunit:-4 #endif #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifdef USE_MULTIVIEW uniform highp sampler2DArray depth_buffer; // texunit:-6 uniform highp sampler2DArray color_buffer; // texunit:-5 vec3 multiview_uv(vec2 uv) { return vec3(uv, ViewIndex); } #else uniform highp sampler2D depth_buffer; // texunit:-6 uniform highp sampler2D color_buffer; // texunit:-5 vec2 multiview_uv(vec2 uv) { return uv; } #endif uniform highp mat4 world_transform; uniform mediump float opaque_prepass_threshold; layout(location = 0) out vec4 frag_color; vec3 F0(float metallic, float specular, vec3 albedo) { float dielectric = 0.16 * specular * specular; // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials; // see https://google.github.io/filament/Filament.md.html return mix(vec3(dielectric), albedo, vec3(metallic)); } #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) float D_GGX(float cos_theta_m, float alpha) { float a = cos_theta_m * alpha; float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a); return k * k * (1.0 / M_PI); } // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX float V_GGX(float NdotL, float NdotV, float alpha) { return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha); } float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { float alpha2 = alpha_x * alpha_y; highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m); highp float v2 = dot(v, v); float w2 = alpha2 / v2; float D = alpha2 * w2 * w2 * (1.0 / M_PI); return D; } float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) { float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV)); float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL)); return 0.5 / (Lambda_V + Lambda_L); } float SchlickFresnel(float u) { float m = 1.0 - u; float m2 = m * m; return m2 * m2 * m; // pow(m,5) } void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_directional, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 B, vec3 T, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { #if defined(USE_LIGHT_SHADER_CODE) // light is written by the light shader highp mat4 model_matrix = world_transform; mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; vec3 normal = N; vec3 light = L; vec3 view = V; /* clang-format off */ /* clang-format on */ #else float NdotL = min(A + dot(N, L), 1.0); float cNdotL = max(NdotL, 0.0); // clamped NdotL float NdotV = dot(N, V); float cNdotV = max(NdotV, 1e-4); #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) vec3 H = normalize(V + L); #endif #if defined(SPECULAR_SCHLICK_GGX) float cNdotH = clamp(A + dot(N, H), 0.0, 1.0); #endif #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) float cLdotH = clamp(A + dot(L, H), 0.0, 1.0); #endif if (metallic < 1.0) { float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance #if defined(DIFFUSE_LAMBERT_WRAP) // Energy conserving lambert wrap shader. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/ diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI); #elif defined(DIFFUSE_TOON) diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI); #elif defined(DIFFUSE_BURLEY) { 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 / M_PI) * FdV * FdL * cNdotL; } #else // Lambert diffuse_brdf_NL = cNdotL * (1.0 / M_PI); #endif diffuse_light += light_color * diffuse_brdf_NL * attenuation; #if defined(LIGHT_BACKLIGHT_USED) diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation; #endif #if defined(LIGHT_RIM_USED) // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0)); diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color; #endif } if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely // D #if defined(SPECULAR_TOON) vec3 R = normalize(-reflect(L, N)); float RdotV = dot(R, V); float mid = 1.0 - roughness; mid *= mid; float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid; diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection #elif defined(SPECULAR_DISABLED) // none.. #elif defined(SPECULAR_SCHLICK_GGX) // shlick+ggx as default float alpha_ggx = roughness * roughness; #if defined(LIGHT_ANISOTROPY_USED) float aspect = sqrt(1.0 - anisotropy * 0.9); float ax = alpha_ggx / aspect; float ay = alpha_ggx * aspect; float XdotH = dot(T, H); float YdotH = dot(B, H); float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH); float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL); #else float D = D_GGX(cNdotH, alpha_ggx); float G = V_GGX(cNdotL, cNdotV, alpha_ggx); #endif // LIGHT_ANISOTROPY_USED // F float cLdotH5 = SchlickFresnel(cLdotH); // Calculate Fresnel using cheap approximate specular occlusion term from Filament: // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion float f90 = clamp(50.0 * f0.g, 0.0, 1.0); vec3 F = f0 + (f90 - f0) * cLdotH5; vec3 specular_brdf_NL = cNdotL * D * F * G; specular_light += specular_brdf_NL * light_color * attenuation * specular_amount; #endif #if defined(LIGHT_CLEARCOAT_USED) // Clearcoat ignores normal_map, use vertex normal instead float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0); float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0); float ccNdotV = max(dot(vertex_normal, V), 1e-4); #if !defined(SPECULAR_SCHLICK_GGX) float cLdotH5 = SchlickFresnel(cLdotH); #endif float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness)); float Gr = 0.25 / (cLdotH * cLdotH); float Fr = mix(.04, 1.0, cLdotH5); float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL; specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount; // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR) // but to do so we need to rearrange this entire function #endif // LIGHT_CLEARCOAT_USED } #ifdef USE_SHADOW_TO_OPACITY alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0)); #endif #endif // USE_LIGHT_SHADER_CODE } float get_omni_spot_attenuation(float distance, float inv_range, float decay) { float nd = distance * inv_range; nd *= nd; nd *= nd; // nd^4 nd = max(1.0 - nd, 0.0); nd *= nd; // nd^2 return nd * pow(max(distance, 0.0001), -decay); } #ifndef DISABLE_LIGHT_OMNI void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = omni_lights[idx].position - vertex; float light_length = length(light_rel_vec); float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation); vec3 color = omni_lights[idx].color; float size_A = 0.0; if (omni_lights[idx].size > 0.0) { float t = omni_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * omni_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = spot_lights[idx].position - vertex; float light_length = length(light_rel_vec); float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation); vec3 spot_dir = spot_lights[idx].direction; float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle); float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle)); spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].cone_attenuation); vec3 color = spot_lights[idx].color; float size_A = 0.0; if (spot_lights[idx].size > 0.0) { float t = spot_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * spot_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifndef MODE_RENDER_DEPTH vec4 fog_process(vec3 vertex) { vec3 fog_color = scene_data.fog_light_color; #ifdef USE_RADIANCE_MAP /* if (scene_data.fog_aerial_perspective > 0.0) { vec3 sky_fog_color = vec3(0.0); vec3 cube_view = scene_data.radiance_inverse_xform * vertex; // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near)); sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb; fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective); } */ #endif #ifndef DISABLE_LIGHT_DIRECTIONAL if (scene_data.fog_sun_scatter > 0.001) { vec4 sun_scatter = vec4(0.0); float sun_total = 0.0; vec3 view = normalize(vertex); for (uint i = uint(0); i < scene_data.directional_light_count; i++) { vec3 light_color = directional_lights[i].color * directional_lights[i].energy; float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); fog_color += light_color * light_amount * scene_data.fog_sun_scatter; } } #endif // !DISABLE_LIGHT_DIRECTIONAL float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density)); if (abs(scene_data.fog_height_density) >= 0.0001) { float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y; float y_dist = y - scene_data.fog_height; float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density)); fog_amount = max(vfog_amount, fog_amount); } return vec4(fog_color, fog_amount); } #endif // !MODE_RENDER_DEPTH void main() { //lay out everything, whatever is unused is optimized away anyway vec3 vertex = vertex_interp; #ifdef USE_MULTIVIEW vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz; vec3 view = -normalize(vertex_interp - eye_offset); mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex]; mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex]; #else vec3 eye_offset = vec3(0.0, 0.0, 0.0); vec3 view = -normalize(vertex_interp); mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; #endif highp mat4 model_matrix = world_transform; vec3 albedo = vec3(1.0); vec3 backlight = vec3(0.0); vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0); float transmittance_depth = 0.0; float transmittance_boost = 0.0; float metallic = 0.0; float specular = 0.5; vec3 emission = vec3(0.0); float roughness = 1.0; float rim = 0.0; float rim_tint = 0.0; float clearcoat = 0.0; float clearcoat_roughness = 0.0; float anisotropy = 0.0; vec2 anisotropy_flow = vec2(1.0, 0.0); vec4 fog = vec4(0.0); #if defined(CUSTOM_RADIANCE_USED) vec4 custom_radiance = vec4(0.0); #endif #if defined(CUSTOM_IRRADIANCE_USED) vec4 custom_irradiance = vec4(0.0); #endif float ao = 1.0; float ao_light_affect = 0.0; float alpha = 1.0; #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) vec3 binormal = normalize(binormal_interp); vec3 tangent = normalize(tangent_interp); #else vec3 binormal = vec3(0.0); vec3 tangent = vec3(0.0); #endif #ifdef NORMAL_USED vec3 normal = normalize(normal_interp); #if defined(DO_SIDE_CHECK) if (!gl_FrontFacing) { normal = -normal; } #endif #endif //NORMAL_USED #ifdef UV_USED vec2 uv = uv_interp; #endif #if defined(UV2_USED) || defined(USE_LIGHTMAP) vec2 uv2 = uv2_interp; #endif #if defined(COLOR_USED) vec4 color = color_interp; #endif #if defined(NORMAL_MAP_USED) vec3 normal_map = vec3(0.5); #endif float normal_map_depth = 1.0; vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size; float sss_strength = 0.0; #ifdef ALPHA_SCISSOR_USED float alpha_scissor_threshold = 1.0; #endif // ALPHA_SCISSOR_USED #ifdef ALPHA_HASH_USED float alpha_hash_scale = 1.0; #endif // ALPHA_HASH_USED #ifdef ALPHA_ANTIALIASING_EDGE_USED float alpha_antialiasing_edge = 0.0; vec2 alpha_texture_coordinate = vec2(0.0, 0.0); #endif // ALPHA_ANTIALIASING_EDGE_USED { } #ifndef USE_SHADOW_TO_OPACITY #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor_threshold) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // !USE_SHADOW_TO_OPACITY #ifdef NORMAL_MAP_USED normal_map.xy = normal_map.xy * 2.0 - 1.0; normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth)); #endif #ifdef LIGHT_ANISOTROPY_USED if (anisotropy > 0.01) { //rotation matrix mat3 rot = mat3(tangent, binormal, normal); //make local to space tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0)); binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0)); } #endif #ifndef MODE_RENDER_DEPTH #ifndef CUSTOM_FOG_USED #ifndef DISABLE_FOG // fog must be processed as early as possible and then packed. // to maximize VGPR usage if (scene_data.fog_enabled) { fog = fog_process(vertex); } #endif // !DISABLE_FOG #endif // !CUSTOM_FOG_USED uint fog_rg = packHalf2x16(fog.rg); uint fog_ba = packHalf2x16(fog.ba); // Convert colors to linear albedo = srgb_to_linear(albedo); emission = srgb_to_linear(emission); // TODO Backlight and transmittance when used #ifndef MODE_UNSHADED vec3 f0 = F0(metallic, specular, albedo); vec3 specular_light = vec3(0.0, 0.0, 0.0); vec3 diffuse_light = vec3(0.0, 0.0, 0.0); vec3 ambient_light = vec3(0.0, 0.0, 0.0); #ifdef BASE_PASS /////////////////////// LIGHTING ////////////////////////////// // IBL precalculations float ndotv = clamp(dot(normal, view), 0.0, 1.0); vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0); #ifdef USE_RADIANCE_MAP if (scene_data.use_reflection_cubemap) { #ifdef LIGHT_ANISOTROPY_USED // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent; vec3 anisotropic_tangent = cross(anisotropic_direction, view); vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction); vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0))); vec3 ref_vec = reflect(-view, bent_normal); #else vec3 ref_vec = reflect(-view, normal); #endif ref_vec = mix(ref_vec, normal, roughness * roughness); float horizon = min(1.0 + dot(ref_vec, normal), 1.0); ref_vec = scene_data.radiance_inverse_xform * ref_vec; specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb; specular_light = srgb_to_linear(specular_light); specular_light *= horizon * horizon; specular_light *= scene_data.ambient_light_color_energy.a; } #endif // Calculate Reflection probes // Calculate Lightmaps #if defined(CUSTOM_RADIANCE_USED) specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a); #endif // CUSTOM_RADIANCE_USED #ifndef USE_LIGHTMAP //lightmap overrides everything if (scene_data.use_ambient_light) { ambient_light = scene_data.ambient_light_color_energy.rgb; #ifdef USE_RADIANCE_MAP if (scene_data.use_ambient_cubemap) { vec3 ambient_dir = scene_data.radiance_inverse_xform * normal; vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb; cubemap_ambient = srgb_to_linear(cubemap_ambient); ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix); } #endif } #endif // USE_LIGHTMAP #if defined(CUSTOM_IRRADIANCE_USED) ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a); #endif // CUSTOM_IRRADIANCE_USED { #if defined(AMBIENT_LIGHT_DISABLED) ambient_light = vec3(0.0, 0.0, 0.0); #else ambient_light *= albedo.rgb; ambient_light *= ao; #endif // AMBIENT_LIGHT_DISABLED } // convert ao to direct light ao ao = mix(1.0, ao, ao_light_affect); { #if defined(DIFFUSE_TOON) //simplify for toon, as specular_light *= specular * metallic * albedo * 2.0; #else // scales the specular reflections, needs to be be computed before lighting happens, // but after environment, GI, and reflection probes are added // Environment brdf approximation (Lazarov 2013) // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); vec4 r = roughness * c0 + c1; float ndotv = clamp(dot(normal, view), 0.0, 1.0); float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y; vec2 env = vec2(-1.04, 1.04) * a004 + r.zw; specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0); #endif } #endif // BASE_PASS #ifndef DISABLE_LIGHT_DIRECTIONAL //diffuse_light = normal; // for (uint i = uint(0); i < scene_data.directional_light_count; i++) { light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, true, 1.0, f0, roughness, metallic, 1.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_DIRECTIONAL #ifndef DISABLE_LIGHT_OMNI for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= omni_light_count) { break; } light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= spot_light_count) { break; } light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED tangent, binormal, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !MODE_UNSHADED #endif // !MODE_RENDER_DEPTH #if defined(USE_SHADOW_TO_OPACITY) alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0)); #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // USE_SHADOW_TO_OPACITY #ifdef MODE_RENDER_DEPTH //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) #else // !MODE_RENDER_DEPTH #ifdef MODE_UNSHADED frag_color = vec4(albedo, alpha); #else diffuse_light *= albedo; diffuse_light *= 1.0 - metallic; ambient_light *= 1.0 - metallic; frag_color = vec4(diffuse_light + specular_light, alpha); #ifdef BASE_PASS frag_color.rgb += emission + ambient_light; #endif #endif //MODE_UNSHADED fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba)); #ifndef DISABLE_FOG if (scene_data.fog_enabled) { #ifdef BASE_PASS frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a); #else frag_color.rgb *= (1.0 - fog.a); #endif // BASE_PASS } #endif // Tonemap before writing as we are writing to an sRGB framebuffer frag_color.rgb *= exposure; frag_color.rgb = apply_tonemapping(frag_color.rgb, white); frag_color.rgb = linear_to_srgb(frag_color.rgb); #ifdef USE_BCS frag_color.rgb = apply_bcs(frag_color.rgb, bcs); #endif #ifdef USE_COLOR_CORRECTION frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction); #endif #endif //!MODE_RENDER_DEPTH } #version 330 #define USE_GLES_OVER_GL #define USE_RADIANCE_MAP #define MAX_GLOBAL_SHADER_UNIFORMS 256 #define MAX_LIGHT_DATA_STRUCTS 32 #define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS 8 #define MAX_FORWARD_LIGHTS uint(8) #define MODE_RENDER_DEPTH #ifdef USE_MULTIVIEW #if defined(GL_OVR_multiview2) #extension GL_OVR_multiview2 : require #elif defined(GL_OVR_multiview) #extension GL_OVR_multiview : require #endif #define ViewIndex gl_ViewID_OVR #define MAX_VIEWS 2 #else #define ViewIndex uint(0) #define MAX_VIEWS 1 #endif precision highp float; precision highp int; // Default to SPECULAR_SCHLICK_GGX. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON) #define SPECULAR_SCHLICK_GGX #endif #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED) #ifndef NORMAL_USED #define NORMAL_USED #endif #endif #ifndef MODE_RENDER_DEPTH #ifdef USE_BCS uniform vec3 bcs; #endif #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT uniform sampler2D source_color_correction; //texunit:-1 #else uniform sampler3D source_color_correction; //texunit:-1 #endif #endif layout(std140) uniform TonemapData { //ubo:0 float exposure; float white; int tonemapper; int pad; }; vec3 apply_bcs(vec3 color, vec3 bcs) { color = mix(vec3(0.0), color, bcs.x); color = mix(vec3(0.5), color, bcs.y); color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z); return color; } #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT vec3 apply_color_correction(vec3 color) { color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r; color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g; color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b; return color; } #else vec3 apply_color_correction(vec3 color) { return textureLod(source_color_correction, color, 0.0).rgb; } #endif #endif vec3 tonemap_filmic(vec3 color, float p_white) { // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) // has no effect on the curve's general shape or visual properties const float exposure_bias = 2.0f; const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance const float B = 0.30f * exposure_bias; const float C = 0.10f; const float D = 0.20f; const float E = 0.01f; const float F = 0.30f; vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F; return color_tonemapped / p_white_tonemapped; } // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl // (MIT License). vec3 tonemap_aces(vec3 color, float p_white) { const float exposure_bias = 1.8f; const float A = 0.0245786f; const float B = 0.000090537f; const float C = 0.983729f; const float D = 0.432951f; const float E = 0.238081f; // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias` const mat3 rgb_to_rrt = mat3( vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias), vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias), vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias)); const mat3 odt_to_rgb = mat3( vec3(1.60475f, -0.53108f, -0.07367f), vec3(-0.10208f, 1.10813f, -0.00605f), vec3(-0.00327f, -0.07276f, 1.07602f)); color *= rgb_to_rrt; vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E); color_tonemapped *= odt_to_rgb; p_white *= exposure_bias; float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E); return color_tonemapped / p_white_tonemapped; } vec3 tonemap_reinhard(vec3 color, float p_white) { return (p_white * color + color) / (color * p_white + p_white); } // This expects 0-1 range input. vec3 linear_to_srgb(vec3 color) { //color = clamp(color, vec3(0.0), vec3(1.0)); //const vec3 a = vec3(0.055f); //return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0)); } // This expects 0-1 range input, outside that range it behaves poorly. vec3 srgb_to_linear(vec3 color) { // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878); } #define TONEMAPPER_LINEAR 0 #define TONEMAPPER_REINHARD 1 #define TONEMAPPER_FILMIC 2 #define TONEMAPPER_ACES 3 vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color // Ensure color values passed to tonemappers are positive. // They can be negative in the case of negative lights, which leads to undesired behavior. if (tonemapper == TONEMAPPER_LINEAR) { return color; } else if (tonemapper == TONEMAPPER_REINHARD) { return tonemap_reinhard(max(vec3(0.0f), color), p_white); } else if (tonemapper == TONEMAPPER_FILMIC) { return tonemap_filmic(max(vec3(0.0f), color), p_white); } else { // TONEMAPPER_ACES return tonemap_aces(max(vec3(0.0f), color), p_white); } } #endif #ifdef USE_GLES_OVER_GL // Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile. uint float2half(uint f) { uint e = f & uint(0x7f800000); if (e <= uint(0x38000000)) { return uint(0); } else { return ((f >> uint(16)) & uint(0x8000)) | (((e - uint(0x38000000)) >> uint(13)) & uint(0x7c00)) | ((f >> uint(13)) & uint(0x03ff)); } } uint half2float(uint h) { uint h_e = h & uint(0x7c00); return ((h & uint(0x8000)) << uint(16)) | uint((h_e >> uint(10)) != uint(0)) * (((h_e + uint(0x1c000)) << uint(13)) | ((h & uint(0x03ff)) << uint(13))); } uint packHalf2x16(vec2 v) { return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16); } vec2 unpackHalf2x16(uint v) { return vec2(uintBitsToFloat(half2float(v & uint(0xffff))), uintBitsToFloat(half2float(v >> uint(16)))); } uint packUnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0)); return uv.x | uv.y << uint(16); } vec2 unpackUnorm2x16(uint p) { return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization } uint packSnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0); return uv.x | uv.y << uint(16); } vec2 unpackSnorm2x16(uint p) { vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16))); return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0)); } #endif // Compatibility renames. These are exposed with the "godot_" prefix // to work around an Adreno bug which was exposing these ES310 functions // in ES300 shaders. Internally, we must use the "godot_" prefix, but user shaders // will be mapped automatically. uint godot_packUnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0)); return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24)); } vec4 godot_unpackUnorm4x8(uint p) { return vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))) * 0.00392156862; // 1.0 / 255.0 } uint godot_packSnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0); return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24); } vec4 godot_unpackSnorm4x8(uint p) { vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))); return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0)); } #define packUnorm4x8 godot_packUnorm4x8 #define unpackUnorm4x8 godot_unpackUnorm4x8 #define packSnorm4x8 godot_packSnorm4x8 #define unpackSnorm4x8 godot_unpackSnorm4x8 /* texture unit usage, N is max_texture_unity-N 1-color correction // In tonemap_inc.glsl 2-radiance 3-directional_shadow 4-positional_shadow 5-screen 6-depth */ #define M_PI 3.14159265359 /* clang-format on */ #define SHADER_IS_SRGB true /* Varyings */ #if defined(COLOR_USED) in vec4 color_interp; #endif #if defined(UV_USED) in vec2 uv_interp; #endif #if defined(UV2_USED) in vec2 uv2_interp; #else #ifdef USE_LIGHTMAP in vec2 uv2_interp; #endif #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) in vec3 tangent_interp; in vec3 binormal_interp; #endif #ifdef NORMAL_USED in vec3 normal_interp; #endif in highp vec3 vertex_interp; #ifdef USE_RADIANCE_MAP #define RADIANCE_MAX_LOD 5.0 uniform samplerCube radiance_map; // texunit:-2 #endif layout(std140) uniform GlobalShaderUniformData { //ubo:1 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS]; }; /* Material Uniforms */ #ifdef MATERIAL_UNIFORMS_USED /* clang-format off */ layout(std140) uniform MaterialUniforms { // ubo:3 }; /* clang-format on */ #endif layout(std140) uniform SceneData { // ubo:2 highp mat4 projection_matrix; highp mat4 inv_projection_matrix; highp mat4 inv_view_matrix; highp mat4 view_matrix; vec2 viewport_size; vec2 screen_pixel_size; mediump vec4 ambient_light_color_energy; mediump float ambient_color_sky_mix; bool material_uv2_mode; float emissive_exposure_normalization; bool use_ambient_light; bool use_ambient_cubemap; bool use_reflection_cubemap; float fog_aerial_perspective; float time; mat3 radiance_inverse_xform; uint directional_light_count; float z_far; float z_near; float IBL_exposure_normalization; bool fog_enabled; float fog_density; float fog_height; float fog_height_density; vec3 fog_light_color; float fog_sun_scatter; uint camera_visible_layers; uint pad3; uint pad4; uint pad5; } scene_data; #ifdef USE_MULTIVIEW layout(std140) uniform MultiviewData { // ubo:8 highp mat4 projection_matrix_view[MAX_VIEWS]; highp mat4 inv_projection_matrix_view[MAX_VIEWS]; highp vec4 eye_offset[MAX_VIEWS]; } multiview_data; #endif /* clang-format off */ /* clang-format on */ // Directional light data. #ifndef DISABLE_LIGHT_DIRECTIONAL struct DirectionalLightData { mediump vec3 direction; mediump float energy; mediump vec3 color; mediump float size; mediump vec3 pad; mediump float specular; }; layout(std140) uniform DirectionalLights { // ubo:7 DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; }; #endif // !DISABLE_LIGHT_DIRECTIONAL // Omni and spot light data. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) struct LightData { // This structure needs to be as packed as possible. highp vec3 position; highp float inv_radius; mediump vec3 direction; highp float size; mediump vec3 color; mediump float attenuation; mediump float cone_attenuation; mediump float cone_angle; mediump float specular_amount; mediump float shadow_opacity; }; #ifndef DISABLE_LIGHT_OMNI layout(std140) uniform OmniLightData { // ubo:5 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint omni_light_indices[MAX_FORWARD_LIGHTS]; uniform uint omni_light_count; #endif #ifndef DISABLE_LIGHT_SPOT layout(std140) uniform SpotLightData { // ubo:6 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint spot_light_indices[MAX_FORWARD_LIGHTS]; uniform uint spot_light_count; #endif #ifdef USE_ADDITIVE_LIGHTING uniform highp samplerCubeShadow positional_shadow; // texunit:-4 #endif #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifdef USE_MULTIVIEW uniform highp sampler2DArray depth_buffer; // texunit:-6 uniform highp sampler2DArray color_buffer; // texunit:-5 vec3 multiview_uv(vec2 uv) { return vec3(uv, ViewIndex); } #else uniform highp sampler2D depth_buffer; // texunit:-6 uniform highp sampler2D color_buffer; // texunit:-5 vec2 multiview_uv(vec2 uv) { return uv; } #endif uniform highp mat4 world_transform; uniform mediump float opaque_prepass_threshold; layout(location = 0) out vec4 frag_color; vec3 F0(float metallic, float specular, vec3 albedo) { float dielectric = 0.16 * specular * specular; // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials; // see https://google.github.io/filament/Filament.md.html return mix(vec3(dielectric), albedo, vec3(metallic)); } #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) float D_GGX(float cos_theta_m, float alpha) { float a = cos_theta_m * alpha; float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a); return k * k * (1.0 / M_PI); } // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX float V_GGX(float NdotL, float NdotV, float alpha) { return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha); } float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { float alpha2 = alpha_x * alpha_y; highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m); highp float v2 = dot(v, v); float w2 = alpha2 / v2; float D = alpha2 * w2 * w2 * (1.0 / M_PI); return D; } float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) { float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV)); float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL)); return 0.5 / (Lambda_V + Lambda_L); } float SchlickFresnel(float u) { float m = 1.0 - u; float m2 = m * m; return m2 * m2 * m; // pow(m,5) } void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_directional, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 B, vec3 T, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { #if defined(USE_LIGHT_SHADER_CODE) // light is written by the light shader highp mat4 model_matrix = world_transform; mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; vec3 normal = N; vec3 light = L; vec3 view = V; /* clang-format off */ /* clang-format on */ #else float NdotL = min(A + dot(N, L), 1.0); float cNdotL = max(NdotL, 0.0); // clamped NdotL float NdotV = dot(N, V); float cNdotV = max(NdotV, 1e-4); #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) vec3 H = normalize(V + L); #endif #if defined(SPECULAR_SCHLICK_GGX) float cNdotH = clamp(A + dot(N, H), 0.0, 1.0); #endif #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) float cLdotH = clamp(A + dot(L, H), 0.0, 1.0); #endif if (metallic < 1.0) { float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance #if defined(DIFFUSE_LAMBERT_WRAP) // Energy conserving lambert wrap shader. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/ diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI); #elif defined(DIFFUSE_TOON) diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI); #elif defined(DIFFUSE_BURLEY) { 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 / M_PI) * FdV * FdL * cNdotL; } #else // Lambert diffuse_brdf_NL = cNdotL * (1.0 / M_PI); #endif diffuse_light += light_color * diffuse_brdf_NL * attenuation; #if defined(LIGHT_BACKLIGHT_USED) diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation; #endif #if defined(LIGHT_RIM_USED) // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0)); diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color; #endif } if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely // D #if defined(SPECULAR_TOON) vec3 R = normalize(-reflect(L, N)); float RdotV = dot(R, V); float mid = 1.0 - roughness; mid *= mid; float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid; diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection #elif defined(SPECULAR_DISABLED) // none.. #elif defined(SPECULAR_SCHLICK_GGX) // shlick+ggx as default float alpha_ggx = roughness * roughness; #if defined(LIGHT_ANISOTROPY_USED) float aspect = sqrt(1.0 - anisotropy * 0.9); float ax = alpha_ggx / aspect; float ay = alpha_ggx * aspect; float XdotH = dot(T, H); float YdotH = dot(B, H); float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH); float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL); #else float D = D_GGX(cNdotH, alpha_ggx); float G = V_GGX(cNdotL, cNdotV, alpha_ggx); #endif // LIGHT_ANISOTROPY_USED // F float cLdotH5 = SchlickFresnel(cLdotH); // Calculate Fresnel using cheap approximate specular occlusion term from Filament: // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion float f90 = clamp(50.0 * f0.g, 0.0, 1.0); vec3 F = f0 + (f90 - f0) * cLdotH5; vec3 specular_brdf_NL = cNdotL * D * F * G; specular_light += specular_brdf_NL * light_color * attenuation * specular_amount; #endif #if defined(LIGHT_CLEARCOAT_USED) // Clearcoat ignores normal_map, use vertex normal instead float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0); float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0); float ccNdotV = max(dot(vertex_normal, V), 1e-4); #if !defined(SPECULAR_SCHLICK_GGX) float cLdotH5 = SchlickFresnel(cLdotH); #endif float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness)); float Gr = 0.25 / (cLdotH * cLdotH); float Fr = mix(.04, 1.0, cLdotH5); float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL; specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount; // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR) // but to do so we need to rearrange this entire function #endif // LIGHT_CLEARCOAT_USED } #ifdef USE_SHADOW_TO_OPACITY alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0)); #endif #endif // USE_LIGHT_SHADER_CODE } float get_omni_spot_attenuation(float distance, float inv_range, float decay) { float nd = distance * inv_range; nd *= nd; nd *= nd; // nd^4 nd = max(1.0 - nd, 0.0); nd *= nd; // nd^2 return nd * pow(max(distance, 0.0001), -decay); } #ifndef DISABLE_LIGHT_OMNI void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = omni_lights[idx].position - vertex; float light_length = length(light_rel_vec); float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation); vec3 color = omni_lights[idx].color; float size_A = 0.0; if (omni_lights[idx].size > 0.0) { float t = omni_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * omni_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = spot_lights[idx].position - vertex; float light_length = length(light_rel_vec); float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation); vec3 spot_dir = spot_lights[idx].direction; float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle); float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle)); spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].cone_attenuation); vec3 color = spot_lights[idx].color; float size_A = 0.0; if (spot_lights[idx].size > 0.0) { float t = spot_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * spot_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifndef MODE_RENDER_DEPTH vec4 fog_process(vec3 vertex) { vec3 fog_color = scene_data.fog_light_color; #ifdef USE_RADIANCE_MAP /* if (scene_data.fog_aerial_perspective > 0.0) { vec3 sky_fog_color = vec3(0.0); vec3 cube_view = scene_data.radiance_inverse_xform * vertex; // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near)); sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb; fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective); } */ #endif #ifndef DISABLE_LIGHT_DIRECTIONAL if (scene_data.fog_sun_scatter > 0.001) { vec4 sun_scatter = vec4(0.0); float sun_total = 0.0; vec3 view = normalize(vertex); for (uint i = uint(0); i < scene_data.directional_light_count; i++) { vec3 light_color = directional_lights[i].color * directional_lights[i].energy; float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); fog_color += light_color * light_amount * scene_data.fog_sun_scatter; } } #endif // !DISABLE_LIGHT_DIRECTIONAL float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density)); if (abs(scene_data.fog_height_density) >= 0.0001) { float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y; float y_dist = y - scene_data.fog_height; float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density)); fog_amount = max(vfog_amount, fog_amount); } return vec4(fog_color, fog_amount); } #endif // !MODE_RENDER_DEPTH void main() { //lay out everything, whatever is unused is optimized away anyway vec3 vertex = vertex_interp; #ifdef USE_MULTIVIEW vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz; vec3 view = -normalize(vertex_interp - eye_offset); mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex]; mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex]; #else vec3 eye_offset = vec3(0.0, 0.0, 0.0); vec3 view = -normalize(vertex_interp); mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; #endif highp mat4 model_matrix = world_transform; vec3 albedo = vec3(1.0); vec3 backlight = vec3(0.0); vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0); float transmittance_depth = 0.0; float transmittance_boost = 0.0; float metallic = 0.0; float specular = 0.5; vec3 emission = vec3(0.0); float roughness = 1.0; float rim = 0.0; float rim_tint = 0.0; float clearcoat = 0.0; float clearcoat_roughness = 0.0; float anisotropy = 0.0; vec2 anisotropy_flow = vec2(1.0, 0.0); vec4 fog = vec4(0.0); #if defined(CUSTOM_RADIANCE_USED) vec4 custom_radiance = vec4(0.0); #endif #if defined(CUSTOM_IRRADIANCE_USED) vec4 custom_irradiance = vec4(0.0); #endif float ao = 1.0; float ao_light_affect = 0.0; float alpha = 1.0; #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) vec3 binormal = normalize(binormal_interp); vec3 tangent = normalize(tangent_interp); #else vec3 binormal = vec3(0.0); vec3 tangent = vec3(0.0); #endif #ifdef NORMAL_USED vec3 normal = normalize(normal_interp); #if defined(DO_SIDE_CHECK) if (!gl_FrontFacing) { normal = -normal; } #endif #endif //NORMAL_USED #ifdef UV_USED vec2 uv = uv_interp; #endif #if defined(UV2_USED) || defined(USE_LIGHTMAP) vec2 uv2 = uv2_interp; #endif #if defined(COLOR_USED) vec4 color = color_interp; #endif #if defined(NORMAL_MAP_USED) vec3 normal_map = vec3(0.5); #endif float normal_map_depth = 1.0; vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size; float sss_strength = 0.0; #ifdef ALPHA_SCISSOR_USED float alpha_scissor_threshold = 1.0; #endif // ALPHA_SCISSOR_USED #ifdef ALPHA_HASH_USED float alpha_hash_scale = 1.0; #endif // ALPHA_HASH_USED #ifdef ALPHA_ANTIALIASING_EDGE_USED float alpha_antialiasing_edge = 0.0; vec2 alpha_texture_coordinate = vec2(0.0, 0.0); #endif // ALPHA_ANTIALIASING_EDGE_USED { } #ifndef USE_SHADOW_TO_OPACITY #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor_threshold) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // !USE_SHADOW_TO_OPACITY #ifdef NORMAL_MAP_USED normal_map.xy = normal_map.xy * 2.0 - 1.0; normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth)); #endif #ifdef LIGHT_ANISOTROPY_USED if (anisotropy > 0.01) { //rotation matrix mat3 rot = mat3(tangent, binormal, normal); //make local to space tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0)); binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0)); } #endif #ifndef MODE_RENDER_DEPTH #ifndef CUSTOM_FOG_USED #ifndef DISABLE_FOG // fog must be processed as early as possible and then packed. // to maximize VGPR usage if (scene_data.fog_enabled) { fog = fog_process(vertex); } #endif // !DISABLE_FOG #endif // !CUSTOM_FOG_USED uint fog_rg = packHalf2x16(fog.rg); uint fog_ba = packHalf2x16(fog.ba); // Convert colors to linear albedo = srgb_to_linear(albedo); emission = srgb_to_linear(emission); // TODO Backlight and transmittance when used #ifndef MODE_UNSHADED vec3 f0 = F0(metallic, specular, albedo); vec3 specular_light = vec3(0.0, 0.0, 0.0); vec3 diffuse_light = vec3(0.0, 0.0, 0.0); vec3 ambient_light = vec3(0.0, 0.0, 0.0); #ifdef BASE_PASS /////////////////////// LIGHTING ////////////////////////////// // IBL precalculations float ndotv = clamp(dot(normal, view), 0.0, 1.0); vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0); #ifdef USE_RADIANCE_MAP if (scene_data.use_reflection_cubemap) { #ifdef LIGHT_ANISOTROPY_USED // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent; vec3 anisotropic_tangent = cross(anisotropic_direction, view); vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction); vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0))); vec3 ref_vec = reflect(-view, bent_normal); #else vec3 ref_vec = reflect(-view, normal); #endif ref_vec = mix(ref_vec, normal, roughness * roughness); float horizon = min(1.0 + dot(ref_vec, normal), 1.0); ref_vec = scene_data.radiance_inverse_xform * ref_vec; specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb; specular_light = srgb_to_linear(specular_light); specular_light *= horizon * horizon; specular_light *= scene_data.ambient_light_color_energy.a; } #endif // Calculate Reflection probes // Calculate Lightmaps #if defined(CUSTOM_RADIANCE_USED) specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a); #endif // CUSTOM_RADIANCE_USED #ifndef USE_LIGHTMAP //lightmap overrides everything if (scene_data.use_ambient_light) { ambient_light = scene_data.ambient_light_color_energy.rgb; #ifdef USE_RADIANCE_MAP if (scene_data.use_ambient_cubemap) { vec3 ambient_dir = scene_data.radiance_inverse_xform * normal; vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb; cubemap_ambient = srgb_to_linear(cubemap_ambient); ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix); } #endif } #endif // USE_LIGHTMAP #if defined(CUSTOM_IRRADIANCE_USED) ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a); #endif // CUSTOM_IRRADIANCE_USED { #if defined(AMBIENT_LIGHT_DISABLED) ambient_light = vec3(0.0, 0.0, 0.0); #else ambient_light *= albedo.rgb; ambient_light *= ao; #endif // AMBIENT_LIGHT_DISABLED } // convert ao to direct light ao ao = mix(1.0, ao, ao_light_affect); { #if defined(DIFFUSE_TOON) //simplify for toon, as specular_light *= specular * metallic * albedo * 2.0; #else // scales the specular reflections, needs to be be computed before lighting happens, // but after environment, GI, and reflection probes are added // Environment brdf approximation (Lazarov 2013) // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); vec4 r = roughness * c0 + c1; float ndotv = clamp(dot(normal, view), 0.0, 1.0); float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y; vec2 env = vec2(-1.04, 1.04) * a004 + r.zw; specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0); #endif } #endif // BASE_PASS #ifndef DISABLE_LIGHT_DIRECTIONAL //diffuse_light = normal; // for (uint i = uint(0); i < scene_data.directional_light_count; i++) { light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, true, 1.0, f0, roughness, metallic, 1.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_DIRECTIONAL #ifndef DISABLE_LIGHT_OMNI for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= omni_light_count) { break; } light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= spot_light_count) { break; } light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED tangent, binormal, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !MODE_UNSHADED #endif // !MODE_RENDER_DEPTH #if defined(USE_SHADOW_TO_OPACITY) alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0)); #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // USE_SHADOW_TO_OPACITY #ifdef MODE_RENDER_DEPTH //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) #else // !MODE_RENDER_DEPTH #ifdef MODE_UNSHADED frag_color = vec4(albedo, alpha); #else diffuse_light *= albedo; diffuse_light *= 1.0 - metallic; ambient_light *= 1.0 - metallic; frag_color = vec4(diffuse_light + specular_light, alpha); #ifdef BASE_PASS frag_color.rgb += emission + ambient_light; #endif #endif //MODE_UNSHADED fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba)); #ifndef DISABLE_FOG if (scene_data.fog_enabled) { #ifdef BASE_PASS frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a); #else frag_color.rgb *= (1.0 - fog.a); #endif // BASE_PASS } #endif // Tonemap before writing as we are writing to an sRGB framebuffer frag_color.rgb *= exposure; frag_color.rgb = apply_tonemapping(frag_color.rgb, white); frag_color.rgb = linear_to_srgb(frag_color.rgb); #ifdef USE_BCS frag_color.rgb = apply_bcs(frag_color.rgb, bcs); #endif #ifdef USE_COLOR_CORRECTION frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction); #endif #endif //!MODE_RENDER_DEPTH } #version 330 #define USE_GLES_OVER_GL #define USE_RADIANCE_MAP #define MAX_GLOBAL_SHADER_UNIFORMS 256 #define MAX_LIGHT_DATA_STRUCTS 32 #define MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS 8 #define MAX_FORWARD_LIGHTS uint(8) #define MODE_RENDER_DEPTH #define USE_INSTANCING #ifdef USE_MULTIVIEW #if defined(GL_OVR_multiview2) #extension GL_OVR_multiview2 : require #elif defined(GL_OVR_multiview) #extension GL_OVR_multiview : require #endif #define ViewIndex gl_ViewID_OVR #define MAX_VIEWS 2 #else #define ViewIndex uint(0) #define MAX_VIEWS 1 #endif precision highp float; precision highp int; // Default to SPECULAR_SCHLICK_GGX. #if !defined(SPECULAR_DISABLED) && !defined(SPECULAR_SCHLICK_GGX) && !defined(SPECULAR_TOON) #define SPECULAR_SCHLICK_GGX #endif #if !defined(MODE_RENDER_DEPTH) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) ||defined(LIGHT_CLEARCOAT_USED) #ifndef NORMAL_USED #define NORMAL_USED #endif #endif #ifndef MODE_RENDER_DEPTH #ifdef USE_BCS uniform vec3 bcs; #endif #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT uniform sampler2D source_color_correction; //texunit:-1 #else uniform sampler3D source_color_correction; //texunit:-1 #endif #endif layout(std140) uniform TonemapData { //ubo:0 float exposure; float white; int tonemapper; int pad; }; vec3 apply_bcs(vec3 color, vec3 bcs) { color = mix(vec3(0.0), color, bcs.x); color = mix(vec3(0.5), color, bcs.y); color = mix(vec3(dot(vec3(1.0), color) * 0.33333), color, bcs.z); return color; } #ifdef USE_COLOR_CORRECTION #ifdef USE_1D_LUT vec3 apply_color_correction(vec3 color) { color.r = texture(source_color_correction, vec2(color.r, 0.0f)).r; color.g = texture(source_color_correction, vec2(color.g, 0.0f)).g; color.b = texture(source_color_correction, vec2(color.b, 0.0f)).b; return color; } #else vec3 apply_color_correction(vec3 color) { return textureLod(source_color_correction, color, 0.0).rgb; } #endif #endif vec3 tonemap_filmic(vec3 color, float p_white) { // exposure bias: input scale (color *= bias, white *= bias) to make the brightness consistent with other tonemappers // also useful to scale the input to the range that the tonemapper is designed for (some require very high input values) // has no effect on the curve's general shape or visual properties const float exposure_bias = 2.0f; const float A = 0.22f * exposure_bias * exposure_bias; // bias baked into constants for performance const float B = 0.30f * exposure_bias; const float C = 0.10f; const float D = 0.20f; const float E = 0.01f; const float F = 0.30f; vec3 color_tonemapped = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; float p_white_tonemapped = ((p_white * (A * p_white + C * B) + D * E) / (p_white * (A * p_white + B) + D * F)) - E / F; return color_tonemapped / p_white_tonemapped; } // Adapted from https://github.com/TheRealMJP/BakingLab/blob/master/BakingLab/ACES.hlsl // (MIT License). vec3 tonemap_aces(vec3 color, float p_white) { const float exposure_bias = 1.8f; const float A = 0.0245786f; const float B = 0.000090537f; const float C = 0.983729f; const float D = 0.432951f; const float E = 0.238081f; // Exposure bias baked into transform to save shader instructions. Equivalent to `color *= exposure_bias` const mat3 rgb_to_rrt = mat3( vec3(0.59719f * exposure_bias, 0.35458f * exposure_bias, 0.04823f * exposure_bias), vec3(0.07600f * exposure_bias, 0.90834f * exposure_bias, 0.01566f * exposure_bias), vec3(0.02840f * exposure_bias, 0.13383f * exposure_bias, 0.83777f * exposure_bias)); const mat3 odt_to_rgb = mat3( vec3(1.60475f, -0.53108f, -0.07367f), vec3(-0.10208f, 1.10813f, -0.00605f), vec3(-0.00327f, -0.07276f, 1.07602f)); color *= rgb_to_rrt; vec3 color_tonemapped = (color * (color + A) - B) / (color * (C * color + D) + E); color_tonemapped *= odt_to_rgb; p_white *= exposure_bias; float p_white_tonemapped = (p_white * (p_white + A) - B) / (p_white * (C * p_white + D) + E); return color_tonemapped / p_white_tonemapped; } vec3 tonemap_reinhard(vec3 color, float p_white) { return (p_white * color + color) / (color * p_white + p_white); } // This expects 0-1 range input. vec3 linear_to_srgb(vec3 color) { //color = clamp(color, vec3(0.0), vec3(1.0)); //const vec3 a = vec3(0.055f); //return mix((vec3(1.0f) + a) * pow(color.rgb, vec3(1.0f / 2.4f)) - a, 12.92f * color.rgb, lessThan(color.rgb, vec3(0.0031308f))); // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return max(vec3(1.055) * pow(color, vec3(0.416666667)) - vec3(0.055), vec3(0.0)); } // This expects 0-1 range input, outside that range it behaves poorly. vec3 srgb_to_linear(vec3 color) { // Approximation from http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html return color * (color * (color * 0.305306011 + 0.682171111) + 0.012522878); } #define TONEMAPPER_LINEAR 0 #define TONEMAPPER_REINHARD 1 #define TONEMAPPER_FILMIC 2 #define TONEMAPPER_ACES 3 vec3 apply_tonemapping(vec3 color, float p_white) { // inputs are LINEAR, always outputs clamped [0;1] color // Ensure color values passed to tonemappers are positive. // They can be negative in the case of negative lights, which leads to undesired behavior. if (tonemapper == TONEMAPPER_LINEAR) { return color; } else if (tonemapper == TONEMAPPER_REINHARD) { return tonemap_reinhard(max(vec3(0.0f), color), p_white); } else if (tonemapper == TONEMAPPER_FILMIC) { return tonemap_filmic(max(vec3(0.0f), color), p_white); } else { // TONEMAPPER_ACES return tonemap_aces(max(vec3(0.0f), color), p_white); } } #endif #ifdef USE_GLES_OVER_GL // Floating point pack/unpack functions are part of the GLSL ES 300 specification used by web and mobile. uint float2half(uint f) { uint e = f & uint(0x7f800000); if (e <= uint(0x38000000)) { return uint(0); } else { return ((f >> uint(16)) & uint(0x8000)) | (((e - uint(0x38000000)) >> uint(13)) & uint(0x7c00)) | ((f >> uint(13)) & uint(0x03ff)); } } uint half2float(uint h) { uint h_e = h & uint(0x7c00); return ((h & uint(0x8000)) << uint(16)) | uint((h_e >> uint(10)) != uint(0)) * (((h_e + uint(0x1c000)) << uint(13)) | ((h & uint(0x03ff)) << uint(13))); } uint packHalf2x16(vec2 v) { return float2half(floatBitsToUint(v.x)) | float2half(floatBitsToUint(v.y)) << uint(16); } vec2 unpackHalf2x16(uint v) { return vec2(uintBitsToFloat(half2float(v & uint(0xffff))), uintBitsToFloat(half2float(v >> uint(16)))); } uint packUnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(0.0), vec2(1.0)) * 65535.0)); return uv.x | uv.y << uint(16); } vec2 unpackUnorm2x16(uint p) { return vec2(float(p & uint(0xffff)), float(p >> uint(16))) * 0.000015259021; // 1.0 / 65535.0 optimization } uint packSnorm2x16(vec2 v) { uvec2 uv = uvec2(round(clamp(v, vec2(-1.0), vec2(1.0)) * 32767.0) + 32767.0); return uv.x | uv.y << uint(16); } vec2 unpackSnorm2x16(uint p) { vec2 v = vec2(float(p & uint(0xffff)), float(p >> uint(16))); return clamp((v - 32767.0) * vec2(0.00003051851), vec2(-1.0), vec2(1.0)); } #endif // Compatibility renames. These are exposed with the "godot_" prefix // to work around an Adreno bug which was exposing these ES310 functions // in ES300 shaders. Internally, we must use the "godot_" prefix, but user shaders // will be mapped automatically. uint godot_packUnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(0.0), vec4(1.0)) * 255.0)); return uv.x | (uv.y << uint(8)) | (uv.z << uint(16)) | (uv.w << uint(24)); } vec4 godot_unpackUnorm4x8(uint p) { return vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))) * 0.00392156862; // 1.0 / 255.0 } uint godot_packSnorm4x8(vec4 v) { uvec4 uv = uvec4(round(clamp(v, vec4(-1.0), vec4(1.0)) * 127.0) + 127.0); return uv.x | uv.y << uint(8) | uv.z << uint(16) | uv.w << uint(24); } vec4 godot_unpackSnorm4x8(uint p) { vec4 v = vec4(float(p & uint(0xff)), float((p >> uint(8)) & uint(0xff)), float((p >> uint(16)) & uint(0xff)), float(p >> uint(24))); return clamp((v - vec4(127.0)) * vec4(0.00787401574), vec4(-1.0), vec4(1.0)); } #define packUnorm4x8 godot_packUnorm4x8 #define unpackUnorm4x8 godot_unpackUnorm4x8 #define packSnorm4x8 godot_packSnorm4x8 #define unpackSnorm4x8 godot_unpackSnorm4x8 /* texture unit usage, N is max_texture_unity-N 1-color correction // In tonemap_inc.glsl 2-radiance 3-directional_shadow 4-positional_shadow 5-screen 6-depth */ #define M_PI 3.14159265359 /* clang-format on */ #define SHADER_IS_SRGB true /* Varyings */ #if defined(COLOR_USED) in vec4 color_interp; #endif #if defined(UV_USED) in vec2 uv_interp; #endif #if defined(UV2_USED) in vec2 uv2_interp; #else #ifdef USE_LIGHTMAP in vec2 uv2_interp; #endif #endif #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) in vec3 tangent_interp; in vec3 binormal_interp; #endif #ifdef NORMAL_USED in vec3 normal_interp; #endif in highp vec3 vertex_interp; #ifdef USE_RADIANCE_MAP #define RADIANCE_MAX_LOD 5.0 uniform samplerCube radiance_map; // texunit:-2 #endif layout(std140) uniform GlobalShaderUniformData { //ubo:1 vec4 global_shader_uniforms[MAX_GLOBAL_SHADER_UNIFORMS]; }; /* Material Uniforms */ #ifdef MATERIAL_UNIFORMS_USED /* clang-format off */ layout(std140) uniform MaterialUniforms { // ubo:3 }; /* clang-format on */ #endif layout(std140) uniform SceneData { // ubo:2 highp mat4 projection_matrix; highp mat4 inv_projection_matrix; highp mat4 inv_view_matrix; highp mat4 view_matrix; vec2 viewport_size; vec2 screen_pixel_size; mediump vec4 ambient_light_color_energy; mediump float ambient_color_sky_mix; bool material_uv2_mode; float emissive_exposure_normalization; bool use_ambient_light; bool use_ambient_cubemap; bool use_reflection_cubemap; float fog_aerial_perspective; float time; mat3 radiance_inverse_xform; uint directional_light_count; float z_far; float z_near; float IBL_exposure_normalization; bool fog_enabled; float fog_density; float fog_height; float fog_height_density; vec3 fog_light_color; float fog_sun_scatter; uint camera_visible_layers; uint pad3; uint pad4; uint pad5; } scene_data; #ifdef USE_MULTIVIEW layout(std140) uniform MultiviewData { // ubo:8 highp mat4 projection_matrix_view[MAX_VIEWS]; highp mat4 inv_projection_matrix_view[MAX_VIEWS]; highp vec4 eye_offset[MAX_VIEWS]; } multiview_data; #endif /* clang-format off */ /* clang-format on */ // Directional light data. #ifndef DISABLE_LIGHT_DIRECTIONAL struct DirectionalLightData { mediump vec3 direction; mediump float energy; mediump vec3 color; mediump float size; mediump vec3 pad; mediump float specular; }; layout(std140) uniform DirectionalLights { // ubo:7 DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS]; }; #endif // !DISABLE_LIGHT_DIRECTIONAL // Omni and spot light data. #if !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) struct LightData { // This structure needs to be as packed as possible. highp vec3 position; highp float inv_radius; mediump vec3 direction; highp float size; mediump vec3 color; mediump float attenuation; mediump float cone_attenuation; mediump float cone_angle; mediump float specular_amount; mediump float shadow_opacity; }; #ifndef DISABLE_LIGHT_OMNI layout(std140) uniform OmniLightData { // ubo:5 LightData omni_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint omni_light_indices[MAX_FORWARD_LIGHTS]; uniform uint omni_light_count; #endif #ifndef DISABLE_LIGHT_SPOT layout(std140) uniform SpotLightData { // ubo:6 LightData spot_lights[MAX_LIGHT_DATA_STRUCTS]; }; uniform uint spot_light_indices[MAX_FORWARD_LIGHTS]; uniform uint spot_light_count; #endif #ifdef USE_ADDITIVE_LIGHTING uniform highp samplerCubeShadow positional_shadow; // texunit:-4 #endif #endif // !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifdef USE_MULTIVIEW uniform highp sampler2DArray depth_buffer; // texunit:-6 uniform highp sampler2DArray color_buffer; // texunit:-5 vec3 multiview_uv(vec2 uv) { return vec3(uv, ViewIndex); } #else uniform highp sampler2D depth_buffer; // texunit:-6 uniform highp sampler2D color_buffer; // texunit:-5 vec2 multiview_uv(vec2 uv) { return uv; } #endif uniform highp mat4 world_transform; uniform mediump float opaque_prepass_threshold; layout(location = 0) out vec4 frag_color; vec3 F0(float metallic, float specular, vec3 albedo) { float dielectric = 0.16 * specular * specular; // use albedo * metallic as colored specular reflectance at 0 angle for metallic materials; // see https://google.github.io/filament/Filament.md.html return mix(vec3(dielectric), albedo, vec3(metallic)); } #if !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) float D_GGX(float cos_theta_m, float alpha) { float a = cos_theta_m * alpha; float k = alpha / (1.0 - cos_theta_m * cos_theta_m + a * a); return k * k * (1.0 / M_PI); } // From Earl Hammon, Jr. "PBR Diffuse Lighting for GGX+Smith Microsurfaces" https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX float V_GGX(float NdotL, float NdotV, float alpha) { return 0.5 / mix(2.0 * NdotL * NdotV, NdotL + NdotV, alpha); } float D_GGX_anisotropic(float cos_theta_m, float alpha_x, float alpha_y, float cos_phi, float sin_phi) { float alpha2 = alpha_x * alpha_y; highp vec3 v = vec3(alpha_y * cos_phi, alpha_x * sin_phi, alpha2 * cos_theta_m); highp float v2 = dot(v, v); float w2 = alpha2 / v2; float D = alpha2 * w2 * w2 * (1.0 / M_PI); return D; } float V_GGX_anisotropic(float alpha_x, float alpha_y, float TdotV, float TdotL, float BdotV, float BdotL, float NdotV, float NdotL) { float Lambda_V = NdotL * length(vec3(alpha_x * TdotV, alpha_y * BdotV, NdotV)); float Lambda_L = NdotV * length(vec3(alpha_x * TdotL, alpha_y * BdotL, NdotL)); return 0.5 / (Lambda_V + Lambda_L); } float SchlickFresnel(float u) { float m = 1.0 - u; float m2 = m * m; return m2 * m2 * m; // pow(m,5) } void light_compute(vec3 N, vec3 L, vec3 V, float A, vec3 light_color, bool is_directional, float attenuation, vec3 f0, float roughness, float metallic, float specular_amount, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 B, vec3 T, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { #if defined(USE_LIGHT_SHADER_CODE) // light is written by the light shader highp mat4 model_matrix = world_transform; mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; vec3 normal = N; vec3 light = L; vec3 view = V; /* clang-format off */ /* clang-format on */ #else float NdotL = min(A + dot(N, L), 1.0); float cNdotL = max(NdotL, 0.0); // clamped NdotL float NdotV = dot(N, V); float cNdotV = max(NdotV, 1e-4); #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) vec3 H = normalize(V + L); #endif #if defined(SPECULAR_SCHLICK_GGX) float cNdotH = clamp(A + dot(N, H), 0.0, 1.0); #endif #if defined(DIFFUSE_BURLEY) || defined(SPECULAR_SCHLICK_GGX) || defined(LIGHT_CLEARCOAT_USED) float cLdotH = clamp(A + dot(L, H), 0.0, 1.0); #endif if (metallic < 1.0) { float diffuse_brdf_NL; // BRDF times N.L for calculating diffuse radiance #if defined(DIFFUSE_LAMBERT_WRAP) // Energy conserving lambert wrap shader. // https://web.archive.org/web/20210228210901/http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/ diffuse_brdf_NL = max(0.0, (NdotL + roughness) / ((1.0 + roughness) * (1.0 + roughness))) * (1.0 / M_PI); #elif defined(DIFFUSE_TOON) diffuse_brdf_NL = smoothstep(-roughness, max(roughness, 0.01), NdotL) * (1.0 / M_PI); #elif defined(DIFFUSE_BURLEY) { 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 / M_PI) * FdV * FdL * cNdotL; } #else // Lambert diffuse_brdf_NL = cNdotL * (1.0 / M_PI); #endif diffuse_light += light_color * diffuse_brdf_NL * attenuation; #if defined(LIGHT_BACKLIGHT_USED) diffuse_light += light_color * (vec3(1.0 / M_PI) - diffuse_brdf_NL) * backlight * attenuation; #endif #if defined(LIGHT_RIM_USED) // Epsilon min to prevent pow(0, 0) singularity which results in undefined behavior. float rim_light = pow(max(1e-4, 1.0 - cNdotV), max(0.0, (1.0 - roughness) * 16.0)); diffuse_light += rim_light * rim * mix(vec3(1.0), albedo, rim_tint) * light_color; #endif } if (roughness > 0.0) { // FIXME: roughness == 0 should not disable specular light entirely // D #if defined(SPECULAR_TOON) vec3 R = normalize(-reflect(L, N)); float RdotV = dot(R, V); float mid = 1.0 - roughness; mid *= mid; float intensity = smoothstep(mid - roughness * 0.5, mid + roughness * 0.5, RdotV) * mid; diffuse_light += light_color * intensity * attenuation * specular_amount; // write to diffuse_light, as in toon shading you generally want no reflection #elif defined(SPECULAR_DISABLED) // none.. #elif defined(SPECULAR_SCHLICK_GGX) // shlick+ggx as default float alpha_ggx = roughness * roughness; #if defined(LIGHT_ANISOTROPY_USED) float aspect = sqrt(1.0 - anisotropy * 0.9); float ax = alpha_ggx / aspect; float ay = alpha_ggx * aspect; float XdotH = dot(T, H); float YdotH = dot(B, H); float D = D_GGX_anisotropic(cNdotH, ax, ay, XdotH, YdotH); float G = V_GGX_anisotropic(ax, ay, dot(T, V), dot(T, L), dot(B, V), dot(B, L), cNdotV, cNdotL); #else float D = D_GGX(cNdotH, alpha_ggx); float G = V_GGX(cNdotL, cNdotV, alpha_ggx); #endif // LIGHT_ANISOTROPY_USED // F float cLdotH5 = SchlickFresnel(cLdotH); // Calculate Fresnel using cheap approximate specular occlusion term from Filament: // https://google.github.io/filament/Filament.html#lighting/occlusion/specularocclusion float f90 = clamp(50.0 * f0.g, 0.0, 1.0); vec3 F = f0 + (f90 - f0) * cLdotH5; vec3 specular_brdf_NL = cNdotL * D * F * G; specular_light += specular_brdf_NL * light_color * attenuation * specular_amount; #endif #if defined(LIGHT_CLEARCOAT_USED) // Clearcoat ignores normal_map, use vertex normal instead float ccNdotL = max(min(A + dot(vertex_normal, L), 1.0), 0.0); float ccNdotH = clamp(A + dot(vertex_normal, H), 0.0, 1.0); float ccNdotV = max(dot(vertex_normal, V), 1e-4); #if !defined(SPECULAR_SCHLICK_GGX) float cLdotH5 = SchlickFresnel(cLdotH); #endif float Dr = D_GGX(ccNdotH, mix(0.001, 0.1, clearcoat_roughness)); float Gr = 0.25 / (cLdotH * cLdotH); float Fr = mix(.04, 1.0, cLdotH5); float clearcoat_specular_brdf_NL = clearcoat * Gr * Fr * Dr * cNdotL; specular_light += clearcoat_specular_brdf_NL * light_color * attenuation * specular_amount; // TODO: Clearcoat adds light to the scene right now (it is non-energy conserving), both diffuse and specular need to be scaled by (1.0 - FR) // but to do so we need to rearrange this entire function #endif // LIGHT_CLEARCOAT_USED } #ifdef USE_SHADOW_TO_OPACITY alpha = min(alpha, clamp(1.0 - attenuation, 0.0, 1.0)); #endif #endif // USE_LIGHT_SHADER_CODE } float get_omni_spot_attenuation(float distance, float inv_range, float decay) { float nd = distance * inv_range; nd *= nd; nd *= nd; // nd^4 nd = max(1.0 - nd, 0.0); nd *= nd; // nd^2 return nd * pow(max(distance, 0.0001), -decay); } #ifndef DISABLE_LIGHT_OMNI void light_process_omni(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = omni_lights[idx].position - vertex; float light_length = length(light_rel_vec); float omni_attenuation = get_omni_spot_attenuation(light_length, omni_lights[idx].inv_radius, omni_lights[idx].attenuation); vec3 color = omni_lights[idx].color; float size_A = 0.0; if (omni_lights[idx].size > 0.0) { float t = omni_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, omni_attenuation, f0, roughness, metallic, omni_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * omni_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT void light_process_spot(uint idx, vec3 vertex, vec3 eye_vec, vec3 normal, vec3 f0, float roughness, float metallic, float shadow, vec3 albedo, inout float alpha, #ifdef LIGHT_BACKLIGHT_USED vec3 backlight, #endif #ifdef LIGHT_RIM_USED float rim, float rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED float clearcoat, float clearcoat_roughness, vec3 vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED vec3 binormal, vec3 tangent, float anisotropy, #endif inout vec3 diffuse_light, inout vec3 specular_light) { vec3 light_rel_vec = spot_lights[idx].position - vertex; float light_length = length(light_rel_vec); float spot_attenuation = get_omni_spot_attenuation(light_length, spot_lights[idx].inv_radius, spot_lights[idx].attenuation); vec3 spot_dir = spot_lights[idx].direction; float scos = max(dot(-normalize(light_rel_vec), spot_dir), spot_lights[idx].cone_angle); float spot_rim = max(0.0001, (1.0 - scos) / (1.0 - spot_lights[idx].cone_angle)); spot_attenuation *= 1.0 - pow(spot_rim, spot_lights[idx].cone_attenuation); vec3 color = spot_lights[idx].color; float size_A = 0.0; if (spot_lights[idx].size > 0.0) { float t = spot_lights[idx].size / max(0.001, light_length); size_A = max(0.0, 1.0 - 1.0 / sqrt(1.0 + t * t)); } light_compute(normal, normalize(light_rel_vec), eye_vec, size_A, color, false, spot_attenuation, f0, roughness, metallic, spot_lights[idx].specular_amount, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim * spot_attenuation, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, vertex_normal, #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !defined(DISABLE_LIGHT_DIRECTIONAL) || !defined(DISABLE_LIGHT_OMNI) || !defined(DISABLE_LIGHT_SPOT) #ifndef MODE_RENDER_DEPTH vec4 fog_process(vec3 vertex) { vec3 fog_color = scene_data.fog_light_color; #ifdef USE_RADIANCE_MAP /* if (scene_data.fog_aerial_perspective > 0.0) { vec3 sky_fog_color = vec3(0.0); vec3 cube_view = scene_data.radiance_inverse_xform * vertex; // mip_level always reads from the second mipmap and higher so the fog is always slightly blurred float mip_level = mix(1.0 / MAX_ROUGHNESS_LOD, 1.0, 1.0 - (abs(vertex.z) - scene_data.z_near) / (scene_data.z_far - scene_data.z_near)); sky_fog_color = textureLod(radiance_map, cube_view, mip_level * RADIANCE_MAX_LOD).rgb; fog_color = mix(fog_color, sky_fog_color, scene_data.fog_aerial_perspective); } */ #endif #ifndef DISABLE_LIGHT_DIRECTIONAL if (scene_data.fog_sun_scatter > 0.001) { vec4 sun_scatter = vec4(0.0); float sun_total = 0.0; vec3 view = normalize(vertex); for (uint i = uint(0); i < scene_data.directional_light_count; i++) { vec3 light_color = directional_lights[i].color * directional_lights[i].energy; float light_amount = pow(max(dot(view, directional_lights[i].direction), 0.0), 8.0); fog_color += light_color * light_amount * scene_data.fog_sun_scatter; } } #endif // !DISABLE_LIGHT_DIRECTIONAL float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density)); if (abs(scene_data.fog_height_density) >= 0.0001) { float y = (scene_data.inv_view_matrix * vec4(vertex, 1.0)).y; float y_dist = y - scene_data.fog_height; float vfog_amount = 1.0 - exp(min(0.0, y_dist * scene_data.fog_height_density)); fog_amount = max(vfog_amount, fog_amount); } return vec4(fog_color, fog_amount); } #endif // !MODE_RENDER_DEPTH void main() { //lay out everything, whatever is unused is optimized away anyway vec3 vertex = vertex_interp; #ifdef USE_MULTIVIEW vec3 eye_offset = multiview_data.eye_offset[ViewIndex].xyz; vec3 view = -normalize(vertex_interp - eye_offset); mat4 projection_matrix = multiview_data.projection_matrix_view[ViewIndex]; mat4 inv_projection_matrix = multiview_data.inv_projection_matrix_view[ViewIndex]; #else vec3 eye_offset = vec3(0.0, 0.0, 0.0); vec3 view = -normalize(vertex_interp); mat4 projection_matrix = scene_data.projection_matrix; mat4 inv_projection_matrix = scene_data.inv_projection_matrix; #endif highp mat4 model_matrix = world_transform; vec3 albedo = vec3(1.0); vec3 backlight = vec3(0.0); vec4 transmittance_color = vec4(0.0, 0.0, 0.0, 1.0); float transmittance_depth = 0.0; float transmittance_boost = 0.0; float metallic = 0.0; float specular = 0.5; vec3 emission = vec3(0.0); float roughness = 1.0; float rim = 0.0; float rim_tint = 0.0; float clearcoat = 0.0; float clearcoat_roughness = 0.0; float anisotropy = 0.0; vec2 anisotropy_flow = vec2(1.0, 0.0); vec4 fog = vec4(0.0); #if defined(CUSTOM_RADIANCE_USED) vec4 custom_radiance = vec4(0.0); #endif #if defined(CUSTOM_IRRADIANCE_USED) vec4 custom_irradiance = vec4(0.0); #endif float ao = 1.0; float ao_light_affect = 0.0; float alpha = 1.0; #if defined(TANGENT_USED) || defined(NORMAL_MAP_USED) || defined(LIGHT_ANISOTROPY_USED) vec3 binormal = normalize(binormal_interp); vec3 tangent = normalize(tangent_interp); #else vec3 binormal = vec3(0.0); vec3 tangent = vec3(0.0); #endif #ifdef NORMAL_USED vec3 normal = normalize(normal_interp); #if defined(DO_SIDE_CHECK) if (!gl_FrontFacing) { normal = -normal; } #endif #endif //NORMAL_USED #ifdef UV_USED vec2 uv = uv_interp; #endif #if defined(UV2_USED) || defined(USE_LIGHTMAP) vec2 uv2 = uv2_interp; #endif #if defined(COLOR_USED) vec4 color = color_interp; #endif #if defined(NORMAL_MAP_USED) vec3 normal_map = vec3(0.5); #endif float normal_map_depth = 1.0; vec2 screen_uv = gl_FragCoord.xy * scene_data.screen_pixel_size; float sss_strength = 0.0; #ifdef ALPHA_SCISSOR_USED float alpha_scissor_threshold = 1.0; #endif // ALPHA_SCISSOR_USED #ifdef ALPHA_HASH_USED float alpha_hash_scale = 1.0; #endif // ALPHA_HASH_USED #ifdef ALPHA_ANTIALIASING_EDGE_USED float alpha_antialiasing_edge = 0.0; vec2 alpha_texture_coordinate = vec2(0.0, 0.0); #endif // ALPHA_ANTIALIASING_EDGE_USED { } #ifndef USE_SHADOW_TO_OPACITY #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor_threshold) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // !USE_SHADOW_TO_OPACITY #ifdef NORMAL_MAP_USED normal_map.xy = normal_map.xy * 2.0 - 1.0; normal_map.z = sqrt(max(0.0, 1.0 - dot(normal_map.xy, normal_map.xy))); //always ignore Z, as it can be RG packed, Z may be pos/neg, etc. normal = normalize(mix(normal, tangent * normal_map.x + binormal * normal_map.y + normal * normal_map.z, normal_map_depth)); #endif #ifdef LIGHT_ANISOTROPY_USED if (anisotropy > 0.01) { //rotation matrix mat3 rot = mat3(tangent, binormal, normal); //make local to space tangent = normalize(rot * vec3(anisotropy_flow.x, anisotropy_flow.y, 0.0)); binormal = normalize(rot * vec3(-anisotropy_flow.y, anisotropy_flow.x, 0.0)); } #endif #ifndef MODE_RENDER_DEPTH #ifndef CUSTOM_FOG_USED #ifndef DISABLE_FOG // fog must be processed as early as possible and then packed. // to maximize VGPR usage if (scene_data.fog_enabled) { fog = fog_process(vertex); } #endif // !DISABLE_FOG #endif // !CUSTOM_FOG_USED uint fog_rg = packHalf2x16(fog.rg); uint fog_ba = packHalf2x16(fog.ba); // Convert colors to linear albedo = srgb_to_linear(albedo); emission = srgb_to_linear(emission); // TODO Backlight and transmittance when used #ifndef MODE_UNSHADED vec3 f0 = F0(metallic, specular, albedo); vec3 specular_light = vec3(0.0, 0.0, 0.0); vec3 diffuse_light = vec3(0.0, 0.0, 0.0); vec3 ambient_light = vec3(0.0, 0.0, 0.0); #ifdef BASE_PASS /////////////////////// LIGHTING ////////////////////////////// // IBL precalculations float ndotv = clamp(dot(normal, view), 0.0, 1.0); vec3 F = f0 + (max(vec3(1.0 - roughness), f0) - f0) * pow(1.0 - ndotv, 5.0); #ifdef USE_RADIANCE_MAP if (scene_data.use_reflection_cubemap) { #ifdef LIGHT_ANISOTROPY_USED // https://google.github.io/filament/Filament.html#lighting/imagebasedlights/anisotropy vec3 anisotropic_direction = anisotropy >= 0.0 ? binormal : tangent; vec3 anisotropic_tangent = cross(anisotropic_direction, view); vec3 anisotropic_normal = cross(anisotropic_tangent, anisotropic_direction); vec3 bent_normal = normalize(mix(normal, anisotropic_normal, abs(anisotropy) * clamp(5.0 * roughness, 0.0, 1.0))); vec3 ref_vec = reflect(-view, bent_normal); #else vec3 ref_vec = reflect(-view, normal); #endif ref_vec = mix(ref_vec, normal, roughness * roughness); float horizon = min(1.0 + dot(ref_vec, normal), 1.0); ref_vec = scene_data.radiance_inverse_xform * ref_vec; specular_light = textureLod(radiance_map, ref_vec, sqrt(roughness) * RADIANCE_MAX_LOD).rgb; specular_light = srgb_to_linear(specular_light); specular_light *= horizon * horizon; specular_light *= scene_data.ambient_light_color_energy.a; } #endif // Calculate Reflection probes // Calculate Lightmaps #if defined(CUSTOM_RADIANCE_USED) specular_light = mix(specular_light, custom_radiance.rgb, custom_radiance.a); #endif // CUSTOM_RADIANCE_USED #ifndef USE_LIGHTMAP //lightmap overrides everything if (scene_data.use_ambient_light) { ambient_light = scene_data.ambient_light_color_energy.rgb; #ifdef USE_RADIANCE_MAP if (scene_data.use_ambient_cubemap) { vec3 ambient_dir = scene_data.radiance_inverse_xform * normal; vec3 cubemap_ambient = textureLod(radiance_map, ambient_dir, RADIANCE_MAX_LOD).rgb; cubemap_ambient = srgb_to_linear(cubemap_ambient); ambient_light = mix(ambient_light, cubemap_ambient * scene_data.ambient_light_color_energy.a, scene_data.ambient_color_sky_mix); } #endif } #endif // USE_LIGHTMAP #if defined(CUSTOM_IRRADIANCE_USED) ambient_light = mix(ambient_light, custom_irradiance.rgb, custom_irradiance.a); #endif // CUSTOM_IRRADIANCE_USED { #if defined(AMBIENT_LIGHT_DISABLED) ambient_light = vec3(0.0, 0.0, 0.0); #else ambient_light *= albedo.rgb; ambient_light *= ao; #endif // AMBIENT_LIGHT_DISABLED } // convert ao to direct light ao ao = mix(1.0, ao, ao_light_affect); { #if defined(DIFFUSE_TOON) //simplify for toon, as specular_light *= specular * metallic * albedo * 2.0; #else // scales the specular reflections, needs to be be computed before lighting happens, // but after environment, GI, and reflection probes are added // Environment brdf approximation (Lazarov 2013) // see https://www.unrealengine.com/en-US/blog/physically-based-shading-on-mobile const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); vec4 r = roughness * c0 + c1; float ndotv = clamp(dot(normal, view), 0.0, 1.0); float a004 = min(r.x * r.x, exp2(-9.28 * ndotv)) * r.x + r.y; vec2 env = vec2(-1.04, 1.04) * a004 + r.zw; specular_light *= env.x * f0 + env.y * clamp(50.0 * f0.g, metallic, 1.0); #endif } #endif // BASE_PASS #ifndef DISABLE_LIGHT_DIRECTIONAL //diffuse_light = normal; // for (uint i = uint(0); i < scene_data.directional_light_count; i++) { light_compute(normal, normalize(directional_lights[i].direction), normalize(view), directional_lights[i].size, directional_lights[i].color * directional_lights[i].energy, true, 1.0, f0, roughness, metallic, 1.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_DIRECTIONAL #ifndef DISABLE_LIGHT_OMNI for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= omni_light_count) { break; } light_process_omni(omni_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED binormal, tangent, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_OMNI #ifndef DISABLE_LIGHT_SPOT for (uint i = 0u; i < MAX_FORWARD_LIGHTS; i++) { if (i >= spot_light_count) { break; } light_process_spot(spot_light_indices[i], vertex, view, normal, f0, roughness, metallic, 0.0, albedo, alpha, #ifdef LIGHT_BACKLIGHT_USED backlight, #endif #ifdef LIGHT_RIM_USED rim, rim_tint, #endif #ifdef LIGHT_CLEARCOAT_USED clearcoat, clearcoat_roughness, normalize(normal_interp), #endif #ifdef LIGHT_ANISOTROPY_USED tangent, binormal, anisotropy, #endif diffuse_light, specular_light); } #endif // !DISABLE_LIGHT_SPOT #endif // !MODE_UNSHADED #endif // !MODE_RENDER_DEPTH #if defined(USE_SHADOW_TO_OPACITY) alpha = min(alpha, clamp(length(ambient_light), 0.0, 1.0)); #if defined(ALPHA_SCISSOR_USED) if (alpha < alpha_scissor) { discard; } #endif // ALPHA_SCISSOR_USED #ifdef USE_OPAQUE_PREPASS #if !defined(ALPHA_SCISSOR_USED) if (alpha < opaque_prepass_threshold) { discard; } #endif // not ALPHA_SCISSOR_USED #endif // USE_OPAQUE_PREPASS #endif // USE_SHADOW_TO_OPACITY #ifdef MODE_RENDER_DEPTH //nothing happens, so a tree-ssa optimizer will result in no fragment shader :) #else // !MODE_RENDER_DEPTH #ifdef MODE_UNSHADED frag_color = vec4(albedo, alpha); #else diffuse_light *= albedo; diffuse_light *= 1.0 - metallic; ambient_light *= 1.0 - metallic; frag_color = vec4(diffuse_light + specular_light, alpha); #ifdef BASE_PASS frag_color.rgb += emission + ambient_light; #endif #endif //MODE_UNSHADED fog = vec4(unpackHalf2x16(fog_rg), unpackHalf2x16(fog_ba)); #ifndef DISABLE_FOG if (scene_data.fog_enabled) { #ifdef BASE_PASS frag_color.rgb = mix(frag_color.rgb, fog.rgb, fog.a); #else frag_color.rgb *= (1.0 - fog.a); #endif // BASE_PASS } #endif // Tonemap before writing as we are writing to an sRGB framebuffer frag_color.rgb *= exposure; frag_color.rgb = apply_tonemapping(frag_color.rgb, white); frag_color.rgb = linear_to_srgb(frag_color.rgb); #ifdef USE_BCS frag_color.rgb = apply_bcs(frag_color.rgb, bcs); #endif #ifdef USE_COLOR_CORRECTION frag_color.rgb = apply_color_correction(frag_color.rgb, color_correction); #endif #endif //!MODE_RENDER_DEPTH } Terminate batch job (Y/N)?