utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. function arrayMin( array ) {
  2. if ( array.length === 0 ) return Infinity;
  3. let min = array[ 0 ];
  4. for ( let i = 1, l = array.length; i < l; ++ i ) {
  5. if ( array[ i ] < min ) min = array[ i ];
  6. }
  7. return min;
  8. }
  9. function arrayMax( array ) {
  10. if ( array.length === 0 ) return - Infinity;
  11. let max = array[ 0 ];
  12. for ( let i = 1, l = array.length; i < l; ++ i ) {
  13. if ( array[ i ] > max ) max = array[ i ];
  14. }
  15. return max;
  16. }
  17. function arrayNeedsUint32( array ) {
  18. // assumes larger values usually on last
  19. for ( let i = array.length - 1; i >= 0; -- i ) {
  20. if ( array[ i ] >= 65535 ) return true; // account for PRIMITIVE_RESTART_FIXED_INDEX, #24565
  21. }
  22. return false;
  23. }
  24. const TYPED_ARRAYS = {
  25. Int8Array: Int8Array,
  26. Uint8Array: Uint8Array,
  27. Uint8ClampedArray: Uint8ClampedArray,
  28. Int16Array: Int16Array,
  29. Uint16Array: Uint16Array,
  30. Int32Array: Int32Array,
  31. Uint32Array: Uint32Array,
  32. Float32Array: Float32Array,
  33. Float64Array: Float64Array
  34. };
  35. function getTypedArray( type, buffer ) {
  36. return new TYPED_ARRAYS[ type ]( buffer );
  37. }
  38. /**
  39. * Returns `true` if the given object is a typed array.
  40. *
  41. * @param {any} array - The object to check.
  42. * @return {boolean} Whether the given object is a typed array.
  43. */
  44. function isTypedArray( array ) {
  45. return ArrayBuffer.isView( array ) && ! ( array instanceof DataView );
  46. }
  47. function createElementNS( name ) {
  48. return document.createElementNS( 'http://www.w3.org/1999/xhtml', name );
  49. }
  50. function createCanvasElement() {
  51. const canvas = createElementNS( 'canvas' );
  52. canvas.style.display = 'block';
  53. return canvas;
  54. }
  55. const _cache = {};
  56. let _setConsoleFunction = null;
  57. function setConsoleFunction( fn ) {
  58. _setConsoleFunction = fn;
  59. }
  60. function getConsoleFunction() {
  61. return _setConsoleFunction;
  62. }
  63. function log( ...params ) {
  64. const message = 'THREE.' + params.shift();
  65. if ( _setConsoleFunction ) {
  66. _setConsoleFunction( 'log', message, ...params );
  67. } else {
  68. console.log( message, ...params );
  69. }
  70. }
  71. function warn( ...params ) {
  72. const message = 'THREE.' + params.shift();
  73. if ( _setConsoleFunction ) {
  74. _setConsoleFunction( 'warn', message, ...params );
  75. } else {
  76. console.warn( message, ...params );
  77. }
  78. }
  79. function error( ...params ) {
  80. const message = 'THREE.' + params.shift();
  81. if ( _setConsoleFunction ) {
  82. _setConsoleFunction( 'error', message, ...params );
  83. } else {
  84. console.error( message, ...params );
  85. }
  86. }
  87. function warnOnce( ...params ) {
  88. const message = params.join( ' ' );
  89. if ( message in _cache ) return;
  90. _cache[ message ] = true;
  91. warn( ...params );
  92. }
  93. function probeAsync( gl, sync, interval ) {
  94. return new Promise( function ( resolve, reject ) {
  95. function probe() {
  96. switch ( gl.clientWaitSync( sync, gl.SYNC_FLUSH_COMMANDS_BIT, 0 ) ) {
  97. case gl.WAIT_FAILED:
  98. reject();
  99. break;
  100. case gl.TIMEOUT_EXPIRED:
  101. setTimeout( probe, interval );
  102. break;
  103. default:
  104. resolve();
  105. }
  106. }
  107. setTimeout( probe, interval );
  108. } );
  109. }
  110. function toNormalizedProjectionMatrix( projectionMatrix ) {
  111. const m = projectionMatrix.elements;
  112. // Convert [-1, 1] to [0, 1] projection matrix
  113. m[ 2 ] = 0.5 * m[ 2 ] + 0.5 * m[ 3 ];
  114. m[ 6 ] = 0.5 * m[ 6 ] + 0.5 * m[ 7 ];
  115. m[ 10 ] = 0.5 * m[ 10 ] + 0.5 * m[ 11 ];
  116. m[ 14 ] = 0.5 * m[ 14 ] + 0.5 * m[ 15 ];
  117. }
  118. function toReversedProjectionMatrix( projectionMatrix ) {
  119. const m = projectionMatrix.elements;
  120. const isPerspectiveMatrix = m[ 11 ] === - 1;
  121. // Reverse [0, 1] projection matrix
  122. if ( isPerspectiveMatrix ) {
  123. m[ 10 ] = - m[ 10 ] - 1;
  124. m[ 14 ] = - m[ 14 ];
  125. } else {
  126. m[ 10 ] = - m[ 10 ];
  127. m[ 14 ] = - m[ 14 ] + 1;
  128. }
  129. }
  130. export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS, createCanvasElement, setConsoleFunction, getConsoleFunction, log, warn, error, warnOnce, probeAsync, toNormalizedProjectionMatrix, toReversedProjectionMatrix, isTypedArray };
粤ICP备19079148号