EXRLoader: add support HalfFloat texture from Float image

上级 7feede6b
...@@ -117,18 +117,19 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade ...@@ -117,18 +117,19 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade
const logBase = Math.pow( 2.7182818, 2.2 ); const logBase = Math.pow( 2.7182818, 2.2 );
var tmpDataView = new DataView( new ArrayBuffer( 8 ) );
function frexp( value ) { function frexp( value ) {
if ( value === 0 ) return [ value, 0 ]; if ( value === 0 ) return [ value, 0 ];
var data = new DataView( new ArrayBuffer( 8 ) ); tmpDataView.setFloat64( 0, value );
data.setFloat64( 0, value );
var bits = ( data.getUint32( 0 ) >>> 20 ) & 0x7FF; var bits = ( tmpDataView.getUint32( 0 ) >>> 20 ) & 0x7FF;
if ( bits === 0 ) { // denormal if ( bits === 0 ) { // denormal
data.setFloat64( 0, value * Math.pow( 2, 64 ) ); // exp + 64 tmpDataView.setFloat64( 0, value * Math.pow( 2, 64 ) ); // exp + 64
bits = ( ( data.getUint32( 0 ) >>> 20 ) & 0x7FF ) - 64; bits = ( ( tmpDataView.getUint32( 0 ) >>> 20 ) & 0x7FF ) - 64;
} }
var exponent = bits - 1022; var exponent = bits - 1022;
...@@ -1676,6 +1677,12 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade ...@@ -1676,6 +1677,12 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade
} }
function decodeFloat32( dataView, offset ) {
return encodeFloat16( parseFloat32( dataView, offset ) );
}
// https://stackoverflow.com/questions/5678432/decompressing-half-precision-floats-in-javascript // https://stackoverflow.com/questions/5678432/decompressing-half-precision-floats-in-javascript
function decodeFloat16( binary ) { function decodeFloat16( binary ) {
...@@ -1694,60 +1701,54 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade ...@@ -1694,60 +1701,54 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade
} }
var encodeFloat16 = ( function () { // http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
function encodeFloat16( val ) {
// Source: http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
var floatView = new Float32Array( 1 );
var int32View = new Int32Array( floatView.buffer );
/* This method is faster than the OpenEXR implementation (very often /* This method is faster than the OpenEXR implementation (very often
* used, eg. in Ogre), with the additional benefit of rounding, inspired * used, eg. in Ogre), with the additional benefit of rounding, inspired
* by James Tursa?s half-precision code. */ * by James Tursa?s half-precision code.
return function toHalf( val ) { */
floatView[ 0 ] = val;
var x = int32View[ 0 ];
var bits = ( x >> 16 ) & 0x8000; /* Get the sign */
var m = ( x >> 12 ) & 0x07ff; /* Keep one extra bit for rounding */
var e = ( x >> 23 ) & 0xff; /* Using int is faster here */
/* If zero, or denormal, or exponent underflows too much for a denormal tmpDataView.setFloat32( 0, val );
* half, return signed zero. */ var x = tmpDataView.getInt32( 0 );
if ( e < 103 ) return bits;
/* If NaN, return NaN. If Inf or exponent overflow, return Inf. */ var bits = ( x >> 16 ) & 0x8000; /* Get the sign */
if ( e > 142 ) { var m = ( x >> 12 ) & 0x07ff; /* Keep one extra bit for rounding */
var e = ( x >> 23 ) & 0xff; /* Using int is faster here */
bits |= 0x7c00; /* If zero, or denormal, or exponent underflows too much for a denormal
/* If exponent was 0xff and one mantissa bit was set, it means NaN, * half, return signed zero. */
* not Inf, so make sure we set one mantissa bit too. */ if ( e < 103 ) return bits;
bits |= ( ( e == 255 ) ? 0 : 1 ) && ( x & 0x007fffff );
return bits;
} /* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
if ( e > 142 ) {
/* If exponent underflows but not too much, return a denormal */ bits |= 0x7c00;
if ( e < 113 ) { /* If exponent was 0xff and one mantissa bit was set, it means NaN,
* not Inf, so make sure we set one mantissa bit too. */
bits |= ( ( e == 255 ) ? 0 : 1 ) && ( x & 0x007fffff );
return bits;
m |= 0x0800; }
/* Extra rounding may overflow and set mantissa to 0 and exponent
* to 1, which is OK. */
bits |= ( m >> ( 114 - e ) ) + ( ( m >> ( 113 - e ) ) & 1 );
return bits;
} /* If exponent underflows but not too much, return a denormal */
if ( e < 113 ) {
bits |= ( ( e - 112 ) << 10 ) | ( m >> 1 ); m |= 0x0800;
/* Extra rounding. An overflow will set mantissa to 0 and increment /* Extra rounding may overflow and set mantissa to 0 and exponent
* the exponent, which is OK. */ * to 1, which is OK. */
bits += m & 1; bits |= ( m >> ( 114 - e ) ) + ( ( m >> ( 113 - e ) ) & 1 );
return bits; return bits;
}; }
bits |= ( ( e - 112 ) << 10 ) | ( m >> 1 );
/* Extra rounding. An overflow will set mantissa to 0 and increment
* the exponent, which is OK. */
bits += m & 1;
return bits;
} )(); }
function parseUint16( dataView, offset ) { function parseUint16( dataView, offset ) {
...@@ -2046,7 +2047,8 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade ...@@ -2046,7 +2047,8 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade
case THREE.HalfFloatType: case THREE.HalfFloatType:
throw 'EXRLoader.parse: unsupported HalfFloatType texture for FloatType image file.'; getValue = decodeFloat32;
size_t = FLOAT32_SIZE;
} }
......
...@@ -132,18 +132,19 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype ...@@ -132,18 +132,19 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype
const logBase = Math.pow( 2.7182818, 2.2 ); const logBase = Math.pow( 2.7182818, 2.2 );
var tmpDataView = new DataView( new ArrayBuffer( 8 ) );
function frexp( value ) { function frexp( value ) {
if ( value === 0 ) return [ value, 0 ]; if ( value === 0 ) return [ value, 0 ];
var data = new DataView( new ArrayBuffer( 8 ) ); tmpDataView.setFloat64( 0, value );
data.setFloat64( 0, value );
var bits = ( data.getUint32( 0 ) >>> 20 ) & 0x7FF; var bits = ( tmpDataView.getUint32( 0 ) >>> 20 ) & 0x7FF;
if ( bits === 0 ) { // denormal if ( bits === 0 ) { // denormal
data.setFloat64( 0, value * Math.pow( 2, 64 ) ); // exp + 64 tmpDataView.setFloat64( 0, value * Math.pow( 2, 64 ) ); // exp + 64
bits = ( ( data.getUint32( 0 ) >>> 20 ) & 0x7FF ) - 64; bits = ( ( tmpDataView.getUint32( 0 ) >>> 20 ) & 0x7FF ) - 64;
} }
var exponent = bits - 1022; var exponent = bits - 1022;
...@@ -1691,6 +1692,12 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype ...@@ -1691,6 +1692,12 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype
} }
function decodeFloat32( dataView, offset ) {
return encodeFloat16( parseFloat32( dataView, offset ) );
}
// https://stackoverflow.com/questions/5678432/decompressing-half-precision-floats-in-javascript // https://stackoverflow.com/questions/5678432/decompressing-half-precision-floats-in-javascript
function decodeFloat16( binary ) { function decodeFloat16( binary ) {
...@@ -1709,60 +1716,54 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype ...@@ -1709,60 +1716,54 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype
} }
var encodeFloat16 = ( function () { // http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
function encodeFloat16( val ) {
// Source: http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
var floatView = new Float32Array( 1 );
var int32View = new Int32Array( floatView.buffer );
/* This method is faster than the OpenEXR implementation (very often /* This method is faster than the OpenEXR implementation (very often
* used, eg. in Ogre), with the additional benefit of rounding, inspired * used, eg. in Ogre), with the additional benefit of rounding, inspired
* by James Tursa?s half-precision code. */ * by James Tursa?s half-precision code.
return function toHalf( val ) { */
floatView[ 0 ] = val;
var x = int32View[ 0 ];
var bits = ( x >> 16 ) & 0x8000; /* Get the sign */
var m = ( x >> 12 ) & 0x07ff; /* Keep one extra bit for rounding */
var e = ( x >> 23 ) & 0xff; /* Using int is faster here */
/* If zero, or denormal, or exponent underflows too much for a denormal tmpDataView.setFloat32( 0, val );
* half, return signed zero. */ var x = tmpDataView.getInt32( 0 );
if ( e < 103 ) return bits;
/* If NaN, return NaN. If Inf or exponent overflow, return Inf. */ var bits = ( x >> 16 ) & 0x8000; /* Get the sign */
if ( e > 142 ) { var m = ( x >> 12 ) & 0x07ff; /* Keep one extra bit for rounding */
var e = ( x >> 23 ) & 0xff; /* Using int is faster here */
bits |= 0x7c00; /* If zero, or denormal, or exponent underflows too much for a denormal
/* If exponent was 0xff and one mantissa bit was set, it means NaN, * half, return signed zero. */
* not Inf, so make sure we set one mantissa bit too. */ if ( e < 103 ) return bits;
bits |= ( ( e == 255 ) ? 0 : 1 ) && ( x & 0x007fffff );
return bits;
} /* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
if ( e > 142 ) {
/* If exponent underflows but not too much, return a denormal */ bits |= 0x7c00;
if ( e < 113 ) { /* If exponent was 0xff and one mantissa bit was set, it means NaN,
* not Inf, so make sure we set one mantissa bit too. */
bits |= ( ( e == 255 ) ? 0 : 1 ) && ( x & 0x007fffff );
return bits;
m |= 0x0800; }
/* Extra rounding may overflow and set mantissa to 0 and exponent
* to 1, which is OK. */
bits |= ( m >> ( 114 - e ) ) + ( ( m >> ( 113 - e ) ) & 1 );
return bits;
} /* If exponent underflows but not too much, return a denormal */
if ( e < 113 ) {
bits |= ( ( e - 112 ) << 10 ) | ( m >> 1 ); m |= 0x0800;
/* Extra rounding. An overflow will set mantissa to 0 and increment /* Extra rounding may overflow and set mantissa to 0 and exponent
* the exponent, which is OK. */ * to 1, which is OK. */
bits += m & 1; bits |= ( m >> ( 114 - e ) ) + ( ( m >> ( 113 - e ) ) & 1 );
return bits; return bits;
}; }
bits |= ( ( e - 112 ) << 10 ) | ( m >> 1 );
/* Extra rounding. An overflow will set mantissa to 0 and increment
* the exponent, which is OK. */
bits += m & 1;
return bits;
} )(); }
function parseUint16( dataView, offset ) { function parseUint16( dataView, offset ) {
...@@ -2061,7 +2062,8 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype ...@@ -2061,7 +2062,8 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype
case HalfFloatType: case HalfFloatType:
throw 'EXRLoader.parse: unsupported HalfFloatType texture for FloatType image file.'; getValue = decodeFloat32;
size_t = FLOAT32_SIZE;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册