123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- #if defined(BUMP) || !defined(NORMAL) || defined(FORCENORMALFORWARD) || defined(SPECULARAA) || defined(CLEARCOAT_BUMP) || defined(ANISOTROPIC)
- #extension GL_OES_standard_derivatives : enable
- #endif
- #ifdef LODBASEDMICROSFURACE
- #extension GL_EXT_shader_texture_lod : enable
- #endif
- #define CUSTOM_FRAGMENT_BEGIN
- #ifdef LOGARITHMICDEPTH
- #extension GL_EXT_frag_depth : enable
- #endif
- precision highp float;
- #include<prePassDeclaration>[SCENE_MRT_COUNT]
- // Forces linear space for image processing
- #ifndef FROMLINEARSPACE
- #define FROMLINEARSPACE
- #endif
- // Declaration
- #include<__decl__pbrFragment>
- #include<pbrFragmentExtraDeclaration>
- #include<__decl__lightFragment>[0..maxSimultaneousLights]
- #include<pbrFragmentSamplersDeclaration>
- #include<imageProcessingDeclaration>
- #include<clipPlaneFragmentDeclaration>
- #include<logDepthDeclaration>
- #include<fogFragmentDeclaration>
- // Helper Functions
- #include<helperFunctions>
- #include<subSurfaceScatteringFunctions>
- #include<importanceSampling>
- #include<pbrHelperFunctions>
- #include<imageProcessingFunctions>
- #include<shadowsFragmentFunctions>
- #include<harmonicsFunctions>
- #include<pbrDirectLightingSetupFunctions>
- #include<pbrDirectLightingFalloffFunctions>
- #include<pbrBRDFFunctions>
- #include<hdrFilteringFunctions>
- #include<pbrDirectLightingFunctions>
- #include<pbrIBLFunctions>
- #include<bumpFragmentMainFunctions>
- #include<bumpFragmentFunctions>
- #ifdef REFLECTION
- #include<reflectionFunction>
- #endif
- #define CUSTOM_FRAGMENT_DEFINITIONS
- #include<pbrBlockAlbedoOpacity>
- #include<pbrBlockReflectivity>
- #include<pbrBlockAmbientOcclusion>
- #include<pbrBlockAlphaFresnel>
- #include<pbrBlockAnisotropic>
- #include<pbrBlockReflection>
- #include<pbrBlockSheen>
- #include<pbrBlockClearcoat>
- #include<pbrBlockSubSurface>
- // _____________________________ MAIN FUNCTION ____________________________
- void main(void) {
- #define CUSTOM_FRAGMENT_MAIN_BEGIN
- #include<clipPlaneFragment>
- // _____________________________ Geometry Information ____________________________
- #include<pbrBlockNormalGeometric>
- #include<bumpFragment>
- #include<pbrBlockNormalFinal>
- // _____________________________ Albedo & Opacity ______________________________
- albedoOpacityOutParams albedoOpacityOut;
- #ifdef ALBEDO
- vec4 albedoTexture = texture2D(albedoSampler, vAlbedoUV + uvOffset);
- #endif
- #ifdef OPACITY
- vec4 opacityMap = texture2D(opacitySampler, vOpacityUV + uvOffset);
- #endif
- albedoOpacityBlock(
- vAlbedoColor,
- #ifdef ALBEDO
- albedoTexture,
- vAlbedoInfos,
- #endif
- #ifdef OPACITY
- opacityMap,
- vOpacityInfos,
- #endif
- #ifdef DETAIL
- detailColor,
- vDetailInfos,
- #endif
- albedoOpacityOut
- );
- vec3 surfaceAlbedo = albedoOpacityOut.surfaceAlbedo;
- float alpha = albedoOpacityOut.alpha;
- #define CUSTOM_FRAGMENT_UPDATE_ALPHA
- #include<depthPrePass>
- #define CUSTOM_FRAGMENT_BEFORE_LIGHTS
- // _____________________________ AO _______________________________
- ambientOcclusionOutParams aoOut;
- #ifdef AMBIENT
- vec3 ambientOcclusionColorMap = texture2D(ambientSampler, vAmbientUV + uvOffset).rgb;
- #endif
- ambientOcclusionBlock(
- #ifdef AMBIENT
- ambientOcclusionColorMap,
- vAmbientInfos,
- #endif
- aoOut
- );
- #include<pbrBlockLightmapInit>
- #ifdef UNLIT
- vec3 diffuseBase = vec3(1., 1., 1.);
- #else
- // _____________________________ Reflectivity _______________________________
- vec3 baseColor = surfaceAlbedo;
- reflectivityOutParams reflectivityOut;
- #if defined(REFLECTIVITY)
- vec4 surfaceMetallicOrReflectivityColorMap = texture2D(reflectivitySampler, vReflectivityUV + uvOffset);
- vec4 baseReflectivity = surfaceMetallicOrReflectivityColorMap;
- #ifndef METALLICWORKFLOW
- surfaceMetallicOrReflectivityColorMap = toLinearSpace(surfaceMetallicOrReflectivityColorMap);
- surfaceMetallicOrReflectivityColorMap.rgb *= vReflectivityInfos.y;
- #endif
- #endif
- #if defined(MICROSURFACEMAP)
- vec4 microSurfaceTexel = texture2D(microSurfaceSampler, vMicroSurfaceSamplerUV + uvOffset) * vMicroSurfaceSamplerInfos.y;
- #endif
- #ifdef METALLICWORKFLOW
- vec4 metallicReflectanceFactors = vMetallicReflectanceFactors;
- #ifdef METALLIC_REFLECTANCE
- vec4 metallicReflectanceFactorsMap = texture2D(metallicReflectanceSampler, vMetallicReflectanceUV + uvOffset);
- metallicReflectanceFactorsMap = toLinearSpace(metallicReflectanceFactorsMap);
- metallicReflectanceFactors *= metallicReflectanceFactorsMap;
- #endif
- #endif
- reflectivityBlock(
- vReflectivityColor,
- #ifdef METALLICWORKFLOW
- surfaceAlbedo,
- metallicReflectanceFactors,
- #endif
- #ifdef REFLECTIVITY
- vReflectivityInfos,
- surfaceMetallicOrReflectivityColorMap,
- #endif
- #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
- aoOut.ambientOcclusionColor,
- #endif
- #ifdef MICROSURFACEMAP
- microSurfaceTexel,
- #endif
- #ifdef DETAIL
- detailColor,
- vDetailInfos,
- #endif
- reflectivityOut
- );
- float microSurface = reflectivityOut.microSurface;
- float roughness = reflectivityOut.roughness;
- #ifdef METALLICWORKFLOW
- surfaceAlbedo = reflectivityOut.surfaceAlbedo;
- #endif
- #if defined(METALLICWORKFLOW) && defined(REFLECTIVITY) && defined(AOSTOREINMETALMAPRED)
- aoOut.ambientOcclusionColor = reflectivityOut.ambientOcclusionColor;
- #endif
- // _____________________________ Alpha Fresnel ___________________________________
- #ifdef ALPHAFRESNEL
- #if defined(ALPHATEST) || defined(ALPHABLEND)
- alphaFresnelOutParams alphaFresnelOut;
- alphaFresnelBlock(
- normalW,
- viewDirectionW,
- alpha,
- microSurface,
- alphaFresnelOut
- );
- alpha = alphaFresnelOut.alpha;
- #endif
- #endif
- // _____________________________ Compute Geometry info _________________________________
- #include<pbrBlockGeometryInfo>
- // _____________________________ Anisotropy _______________________________________
- #ifdef ANISOTROPIC
- anisotropicOutParams anisotropicOut;
- #ifdef ANISOTROPIC_TEXTURE
- vec3 anisotropyMapData = texture2D(anisotropySampler, vAnisotropyUV + uvOffset).rgb * vAnisotropyInfos.y;
- #endif
- anisotropicBlock(
- vAnisotropy,
- #ifdef ANISOTROPIC_TEXTURE
- anisotropyMapData,
- #endif
- TBN,
- normalW,
- viewDirectionW,
- anisotropicOut
- );
- #endif
- // _____________________________ Reflection Info _______________________________________
- #ifdef REFLECTION
- reflectionOutParams reflectionOut;
- reflectionBlock(
- vPositionW,
- normalW,
- alphaG,
- vReflectionMicrosurfaceInfos,
- vReflectionInfos,
- vReflectionColor,
- #ifdef ANISOTROPIC
- anisotropicOut,
- #endif
- #if defined(LODINREFLECTIONALPHA) && !defined(REFLECTIONMAP_SKYBOX)
- NdotVUnclamped,
- #endif
- #ifdef LINEARSPECULARREFLECTION
- roughness,
- #endif
- reflectionSampler,
- #if defined(NORMAL) && defined(USESPHERICALINVERTEX)
- vEnvironmentIrradiance,
- #endif
- #ifdef USESPHERICALFROMREFLECTIONMAP
- #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX)
- reflectionMatrix,
- #endif
- #endif
- #ifdef USEIRRADIANCEMAP
- irradianceSampler,
- #endif
- #ifndef LODBASEDMICROSFURACE
- reflectionSamplerLow,
- reflectionSamplerHigh,
- #endif
- reflectionOut
- );
- #endif
- // ___________________ Compute Reflectance aka R0 F0 info _________________________
- #include<pbrBlockReflectance0>
- // ________________________________ Sheen ______________________________
- #ifdef SHEEN
- sheenOutParams sheenOut;
- #ifdef SHEEN_TEXTURE
- vec4 sheenMapData = toLinearSpace(texture2D(sheenSampler, vSheenUV + uvOffset)) * vSheenInfos.y;
- #endif
- sheenBlock(
- vSheenColor,
- #ifdef SHEEN_ROUGHNESS
- vSheenRoughness,
- #endif
- roughness,
- #ifdef SHEEN_TEXTURE
- sheenMapData,
- #endif
- reflectance,
- #ifdef SHEEN_LINKWITHALBEDO
- baseColor,
- surfaceAlbedo,
- #endif
- #ifdef ENVIRONMENTBRDF
- NdotV,
- environmentBrdf,
- #endif
- #if defined(REFLECTION) && defined(ENVIRONMENTBRDF)
- AARoughnessFactors,
- vReflectionMicrosurfaceInfos,
- vReflectionInfos,
- vReflectionColor,
- vLightingIntensity,
- reflectionSampler,
- reflectionOut.reflectionCoords,
- NdotVUnclamped,
- #ifndef LODBASEDMICROSFURACE
- reflectionSamplerLow,
- reflectionSamplerHigh,
- #endif
- #if !defined(REFLECTIONMAP_SKYBOX) && defined(RADIANCEOCCLUSION)
- seo,
- #endif
- #if !defined(REFLECTIONMAP_SKYBOX) && defined(HORIZONOCCLUSION) && defined(BUMP) && defined(REFLECTIONMAP_3D)
- eho,
- #endif
- #endif
- sheenOut
- );
- #ifdef SHEEN_LINKWITHALBEDO
- surfaceAlbedo = sheenOut.surfaceAlbedo;
- #endif
- #endif
- // _____________________________ Clear Coat ____________________________
- clearcoatOutParams clearcoatOut;
- #ifdef CLEARCOAT
- #ifdef CLEARCOAT_TEXTURE
- vec2 clearCoatMapData = texture2D(clearCoatSampler, vClearCoatUV + uvOffset).rg * vClearCoatInfos.y;
- #endif
- #if defined(CLEARCOAT_TINT) && defined(CLEARCOAT_TINT_TEXTURE)
- vec4 clearCoatTintMapData = toLinearSpace(texture2D(clearCoatTintSampler, vClearCoatTintUV + uvOffset));
- #endif
- #ifdef CLEARCOAT_BUMP
- vec4 clearCoatBumpMapData = texture2D(clearCoatBumpSampler, vClearCoatBumpUV + uvOffset);
- #endif
- clearcoatBlock(
- vPositionW,
- geometricNormalW,
- viewDirectionW,
- vClearCoatParams,
- specularEnvironmentR0,
- #ifdef CLEARCOAT_TEXTURE
- clearCoatMapData,
- #endif
- #ifdef CLEARCOAT_TINT
- vClearCoatTintParams,
- clearCoatColorAtDistance,
- vClearCoatRefractionParams,
- #ifdef CLEARCOAT_TINT_TEXTURE
- clearCoatTintMapData,
- #endif
- #endif
- #ifdef CLEARCOAT_BUMP
- vClearCoatBumpInfos,
- clearCoatBumpMapData,
- vClearCoatBumpUV,
- #if defined(TANGENT) && defined(NORMAL)
- vTBN,
- #else
- vClearCoatTangentSpaceParams,
- #endif
- #ifdef OBJECTSPACE_NORMALMAP
- normalMatrix,
- #endif
- #endif
- #if defined(FORCENORMALFORWARD) && defined(NORMAL)
- faceNormal,
- #endif
- #ifdef REFLECTION
- vReflectionMicrosurfaceInfos,
- vReflectionInfos,
- vReflectionColor,
- vLightingIntensity,
- reflectionSampler,
- #ifndef LODBASEDMICROSFURACE
- reflectionSamplerLow,
- reflectionSamplerHigh,
- #endif
- #endif
- #if defined(ENVIRONMENTBRDF) && !defined(REFLECTIONMAP_SKYBOX)
- #ifdef RADIANCEOCCLUSION
- ambientMonochrome,
- #endif
- #endif
- clearcoatOut
- );
- #else
- clearcoatOut.specularEnvironmentR0 = specularEnvironmentR0;
- #endif
- // _________________________ Specular Environment Reflectance __________________________
- #include<pbrBlockReflectance>
- // ___________________________________ SubSurface ______________________________________
- subSurfaceOutParams subSurfaceOut;
- #ifdef SUBSURFACE
- #ifdef SS_THICKNESSANDMASK_TEXTURE
- vec4 thicknessMap = texture2D(thicknessSampler, vThicknessUV + uvOffset);
- #endif
- subSurfaceBlock(
- vSubSurfaceIntensity,
- vThicknessParam,
- vTintColor,
- normalW,
- specularEnvironmentReflectance,
- #ifdef SS_THICKNESSANDMASK_TEXTURE
- thicknessMap,
- #endif
- #ifdef REFLECTION
- #ifdef SS_TRANSLUCENCY
- reflectionMatrix,
- #ifdef USESPHERICALFROMREFLECTIONMAP
- #if !defined(NORMAL) || !defined(USESPHERICALINVERTEX)
- reflectionOut.irradianceVector,
- #endif
- #endif
- #ifdef USEIRRADIANCEMAP
- irradianceSampler,
- #endif
- #endif
- #endif
- #ifdef SS_REFRACTION
- vPositionW,
- viewDirectionW,
- view,
- surfaceAlbedo,
- vRefractionInfos,
- refractionMatrix,
- vRefractionMicrosurfaceInfos,
- vLightingIntensity,
- #ifdef SS_LINKREFRACTIONTOTRANSPARENCY
- alpha,
- #endif
- #ifdef SS_LODINREFRACTIONALPHA
- NdotVUnclamped,
- #endif
- #ifdef SS_LINEARSPECULARREFRACTION
- roughness,
- #else
- alphaG,
- #endif
- refractionSampler,
- #ifndef LODBASEDMICROSFURACE
- refractionSamplerLow,
- refractionSamplerHigh,
- #endif
- #ifdef ANISOTROPIC
- anisotropicOut,
- #endif
- #endif
- #ifdef SS_TRANSLUCENCY
- vDiffusionDistance,
- #endif
- subSurfaceOut
- );
- #ifdef SS_REFRACTION
- surfaceAlbedo = subSurfaceOut.surfaceAlbedo;
- #ifdef SS_LINKREFRACTIONTOTRANSPARENCY
- alpha = subSurfaceOut.alpha;
- #endif
- #endif
- #else
- subSurfaceOut.specularEnvironmentReflectance = specularEnvironmentReflectance;
- #endif
- // _____________________________ Direct Lighting Info __________________________________
- #include<pbrBlockDirectLighting>
- #include<lightFragment>[0..maxSimultaneousLights]
- // _____________________________ Compute Final Lit Components ________________________
- #include<pbrBlockFinalLitComponents>
- #endif // UNLIT
- #include<pbrBlockFinalUnlitComponents>
- #include<pbrBlockFinalColorComposition>
- #include<logDepthFragment>
- #include<fogFragment>(color, finalColor)
- #include<pbrBlockImageProcessing>
- #define CUSTOM_FRAGMENT_BEFORE_FRAGCOLOR
- #ifdef PREPASS
- #ifdef PREPASS_POSITION
- gl_FragData[PREPASS_POSITION_INDEX] = vec4(vPositionW, 1.0);
- #endif
- #ifdef PREPASS_VELOCITY
- vec2 a = (vCurrentPosition.xy / vCurrentPosition.w) * 0.5 + 0.5;
- vec2 b = (vPreviousPosition.xy / vPreviousPosition.w) * 0.5 + 0.5;
- vec2 velocity = abs(a - b);
- velocity = vec2(pow(velocity.x, 1.0 / 3.0), pow(velocity.y, 1.0 / 3.0)) * sign(a - b) * 0.5 + 0.5;
- gl_FragData[PREPASS_VELOCITY_INDEX] = vec4(velocity, 0.0, 1.0);
- #endif
- #ifdef PREPASS_IRRADIANCE
- vec3 irradiance = finalDiffuse;
- #ifndef UNLIT
- #ifdef REFLECTION
- irradiance += finalIrradiance;
- #endif
- #endif
- vec3 sqAlbedo = sqrt(surfaceAlbedo); // for pre and post scatter
- gl_FragData[0] = vec4(finalColor.rgb - irradiance, finalColor.a); // Split irradiance from final color
- irradiance /= sqAlbedo;
-
- #ifndef SS_SCATTERING
- float scatteringDiffusionProfile = 255.;
- #endif
- gl_FragData[PREPASS_IRRADIANCE_INDEX] = vec4(tagLightingForSSS(irradiance), scatteringDiffusionProfile / 255.); // Irradiance + SS diffusion profile
- #else
- gl_FragData[0] = vec4(finalColor.rgb, finalColor.a);
- #endif
- #ifdef PREPASS_DEPTHNORMAL
- gl_FragData[PREPASS_DEPTHNORMAL_INDEX] = vec4(vViewPos.z, (view * vec4(normalW, 0.0)).rgb); // Linear depth + normal
- #endif
- #ifdef PREPASS_ALBEDO
- gl_FragData[PREPASS_ALBEDO_INDEX] = vec4(sqAlbedo, 1.0); // albedo, for pre and post scatter
- #endif
- #ifdef PREPASS_REFLECTIVITY
- #if defined(REFLECTIVITY)
- gl_FragData[PREPASS_REFLECTIVITY_INDEX] = vec4(baseReflectivity.rgb, 1.0);
- #else
- gl_FragData[PREPASS_REFLECTIVITY_INDEX] = vec4(0.0, 0.0, 0.0, 1.0);
- #endif
- #endif
- #endif
- gl_FragColor = finalColor;
- #include<pbrDebug>
- }
|