Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial implementation for Area Lights #16078

Merged
merged 96 commits into from
Jan 23, 2025
Merged
Changes from 1 commit
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
a82ca13
Added area lights object
SergioRZMasson Oct 18, 2024
19f3e2e
Added area lights object
SergioRZMasson Oct 18, 2024
cc027a1
Added initial shader implementation
SergioRZMasson Oct 21, 2024
6657813
Fixed shader tipo
SergioRZMasson Oct 21, 2024
44553d8
Fixed shader compilation errors
SergioRZMasson Oct 21, 2024
267157e
Fixed texures bind
SergioRZMasson Oct 21, 2024
8c3786a
Trying to get textures to work
SergioRZMasson Oct 21, 2024
e1f9c2d
Get specular to work
SergioRZMasson Oct 22, 2024
f4f10a3
Small fixes
SergioRZMasson Oct 22, 2024
9d38e67
Fixed sampling
SergioRZMasson Oct 22, 2024
b135647
Merge branch 'master' of https://github.com/SergioRZMasson/Babylon.js…
SergioRZMasson Nov 27, 2024
7b5e66c
Changed AreaLights to use roughness in the Standard material instead …
SergioRZMasson Nov 27, 2024
71e8de9
Initial implementation for PBR
SergioRZMasson Nov 28, 2024
5552e12
Cleanup of PBR Area light code
SergioRZMasson Nov 28, 2024
db54467
Moved LTC texture to side tool
SergioRZMasson Dec 2, 2024
812e12f
Merge branch 'master' into sergio/area-light
SergioRZMasson Dec 5, 2024
2834639
Merged with master
SergioRZMasson Dec 9, 2024
5d4ee0d
Changed area lights to use base64 encoded float16
SergioRZMasson Dec 10, 2024
6a82d4e
Merged ltc data into single array
SergioRZMasson Dec 10, 2024
80848a8
Changed LTC2 texture to be RGB
SergioRZMasson Dec 10, 2024
026ac3d
Changed base64 to use Babylon's encoder
SergioRZMasson Dec 10, 2024
653f990
Moved area lights to be bind at the material level
SergioRZMasson Dec 12, 2024
d770a0a
Fixed shaders for standard material
SergioRZMasson Dec 12, 2024
bffff65
Small code cleanup
SergioRZMasson Dec 12, 2024
1ff3a5c
Moved LTC texture tools to the correct place
SergioRZMasson Dec 16, 2024
c6f3ead
Added LTC as binary folder
SergioRZMasson Dec 16, 2024
af2ca91
Added ltc bin to cdn
SergioRZMasson Dec 16, 2024
3c733e7
Added implementation to standard material
SergioRZMasson Dec 16, 2024
415818b
Added inspector element for areaLights
SergioRZMasson Jan 8, 2025
e137096
Renamed areaLight to rectAreaLight
SergioRZMasson Jan 8, 2025
484561b
Renamed areaLight to rectAreaLight
SergioRZMasson Jan 8, 2025
2e96a62
Merged with master
SergioRZMasson Jan 8, 2025
2ba432e
Moved shader code to not be compiled when there are no Area Lights
SergioRZMasson Jan 13, 2025
87311c8
Fixed file names and function signatures
SergioRZMasson Jan 13, 2025
edc1904
Added notes to handle NME
SergioRZMasson Jan 13, 2025
98d0a40
Reduced code complexity for setting area lights width and height
SergioRZMasson Jan 13, 2025
6eb0f1f
Fixed documentation
SergioRZMasson Jan 13, 2025
3191ce7
Small documentation update
SergioRZMasson Jan 13, 2025
7469000
Removed area lights code from scene and materials
SergioRZMasson Jan 14, 2025
c474b93
Moved is loading area lights flag to scene
SergioRZMasson Jan 14, 2025
a8dc927
Added IAreaLightLTCProvider interface to allow users to provide their…
SergioRZMasson Jan 15, 2025
978bc65
Merge branch 'master' of https://github.com/SergioRZMasson/Babylon.js…
SergioRZMasson Jan 15, 2025
3b9b579
Cleanup uncessary changes
SergioRZMasson Jan 15, 2025
aca386a
Small code cleanup
SergioRZMasson Jan 15, 2025
4a876a2
Fixed WGSL shader
SergioRZMasson Jan 15, 2025
9973803
Fixed WGSL shader
SergioRZMasson Jan 15, 2025
a272ad8
Added initial WebGPU implementation for StandardMaterial
SergioRZMasson Jan 15, 2025
4a9865e
Fixed issue with diffuse color on WebGPU
SergioRZMasson Jan 16, 2025
c741a61
Finished area lights implementation for WebGPU
SergioRZMasson Jan 16, 2025
cbea906
Added visualization tests
SergioRZMasson Jan 16, 2025
a695c17
Removed area lights LTC from babylon CDN, moved it assets repo
SergioRZMasson Jan 16, 2025
fac1e8a
Fixed Area Lights validation tests
SergioRZMasson Jan 16, 2025
547af10
Fixed tab issues in shaders
SergioRZMasson Jan 16, 2025
8021848
Fixed tab issues in shaders
SergioRZMasson Jan 16, 2025
d201e6c
Separated AreaLights and RectAreaLights to improver future backwards …
SergioRZMasson Jan 16, 2025
179ac1d
Separated AreaLights and RectAreaLights to improver future backwards …
SergioRZMasson Jan 16, 2025
51e0e8b
Small documentation fix
SergioRZMasson Jan 16, 2025
86a5b1d
Update packages/dev/core/src/Lights/LTC/ltcTextureTool.ts
SergioRZMasson Jan 17, 2025
90b6ff9
Update packages/dev/core/src/Lights/LTC/ltcTextureTool.ts
SergioRZMasson Jan 17, 2025
2194de1
Update packages/dev/core/src/Lights/LTC/ltcTextureTool.ts
SergioRZMasson Jan 17, 2025
6bdbdaa
Update packages/dev/core/src/Lights/areaLight.ts
SergioRZMasson Jan 17, 2025
c05a12e
Added review suggestions
SergioRZMasson Jan 17, 2025
ddc0a6b
Tried increasing frame count to allow LTC texture to be loaded during…
SergioRZMasson Jan 17, 2025
23efd48
Changed _position to be public position
SergioRZMasson Jan 17, 2025
f6a3e19
Update packages/dev/inspector/src/components/actionTabs/tabs/property…
SergioRZMasson Jan 17, 2025
0aeed06
Update packages/dev/core/src/Lights/areaLight.ts
SergioRZMasson Jan 17, 2025
4e60f5b
Fixed local variable naming convention
SergioRZMasson Jan 17, 2025
bca4291
Merge branch 'sergio/area-light' of https://github.com/SergioRZMasson…
SergioRZMasson Jan 17, 2025
0a30821
Fixed property name
SergioRZMasson Jan 17, 2025
bb4f0cf
Added small code improvements to shaders, fixed width and height calc…
SergioRZMasson Jan 17, 2025
1ae8039
Added inspiration credits
SergioRZMasson Jan 17, 2025
bd91bcc
Replaced link with perma link
SergioRZMasson Jan 17, 2025
67da5e5
Update packages/dev/core/src/Lights/LTC/ltcTextureTool.ts
SergioRZMasson Jan 17, 2025
51a0067
Added way to wait for Area Lights to be ready. Removed renderCount fr…
SergioRZMasson Jan 17, 2025
9342a82
Merge branch 'sergio/area-light' of https://github.com/SergioRZMasson…
SergioRZMasson Jan 17, 2025
455cb3d
Changed logic so that area Light works with scene.whenReadyAsync
SergioRZMasson Jan 17, 2025
a1c4ae4
Added way for users to set URL used by DefaultAreaLightLTCProvider
SergioRZMasson Jan 20, 2025
aca6725
Fixed ES6 issue
SergioRZMasson Jan 20, 2025
d739f4c
Merge branch 'master' of https://github.com/SergioRZMasson/Babylon.js…
SergioRZMasson Jan 20, 2025
79ef634
Updated validation tests
SergioRZMasson Jan 20, 2025
d9b601b
Improved code separation on shaders
SergioRZMasson Jan 20, 2025
62b01cd
Merge branch 'master' of https://github.com/SergioRZMasson/Babylon.js…
SergioRZMasson Jan 20, 2025
0cdf4e6
Changed DefaultAreaLightLTCProvider to use GetAssetUrl to load CDN files
SergioRZMasson Jan 20, 2025
b4195c2
Added missing uniforms to uniformList
SergioRZMasson Jan 21, 2025
947a714
Changed implementation of LTC textures on scene
SergioRZMasson Jan 21, 2025
b7e7dc9
Merge branch 'sergio/area-light' of https://github.com/SergioRZMasson…
SergioRZMasson Jan 22, 2025
0c29947
Changed area Light implementation to use texture ready to flag materi…
SergioRZMasson Jan 22, 2025
6b68b01
Updated tests reference images
SergioRZMasson Jan 22, 2025
46974b6
Changed rawtexture to flag isReady only when it has data
SergioRZMasson Jan 22, 2025
495dc97
Added flag to raw texture to keep backwards compatibility
SergioRZMasson Jan 22, 2025
4ccd087
Update packages/dev/core/src/Materials/Textures/rawTexture.ts
SergioRZMasson Jan 22, 2025
054986c
Moved material checks to before effect gets created
SergioRZMasson Jan 22, 2025
8afc2f8
Moved material checks to before effect gets created
SergioRZMasson Jan 22, 2025
c1733c7
Changed function names to uppercase
SergioRZMasson Jan 22, 2025
2c536d0
Fix shader crash for materials that don't define vReflectionInfos
SergioRZMasson Jan 23, 2025
2ade030
Added more elegant way to handle shader support
SergioRZMasson Jan 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Cleanup of PBR Area light code
SergioRZMasson committed Nov 28, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 5552e1209d6cfa66fa70a2dd0084b4d46021f45f
128 changes: 65 additions & 63 deletions packages/dev/core/src/Shaders/ShadersInclude/lightFragment.fx
Original file line number Diff line number Diff line change
@@ -7,85 +7,87 @@
#define CUSTOM_LIGHT{X}_COLOR // Use to modify light color. Currently only supports diffuse.

#ifdef PBR
// Compute Pre Lighting infos
#ifdef SPOTLIGHT{X}
preInfo = computePointAndSpotPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW, vPositionW);
#elif defined(POINTLIGHT{X})
preInfo = computePointAndSpotPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW, vPositionW);
#elif defined(HEMILIGHT{X})
preInfo = computeHemisphericPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW);
#elif defined(DIRLIGHT{X})
preInfo = computeDirectionalPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW);
#elif defined(AREALIGHT{X})
preInfo = computeAreaPreLightingInfo(areaLightsLTC1{X}, areaLightsLTC2{X}, viewDirectionW, normalW, vPositionW, light{X}.vLightData, light{X}.vLightWidth.rgb, light{X}.vLightHeight.rgb, light{X}.vLightSpecular.rgb, roughness);
#endif

#if defined(AREALIGHT{X})
info.diffuse = computeAreaLightingDiffuse(areaLightsLTC1{X}, areaLightsLTC2{X}, viewDirectionW, normalW, vPositionW, light{X}.vLightData, light{X}.vLightWidth.rgb, light{X}.vLightHeight.rgb, diffuse{X}.rgb, light{X}.vLightSpecular.rgb, roughness);
#ifdef SPECULARTERM
info.specular = computeAreaLightingSpecular(areaLightsLTC1{X}, areaLightsLTC2{X}, viewDirectionW, normalW, vPositionW, light{X}.vLightData, light{X}.vLightWidth.rgb, light{X}.vLightHeight.rgb, diffuse{X}.rgb, light{X}.vLightSpecular.rgb, roughness);
#endif
#else
// Compute Pre Lighting infos
#ifdef SPOTLIGHT{X}
preInfo = computePointAndSpotPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW, vPositionW);
#elif defined(POINTLIGHT{X})
preInfo = computePointAndSpotPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW, vPositionW);
#elif defined(HEMILIGHT{X})
preInfo = computeHemisphericPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW);
#elif defined(DIRLIGHT{X})
preInfo = computeDirectionalPreLightingInfo(light{X}.vLightData, viewDirectionW, normalW);
#endif

preInfo.NdotV = NdotV;
preInfo.NdotV = NdotV;

// Compute Attenuation infos
#ifdef SPOTLIGHT{X}
#ifdef LIGHT_FALLOFF_GLTF{X}
preInfo.attenuation = computeDistanceLightFalloff_GLTF(preInfo.lightDistanceSquared, light{X}.vLightFalloff.y);
preInfo.attenuation *= computeDirectionalLightFalloff_GLTF(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightFalloff.z, light{X}.vLightFalloff.w);
#elif defined(LIGHT_FALLOFF_PHYSICAL{X})
preInfo.attenuation = computeDistanceLightFalloff_Physical(preInfo.lightDistanceSquared);
preInfo.attenuation *= computeDirectionalLightFalloff_Physical(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w);
#elif defined(LIGHT_FALLOFF_STANDARD{X})
preInfo.attenuation = computeDistanceLightFalloff_Standard(preInfo.lightOffset, light{X}.vLightFalloff.x);
preInfo.attenuation *= computeDirectionalLightFalloff_Standard(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w, light{X}.vLightData.w);
#else
preInfo.attenuation = computeDistanceLightFalloff(preInfo.lightOffset, preInfo.lightDistanceSquared, light{X}.vLightFalloff.x, light{X}.vLightFalloff.y);
preInfo.attenuation *= computeDirectionalLightFalloff(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w, light{X}.vLightData.w, light{X}.vLightFalloff.z, light{X}.vLightFalloff.w);
#endif
#elif defined(POINTLIGHT{X})
#ifdef LIGHT_FALLOFF_GLTF{X}
preInfo.attenuation = computeDistanceLightFalloff_GLTF(preInfo.lightDistanceSquared, light{X}.vLightFalloff.y);
#elif defined(LIGHT_FALLOFF_PHYSICAL{X})
preInfo.attenuation = computeDistanceLightFalloff_Physical(preInfo.lightDistanceSquared);
#elif defined(LIGHT_FALLOFF_STANDARD{X})
preInfo.attenuation = computeDistanceLightFalloff_Standard(preInfo.lightOffset, light{X}.vLightFalloff.x);
#else
preInfo.attenuation = computeDistanceLightFalloff(preInfo.lightOffset, preInfo.lightDistanceSquared, light{X}.vLightFalloff.x, light{X}.vLightFalloff.y);
#endif
// Compute Attenuation infos
#ifdef SPOTLIGHT{X}
#ifdef LIGHT_FALLOFF_GLTF{X}
preInfo.attenuation = computeDistanceLightFalloff_GLTF(preInfo.lightDistanceSquared, light{X}.vLightFalloff.y);
preInfo.attenuation *= computeDirectionalLightFalloff_GLTF(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightFalloff.z, light{X}.vLightFalloff.w);
#elif defined(LIGHT_FALLOFF_PHYSICAL{X})
preInfo.attenuation = computeDistanceLightFalloff_Physical(preInfo.lightDistanceSquared);
preInfo.attenuation *= computeDirectionalLightFalloff_Physical(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w);
#elif defined(LIGHT_FALLOFF_STANDARD{X})
preInfo.attenuation = computeDistanceLightFalloff_Standard(preInfo.lightOffset, light{X}.vLightFalloff.x);
preInfo.attenuation *= computeDirectionalLightFalloff_Standard(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w, light{X}.vLightData.w);
#else
preInfo.attenuation = 1.0;
preInfo.attenuation = computeDistanceLightFalloff(preInfo.lightOffset, preInfo.lightDistanceSquared, light{X}.vLightFalloff.x, light{X}.vLightFalloff.y);
preInfo.attenuation *= computeDirectionalLightFalloff(light{X}.vLightDirection.xyz, preInfo.L, light{X}.vLightDirection.w, light{X}.vLightData.w, light{X}.vLightFalloff.z, light{X}.vLightFalloff.w);
#endif

// Simulates Light radius for diffuse and spec term
// clear coat is using a dedicated roughness
#ifdef HEMILIGHT{X}
preInfo.roughness = roughness;
#elif defined(POINTLIGHT{X})
#ifdef LIGHT_FALLOFF_GLTF{X}
preInfo.attenuation = computeDistanceLightFalloff_GLTF(preInfo.lightDistanceSquared, light{X}.vLightFalloff.y);
#elif defined(LIGHT_FALLOFF_PHYSICAL{X})
preInfo.attenuation = computeDistanceLightFalloff_Physical(preInfo.lightDistanceSquared);
#elif defined(LIGHT_FALLOFF_STANDARD{X})
preInfo.attenuation = computeDistanceLightFalloff_Standard(preInfo.lightOffset, light{X}.vLightFalloff.x);
#else
preInfo.roughness = adjustRoughnessFromLightProperties(roughness, light{X}.vLightSpecular.a, preInfo.lightDistance);
preInfo.attenuation = computeDistanceLightFalloff(preInfo.lightOffset, preInfo.lightDistanceSquared, light{X}.vLightFalloff.x, light{X}.vLightFalloff.y);
#endif
#else
preInfo.attenuation = 1.0;
#endif

#ifdef IRIDESCENCE
preInfo.iridescenceIntensity = iridescenceIntensity;
#endif
// Simulates Light radius for diffuse and spec term
// clear coat is using a dedicated roughness
#if defined(HEMILIGHT{X}) || defined(AREALIGHT{X})
preInfo.roughness = roughness;
#else
preInfo.roughness = adjustRoughnessFromLightProperties(roughness, light{X}.vLightSpecular.a, preInfo.lightDistance);
#endif

// Diffuse contribution
#ifdef HEMILIGHT{X}
info.diffuse = computeHemisphericDiffuseLighting(preInfo, diffuse{X}.rgb, light{X}.vLightGround);
#elif defined(SS_TRANSLUCENCY)
info.diffuse = computeDiffuseAndTransmittedLighting(preInfo, diffuse{X}.rgb, subSurfaceOut.transmittance);
#else
info.diffuse = computeDiffuseLighting(preInfo, diffuse{X}.rgb);
#endif
#ifdef IRIDESCENCE
preInfo.iridescenceIntensity = iridescenceIntensity;
#endif

// Specular contribution
#ifdef SPECULARTERM
// Diffuse contribution
#ifdef HEMILIGHT{X}
info.diffuse = computeHemisphericDiffuseLighting(preInfo, diffuse{X}.rgb, light{X}.vLightGround);
#elif defined(AREALIGHT{X})
info.diffuse = computeAreaDiffuseLighting(preInfo, diffuse{X}.rgb);
#elif defined(SS_TRANSLUCENCY)
info.diffuse = computeDiffuseAndTransmittedLighting(preInfo, diffuse{X}.rgb, subSurfaceOut.transmittance);
#else
info.diffuse = computeDiffuseLighting(preInfo, diffuse{X}.rgb);
#endif

// Specular contribution
#ifdef SPECULARTERM
#if AREALIGHT{X}
info.specular = computeAreaSpecularLighting(preInfo);
#else
#ifdef ANISOTROPIC
info.specular = computeAnisotropicSpecularLighting(preInfo, viewDirectionW, normalW, anisotropicOut.anisotropicTangent, anisotropicOut.anisotropicBitangent, anisotropicOut.anisotropy, clearcoatOut.specularEnvironmentR0, specularEnvironmentR90, AARoughnessFactors.x, diffuse{X}.rgb);
#else
info.specular = computeSpecularLighting(preInfo, normalW, clearcoatOut.specularEnvironmentR0, specularEnvironmentR90, AARoughnessFactors.x, diffuse{X}.rgb);
#endif
#endif
#endif

#ifndef AREALIGHT{X}
// Sheen contribution
#ifdef SHEEN
#ifdef SHEEN_LINKWITHALBEDO
Original file line number Diff line number Diff line change
@@ -34,173 +34,17 @@ vec3 computeHemisphericDiffuseLighting(preLightingInfo info, vec3 lightColor, ve
return mix(groundColor, lightColor, info.NdotL);
}

#ifdef AREALIGHTUSED
vec3 computeAreaDiffuseLighting(preLightingInfo info, vec3 lightColor) {
return info.areaLightDiffuse * lightColor;
}
#endif

vec3 computeDiffuseLighting(preLightingInfo info, vec3 lightColor) {
float diffuseTerm = diffuseBRDF_Burley(info.NdotL, info.NdotV, info.VdotH, info.roughness);
return diffuseTerm * info.attenuation * info.NdotL * lightColor;
}

#ifdef AREALIGHTUSED

// Area Light

// Real-Time Polygonal-Light Shading with Linearly Transformed Cosines
// by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt
// code: https://github.com/selfshadow/ltc_code/

vec2 LTCUv( const in vec3 N, const in vec3 V, const in float roughness ) {

const float LUTSIZE = 64.0;
const float LUTSCALE = ( LUTSIZE - 1.0 ) / LUTSIZE;
const float LUTBIAS = 0.5 / LUTSIZE;

float dotNV = saturate( dot( N, V ) );

// texture parameterized by sqrt( GGX alpha ) and sqrt( 1 - cos( theta ) )
vec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );

uv = uv * LUTSCALE + LUTBIAS;

return uv;

}

float LTCClippedSphereFormFactor( const in vec3 f ) {

// Real-Time Area Lighting: a Journey from Research to Production (p.102)
// An approximation of the form factor of a horizon-clipped rectangle.
float l = length( f );
return max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );
}

vec3 LTCEdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {

float x = dot( v1, v2 );

float y = abs( x );

// rational polynomial approximation to theta / sin( theta ) / 2PI
float a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;
float b = 3.4175940 + ( 4.1616724 + y ) * y;
float v = a / b;

float thetaSintheta = 0.0;

if( x > 0.0 )
{
thetaSintheta = v;
}
else
{
thetaSintheta = 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;
}
return cross( v1, v2 ) * thetaSintheta;
}

vec3 LTCEvaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {

// bail if point is on back side of plane of light
// assumes ccw winding order of light vertices
vec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];
vec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];
vec3 lightNormal = cross( v1, v2 );

if( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );

// construct orthonormal basis around N
vec3 T1, T2;
T1 = normalize( V - N * dot( V, N ) );
T2 = - cross( N, T1 ); // negated from paper; possibly due to a different handedness of world coordinate system

// compute transform
mat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );

// transform rect
vec3 coords[ 4 ];
coords[ 0 ] = mat * ( rectCoords[ 0 ] - P );
coords[ 1 ] = mat * ( rectCoords[ 1 ] - P );
coords[ 2 ] = mat * ( rectCoords[ 2 ] - P );
coords[ 3 ] = mat * ( rectCoords[ 3 ] - P );

// project rect onto sphere
coords[ 0 ] = normalize( coords[ 0 ] );
coords[ 1 ] = normalize( coords[ 1 ] );
coords[ 2 ] = normalize( coords[ 2 ] );
coords[ 3 ] = normalize( coords[ 3 ] );

// calculate vector form factor
vec3 vectorFormFactor = vec3( 0.0 );
vectorFormFactor += LTCEdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );
vectorFormFactor += LTCEdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );
vectorFormFactor += LTCEdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );
vectorFormFactor += LTCEdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );

// adjust for horizon clipping
float result = LTCClippedSphereFormFactor( vectorFormFactor );
return vec3( result );
}

vec3 computeAreaLightingDiffuse(sampler2D areaLightsLTC1, sampler2D areaLightsLTC2, vec3 viewDirectionW, vec3 vNormal, vec3 vPosition, vec4 lightData, vec3 halfWidth, vec3 halfHeight, vec3 diffuseColor, vec3 specularColor, float roughness )
{
vec3 normal = vNormal;
vec3 viewDir = viewDirectionW;
vec3 position = vPosition;
vec3 lightPos = lightData.xyz;

vec3 rectCoords[ 4 ];
rectCoords[ 0 ] = lightPos + halfWidth - halfHeight; // counterclockwise; light shines in local neg z direction
rectCoords[ 1 ] = lightPos - halfWidth - halfHeight;
rectCoords[ 2 ] = lightPos - halfWidth + halfHeight;
rectCoords[ 3 ] = lightPos + halfWidth + halfHeight;

vec2 uv = LTCUv( normal, viewDir, roughness );

vec4 t1 = texture2D( areaLightsLTC1, uv );
vec4 t2 = texture2D( areaLightsLTC2, uv );

mat3 mInv = mat3(
vec3( t1.x, 0, t1.y ),
vec3( 0, 1, 0 ),
vec3( t1.z, 0, t1.w )
);

return diffuseColor * LTCEvaluate( normal, viewDir, position, mat3( 1.0 ), rectCoords );
}

#ifdef SPECULARTERM

vec3 computeAreaLightingSpecular(sampler2D areaLightsLTC1, sampler2D areaLightsLTC2, vec3 viewDirectionW, vec3 vNormal, vec3 vPosition, vec4 lightData, vec3 halfWidth, vec3 halfHeight, vec3 diffuseColor, vec3 specularColor, float roughness )
{
vec3 normal = vNormal;
vec3 viewDir = viewDirectionW;
vec3 position = vPosition;
vec3 lightPos = lightData.xyz;

vec3 rectCoords[ 4 ];
rectCoords[ 0 ] = lightPos + halfWidth - halfHeight; // counterclockwise; light shines in local neg z direction
rectCoords[ 1 ] = lightPos - halfWidth - halfHeight;
rectCoords[ 2 ] = lightPos - halfWidth + halfHeight;
rectCoords[ 3 ] = lightPos + halfWidth + halfHeight;

vec2 uv = LTCUv( normal, viewDir, roughness );

vec4 t1 = texture2D( areaLightsLTC1, uv );
vec4 t2 = texture2D( areaLightsLTC2, uv );

mat3 mInv = mat3(
vec3( t1.x, 0, t1.y ),
vec3( 0, 1, 0 ),
vec3( t1.z, 0, t1.w )
);


// LTC Fresnel Approximation by Stephen Hill
// http://blog.selfshadow.com/publications/s2016-advances/s2016_ltc_fresnel.pdf
vec3 fresnel = ( specularColor * t2.x + ( vec3( 1.0 ) - specularColor ) * t2.y );
return specularColor * fresnel * LTCEvaluate( normal, viewDir, position, mInv, rectCoords );
}
#endif
#endif

#define inline
vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler, mat4 textureProjectionMatrix, vec3 posW){
vec4 strq = textureProjectionMatrix * vec4(posW, 1.0);
@@ -248,6 +92,13 @@ vec3 computeProjectionTextureDiffuseLighting(sampler2D projectionLightSampler, m
vec3 specTerm = fresnel * distribution * smithVisibility;
return specTerm * info.attenuation * info.NdotL * lightColor;
}

#ifdef AREALIGHTUSED
vec3 computeAreaSpecularLighting(preLightingInfo info) {
return info.areaLightSpecular;
}
#endif

#endif

#ifdef ANISOTROPIC
Loading