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

attempt to simplify the phong shaders and lights. #7324

Merged
merged 67 commits into from
Oct 26, 2015
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
ea9cfe0
attempt to simplify the phong shaders and lights.
bhouston Oct 9, 2015
d744760
cleaner use of diffuse.
bhouston Oct 9, 2015
ad75704
debugging uniforms.
bhouston Oct 10, 2015
74d3aa3
parse arrays of struct uniforms, code cleanup.
bhouston Oct 10, 2015
e889e6b
add support for structs not in arrays in WebGLProgram.
bhouston Oct 10, 2015
c1df173
ways to define ThreeJS "uniforms" for structs and struct arrays. way…
bhouston Oct 10, 2015
7985c8b
more work on supporting struct and struct uniform types.
bhouston Oct 10, 2015
8c5bc52
cleaner and more GC efficient.
bhouston Oct 10, 2015
223c383
reduce the need for temporaries, cleanup code.
bhouston Oct 10, 2015
3f4a946
more code removal
bhouston Oct 10, 2015
339e4d3
more code removal
bhouston Oct 10, 2015
bbace72
structs barely work.
bhouston Oct 10, 2015
28754cf
lambert lighting works, defining light helpers only once in common.glsl
bhouston Oct 10, 2015
e21dd08
further cleanup to simplify usage, avoiding premature optimization.
bhouston Oct 11, 2015
d443359
remove unused code.
bhouston Oct 11, 2015
faa1967
separation of light accumulators into diffuse/specular, direct/indirect
bhouston Oct 11, 2015
56b6472
remove debug console output.
bhouston Oct 11, 2015
d8552f3
introduce IncidentLight struct
bhouston Oct 11, 2015
f25fd9b
add ReflectedLight struct to pair with IncidentLight struct.
bhouston Oct 11, 2015
c66fd07
simplier definition of ReflectedLight.
bhouston Oct 11, 2015
fb54728
declare reused variable only once.
bhouston Oct 11, 2015
8d0d454
cleaner design.
bhouston Oct 11, 2015
b101729
add energy perserving rgb/monochrome modes. add BRDF_OrenNayar.
bhouston Oct 12, 2015
ea903ed
introducing the GeometricContext struct, broken hemisphere lights.
bhouston Oct 12, 2015
ea9f26a
hemisphere lights work again.
bhouston Oct 12, 2015
27dc0ad
unify energy conservating with correct diffuse lambertian reflectance
bhouston Oct 12, 2015
9bc5094
geometryContext -> geometry.
bhouston Oct 12, 2015
36c5996
initial AreaLight implementation.
bhouston Oct 13, 2015
5d4ad85
rely less on the preprocessing, add GGX shader with SmithGeometry vis…
bhouston Oct 13, 2015
fa51e8c
initial light probe skeleton.
bhouston Oct 13, 2015
f96f6e4
add faster area light clamping.
bhouston Oct 13, 2015
3f48049
expose energyConserving as a material parameter on MeshPhongMaterial.
bhouston Oct 14, 2015
bbc81cd
start refactoring ShaderSkin.js
bhouston Oct 20, 2015
3c9e00f
get skin, tone mapping and terrain examples working with new BSDF and…
bhouston Oct 20, 2015
fdaad8d
Merge branch 'dev' of git@github.com:mrdoob/three.js into simplified_…
bhouston Oct 20, 2015
0287cab
remove unused code from broken ShaderSkin.js
bhouston Oct 20, 2015
c45b7cf
shadowMask shouldn't be applied to indirect.
bhouston Oct 20, 2015
8c8f538
restore erroneously removed modifications by @WestLangley.
bhouston Oct 20, 2015
c290352
simplify light factories.
bhouston Oct 20, 2015
e40d3f7
remove no longer used hemilight_fragment.glsl
bhouston Oct 20, 2015
a101882
minor bugs.
bhouston Oct 20, 2015
95534e3
refactor physical materials to use new BRDF+Light structs
bhouston Oct 20, 2015
08fe989
fix bug in physical material conversion to structs, add energy preser…
bhouston Oct 20, 2015
05118ea
further consolidation of different material BRDFs via the same lighti…
bhouston Oct 21, 2015
ba207f0
incorporate indirectReflectedLight into lightmap, envmap and oamap pe…
bhouston Oct 21, 2015
b1df023
fix reflectivity (it should modulate specularColor globally), fix GGX…
bhouston Oct 21, 2015
c5e001a
incorporate ambientLightColor into standardized BRDF calculations.
bhouston Oct 21, 2015
28997aa
accumulate all indirect diffuse sources and then apply a single diffu…
bhouston Oct 21, 2015
8610ef3
remove unneeded function.
bhouston Oct 21, 2015
7829fff
Merge branch 'material_variations_examples' of git@github.com:bhousto…
bhouston Oct 21, 2015
23ebff6
split BRDFs from Rendering Equations per @WestLangley's recommendations.
bhouston Oct 21, 2015
26b54cd
validated all bsdfs against preexisting code. materials match except…
bhouston Oct 21, 2015
f89f27d
remove unused code (clear coat), oren-nayar, and make envmap work in …
bhouston Oct 21, 2015
89b3dac
remove energy conserving mode.
bhouston Oct 21, 2015
ce22ecc
perfect backwards compatibility for envmap_fragment for phong.
bhouston Oct 21, 2015
f75c055
add lambert variations example.
bhouston Oct 21, 2015
d59e16a
update basic material to use new indirectReflectedLight structure, ad…
bhouston Oct 21, 2015
e12f233
Merge branch 'material_variations_examples' into simplified_lighting
bhouston Oct 21, 2015
2d3f699
fix double application of ambient light.
bhouston Oct 21, 2015
181973a
Explicitly optimized, rather than theoretically pure.
bhouston Oct 21, 2015
0cf45b8
better description of lights_template. remove double normalize.
bhouston Oct 22, 2015
5c6fc49
simplification of BSDFs.
bhouston Oct 22, 2015
5ad4d10
forgotten function name update.
bhouston Oct 22, 2015
d1499b2
more straightforward optimizations.
bhouston Oct 22, 2015
752fc8b
bug fix.
bhouston Oct 22, 2015
082a19d
add lambert variations now varies double sided.
bhouston Oct 22, 2015
6737d10
cleanup.
bhouston Oct 22, 2015
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
326 changes: 159 additions & 167 deletions src/renderers/WebGLRenderer.js

Large diffs are not rendered by default.

167 changes: 139 additions & 28 deletions src/renderers/shaders/ShaderChunk/common.glsl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#define PI 3.14159
#define PI2 6.28318
#define RECIPROCAL_PI 0.31830988618
#define RECIPROCAL_PI2 0.15915494
#define LOG2 1.442695
#define EPSILON 1e-6

#define PHYSICALLY_BASED_RENDERING
#define saturate(a) clamp( a, 0.0, 1.0 )
#define whiteCompliment(a) ( 1.0 - saturate( a ) )

Expand Down Expand Up @@ -52,77 +53,187 @@ float calcLightAttenuation( float lightDistance, float cutoffDistance, float dec

}

vec3 F_Schlick( in vec3 specularColor, in float dotLH ) {
vec3 inputToLinear( in vec3 a ) {

#ifdef GAMMA_INPUT

return pow( a, vec3( float( GAMMA_FACTOR ) ) );

#else

return a;

#endif

}

vec3 linearToOutput( in vec3 a ) {

#ifdef GAMMA_OUTPUT

return pow( a, vec3( 1.0 / float( GAMMA_FACTOR ) ) );

#else

return a;

#endif

}


struct IncidentLight {
vec3 color;
vec3 direction;
};


vec3 BRDF_Lambert( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 diffuseColor ) {

return incidentLight.color * diffuseColor * ( saturate( dot( normal, incidentLight.direction ) ) );

// the above should be scaled by '' * RECIPROCAL_PI'
}

vec3 F_Schlick( const in vec3 F0, const in float dotLH ) {

// Original approximation by Christophe Schlick '94
//;float fresnel = pow( 1.0 - dotLH, 5.0 );

// Optimized variant (presented by Epic at SIGGRAPH '13)
float fresnel = exp2( ( -5.55437 * dotLH - 6.98316 ) * dotLH );

return ( 1.0 - specularColor ) * fresnel + specularColor;
return F0 + ( 1.0 - F0 ) * fresnel;

}

float G_BlinnPhong_Implicit( /* in float dotNL, in float dotNV */ ) {

// geometry term is (nl)(nv) / 4(nl)(nv)
// geometry term is (n dot l)(n dot v) / 4(n dot l)(n dot v)

return 0.25;

}

float D_BlinnPhong( in float shininess, in float dotNH ) {
float D_BlinnPhong( const in float shininess, const in float dotNH ) {

// factor of 1/PI in distribution term omitted

return ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );

}

vec3 BRDF_BlinnPhong( in vec3 specularColor, in float shininess, in vec3 normal, in vec3 lightDir, in vec3 viewDir ) {
vec3 BRDF_BlinnPhong( const in IncidentLight incidentLight, const in vec3 normal, const in vec3 viewDir, const in vec3 specularColor, const in float shininess ) {

vec3 halfDir = normalize( lightDir + viewDir );

//float dotNL = saturate( dot( normal, lightDir ) );
//float dotNV = saturate( dot( normal, viewDir ) );
vec3 halfDir = normalize( incidentLight.direction + viewDir );
float dotNH = saturate( dot( normal, halfDir ) );
float dotLH = saturate( dot( lightDir, halfDir ) );
float dotLH = saturate( dot( incidentLight.direction, halfDir ) );

vec3 F = F_Schlick( specularColor, dotLH );

float G = G_BlinnPhong_Implicit( /* dotNL, dotNV */ );

float D = D_BlinnPhong( shininess, dotNH );

return F * G * D;
return incidentLight.color * F * ( G * D );

}

vec3 inputToLinear( in vec3 a ) {
#if MAX_DIR_LIGHTS > 0

#ifdef GAMMA_INPUT
struct DirectionalLight {
vec3 direction;
vec3 color;
};

return pow( a, vec3( float( GAMMA_FACTOR ) ) );
uniform DirectionalLight singleTestDirLight;

#else
uniform DirectionalLight directionalLights[ MAX_DIR_LIGHTS ];

return a;
void getDirLightDirect( const in DirectionalLight directionalLight, out IncidentLight incidentLight ) {

incidentLight.color = directionalLight.color;
incidentLight.direction = directionalLight.direction;

#endif
}

}
#endif

#if MAX_POINT_LIGHTS > 0

struct PointLight {
vec3 position;
vec3 color;
float distance;
float decay;
};

uniform PointLight pointLights[ MAX_POINT_LIGHTS ];

void getPointLightDirect( const in PointLight pointLight, const in vec3 position, out IncidentLight incidentLight ) {

vec3 lightPosition = pointLight.position;

vec3 lVector = lightPosition - position;
incidentLight.direction = normalize( lVector );

incidentLight.color = pointLight.color;
incidentLight.color *= calcLightAttenuation( length( lVector ), pointLight.distance, pointLight.decay );

}

vec3 linearToOutput( in vec3 a ) {
#endif

#if MAX_SPOT_LIGHTS > 0

struct SpotLight {
vec3 position;
vec3 direction;
vec3 color;
float distance;
float decay;
float angleCos;
float exponent;
};

uniform SpotLight spotLights[ MAX_SPOT_LIGHTS ];

void getSpotLightDirect( const in SpotLight spotLight, const in vec3 position, out IncidentLight incidentLight ) {

vec3 lightPosition = spotLight.position;

vec3 lVector = lightPosition - position;
incidentLight.direction = normalize( lVector );

float spotEffect = dot( spotLight.direction, incidentLight.direction );
spotEffect = saturate( pow( saturate( spotEffect ), spotLight.exponent ) );

incidentLight.color = spotLight.color;
incidentLight.color *= ( spotEffect * calcLightAttenuation( length( lVector ), spotLight.distance, spotLight.decay ) );

#ifdef GAMMA_OUTPUT
}

return pow( a, vec3( 1.0 / float( GAMMA_FACTOR ) ) );
#endif

#else

return a;
#if MAX_HEMI_LIGHTS > 0

#endif
struct HemisphereLight {
vec3 direction;
vec3 skyColor;
vec3 groundColor;
};

}
uniform HemisphereLight hemisphereLights[ MAX_HEMI_LIGHTS ];

void getHemisphereLightIndirect( const in HemisphereLight hemiLight, const in vec3 normal, out IncidentLight incidentLight ) {

float dotNL = dot( normal, hemiLight.direction );

float hemiDiffuseWeight = 0.5 * dotNL + 0.5;

incidentLight.color = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );

incidentLight.direction = normal;

}

#endif
18 changes: 0 additions & 18 deletions src/renderers/shaders/ShaderChunk/hemilight_fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,18 +0,0 @@
#if MAX_HEMI_LIGHTS > 0

for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {

vec3 lightDir = hemisphereLightDirection[ i ];

float dotProduct = dot( normal, lightDir );

float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;

vec3 lightColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );

totalAmbientLight += lightColor;

}

#endif

36 changes: 0 additions & 36 deletions src/renderers/shaders/ShaderChunk/lights_lambert_pars_vertex.glsl
Original file line number Diff line number Diff line change
@@ -1,37 +1 @@
uniform vec3 ambientLightColor;

#if MAX_DIR_LIGHTS > 0

uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];
uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];

#endif

#if MAX_HEMI_LIGHTS > 0

uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];
uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];
uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];

#endif

#if MAX_POINT_LIGHTS > 0

uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];
uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];
uniform float pointLightDistance[ MAX_POINT_LIGHTS ];
uniform float pointLightDecay[ MAX_POINT_LIGHTS ];

#endif

#if MAX_SPOT_LIGHTS > 0

uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];
uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];
uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];
uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];
uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];
uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];
uniform float spotLightDecay[ MAX_SPOT_LIGHTS ];

#endif
Loading