Skip to content

Commit 1d16377

Browse files
committed
Add defines for favoured glsl functions
1 parent 5603241 commit 1d16377

8 files changed

Lines changed: 15 additions & 30 deletions

File tree

src/renderers/shaders/ShaderChunk/common.glsl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#define LOG2 1.442695
55
#define EPSILON 1e-6
66

7+
#define square(a) a * a
8+
#define saturate(a) clamp( a, 0.0, 1.0 )
9+
#define whiteCompliment(a) 1.0 - saturate( a )
10+
711
vec3 transformDirection( in vec3 normal, in mat4 matrix ) {
812

913
return normalize( ( matrix * vec4( normal, 0.0 ) ).xyz );
@@ -41,7 +45,7 @@ float calcLightAttenuation( float lightDistance, float cutoffDistance, float dec
4145

4246
if ( decayExponent > 0.0 ) {
4347

44-
return pow( clamp( -lightDistance / cutoffDistance + 1.0, 0.0, 1.0 ), decayExponent );
48+
return pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );
4549

4650
}
4751

@@ -75,10 +79,10 @@ vec3 BRDF_BlinnPhong( in vec3 specularColor, in float shininess, in vec3 normal,
7579

7680
vec3 halfDir = normalize( lightDir + viewDir );
7781

78-
//float dotNL = clamp( dot( normal, lightDir ), 0.0, 1.0 );
79-
//float dotNV = clamp( dot( normal, viewDir ), 0.0, 1.0 );
80-
float dotNH = clamp( dot( normal, halfDir ), 0.0, 1.0 );
81-
float dotLH = clamp( dot( lightDir, halfDir ), 0.0, 1.0 );
82+
//float dotNL = saturate( dot( normal, lightDir ) );
83+
//float dotNV = saturate( dot( normal, viewDir ) );
84+
float dotNH = saturate( dot( normal, halfDir ) );
85+
float dotLH = saturate( dot( lightDir, halfDir ) );
8286

8387
vec3 F = F_Schlick( specularColor, dotLH );
8488

src/renderers/shaders/ShaderChunk/envmap_fragment.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
#elif defined( ENVMAP_TYPE_EQUIREC )
3636
vec2 sampleUV;
37-
sampleUV.y = clamp( flipNormal * reflectVec.y * 0.5 + 0.5, 0.0, 1.0 );
37+
sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
3838
sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
3939
vec4 envColor = texture2D( envMap, sampleUV );
4040

src/renderers/shaders/ShaderChunk/fog_fragment.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#ifdef FOG_EXP2
1414

15-
float fogFactor = 1.0 - clamp( exp2( - fogDensity * fogDensity * depth * depth * LOG2 ), 0.0, 1.0 );
15+
float fogFactor = whiteCompliment( exp2( - square( fogDensity ) * square( depth ) * LOG2 ) );
1616

1717
#else
1818

src/renderers/shaders/ShaderChunk/helper_funcs.glsl

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ vec3 totalSpecularLight = vec3( 0.0 );
7878

7979
if ( spotEffect > spotLightAngleCos[ i ] ) {
8080

81-
spotEffect = clamp( pow( clamp( spotEffect, 0.0, 1.0 ), spotLightExponent[ i ] ), 0.0, 1.0 );
81+
spotEffect = saturate( pow( saturate( spotEffect ), spotLightExponent[ i ] ) );
8282

8383
// attenuation
8484

src/renderers/shaders/ShaderChunk/lights_phong_pars_fragment.glsl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ varying vec3 vViewPosition;
5959

6060
vec3 calcCosineTerm( in vec3 normal, in vec3 lightDir ) {
6161

62-
float dotProduct = dot( normal, lightDir );
63-
64-
vec3 cosineTerm = vec3( clamp( dotProduct, 0.0, 1.0 ) );
62+
vec3 cosineTerm = vec3( saturate( dot( normal, lightDir ) ) );
6563

6664
#ifdef WRAP_AROUND
6765

68-
vec3 cosineTermHalf = vec3( clamp( 0.5 * dotProduct + 0.5, 0.0, 1.0 ) );
66+
vec3 cosineTermHalf = vec3( saturate( 0.5 * dotProduct + 0.5 ) );
6967

7068
cosineTerm = mix( cosineTerm, cosineTermHalf, wrapRGB );
7169

src/renderers/shaders/ShaderLib.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ THREE.ShaderLib = {
737737
// " gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
738738
"vec3 direction = normalize( vWorldPosition );",
739739
"vec2 sampleUV;",
740-
"sampleUV.y = clamp( tFlip * direction.y * -0.5 + 0.5, 0.0, 1.0 );",
740+
"sampleUV.y = saturate( tFlip * direction.y * -0.5 + 0.5 );",
741741
"sampleUV.x = atan( direction.z, direction.x ) * RECIPROCAL_PI2 + 0.5;",
742742
"gl_FragColor = texture2D( tEquirect, sampleUV );",
743743

utils/build/includes/common.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@
108108
"src/renderers/shaders/ShaderChunk/envmap_vertex.glsl",
109109
"src/renderers/shaders/ShaderChunk/fog_fragment.glsl",
110110
"src/renderers/shaders/ShaderChunk/fog_pars_fragment.glsl",
111-
"src/renderers/shaders/ShaderChunk/helper_funcs.glsl",
112111
"src/renderers/shaders/ShaderChunk/lightmap_fragment.glsl",
113112
"src/renderers/shaders/ShaderChunk/lightmap_pars_fragment.glsl",
114113
"src/renderers/shaders/ShaderChunk/lights_lambert_pars_vertex.glsl",

0 commit comments

Comments
 (0)