Skip to content

Commit

Permalink
Merge pull request #8232 from bhouston/output_encoding
Browse files Browse the repository at this point in the history
Output encoding
  • Loading branch information
mrdoob committed Feb 27, 2016
2 parents 00fd2ee + 7210aad commit 5062677
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 156 deletions.
53 changes: 21 additions & 32 deletions build/three.js
Original file line number Diff line number Diff line change
Expand Up @@ -29464,38 +29464,6 @@ THREE.WebGLPrograms = function ( renderer, capabilities ) {

}

function getTextureEncodingFromMap( map ) {

if ( ! map ) return false;

var encoding;

if ( map instanceof THREE.Texture ) {

encoding = map.encoding;

} else if ( map instanceof THREE.WebGLRenderTarget ) {

encoding = map.texture.encoding;

} else {

throw new Error( "can not determine texture encoding from map: " + map );

}

// add backwards compatibility for WebGLRenderer.gammaInput parameter, should probably be removed at some point.

if ( encoding === THREE.LinearEncoding && renderer.gammaInput ) {

encoding = THREE.GammaEncoding;

}

return encoding;

}

this.getParameters = function ( material, lights, fog, object ) {

var shaderID = shaderIDs[ material.type ];
Expand All @@ -29517,6 +29485,27 @@ THREE.WebGLPrograms = function ( renderer, capabilities ) {

}

var getTextureEncodingFromMap = function( map ) {
if( ! map ) { // no texture
return false;
}
var encoding;
if( map.encoding !== undefined ) { // standard texture
encoding = map.encoding;
}
else if( map.texture !== undefined ) { // render target pretending to be a texture, get the texture inside it.
encoding = map.texture.encoding;
}
else {
throw new Error( "can not determine texture encoding from map: " + map );
}
// add backwards compatibility for WebGLRenderer.gammaInput parameter, should probably be removed at some point.
if( encoding === THREE.LinearEncoding && renderer.gammaInput ) {
encoding = THREE.GammaEncoding;
}
return encoding;
}

var parameters = {

shaderID: shaderID,
Expand Down
52 changes: 26 additions & 26 deletions build/three.min.js

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions examples/js/ShaderSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,9 @@ THREE.ShaderSkin = {

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

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
"gl_FragColor = linearToOutputTexel( vec4( outgoingLight, diffuseColor.a ) );", // TODO, this should be pre-multiplied to allow for bright highlights on very transparent objects

THREE.ShaderChunk[ "fog_fragment" ],

"}"

Expand Down Expand Up @@ -550,10 +548,10 @@ THREE.ShaderSkin = {

"}",

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

THREE.ShaderChunk[ "fog_fragment" ],

"}"

].join( "\n" ),
Expand Down
9 changes: 6 additions & 3 deletions src/Three.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,12 @@ THREE.TriangleFanDrawMode = 2;

THREE.LinearEncoding = 3000; // No encoding at all.
THREE.sRGBEncoding = 3001;
THREE.RGBEEncoding = 3002; // AKA Radiance
THREE.GammaEncoding = 3007; // uses GAMMA_FACTOR, for backwards compatibility with WebGLRenderer.gammaInput/gammaOutput

// The following Texture Encodings are for RGB-only (no alpha) HDR light emission sources.
// These encodings should not specified as output encodings except in rare situations.
THREE.RGBEEncoding = 3002; // AKA Radiance.
THREE.LogLuvEncoding = 3003;
THREE.RGBM7Encoding = 3004;
THREE.RGBM16Encoding = 3005;
THREE.RGBDEncoding = 3006; // MaxRange is 256
THREE.GammaEncoding = 3007; // uses GAMMA_FACTOR
THREE.RGBDEncoding = 3006; // MaxRange is 256.
6 changes: 6 additions & 0 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3365,6 +3365,12 @@ THREE.WebGLRenderer = function ( parameters ) {

}

this.getCurrentRenderTarget = function() {

return _currentRenderTarget;

}

this.setRenderTarget = function ( renderTarget ) {

_currentRenderTarget = renderTarget;
Expand Down
28 changes: 0 additions & 28 deletions src/renderers/shaders/ShaderChunk/common.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,3 @@ vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 poi
return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;

}

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

}
2 changes: 1 addition & 1 deletion src/renderers/shaders/ShaderChunk/encodings.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ vec4 LinearToLinear( in vec4 value ) {
vec4 GammaToLinear( in vec4 value, in float gammaFactor ) {
return vec4( pow( value.xyz, vec3( gammaFactor ) ), value.w );
}
vec4 LinearTosGamma( in vec4 value, in float gammaFactor ) {
vec4 LinearToGamma( in vec4 value, in float gammaFactor ) {
return vec4( pow( value.xyz, vec3( 1.0 / gammaFactor ) ), value.w );
}

Expand Down
6 changes: 3 additions & 3 deletions src/renderers/shaders/ShaderChunk/fog_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
float fogFactor = smoothstep( fogNear, fogFar, depth );

#endif

outgoingLight = mix( outgoingLight, fogColor, fogFactor );

#endif
gl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );

#endif

This file was deleted.

4 changes: 2 additions & 2 deletions src/renderers/shaders/ShaderLib/linedashed_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ void main() {

outgoingLight = diffuseColor.rgb; // simple shader

#include <fog_fragment>
gl_FragColor = linearToOutputTexel( vec4( outgoingLight, diffuseColor.a ) );

gl_FragColor = vec4( outgoingLight, diffuseColor.a );
#include <fog_fragment>

}
6 changes: 3 additions & 3 deletions src/renderers/shaders/ShaderLib/meshbasic_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ void main() {
vec3 outgoingLight = reflectedLight.indirectDiffuse;

#include <envmap_fragment>
#include <linear_to_gamma_fragment>
#include <fog_fragment>

gl_FragColor = vec4( outgoingLight, diffuseColor.a );
gl_FragColor = linearToOutputTexel( vec4( outgoingLight, diffuseColor.a ) );

#include <fog_fragment>

}
6 changes: 3 additions & 3 deletions src/renderers/shaders/ShaderLib/meshlambert_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ void main() {
vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveLight;

#include <envmap_fragment>
#include <linear_to_gamma_fragment>
#include <fog_fragment>

gl_FragColor = vec4( outgoingLight, diffuseColor.a );
gl_FragColor = linearToOutputTexel( vec4( outgoingLight, diffuseColor.a ) );

#include <fog_fragment>

}
5 changes: 2 additions & 3 deletions src/renderers/shaders/ShaderLib/meshphong_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ void main() {
vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight;

#include <envmap_fragment>
#include <linear_to_gamma_fragment>

#include <fog_fragment>
gl_FragColor = linearToOutputTexel( vec4( outgoingLight, diffuseColor.a ) );

gl_FragColor = vec4( outgoingLight, diffuseColor.a );
#include <fog_fragment>

}
4 changes: 1 addition & 3 deletions src/renderers/shaders/ShaderLib/meshstandard_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ void main() {

vec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveLight;

#include <linear_to_gamma_fragment>
gl_FragColor = linearToOutputTexel( vec4( outgoingLight, diffuseColor.a ) );

#include <fog_fragment>

gl_FragColor = vec4( outgoingLight, diffuseColor.a );

}
4 changes: 2 additions & 2 deletions src/renderers/shaders/ShaderLib/points_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ void main() {

outgoingLight = diffuseColor.rgb;

#include <fog_fragment>
gl_FragColor = linearToOutputTexel( vec4( outgoingLight, diffuseColor.a ) );

gl_FragColor = vec4( outgoingLight, diffuseColor.a );
#include <fog_fragment>

}
43 changes: 23 additions & 20 deletions src/renderers/webgl/WebGLProgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,42 @@ THREE.WebGLProgram = ( function () {
var arrayStructRe = /^([\w\d_]+)\[(\d+)\]\.([\w\d_]+)$/;
var arrayRe = /^([\w\d_]+)\[0\]$/;

function getTexelDecodingFunction( functionName, encoding ) {

var code = "vec4 " + functionName + "( vec4 value ) { return ";
function getEncodingComponents( encoding ) {

switch ( encoding ) {

case THREE.LinearEncoding:
code += "value";
break;
return ['Linear','( value )'];
case THREE.sRGBEncoding:
code += "sRGBToLinear( value )";
break;
return ['sRGB','( value )'];
case THREE.RGBEEncoding:
code += "RGBEToLinear( value )";
break;
return ['RGBE','( value )'];
case THREE.RGBM7Encoding:
code += "RGBMToLinear( value, 7.0 )";
break;
return ['RGBM','( value, 7.0 )'];
case THREE.RGBM16Encoding:
code += "RGBMToLinear( value, 16.0 )";
break;
return ['RGBM','( value, 16.0 )'];
case THREE.RGBDEncoding:
code += "RGBDToLinear( value, 256.0 )";
break;
return ['RGBD','( value, 256.0 )'];
case THREE.GammaEncoding:
code += "GammaToLinear( value, float( GAMMA_FACTOR ) )";
break;
return ['Gamma','( value, float( GAMMA_FACTOR ) )'];
default:
throw new Error( 'unsupported encoding: ' + encoding );

}

code += "; }";
return code;
}

function getTexelDecodingFunction( functionName, encoding ) {

var components = getEncodingComponents( encoding );
return "vec4 " + functionName + "( vec4 value ) { return " + components[0] + "ToLinear" + components[1] + "; }";

}

function getTexelEncodingFunction( functionName, encoding ) {

var components = getEncodingComponents( encoding );
return "vec4 " + functionName + "( vec4 value ) { return LinearTo" + components[0] + components[1] + "; }";

}

Expand Down Expand Up @@ -496,11 +498,12 @@ THREE.WebGLProgram = ( function () {
'uniform mat4 viewMatrix;',
'uniform vec3 cameraPosition;',

( parameters.mapEncoding || parameters.envMapEncoding || parameters.emissiveMapEncoding ) ? THREE.ShaderChunk[ 'encodings' ] : '',
( parameters.outputEncoding || parameters.mapEncoding || parameters.envMapEncoding || parameters.emissiveMapEncoding ) ? THREE.ShaderChunk[ 'encodings' ] : '',

parameters.mapEncoding ? getTexelDecodingFunction( 'mapTexelToLinear', parameters.mapEncoding ) : '',
parameters.envMapEncoding ? getTexelDecodingFunction( 'envMapTexelToLinear', parameters.envMapEncoding ) : '',
parameters.emissiveMapEncoding ? getTexelDecodingFunction( 'emissiveMapTexelToLinear', parameters.emissiveMapEncoding ) : '',
parameters.outputEncoding ? getTexelEncodingFunction( "linearToOutputTexel", parameters.outputEncoding ) : '',

'\n'

Expand Down
Loading

0 comments on commit 5062677

Please sign in to comment.