123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- /**
- * @author alteredq / http://alteredqualia.com/
- * @author mrdoob / http://mrdoob.com/
- */
- THREE.Math = {
- PI2: Math.PI * 2,
- generateUUID: function () {
- // http://www.broofa.com/Tools/Math.uuid.htm
-
- var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
- var uuid = new Array(36);
- var rnd = 0, r;
- return function () {
- for ( var i = 0; i < 36; i ++ ) {
- if ( i == 8 || i == 13 || i == 18 || i == 23 ) {
-
- uuid[ i ] = '-';
-
- } else if ( i == 14 ) {
-
- uuid[ i ] = '4';
-
- } else {
-
- if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
- r = rnd & 0xf;
- rnd = rnd >> 4;
- uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
- }
- }
-
- return uuid.join('');
- };
- }(),
- // Clamp value to range <a, b>
- clamp: function ( x, a, b ) {
- return ( x < a ) ? a : ( ( x > b ) ? b : x );
- },
- // Clamp value to range <a, inf)
- clampBottom: function ( x, a ) {
- return x < a ? a : x;
- },
- // Linear mapping from range <a1, a2> to range <b1, b2>
- mapLinear: function ( x, a1, a2, b1, b2 ) {
- return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
- },
- // http://en.wikipedia.org/wiki/Smoothstep
- smoothstep: function ( x, min, max ) {
- if ( x <= min ) return 0;
- if ( x >= max ) return 1;
- x = ( x - min )/( max - min );
- return x*x*(3 - 2*x);
- },
- smootherstep: function ( x, min, max ) {
- if ( x <= min ) return 0;
- if ( x >= max ) return 1;
- x = ( x - min )/( max - min );
- return x*x*x*(x*(x*6 - 15) + 10);
- },
- // Random float from <0, 1> with 16 bits of randomness
- // (standard Math.random() creates repetitive patterns when applied over larger space)
- random16: function () {
- return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
- },
- // Random integer from <low, high> interval
- randInt: function ( low, high ) {
- return low + Math.floor( Math.random() * ( high - low + 1 ) );
- },
- // Random float from <low, high> interval
- randFloat: function ( low, high ) {
- return low + Math.random() * ( high - low );
- },
- // Random float from <-range/2, range/2> interval
- randFloatSpread: function ( range ) {
- return range * ( 0.5 - Math.random() );
- },
- sign: function ( x ) {
- return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
- },
- degToRad: function() {
- var degreeToRadiansFactor = Math.PI / 180;
- return function ( degrees ) {
- return degrees * degreeToRadiansFactor;
- };
- }(),
- radToDeg: function() {
- var radianToDegreesFactor = 180 / Math.PI;
- return function ( radians ) {
- return radians * radianToDegreesFactor;
- };
- }(),
- isPowerOfTwo: function ( value ) {
- return ( value & ( value - 1 ) ) === 0 && value !== 0;
- }
- };
|