Răsfoiți Sursa

Sync js and jsm.

yellowtailfan 4 ani în urmă
părinte
comite
78436f7277

+ 78 - 4
examples/js/lights/RectAreaLightUniformsLib.js

@@ -16,6 +16,61 @@ console.warn( "THREE.RectAreaLightUniformsLib: As part of the transition to ES6
 // by Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt
 // code: https://github.com/selfshadow/ltc_code/
 
+// toHalf(val) - Convert float32 array value to float16 stored in uint16 array value
+//
+// This function is duplicated from examples/jsm/loaders/RGBELoader.js
+// 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
+* used, eg. in Ogre), with the additional benefit of rounding, inspired
+* by James Tursa?s half-precision code. */
+
+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
+		* half, return signed zero. */
+	if ( e < 103 ) return bits;
+
+	/* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
+	if ( e > 142 ) {
+
+		bits |= 0x7c00;
+		/* 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;
+
+	}
+
+	/* If exponent underflows but not too much, return a denormal */
+	if ( e < 113 ) {
+
+		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;
+
+	}
+
+	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;
+
+}
+
 THREE.RectAreaLightUniformsLib = {
 
 	init: function () {
@@ -28,11 +83,30 @@ THREE.RectAreaLightUniformsLib = {
 
 		// data textures
 
-		var ltc_1 = new THREE.DataTexture( new Float32Array( LTC_MAT_1 ), 64, 64, THREE.RGBAFormat, THREE.FloatType, THREE.UVMapping, THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.LinearFilter, THREE.NearestFilter, 1 );
-		var ltc_2 = new THREE.DataTexture( new Float32Array( LTC_MAT_2 ), 64, 64, THREE.RGBAFormat, THREE.FloatType, THREE.UVMapping, THREE.ClampToEdgeWrapping, THREE.ClampToEdgeWrapping, THREE.LinearFilter, THREE.NearestFilter, 1 );
+		const ltc_float_1 = new Float32Array( LTC_MAT_1 );
+		const ltc_float_2 = new Float32Array( LTC_MAT_2 );
+
+		UniformsLib.LTC_FLOAT_1 = new DataTexture( ltc_float_1, 64, 64, RGBAFormat, FloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
+		UniformsLib.LTC_FLOAT_2 = new DataTexture( ltc_float_2, 64, 64, RGBAFormat, FloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
+
+		const ltc_half_1 = new Uint16Array( LTC_MAT_1.length );
+
+		LTC_MAT_1.forEach( function ( x, index ) {
+
+			ltc_half_1[ index ] = toHalf( x );
+
+		} );
+
+		const ltc_half_2 = new Uint16Array( LTC_MAT_2.length );
+
+		LTC_MAT_2.forEach( function ( x, index ) {
+
+			ltc_half_2[ index ] = toHalf( x );
+
+		} );
 
-		THREE.UniformsLib.LTC_1 = ltc_1;
-		THREE.UniformsLib.LTC_2 = ltc_2;
+		UniformsLib.LTC_HALF_1 = new DataTexture( ltc_half_1, 64, 64, RGBAFormat, HalfFloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
+		UniformsLib.LTC_HALF_2 = new DataTexture( ltc_half_2, 64, 64, RGBAFormat, HalfFloatType, UVMapping, ClampToEdgeWrapping, ClampToEdgeWrapping, LinearFilter, NearestFilter, 1 );
 
 	}
 

+ 0 - 1
examples/jsm/lights/RectAreaLightUniformsLib.js

@@ -83,7 +83,6 @@ function toHalf( val ) {
 
 var RectAreaLightUniformsLib = {
 
-	// renderer should be an instance of THREE.WebGLRenderer
 	init: function () {
 
 		// source: https://github.com/selfshadow/ltc_code/tree/master/fit/results/ltc.js