EXRLoader: add support HalfFloat texture from Float image

上级 7feede6b
......@@ -117,18 +117,19 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade
const logBase = Math.pow( 2.7182818, 2.2 );
var tmpDataView = new DataView( new ArrayBuffer( 8 ) );
function frexp( value ) {
if ( value === 0 ) return [ value, 0 ];
var data = new DataView( new ArrayBuffer( 8 ) );
data.setFloat64( 0, value );
tmpDataView.setFloat64( 0, value );
var bits = ( data.getUint32( 0 ) >>> 20 ) & 0x7FF;
var bits = ( tmpDataView.getUint32( 0 ) >>> 20 ) & 0x7FF;
if ( bits === 0 ) { // denormal
data.setFloat64( 0, value * Math.pow( 2, 64 ) ); // exp + 64
bits = ( ( data.getUint32( 0 ) >>> 20 ) & 0x7FF ) - 64;
tmpDataView.setFloat64( 0, value * Math.pow( 2, 64 ) ); // exp + 64
bits = ( ( tmpDataView.getUint32( 0 ) >>> 20 ) & 0x7FF ) - 64;
}
var exponent = bits - 1022;
......@@ -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
function decodeFloat16( binary ) {
......@@ -1694,20 +1701,16 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade
}
var encodeFloat16 = ( function () {
// 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 );
// http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
function encodeFloat16( val ) {
/* This method is faster than the OpenEXR implementation (very often
* used, eg. in Ogre), with the additional benefit of rounding, inspired
* by James Tursa?s half-precision code. */
return function toHalf( val ) {
* by James Tursa?s half-precision code.
*/
floatView[ 0 ] = val;
var x = int32View[ 0 ];
tmpDataView.setFloat32( 0, val );
var x = tmpDataView.getInt32( 0 );
var bits = ( x >> 16 ) & 0x8000; /* Get the sign */
var m = ( x >> 12 ) & 0x07ff; /* Keep one extra bit for rounding */
......@@ -1745,9 +1748,7 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade
bits += m & 1;
return bits;
};
} )();
}
function parseUint16( dataView, offset ) {
......@@ -2046,7 +2047,8 @@ THREE.EXRLoader.prototype = Object.assign( Object.create( THREE.DataTextureLoade
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
const logBase = Math.pow( 2.7182818, 2.2 );
var tmpDataView = new DataView( new ArrayBuffer( 8 ) );
function frexp( value ) {
if ( value === 0 ) return [ value, 0 ];
var data = new DataView( new ArrayBuffer( 8 ) );
data.setFloat64( 0, value );
tmpDataView.setFloat64( 0, value );
var bits = ( data.getUint32( 0 ) >>> 20 ) & 0x7FF;
var bits = ( tmpDataView.getUint32( 0 ) >>> 20 ) & 0x7FF;
if ( bits === 0 ) { // denormal
data.setFloat64( 0, value * Math.pow( 2, 64 ) ); // exp + 64
bits = ( ( data.getUint32( 0 ) >>> 20 ) & 0x7FF ) - 64;
tmpDataView.setFloat64( 0, value * Math.pow( 2, 64 ) ); // exp + 64
bits = ( ( tmpDataView.getUint32( 0 ) >>> 20 ) & 0x7FF ) - 64;
}
var exponent = bits - 1022;
......@@ -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
function decodeFloat16( binary ) {
......@@ -1709,20 +1716,16 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype
}
var encodeFloat16 = ( function () {
// 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 );
// http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410
function encodeFloat16( val ) {
/* This method is faster than the OpenEXR implementation (very often
* used, eg. in Ogre), with the additional benefit of rounding, inspired
* by James Tursa?s half-precision code. */
return function toHalf( val ) {
* by James Tursa?s half-precision code.
*/
floatView[ 0 ] = val;
var x = int32View[ 0 ];
tmpDataView.setFloat32( 0, val );
var x = tmpDataView.getInt32( 0 );
var bits = ( x >> 16 ) & 0x8000; /* Get the sign */
var m = ( x >> 12 ) & 0x07ff; /* Keep one extra bit for rounding */
......@@ -1760,9 +1763,7 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype
bits += m & 1;
return bits;
};
} )();
}
function parseUint16( dataView, offset ) {
......@@ -2061,7 +2062,8 @@ EXRLoader.prototype = Object.assign( Object.create( DataTextureLoader.prototype
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.
先完成此消息的编辑!
想要评论请 注册