Files
cocos4/editor/assets/chunks/shading-standard-base.chunk
T
Myway 60e44f7564 terrain and model automatic choose lightmap shader (#11006)
* terrain and model automatic choose lightmap shader

* fix eslint error
2022-05-12 01:43:32 +08:00

172 lines
4.7 KiB
Plaintext

// Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
// reference: 'moving frostbite to pbr' & UE4 BRDF.usf
#include <cc-global>
#include <common>
#include <gamma>
#include <cc-shadow-map-base>
#if CC_USE_IBL
#include <cc-environment>
#include <texture-lod>
#include <unpack>
#if CC_USE_DIFFUSEMAP
#include <cc-diffusemap>
#endif
#endif
float GGXMobile (float roughness, float NoH, vec3 H, vec3 N) {
vec3 NxH = cross(N, H);
float OneMinusNoHSqr = dot(NxH, NxH);
float a = roughness * roughness;
float n = NoH * a;
float p = a / (OneMinusNoHSqr + n * n);
return p * p;
}
float CalcSpecular (float roughness, float NoH, vec3 H, vec3 N) {
return (roughness * 0.25 + 0.25) * GGXMobile(roughness, NoH, H, N);
}
vec3 BRDFApprox (vec3 specular, float roughness, float NoV) {
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 a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
vec2 AB = vec2(-1.04, 1.04) * a004 + r.zw;
AB.y *= clamp(50.0 * specular.g, 0.0, 1.0);
return specular * AB.x + AB.y;
}
#if USE_REFLECTION_DENOISE
#pragma extension([GL_OES_standard_derivatives, __VERSION__ < 110])
vec3 GetEnvReflectionWithMipFiltering(vec3 R, float roughness, float mipCount, float denoiseIntensity) {
#if CC_USE_IBL
float mip = roughness * mipCount;
float delta = (dot(dFdx(R), dFdy(R))) * 1000.0;
float mipBias = mix(0.0, 5.0, clamp(delta, 0.0, 1.0));
vec4 biased = fragTextureLod(cc_environment, R, mip + mipBias);
vec4 filtered = texture(cc_environment, R);
#if CC_USE_IBL == IBL_RGBE
biased.rgb = unpackRGBE(biased);
filtered.rgb = unpackRGBE(filtered);
#else
biased.rgb = SRGBToLinear(biased.rgb);
filtered.rgb = SRGBToLinear(filtered.rgb);
#endif
return mix(biased.rgb, filtered.rgb, denoiseIntensity);
#else
return vec3(0.0, 0.0, 0.0);
#endif
}
#endif
struct StandardSurface {
// albedo
vec4 albedo;
// these two need to be in the same coordinate system
HIGHP_VALUE_STRUCT_DEFINE(vec3, position);
vec3 normal;
// emissive
vec3 emissive;
// light map
vec3 lightmap;
float lightmap_test;
// PBR params
float roughness;
float metallic;
float occlusion;
float specularIntensity;
#if CC_RECEIVE_SHADOW
vec2 shadowBias;
#endif
};
vec4 CCStandardShadingBase (StandardSurface s, vec4 shadowPos) {
// Calculate diffuse & specular
vec3 diffuse = s.albedo.rgb * (1.0 - s.metallic);
vec3 specular = mix(vec3(0.08 * s.specularIntensity), s.albedo.rgb, s.metallic);
vec3 position;
HIGHP_VALUE_FROM_STRUCT_DEFINED(position, s.position);
vec3 N = normalize(s.normal);
vec3 V = normalize(cc_cameraPos.xyz - position);
float NV = max(abs(dot(N, V)), 0.0);
specular = BRDFApprox(specular, s.roughness, NV);
vec3 L = normalize(-cc_mainLitDir.xyz);
vec3 H = normalize(L + V);
float NH = max(dot(N, H), 0.0);
float NL = max(dot(N, L), 0.0);
vec3 finalColor = NL * cc_mainLitColor.rgb * cc_mainLitColor.w;
vec3 diffuseContrib = diffuse / PI;
// Cook-Torrance Microfacet Specular BRDF
vec3 specularContrib = specular * CalcSpecular(s.roughness, NH, H, N);
vec3 dirlightContrib = (diffuseContrib + specularContrib);
float shadow = 1.0;
#if CC_RECEIVE_SHADOW
if (NL > 0.0 && cc_mainLitDir.w > 0.0) {
shadow = CCShadowFactorBase(shadowPos, N, s.shadowBias);
}
#endif
dirlightContrib *= shadow;
finalColor *= dirlightContrib;
#if CC_USE_LIGHTMAP && !USE_BATCHING && !CC_FORWARD_ADD
if (s.lightmap_test > EPSILON_LOWP) {
finalColor = diffuse * s.lightmap.rgb * shadow;
}
#endif
float fAmb = 0.5 - N.y * 0.5;
vec3 ambDiff = mix(cc_ambientSky.rgb, cc_ambientGround.rgb, fAmb);
#if CC_USE_IBL
#if CC_USE_DIFFUSEMAP
// Diffuse reflection irradiance
vec4 diffuseMap = texture(cc_diffuseMap, N);
#if CC_USE_DIFFUSEMAP == IBL_RGBE
ambDiff = unpackRGBE(diffuseMap);
#else
ambDiff = SRGBToLinear(diffuseMap.rgb);
#endif
#endif
vec3 R = normalize(reflect(-V, N));
#if USE_REFLECTION_DENOISE
vec3 env = GetEnvReflectionWithMipFiltering(R, s.roughness, cc_ambientGround.w, 0.6);
#else
vec4 envmap = fragTextureLod(cc_environment, R, s.roughness * cc_ambientGround.w);
#if CC_USE_IBL == IBL_RGBE
vec3 env = unpackRGBE(envmap);
#else
vec3 env = SRGBToLinear(envmap.rgb);
#endif
#endif
finalColor += env * cc_ambientSky.w * specular * s.occlusion;
#endif
finalColor += ambDiff.rgb * cc_ambientSky.w * diffuse * s.occlusion;
finalColor += s.emissive;
return vec4(finalColor, s.albedo.a);
}