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 all 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
128 changes: 19 additions & 109 deletions examples/js/ShaderSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,11 @@ THREE.ShaderSkin = {
"varying vec3 vNormal;",
"varying vec2 vUv;",

"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",

"varying vec3 vViewPosition;",

THREE.ShaderChunk[ "common" ],
THREE.ShaderChunk[ "bsdfs" ],
THREE.ShaderChunk[ "lights_pars" ],
THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
THREE.ShaderChunk[ "fog_pars_fragment" ],
THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
Expand Down Expand Up @@ -198,9 +174,9 @@ THREE.ShaderSkin = {

"for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",

"vec3 lVector = pointLightPosition[ i ] + vViewPosition.xyz;",
"vec3 lVector = pointLights[ i ].position + vViewPosition.xyz;",

"float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[i] );",
"float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",

"lVector = normalize( lVector );",

Expand All @@ -210,8 +186,8 @@ THREE.ShaderSkin = {

"float pointSpecularWeight = KS_Skin_Specular( normal, lVector, viewerDirection, uRoughness, uSpecularBrightness );",

"totalDiffuseLight += pointLightColor[ i ] * ( pointDiffuseWeight * attenuation );",
"totalSpecularLight += pointLightColor[ i ] * specular * ( pointSpecularWeight * specularStrength * attenuation );",
"totalDiffuseLight += pointLight[ i ].color * ( pointDiffuseWeight * attenuation );",
"totalSpecularLight += pointLight[ i ].color * specular * ( pointSpecularWeight * specularStrength * attenuation );",

"}",

Expand All @@ -223,16 +199,16 @@ THREE.ShaderSkin = {

"for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",

"vec3 dirVector = directionalLightDirection[ i ];",
"vec3 dirVector = directionalLights[ i ].direction;",

"float dirDiffuseWeightFull = max( dot( normal, dirVector ), 0.0 );",
"float dirDiffuseWeightHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
"vec3 dirDiffuseWeight = mix( vec3 ( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), uWrapRGB );",

"float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewerDirection, uRoughness, uSpecularBrightness );",

"totalDiffuseLight += directionalLightColor[ i ] * dirDiffuseWeight;",
"totalSpecularLight += directionalLightColor[ i ] * ( dirSpecularWeight * specularStrength );",
"totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
"totalSpecularLight += directionalLights[ i ].color * ( dirSpecularWeight * specularStrength );",

"}",

Expand Down Expand Up @@ -394,21 +370,10 @@ THREE.ShaderSkin = {
"varying vec3 vNormal;",
"varying vec2 vUv;",

"uniform vec3 ambientLightColor;",

"#if MAX_DIR_LIGHTS > 0",
"uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
"uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
"#endif",

"#if MAX_POINT_LIGHTS > 0",
"uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
"#endif",

"varying vec3 vViewPosition;",

THREE.ShaderChunk[ "common" ],
THREE.ShaderChunk[ "lights_pars" ],
THREE.ShaderChunk[ "fog_pars_fragment" ],

"float fresnelReflectance( vec3 H, vec3 V, float F0 ) {",
Expand Down Expand Up @@ -490,12 +455,12 @@ THREE.ShaderSkin = {
"#if MAX_POINT_LIGHTS > 0",

"for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",

"vec3 pointVector = normalize( vPointLight[ i ].xyz );",
"float attenuation = vPointLight[ i ].w;",

"vec3 pointVector = normalize( pointLights[ i ].direction );",
"float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",
"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",

"totalDiffuseLight += pointLightColor[ i ] * ( pointDiffuseWeight * attenuation );",

"if ( passID == 1 ) {",
Expand All @@ -516,17 +481,18 @@ THREE.ShaderSkin = {

"for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",

"vec3 dirVector = directionalLightDirection[ i ];",
"vec3 dirVector = directionalLights[ i ].direction;",

"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",

"totalDiffuseLight += directionalLightColor[ i ] * dirDiffuseWeight;",

"totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",

"if ( passID == 1 ) {",

"float dirSpecularWeight = KS_Skin_Specular( normal, dirVector, viewerDirection, uRoughness, uSpecularBrightness );",

"totalSpecularLight += directionalLightColor[ i ] * mSpecular.xyz * dirSpecularWeight;",
"totalSpecularLight += directionalLights[ i ].color * mSpecular.xyz * dirSpecularWeight;",

"}",

Expand Down Expand Up @@ -607,16 +573,6 @@ THREE.ShaderSkin = {
"varying vec3 vNormal;",
"varying vec2 vUv;",

"#if MAX_POINT_LIGHTS > 0",

"uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
"uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
"uniform float pointLightDecay[ MAX_POINT_LIGHTS ];",

"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",

"#endif",

"varying vec3 vViewPosition;",

THREE.ShaderChunk[ "common" ],
Expand All @@ -633,24 +589,6 @@ THREE.ShaderSkin = {

"vUv = uv;",

// point lights

"#if MAX_POINT_LIGHTS > 0",

"for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",

"vec3 lVector = pointLightPosition[ i ] - vViewPosition;",

"float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[i] );",

"lVector = normalize( lVector );",

"vPointLight[ i ] = vec4( lVector, attenuation );",

"}",

"#endif",

// displacement mapping

"#ifdef VERTEX_TEXTURES",
Expand All @@ -675,16 +613,6 @@ THREE.ShaderSkin = {
"varying vec3 vNormal;",
"varying vec2 vUv;",

"#if MAX_POINT_LIGHTS > 0",

"uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
"uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
"uniform float pointLightDecay[ MAX_POINT_LIGHTS ];",

"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",

"#endif",

"varying vec3 vViewPosition;",

THREE.ShaderChunk[ "common" ],
Expand All @@ -701,24 +629,6 @@ THREE.ShaderSkin = {

"vUv = uv;",

// point lights

"#if MAX_POINT_LIGHTS > 0",

"for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",

"vec3 lVector = pointLightPosition[ i ] - vViewPosition;",

"float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[i] );",

"lVector = normalize( lVector );",

"vPointLight[ i ] = vec4( lVector, attenuation );",

"}",

"#endif",

"gl_Position = vec4( uv.x * 2.0 - 1.0, uv.y * 2.0 - 1.0, 0.0, 1.0 );",

"}"
Expand Down
44 changes: 11 additions & 33 deletions examples/js/ShaderTerrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,33 +86,11 @@ THREE.ShaderTerrain = {

"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",

"varying vec3 vViewPosition;",

THREE.ShaderChunk[ "common" ],
THREE.ShaderChunk[ "bsdfs" ],
THREE.ShaderChunk[ "lights_pars" ],
THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
THREE.ShaderChunk[ "fog_pars_fragment" ],

Expand Down Expand Up @@ -168,9 +146,9 @@ THREE.ShaderTerrain = {

"for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",

"vec3 lVector = pointLightPosition[ i ] + vViewPosition.xyz;",
"vec3 lVector = pointLights[ i ].position + vViewPosition.xyz;",

"float attenuation = calcLightAttenuation( length( lVector ), pointLightDistance[ i ], pointLightDecay[i] );",
"float attenuation = calcLightAttenuation( length( lVector ), pointLights[ i ].distance, pointLights[ i ].decay );",

"lVector = normalize( lVector );",

Expand All @@ -181,8 +159,8 @@ THREE.ShaderTerrain = {

"float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",

"totalDiffuseLight += attenuation * pointLightColor[ i ] * pointDiffuseWeight;",
"totalSpecularLight += attenuation * pointLightColor[ i ] * specular * pointSpecularWeight * pointDiffuseWeight;",
"totalDiffuseLight += attenuation * pointLights[ i ].color * pointDiffuseWeight;",
"totalSpecularLight += attenuation * pointLights[ i ].color * specular * pointSpecularWeight * pointDiffuseWeight;",

"}",

Expand All @@ -197,16 +175,16 @@ THREE.ShaderTerrain = {

"for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",

"vec3 dirVector = directionalLightDirection[ i ];",
"vec3 dirVector = directionalLights[ i ].direction;",
"vec3 dirHalfVector = normalize( dirVector + viewPosition );",

"float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",

"float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",

"totalDiffuseLight += directionalLightColor[ i ] * dirDiffuseWeight;",
"totalSpecularLight += directionalLightColor[ i ] * specular * dirSpecularWeight * dirDiffuseWeight;",
"totalDiffuseLight += directionalLights[ i ].color * dirDiffuseWeight;",
"totalSpecularLight += directionalLights[ i ].color * specular * dirSpecularWeight * dirDiffuseWeight;",

"}",

Expand All @@ -228,7 +206,7 @@ THREE.ShaderTerrain = {
"float dotProduct = dot( normal, lVector );",
"float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",

"totalDiffuseLight += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
"totalDiffuseLight += mix( hemisphereLights[ i ].groundColor, hemisphereLights[ i ].skyColor, hemiDiffuseWeight );",

// specular (sky light)

Expand All @@ -246,7 +224,7 @@ THREE.ShaderTerrain = {
"float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
"hemiSpecularWeight += specularTex.r * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",

"totalSpecularLight += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",
"totalSpecularLight += specular * mix( hemisphereLights[ i ].groundColor, hemisphereLights[ i ].skyColor, hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",

"}",

Expand Down
2 changes: 2 additions & 0 deletions examples/webgl_animation_skinning_morph.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@
var loader = new THREE.JSONLoader();
loader.load( "models/skinned/knight.js", function ( geometry, materials ) {

console.log( 'materials', materials );

createScene( geometry, materials, 0, FLOOR, -300, 60 )

} );
Expand Down
2 changes: 1 addition & 1 deletion examples/webgl_lights_pointlights.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

var callback = function( geometry ) {

object = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: 0x555555, specular: 0xffffff, shininess: 50 } ) );
object = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial( { color: 0x555555, specular: 0x111111, shininess: 50 } ) );
object.scale.x = object.scale.y = object.scale.z = 0.80;
scene.add( object );

Expand Down
13 changes: 9 additions & 4 deletions examples/webgl_materials_lambert_variations.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,29 @@

var geometry = new THREE.SphereBufferGeometry( sphereRadius, 32, 16 );


for( var alpha = 0; alpha <= 1.0; alpha += stepSize ) {
for( var alpha = 0, alphaIndex = 0; alpha <= 1.0; alpha += stepSize, alphaIndex ++ ) {

var baseColor = new THREE.Color().setHSL( alpha, 0.5, 0.5 );

if( alpha >= 0.5 ) {
reflectionCube = null;
}

for( var beta = 0; beta <= 1.0; beta += stepSize ) {
for( var beta = 0, betaIndex = 0; beta <= 1.0; beta += stepSize, betaIndex ++ ) {

var reflectivity = beta;

var side = THREE.FrontSide;
if( ( betaIndex % 2 ) === 0 ) {
side = THREE.DoubleSide;
}

for( var gamma = 0; gamma <= 1.0; gamma += stepSize ) {

var diffuseColor = baseColor.clone().multiplyScalar( gamma );

var material = new THREE.MeshLambertMaterial( { map: imgTexture, color: diffuseColor, reflectivity: reflectivity, shading: THREE.SmoothShading, envMap: reflectionCube } )
var material = new THREE.MeshLambertMaterial( { map: imgTexture, color: diffuseColor,
reflectivity: reflectivity, envMap: reflectionCube, side: side } )

var mesh = new THREE.Mesh( geometry, material );

Expand Down
Loading