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

use diffuseColor and outgoingLight instead of repeatedly redefining what gl_FragColor is #5805

Merged
merged 21 commits into from
Mar 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
fd500a6
ensure that lightMap is modulated by surface color and then added to …
bhouston Dec 22, 2014
cc7634e
refactor shaders to use diffuseColor and outgoingLight instead of gl_…
bhouston Dec 23, 2014
80ff7d2
complete application of outgoingLight and diffuseColor to ShaderSkin.js
bhouston Dec 23, 2014
93517e6
fix bug related to semi-transparent maps.
bhouston Dec 23, 2014
728f409
do not modulate emissive by diffuseColor.
bhouston Dec 23, 2014
f15ae3a
replicate previous phong metal look -- it still isn't correct but at …
bhouston Dec 23, 2014
91b34be
fix particle_basic to new outgoingLight/diffuseColor model.
bhouston Dec 23, 2014
487e008
improve settings for light map example now that lightMap is properly …
bhouston Dec 23, 2014
3e23921
Merge branch 'dev' of git@github.com:mrdoob/three.js into phong_diffu…
bhouston Feb 19, 2015
cdd6d71
reduce greeness of light in morph example.
bhouston Feb 19, 2015
8019d61
fix bug in merge - need to use outgoingColor here.
bhouston Feb 19, 2015
74b09dd
for consistency with new outgoingLight variable in shaders.
bhouston Feb 19, 2015
784a890
minor fix to get ShaderSkin skin shader matching previous state exactly.
bhouston Feb 19, 2015
133e539
Fix note #2 of https://github.com/mrdoob/three.js/pull/5805#issuecomm…
bhouston Mar 9, 2015
8cb82c8
Fix note #5 of https://github.com/mrdoob/three.js/pull/5805#issuecomm…
bhouston Mar 9, 2015
47b7f88
Fix note #3 of https://github.com/mrdoob/three.js/pull/5805#issuecomm…
bhouston Mar 9, 2015
7fe874e
Fix note #1 of https://github.com/mrdoob/three.js/pull/5805#issuecomm…
bhouston Mar 9, 2015
2ba715f
Fix note #3 of issue https://github.com/mrdoob/three.js/pull/5805#iss…
bhouston Mar 9, 2015
b0eb1bf
Fix node #6 of issue https://github.com/mrdoob/three.js/pull/5805#iss…
bhouston Mar 9, 2015
98f65da
Fix node #3 of issue https://github.com/mrdoob/three.js/pull/5805#iss…
bhouston Mar 9, 2015
2d218c6
clean up lightmap example to make it look more attractive with normal…
bhouston Mar 9, 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
100 changes: 35 additions & 65 deletions examples/js/ShaderSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ THREE.ShaderSkin = {

"void main() {",

"gl_FragColor = vec4( vec3( 1.0 ), opacity );",
" vec3 outgoingLight = vec3( 0.0, 0.0, 0.0 );", // outgoing light does not have an alpha, the surface does
" vec4 diffuseColor = vec4( diffuse, opacity );",

"vec4 colDiffuse = texture2D( tDiffuse, vUv );",
"colDiffuse.rgb *= colDiffuse.rgb;",

"gl_FragColor = gl_FragColor * colDiffuse;",
"diffuseColor = diffuseColor * colDiffuse;",

"vec3 normal = normalize( vNormal );",
"vec3 viewPosition = normalize( vViewPosition );",
Expand All @@ -190,12 +191,11 @@ THREE.ShaderSkin = {

// point lights

"vec3 specularTotal = vec3( 0.0 );",
"vec3 totalSpecularLight = vec3( 0.0 );",
"vec3 totalDiffuseLight = vec3( 0.0 );",

"#if MAX_POINT_LIGHTS > 0",

"vec3 pointTotal = vec3( 0.0 );",

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

"vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
Expand All @@ -212,8 +212,8 @@ THREE.ShaderSkin = {

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

"pointTotal += attenuation * diffuse * pointLightColor[ i ] * pointDiffuseWeight;",
"specularTotal += attenuation * specular * pointLightColor[ i ] * pointSpecularWeight * specularStrength;",
"totalDiffuseLight += attenuation * pointLightColor[ i ] * pointDiffuseWeight;",
"totalSpecularLight += attenuation * specular * pointLightColor[ i ] * pointSpecularWeight * specularStrength;",

"}",

Expand All @@ -223,8 +223,6 @@ THREE.ShaderSkin = {

"#if MAX_DIR_LIGHTS > 0",

"vec3 dirTotal = vec3( 0.0 );",

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

"vec3 dirVector = transformDirection( directionalLightDirection[ i ], viewMatrix );",
Expand All @@ -235,8 +233,8 @@ THREE.ShaderSkin = {

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

"dirTotal += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
"specularTotal += specular * directionalLightColor[ i ] * dirSpecularWeight * specularStrength;",
"totalDiffuseLight += directionalLightColor[ i ] * dirDiffuseWeight;",
"totalSpecularLight += specular * directionalLightColor[ i ] * dirSpecularWeight * specularStrength;",

"}",

Expand All @@ -246,16 +244,14 @@ THREE.ShaderSkin = {

"#if MAX_HEMI_LIGHTS > 0",

"vec3 hemiTotal = vec3( 0.0 );",

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

"vec3 lVector = transformDirection( hemisphereLightDirection[ i ], viewMatrix );",

"float dotProduct = dot( normal, lVector );",
"float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",

"hemiTotal += diffuse * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
"totalDiffuseLight += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",

// specular (sky light)

Expand All @@ -267,34 +263,21 @@ THREE.ShaderSkin = {
"vec3 lVectorGround = -lVector;",
"hemiSpecularWeight += KS_Skin_Specular( normal, lVectorGround, viewPosition, uRoughness, uSpecularBrightness );",

"specularTotal += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * specularStrength;",
"totalSpecularLight += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * specularStrength;",

"}",

"#endif",

// all lights contribution summation

"vec3 totalLight = vec3( 0.0 );",

"#if MAX_DIR_LIGHTS > 0",
"totalLight += dirTotal;",
"#endif",

"#if MAX_POINT_LIGHTS > 0",
"totalLight += pointTotal;",
"#endif",

"#if MAX_HEMI_LIGHTS > 0",
"totalLight += hemiTotal;",
"#endif",

"gl_FragColor.xyz = gl_FragColor.xyz * ( totalLight + ambientLightColor * diffuse ) + specularTotal;",
"outgoingLight += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor * diffuse ) + totalSpecularLight;",

THREE.ShaderChunk[ "shadowmap_fragment" ],
THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
THREE.ShaderChunk[ "fog_fragment" ],

" gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects


"}"

].join("\n"),
Expand Down Expand Up @@ -468,9 +451,9 @@ THREE.ShaderSkin = {

"void main() {",

"gl_FragColor = vec4( 1.0 );",
" vec3 outgoingLight = vec3( 0.0, 0.0, 0.0 );", // outgoing light does not have an alpha, the surface does
" vec4 diffuseColor = vec4( diffuse, opacity );",

"vec4 mColor = vec4( diffuse, opacity );",
"vec4 mSpecular = vec4( specular, opacity );",

"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
Expand All @@ -480,7 +463,7 @@ THREE.ShaderSkin = {
"vec4 colDiffuse = texture2D( tDiffuse, vUv );",
"colDiffuse *= colDiffuse;",

"gl_FragColor = gl_FragColor * colDiffuse;",
"diffuseColor *= colDiffuse;",

"mat3 tsb = mat3( vTangent, vBinormal, vNormal );",
"vec3 finalNormal = tsb * normalTex;",
Expand All @@ -490,23 +473,22 @@ THREE.ShaderSkin = {

// point lights

"vec3 specularTotal = vec3( 0.0 );",
"vec3 totalDiffuseLight = vec3( 0.0 );",
"vec3 totalSpecularLight = vec3( 0.0 );",

"#if MAX_POINT_LIGHTS > 0",

"vec4 pointTotal = vec4( vec3( 0.0 ), 1.0 );",

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

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

"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",

"pointTotal += pointDistance * vec4( pointLightColor[ i ], 1.0 ) * ( mColor * pointDiffuseWeight );",
"totalDiffuseLight += pointDistance * pointLightColor[ i ] * pointDiffuseWeight;",

"if ( passID == 1 )",
"specularTotal += pointDistance * mSpecular.xyz * pointLightColor[ i ] * KS_Skin_Specular( normal, pointVector, viewPosition, uRoughness, uSpecularBrightness );",
"totalSpecularLight += pointDistance * mSpecular.xyz * pointLightColor[ i ] * KS_Skin_Specular( normal, pointVector, viewPosition, uRoughness, uSpecularBrightness );",

"}",

Expand All @@ -516,52 +498,39 @@ THREE.ShaderSkin = {

"#if MAX_DIR_LIGHTS > 0",

"vec4 dirTotal = vec4( vec3( 0.0 ), 1.0 );",

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

"vec3 dirVector = transformDirection( directionalLightDirection[ i ], viewMatrix );",

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

"dirTotal += vec4( directionalLightColor[ i ], 1.0 ) * ( mColor * dirDiffuseWeight );",
"totalDiffuseLight += directionalLightColor[ i ] * dirDiffuseWeight;",

"if ( passID == 1 )",
"specularTotal += mSpecular.xyz * directionalLightColor[ i ] * KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",
"totalSpecularLight += mSpecular.xyz * directionalLightColor[ i ] * KS_Skin_Specular( normal, dirVector, viewPosition, uRoughness, uSpecularBrightness );",

"}",

"#endif",

// all lights contribution summation

"vec4 totalLight = vec4( vec3( 0.0 ), opacity );",

"#if MAX_DIR_LIGHTS > 0",
"totalLight += dirTotal;",
"#endif",

"#if MAX_POINT_LIGHTS > 0",
"totalLight += pointTotal;",
"#endif",

"gl_FragColor = gl_FragColor * totalLight;",
"outgoingLight += diffuseColor.rgb * ( totalDiffuseLight + totalSpecularLight );",

"if ( passID == 0 ) {",

"gl_FragColor = vec4( sqrt( gl_FragColor.xyz ), gl_FragColor.w );",
"outgoingLight = sqrt( outgoingLight );",

"} else if ( passID == 1 ) {",

//"#define VERSION1",

"#ifdef VERSION1",

"vec3 nonblurColor = sqrt( gl_FragColor.xyz );",
"vec3 nonblurColor = sqrt(outgoingLight );",

"#else",

"vec3 nonblurColor = gl_FragColor.xyz;",
"vec3 nonblurColor = outgoingLight;",

"#endif",

Expand All @@ -578,27 +547,28 @@ THREE.ShaderSkin = {
//"gl_FragColor = vec4( vec3( 0.25, 0.6, 0.8 ) * nonblurColor + vec3( 0.15, 0.25, 0.2 ) * blur1Color + vec3( 0.15, 0.15, 0.0 ) * blur2Color + vec3( 0.45, 0.0, 0.0 ) * blur3Color, gl_FragColor.w );",


"gl_FragColor = vec4( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
"outgoingLight = vec3( vec3( 0.22, 0.437, 0.635 ) * nonblurColor + ",
"vec3( 0.101, 0.355, 0.365 ) * blur1Color + ",
"vec3( 0.119, 0.208, 0.0 ) * blur2Color + ",
"vec3( 0.114, 0.0, 0.0 ) * blur3Color + ",
"vec3( 0.444, 0.0, 0.0 ) * blur4Color",
", gl_FragColor.w );",
"vec3( 0.444, 0.0, 0.0 ) * blur4Color );",

"gl_FragColor.xyz *= pow( colDiffuse.xyz, vec3( 0.5 ) );",
"outgoingLight *= sqrt( colDiffuse.xyz );",

"gl_FragColor.xyz += ambientLightColor * diffuse * colDiffuse.xyz + specularTotal;",
"outgoingLight += ambientLightColor * diffuse * colDiffuse.xyz + totalSpecularLight;",

"#ifndef VERSION1",

"gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
"outgoingLight = sqrt( outgoingLight );",

"#endif",

"}",

THREE.ShaderChunk[ "fog_fragment" ],

" gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects

"}"

].join("\n"),
Expand Down
59 changes: 18 additions & 41 deletions examples/js/ShaderTerrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ THREE.ShaderTerrain = {

"void main() {",

"gl_FragColor = vec4( vec3( 1.0 ), opacity );",
" vec3 outgoingLight = vec3( 0.0, 0.0, 0.0 );", // outgoing light does not have an alpha, the surface does
" vec4 diffuseColor = vec4( diffuse, opacity );",

"vec3 specularTex = vec3( 1.0 );",

Expand All @@ -137,15 +138,15 @@ THREE.ShaderTerrain = {
"colDiffuse1.xyz = inputToLinear( colDiffuse1.xyz );",
"colDiffuse2.xyz = inputToLinear( colDiffuse2.xyz );",

"gl_FragColor = gl_FragColor * mix ( colDiffuse1, colDiffuse2, 1.0 - texture2D( tDisplacement, uvBase ) );",
"diffuseColor *= mix ( colDiffuse1, colDiffuse2, 1.0 - texture2D( tDisplacement, uvBase ) );",

" } else if( enableDiffuse1 ) {",

"gl_FragColor = gl_FragColor * texture2D( tDiffuse1, uvOverlay );",
"diffuseColor *= texture2D( tDiffuse1, uvOverlay );",

"} else if( enableDiffuse2 ) {",

"gl_FragColor = gl_FragColor * texture2D( tDiffuse2, uvOverlay );",
"diffuseColor *= texture2D( tDiffuse2, uvOverlay );",

"}",

Expand All @@ -158,13 +159,13 @@ THREE.ShaderTerrain = {
"vec3 normal = normalize( finalNormal );",
"vec3 viewPosition = normalize( vViewPosition );",

"vec3 totalDiffuseLight = vec3( 0.0 );",
"vec3 totalSpecularLight = vec3( 0.0 );",

// point lights

"#if MAX_POINT_LIGHTS > 0",

"vec3 pointDiffuse = vec3( 0.0 );",
"vec3 pointSpecular = vec3( 0.0 );",

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

"vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
Expand All @@ -181,8 +182,8 @@ THREE.ShaderTerrain = {

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

"pointDiffuse += attenuation * pointLightColor[ i ] * diffuse * pointDiffuseWeight;",
"pointSpecular += attenuation * pointLightColor[ i ] * specular * pointSpecularWeight * pointDiffuseWeight;",
"totalDiffuseLight += attenuation * pointLightColor[ i ] * pointDiffuseWeight;",
"totalSpecularLight += attenuation * pointLightColor[ i ] * specular * pointSpecularWeight * pointDiffuseWeight;",

"}",

Expand All @@ -205,8 +206,8 @@ THREE.ShaderTerrain = {

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

"dirDiffuse += directionalLightColor[ i ] * diffuse * dirDiffuseWeight;",
"dirSpecular += directionalLightColor[ i ] * specular * dirSpecularWeight * dirDiffuseWeight;",
"totalDiffuseLight += directionalLightColor[ i ] * dirDiffuseWeight;",
"totalSpecularLight += directionalLightColor[ i ] * specular * dirSpecularWeight * dirDiffuseWeight;",

"}",

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

"hemiDiffuse += diffuse * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
"totalDiffuseLight += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",

// specular (sky light)

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

"hemiSpecular += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",
"totalSpecularLight += specular * mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight ) * hemiSpecularWeight * hemiDiffuseWeight;",

"}",

"#endif",

// all lights contribution summation

"vec3 totalDiffuse = vec3( 0.0 );",
"vec3 totalSpecular = vec3( 0.0 );",

"#if MAX_DIR_LIGHTS > 0",

"totalDiffuse += dirDiffuse;",
"totalSpecular += dirSpecular;",

"#endif",

"#if MAX_HEMI_LIGHTS > 0",

"totalDiffuse += hemiDiffuse;",
"totalSpecular += hemiSpecular;",

"#endif",

"#if MAX_POINT_LIGHTS > 0",

"totalDiffuse += pointDiffuse;",
"totalSpecular += pointSpecular;",

"#endif",

//"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * diffuse ) + totalSpecular;",
"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * diffuse + totalSpecular );",
"outgoingLight += diffuseColor.xyz * ( totalDiffuseLight + ambientLightColor + totalSpecularLight );",

THREE.ShaderChunk[ "shadowmap_fragment" ],
THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
THREE.ShaderChunk[ "fog_fragment" ],

" gl_FragColor = vec4( outgoingLight, diffuseColor.a );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects


"}"

].join("\n"),
Expand Down
Loading