three-gpu-pathtracer 0.0.5 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +815 -728
- package/build/index.module.js +5474 -3706
- package/build/index.module.js.map +1 -1
- package/build/index.umd.cjs +5479 -3704
- package/build/index.umd.cjs.map +1 -1
- package/package.json +68 -60
- package/src/core/DynamicPathTracingSceneGenerator.js +119 -111
- package/src/core/MaterialReducer.js +256 -256
- package/src/core/PathTracingRenderer.js +270 -255
- package/src/core/PathTracingSceneGenerator.js +4 -3
- package/src/index.js +35 -26
- package/src/materials/AlphaDisplayMaterial.js +48 -48
- package/src/materials/AmbientOcclusionMaterial.js +197 -197
- package/src/materials/BlendMaterial.js +67 -67
- package/src/materials/DenoiseMaterial.js +142 -0
- package/src/materials/GraphMaterial.js +243 -0
- package/src/materials/LambertPathTracingMaterial.js +285 -285
- package/src/materials/MaterialBase.js +56 -56
- package/src/materials/PhysicalPathTracingMaterial.js +970 -848
- package/src/{core → objects}/EquirectCamera.js +13 -13
- package/src/{core → objects}/PhysicalCamera.js +28 -28
- package/src/objects/PhysicalSpotLight.js +14 -0
- package/src/objects/ShapedAreaLight.js +12 -0
- package/src/shader/shaderEnvMapSampling.js +59 -59
- package/src/shader/shaderGGXFunctions.js +100 -108
- package/src/shader/shaderIridescenceFunctions.js +130 -0
- package/src/shader/shaderLightSampling.js +231 -87
- package/src/shader/shaderMaterialSampling.js +542 -501
- package/src/shader/shaderSheenFunctions.js +98 -0
- package/src/shader/shaderStructs.js +321 -191
- package/src/shader/shaderUtils.js +364 -287
- package/src/uniforms/EquirectHdrInfoUniform.js +259 -263
- package/src/uniforms/IESProfilesTexture.js +100 -0
- package/src/uniforms/LightsInfoUniformStruct.js +162 -0
- package/src/uniforms/MaterialsTexture.js +426 -319
- package/src/uniforms/PhysicalCameraUniform.js +36 -36
- package/src/uniforms/RenderTarget2DArray.js +93 -93
- package/src/utils/BlurredEnvMapGenerator.js +113 -113
- package/src/utils/GeometryPreparationUtils.js +21 -1
- package/src/utils/IESLoader.js +325 -0
- package/src/utils/UVUnwrapper.js +101 -101
- package/src/workers/PathTracingSceneWorker.js +42 -41
- package/src/uniforms/LightsTexture.js +0 -83
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { Camera } from 'three';
|
|
2
|
-
|
|
3
|
-
export class EquirectCamera extends Camera {
|
|
4
|
-
|
|
5
|
-
constructor() {
|
|
6
|
-
|
|
7
|
-
super();
|
|
8
|
-
|
|
9
|
-
this.isEquirectCamera = true;
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
}
|
|
1
|
+
import { Camera } from 'three';
|
|
2
|
+
|
|
3
|
+
export class EquirectCamera extends Camera {
|
|
4
|
+
|
|
5
|
+
constructor() {
|
|
6
|
+
|
|
7
|
+
super();
|
|
8
|
+
|
|
9
|
+
this.isEquirectCamera = true;
|
|
10
|
+
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
}
|
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { PerspectiveCamera } from 'three';
|
|
2
|
-
|
|
3
|
-
export class PhysicalCamera extends PerspectiveCamera {
|
|
4
|
-
|
|
5
|
-
set bokehSize( size ) {
|
|
6
|
-
|
|
7
|
-
this.fStop = this.getFocalLength() / size;
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
get bokehSize() {
|
|
12
|
-
|
|
13
|
-
return this.getFocalLength() / this.fStop;
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
constructor( ...args ) {
|
|
18
|
-
|
|
19
|
-
super( ...args );
|
|
20
|
-
this.fStop = 1.4;
|
|
21
|
-
this.apertureBlades = 0;
|
|
22
|
-
this.apertureRotation = 0;
|
|
23
|
-
this.focusDistance = 25;
|
|
24
|
-
this.anamorphicRatio = 1;
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
}
|
|
1
|
+
import { PerspectiveCamera } from 'three';
|
|
2
|
+
|
|
3
|
+
export class PhysicalCamera extends PerspectiveCamera {
|
|
4
|
+
|
|
5
|
+
set bokehSize( size ) {
|
|
6
|
+
|
|
7
|
+
this.fStop = this.getFocalLength() / size;
|
|
8
|
+
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
get bokehSize() {
|
|
12
|
+
|
|
13
|
+
return this.getFocalLength() / this.fStop;
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
constructor( ...args ) {
|
|
18
|
+
|
|
19
|
+
super( ...args );
|
|
20
|
+
this.fStop = 1.4;
|
|
21
|
+
this.apertureBlades = 0;
|
|
22
|
+
this.apertureRotation = 0;
|
|
23
|
+
this.focusDistance = 25;
|
|
24
|
+
this.anamorphicRatio = 1;
|
|
25
|
+
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
}
|
|
@@ -1,59 +1,59 @@
|
|
|
1
|
-
export const shaderEnvMapSampling = /* glsl */`
|
|
2
|
-
|
|
3
|
-
vec3 sampleEquirectEnvMapColor( vec3 direction, sampler2D map ) {
|
|
4
|
-
|
|
5
|
-
return texture2D( map, equirectDirectionToUv( direction ) ).rgb;
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
float envMapDirectionPdf( vec3 direction ) {
|
|
10
|
-
|
|
11
|
-
vec2 uv = equirectDirectionToUv( direction );
|
|
12
|
-
float theta = uv.y * PI;
|
|
13
|
-
float sinTheta = sin( theta );
|
|
14
|
-
if ( sinTheta == 0.0 ) {
|
|
15
|
-
|
|
16
|
-
return 0.0;
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return 1.0 / ( 2.0 * PI * PI * sinTheta );
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
float envMapSample( vec3 direction, EquirectHdrInfo info, out vec3 color ) {
|
|
25
|
-
|
|
26
|
-
vec2 uv = equirectDirectionToUv( direction );
|
|
27
|
-
color = texture2D( info.map, uv ).rgb;
|
|
28
|
-
|
|
29
|
-
float totalSum =
|
|
30
|
-
float lum = colorToLuminance( color );
|
|
31
|
-
ivec2 resolution = textureSize( info.map, 0 );
|
|
32
|
-
float pdf = lum / totalSum;
|
|
33
|
-
|
|
34
|
-
return float( resolution.x * resolution.y ) * pdf * envMapDirectionPdf( direction );
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
float randomEnvMapSample( EquirectHdrInfo info, out vec3 color, out vec3 direction ) {
|
|
39
|
-
|
|
40
|
-
// sample env map cdf
|
|
41
|
-
vec2 r = rand2();
|
|
42
|
-
float v = texture2D( info.marginalWeights, vec2( r.x, 0.0 ) ).x;
|
|
43
|
-
float u = texture2D( info.conditionalWeights, vec2( r.y, v ) ).x;
|
|
44
|
-
vec2 uv = vec2( u, v );
|
|
45
|
-
|
|
46
|
-
vec3 derivedDirection = equirectUvToDirection( uv );
|
|
47
|
-
direction = derivedDirection;
|
|
48
|
-
color = texture2D( info.map, uv ).rgb;
|
|
49
|
-
|
|
50
|
-
float totalSum =
|
|
51
|
-
float lum = colorToLuminance( color );
|
|
52
|
-
ivec2 resolution = textureSize( info.map, 0 );
|
|
53
|
-
float pdf = lum / totalSum;
|
|
54
|
-
|
|
55
|
-
return float( resolution.x * resolution.y ) * pdf * envMapDirectionPdf( direction );
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
`;
|
|
1
|
+
export const shaderEnvMapSampling = /* glsl */`
|
|
2
|
+
|
|
3
|
+
vec3 sampleEquirectEnvMapColor( vec3 direction, sampler2D map ) {
|
|
4
|
+
|
|
5
|
+
return texture2D( map, equirectDirectionToUv( direction ) ).rgb;
|
|
6
|
+
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
float envMapDirectionPdf( vec3 direction ) {
|
|
10
|
+
|
|
11
|
+
vec2 uv = equirectDirectionToUv( direction );
|
|
12
|
+
float theta = uv.y * PI;
|
|
13
|
+
float sinTheta = sin( theta );
|
|
14
|
+
if ( sinTheta == 0.0 ) {
|
|
15
|
+
|
|
16
|
+
return 0.0;
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return 1.0 / ( 2.0 * PI * PI * sinTheta );
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
float envMapSample( vec3 direction, EquirectHdrInfo info, out vec3 color ) {
|
|
25
|
+
|
|
26
|
+
vec2 uv = equirectDirectionToUv( direction );
|
|
27
|
+
color = texture2D( info.map, uv ).rgb;
|
|
28
|
+
|
|
29
|
+
float totalSum = info.totalSumWhole + info.totalSumDecimal;
|
|
30
|
+
float lum = colorToLuminance( color );
|
|
31
|
+
ivec2 resolution = textureSize( info.map, 0 );
|
|
32
|
+
float pdf = lum / totalSum;
|
|
33
|
+
|
|
34
|
+
return float( resolution.x * resolution.y ) * pdf * envMapDirectionPdf( direction );
|
|
35
|
+
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
float randomEnvMapSample( EquirectHdrInfo info, out vec3 color, out vec3 direction ) {
|
|
39
|
+
|
|
40
|
+
// sample env map cdf
|
|
41
|
+
vec2 r = rand2();
|
|
42
|
+
float v = texture2D( info.marginalWeights, vec2( r.x, 0.0 ) ).x;
|
|
43
|
+
float u = texture2D( info.conditionalWeights, vec2( r.y, v ) ).x;
|
|
44
|
+
vec2 uv = vec2( u, v );
|
|
45
|
+
|
|
46
|
+
vec3 derivedDirection = equirectUvToDirection( uv );
|
|
47
|
+
direction = derivedDirection;
|
|
48
|
+
color = texture2D( info.map, uv ).rgb;
|
|
49
|
+
|
|
50
|
+
float totalSum = info.totalSumWhole + info.totalSumDecimal;
|
|
51
|
+
float lum = colorToLuminance( color );
|
|
52
|
+
ivec2 resolution = textureSize( info.map, 0 );
|
|
53
|
+
float pdf = lum / totalSum;
|
|
54
|
+
|
|
55
|
+
return float( resolution.x * resolution.y ) * pdf * envMapDirectionPdf( direction );
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
`;
|
|
@@ -1,108 +1,100 @@
|
|
|
1
|
-
export const shaderGGXFunctions = /* glsl */`
|
|
2
|
-
// The GGX functions provide sampling and distribution information for normals as output so
|
|
3
|
-
// in order to get probability of scatter direction the half vector must be computed and provided.
|
|
4
|
-
// [0] https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf
|
|
5
|
-
// [1] https://hal.archives-ouvertes.fr/hal-01509746/document
|
|
6
|
-
// [2] http://jcgt.org/published/0007/04/01/
|
|
7
|
-
// [4] http://jcgt.org/published/0003/02/03/
|
|
8
|
-
|
|
9
|
-
// trowbridge-reitz === GGX === GTR
|
|
10
|
-
|
|
11
|
-
vec3 ggxDirection( vec3 incidentDir, float roughnessX, float roughnessY, float random1, float random2 ) {
|
|
12
|
-
|
|
13
|
-
// TODO: try GGXVNDF implementation from reference [2], here. Needs to update ggxDistribution
|
|
14
|
-
// function below, as well
|
|
15
|
-
|
|
16
|
-
// Implementation from reference [1]
|
|
17
|
-
// stretch view
|
|
18
|
-
vec3 V = normalize( vec3( roughnessX * incidentDir.x, roughnessY * incidentDir.y, incidentDir.z ) );
|
|
19
|
-
|
|
20
|
-
// orthonormal basis
|
|
21
|
-
vec3 T1 = ( V.z < 0.9999 ) ? normalize( cross( V, vec3( 0.0, 0.0, 1.0 ) ) ) : vec3( 1.0, 0.0, 0.0 );
|
|
22
|
-
vec3 T2 = cross( T1, V );
|
|
23
|
-
|
|
24
|
-
// sample point with polar coordinates (r, phi)
|
|
25
|
-
float a = 1.0 / ( 1.0 + V.z );
|
|
26
|
-
float r = sqrt( random1 );
|
|
27
|
-
float phi = ( random2 < a ) ? random2 / a * PI : PI + ( random2 - a ) / ( 1.0 - a ) * PI;
|
|
28
|
-
float P1 = r * cos( phi );
|
|
29
|
-
float P2 = r * sin( phi ) * ( ( random2 < a ) ? 1.0 : V.z );
|
|
30
|
-
|
|
31
|
-
// compute normal
|
|
32
|
-
vec3 N = P1 * T1 + P2 * T2 + V * sqrt( max( 0.0, 1.0 - P1 * P1 - P2 * P2 ) );
|
|
33
|
-
|
|
34
|
-
// unstretch
|
|
35
|
-
N = normalize( vec3( roughnessX * N.x, roughnessY * N.y, max( 0.0, N.z ) ) );
|
|
36
|
-
|
|
37
|
-
return N;
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// Below are PDF and related functions for use in a Monte Carlo path tracer
|
|
42
|
-
// as specified in Appendix B of the following paper
|
|
43
|
-
// See equation (
|
|
44
|
-
float ggxLamda( float theta, float roughness ) {
|
|
45
|
-
|
|
46
|
-
float tanTheta = tan( theta );
|
|
47
|
-
float tanTheta2 = tanTheta * tanTheta;
|
|
48
|
-
float alpha2 = roughness * roughness;
|
|
49
|
-
|
|
50
|
-
float numerator = - 1.0 + sqrt( 1.0 + alpha2 * tanTheta2 );
|
|
51
|
-
return numerator / 2.0;
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// See equation (
|
|
56
|
-
float ggxShadowMaskG1( float theta, float roughness ) {
|
|
57
|
-
|
|
58
|
-
return 1.0 / ( 1.0 + ggxLamda( theta, roughness ) );
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// See equation (125) from reference [4]
|
|
63
|
-
float ggxShadowMaskG2( vec3 wi, vec3 wo, float roughness ) {
|
|
64
|
-
|
|
65
|
-
float incidentTheta = acos( wi.z );
|
|
66
|
-
float scatterTheta = acos( wo.z );
|
|
67
|
-
return 1.0 / ( 1.0 + ggxLamda( incidentTheta, roughness ) + ggxLamda( scatterTheta, roughness ) );
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
float a2 = roughness * roughness;
|
|
75
|
-
a2 = max( EPSILON, a2 );
|
|
76
|
-
float cosTheta = halfVector.z;
|
|
77
|
-
float cosTheta4 = pow( cosTheta, 4.0 );
|
|
78
|
-
|
|
79
|
-
if ( cosTheta == 0.0 ) return 0.0;
|
|
80
|
-
|
|
81
|
-
float theta = acosSafe( halfVector.z );
|
|
82
|
-
float tanTheta = tan( theta );
|
|
83
|
-
float tanTheta2 = pow( tanTheta, 2.0 );
|
|
84
|
-
|
|
85
|
-
float denom = PI * cosTheta4 * pow( a2 + tanTheta2, 2.0 );
|
|
86
|
-
return ( a2 / denom );
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
float incidentTheta = acos( wi.z );
|
|
102
|
-
float D = ggxDistribution( halfVector, roughness );
|
|
103
|
-
float G1 = ggxShadowMaskG1( incidentTheta, roughness );
|
|
104
|
-
|
|
105
|
-
return D * G1 * max( 0.0, dot( wi, halfVector ) ) / wi.z;
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
`;
|
|
1
|
+
export const shaderGGXFunctions = /* glsl */`
|
|
2
|
+
// The GGX functions provide sampling and distribution information for normals as output so
|
|
3
|
+
// in order to get probability of scatter direction the half vector must be computed and provided.
|
|
4
|
+
// [0] https://www.cs.cornell.edu/~srm/publications/EGSR07-btdf.pdf
|
|
5
|
+
// [1] https://hal.archives-ouvertes.fr/hal-01509746/document
|
|
6
|
+
// [2] http://jcgt.org/published/0007/04/01/
|
|
7
|
+
// [4] http://jcgt.org/published/0003/02/03/
|
|
8
|
+
|
|
9
|
+
// trowbridge-reitz === GGX === GTR
|
|
10
|
+
|
|
11
|
+
vec3 ggxDirection( vec3 incidentDir, float roughnessX, float roughnessY, float random1, float random2 ) {
|
|
12
|
+
|
|
13
|
+
// TODO: try GGXVNDF implementation from reference [2], here. Needs to update ggxDistribution
|
|
14
|
+
// function below, as well
|
|
15
|
+
|
|
16
|
+
// Implementation from reference [1]
|
|
17
|
+
// stretch view
|
|
18
|
+
vec3 V = normalize( vec3( roughnessX * incidentDir.x, roughnessY * incidentDir.y, incidentDir.z ) );
|
|
19
|
+
|
|
20
|
+
// orthonormal basis
|
|
21
|
+
vec3 T1 = ( V.z < 0.9999 ) ? normalize( cross( V, vec3( 0.0, 0.0, 1.0 ) ) ) : vec3( 1.0, 0.0, 0.0 );
|
|
22
|
+
vec3 T2 = cross( T1, V );
|
|
23
|
+
|
|
24
|
+
// sample point with polar coordinates (r, phi)
|
|
25
|
+
float a = 1.0 / ( 1.0 + V.z );
|
|
26
|
+
float r = sqrt( random1 );
|
|
27
|
+
float phi = ( random2 < a ) ? random2 / a * PI : PI + ( random2 - a ) / ( 1.0 - a ) * PI;
|
|
28
|
+
float P1 = r * cos( phi );
|
|
29
|
+
float P2 = r * sin( phi ) * ( ( random2 < a ) ? 1.0 : V.z );
|
|
30
|
+
|
|
31
|
+
// compute normal
|
|
32
|
+
vec3 N = P1 * T1 + P2 * T2 + V * sqrt( max( 0.0, 1.0 - P1 * P1 - P2 * P2 ) );
|
|
33
|
+
|
|
34
|
+
// unstretch
|
|
35
|
+
N = normalize( vec3( roughnessX * N.x, roughnessY * N.y, max( 0.0, N.z ) ) );
|
|
36
|
+
|
|
37
|
+
return N;
|
|
38
|
+
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Below are PDF and related functions for use in a Monte Carlo path tracer
|
|
42
|
+
// as specified in Appendix B of the following paper
|
|
43
|
+
// See equation (34) from reference [0]
|
|
44
|
+
float ggxLamda( float theta, float roughness ) {
|
|
45
|
+
|
|
46
|
+
float tanTheta = tan( theta );
|
|
47
|
+
float tanTheta2 = tanTheta * tanTheta;
|
|
48
|
+
float alpha2 = roughness * roughness;
|
|
49
|
+
|
|
50
|
+
float numerator = - 1.0 + sqrt( 1.0 + alpha2 * tanTheta2 );
|
|
51
|
+
return numerator / 2.0;
|
|
52
|
+
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// See equation (34) from reference [0]
|
|
56
|
+
float ggxShadowMaskG1( float theta, float roughness ) {
|
|
57
|
+
|
|
58
|
+
return 1.0 / ( 1.0 + ggxLamda( theta, roughness ) );
|
|
59
|
+
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// See equation (125) from reference [4]
|
|
63
|
+
float ggxShadowMaskG2( vec3 wi, vec3 wo, float roughness ) {
|
|
64
|
+
|
|
65
|
+
float incidentTheta = acos( wi.z );
|
|
66
|
+
float scatterTheta = acos( wo.z );
|
|
67
|
+
return 1.0 / ( 1.0 + ggxLamda( incidentTheta, roughness ) + ggxLamda( scatterTheta, roughness ) );
|
|
68
|
+
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// See equation (33) from reference [0]
|
|
72
|
+
float ggxDistribution( vec3 halfVector, float roughness ) {
|
|
73
|
+
|
|
74
|
+
float a2 = roughness * roughness;
|
|
75
|
+
a2 = max( EPSILON, a2 );
|
|
76
|
+
float cosTheta = halfVector.z;
|
|
77
|
+
float cosTheta4 = pow( cosTheta, 4.0 );
|
|
78
|
+
|
|
79
|
+
if ( cosTheta == 0.0 ) return 0.0;
|
|
80
|
+
|
|
81
|
+
float theta = acosSafe( halfVector.z );
|
|
82
|
+
float tanTheta = tan( theta );
|
|
83
|
+
float tanTheta2 = pow( tanTheta, 2.0 );
|
|
84
|
+
|
|
85
|
+
float denom = PI * cosTheta4 * pow( a2 + tanTheta2, 2.0 );
|
|
86
|
+
return ( a2 / denom );
|
|
87
|
+
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// See equation (3) from reference [2]
|
|
91
|
+
float ggxPDF( vec3 wi, vec3 halfVector, float roughness ) {
|
|
92
|
+
|
|
93
|
+
float incidentTheta = acos( wi.z );
|
|
94
|
+
float D = ggxDistribution( halfVector, roughness );
|
|
95
|
+
float G1 = ggxShadowMaskG1( incidentTheta, roughness );
|
|
96
|
+
|
|
97
|
+
return D * G1 * max( 0.0, dot( wi, halfVector ) ) / wi.z;
|
|
98
|
+
|
|
99
|
+
}
|
|
100
|
+
`;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
export const shaderIridescenceFunctions = /* glsl */`
|
|
2
|
+
|
|
3
|
+
// XYZ to sRGB color space
|
|
4
|
+
const mat3 XYZ_TO_REC709 = mat3(
|
|
5
|
+
3.2404542, -0.9692660, 0.0556434,
|
|
6
|
+
-1.5371385, 1.8760108, -0.2040259,
|
|
7
|
+
-0.4985314, 0.0415560, 1.0572252
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
vec3 fresnel0ToIor( vec3 fresnel0 ) {
|
|
11
|
+
|
|
12
|
+
vec3 sqrtF0 = sqrt( fresnel0 );
|
|
13
|
+
return ( vec3( 1.0 ) + sqrtF0 ) / ( vec3( 1.0 ) - sqrtF0 );
|
|
14
|
+
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Conversion FO/IOR
|
|
18
|
+
vec3 iorToFresnel0( vec3 transmittedIor, float incidentIor ) {
|
|
19
|
+
|
|
20
|
+
return square( ( transmittedIor - vec3( incidentIor ) ) / ( transmittedIor + vec3( incidentIor ) ) );
|
|
21
|
+
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// ior is a value between 1.0 and 3.0. 1.0 is air interface
|
|
25
|
+
float iorToFresnel0( float transmittedIor, float incidentIor ) {
|
|
26
|
+
|
|
27
|
+
return square( ( transmittedIor - incidentIor ) / ( transmittedIor + incidentIor ) );
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Fresnel equations for dielectric/dielectric interfaces. See https://belcour.github.io/blog/research/2017/05/01/brdf-thin-film.html
|
|
32
|
+
vec3 evalSensitivity( float OPD, vec3 shift ) {
|
|
33
|
+
|
|
34
|
+
float phase = 2.0 * PI * OPD * 1.0e-9;
|
|
35
|
+
|
|
36
|
+
vec3 val = vec3( 5.4856e-13, 4.4201e-13, 5.2481e-13 );
|
|
37
|
+
vec3 pos = vec3( 1.6810e+06, 1.7953e+06, 2.2084e+06 );
|
|
38
|
+
vec3 var = vec3( 4.3278e+09, 9.3046e+09, 6.6121e+09 );
|
|
39
|
+
|
|
40
|
+
vec3 xyz = val * sqrt( 2.0 * PI * var ) * cos( pos * phase + shift ) * exp( - square( phase ) * var );
|
|
41
|
+
xyz.x += 9.7470e-14 * sqrt( 2.0 * PI * 4.5282e+09 ) * cos( 2.2399e+06 * phase + shift[ 0 ] ) * exp( - 4.5282e+09 * square( phase ) );
|
|
42
|
+
xyz /= 1.0685e-7;
|
|
43
|
+
|
|
44
|
+
vec3 srgb = XYZ_TO_REC709 * xyz;
|
|
45
|
+
return srgb;
|
|
46
|
+
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// See Section 4. Analytic Spectral Integration, A Practical Extension to Microfacet Theory for the Modeling of Varying Iridescence, https://hal.archives-ouvertes.fr/hal-01518344/document
|
|
50
|
+
vec3 evalIridescence( float outsideIOR, float eta2, float cosTheta1, float thinFilmThickness, vec3 baseF0 ) {
|
|
51
|
+
|
|
52
|
+
vec3 I;
|
|
53
|
+
|
|
54
|
+
// Force iridescenceIor -> outsideIOR when thinFilmThickness -> 0.0
|
|
55
|
+
float iridescenceIor = mix( outsideIOR, eta2, smoothstep( 0.0, 0.03, thinFilmThickness ) );
|
|
56
|
+
|
|
57
|
+
// Evaluate the cosTheta on the base layer (Snell law)
|
|
58
|
+
float sinTheta2Sq = square( outsideIOR / iridescenceIor ) * ( 1.0 - square( cosTheta1 ) );
|
|
59
|
+
|
|
60
|
+
// Handle TIR:
|
|
61
|
+
float cosTheta2Sq = 1.0 - sinTheta2Sq;
|
|
62
|
+
if ( cosTheta2Sq < 0.0 ) {
|
|
63
|
+
|
|
64
|
+
return vec3( 1.0 );
|
|
65
|
+
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
float cosTheta2 = sqrt( cosTheta2Sq );
|
|
69
|
+
|
|
70
|
+
// First interface
|
|
71
|
+
float R0 = iorToFresnel0( iridescenceIor, outsideIOR );
|
|
72
|
+
float R12 = schlickFresnel( cosTheta1, R0 );
|
|
73
|
+
float R21 = R12;
|
|
74
|
+
float T121 = 1.0 - R12;
|
|
75
|
+
float phi12 = 0.0;
|
|
76
|
+
if ( iridescenceIor < outsideIOR ) {
|
|
77
|
+
|
|
78
|
+
phi12 = PI;
|
|
79
|
+
|
|
80
|
+
}
|
|
81
|
+
float phi21 = PI - phi12;
|
|
82
|
+
|
|
83
|
+
// Second interface
|
|
84
|
+
vec3 baseIOR = fresnel0ToIor( clamp( baseF0, 0.0, 0.9999 ) ); // guard against 1.0
|
|
85
|
+
vec3 R1 = iorToFresnel0( baseIOR, iridescenceIor );
|
|
86
|
+
vec3 R23 = schlickFresnel( cosTheta2, R1 );
|
|
87
|
+
vec3 phi23 = vec3( 0.0 );
|
|
88
|
+
if ( baseIOR[0] < iridescenceIor ) {
|
|
89
|
+
|
|
90
|
+
phi23[ 0 ] = PI;
|
|
91
|
+
|
|
92
|
+
}
|
|
93
|
+
if ( baseIOR[1] < iridescenceIor ) {
|
|
94
|
+
|
|
95
|
+
phi23[ 1 ] = PI;
|
|
96
|
+
|
|
97
|
+
}
|
|
98
|
+
if ( baseIOR[2] < iridescenceIor ) {
|
|
99
|
+
|
|
100
|
+
phi23[ 2 ] = PI;
|
|
101
|
+
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Phase shift
|
|
105
|
+
float OPD = 2.0 * iridescenceIor * thinFilmThickness * cosTheta2;
|
|
106
|
+
vec3 phi = vec3( phi21 ) + phi23;
|
|
107
|
+
|
|
108
|
+
// Compound terms
|
|
109
|
+
vec3 R123 = clamp( R12 * R23, 1e-5, 0.9999 );
|
|
110
|
+
vec3 r123 = sqrt( R123 );
|
|
111
|
+
vec3 Rs = square( T121 ) * R23 / ( vec3( 1.0 ) - R123 );
|
|
112
|
+
|
|
113
|
+
// Reflectance term for m = 0 (DC term amplitude)
|
|
114
|
+
vec3 C0 = R12 + Rs;
|
|
115
|
+
I = C0;
|
|
116
|
+
|
|
117
|
+
// Reflectance term for m > 0 (pairs of diracs)
|
|
118
|
+
vec3 Cm = Rs - T121;
|
|
119
|
+
for ( int m = 1; m <= 2; ++ m )
|
|
120
|
+
{
|
|
121
|
+
Cm *= r123;
|
|
122
|
+
vec3 Sm = 2.0 * evalSensitivity( float( m ) * OPD, float( m ) * phi );
|
|
123
|
+
I += Cm * Sm;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Since out of gamut colors might be produced, negative color values are clamped to 0.
|
|
127
|
+
return max( I, vec3( 0.0 ) );
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
`;
|