utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. function createElementNS( name ) {
  39. return document.createElementNS( 'http://www.w3.org/1999/xhtml', name );
  40. }
  41. function createCanvasElement() {
  42. const canvas = createElementNS( 'canvas' );
  43. canvas.style.display = 'block';
  44. return canvas;
  45. }
  46. const _cache = {};
  47. function warnOnce( message ) {
  48. if ( message in _cache ) return;
  49. _cache[ message ] = true;
  50. console.warn( message );
  51. }
  52. function probeAsync( gl, sync, interval ) {
  53. return new Promise( function ( resolve, reject ) {
  54. function probe() {
  55. switch ( gl.clientWaitSync( sync, gl.SYNC_FLUSH_COMMANDS_BIT, 0 ) ) {
  56. case gl.WAIT_FAILED:
  57. reject();
  58. break;
  59. case gl.TIMEOUT_EXPIRED:
  60. setTimeout( probe, interval );
  61. break;
  62. default:
  63. resolve();
  64. }
  65. }
  66. setTimeout( probe, interval );
  67. } );
  68. }
  69. function toNormalizedProjectionMatrix( projectionMatrix ) {
  70. const m = projectionMatrix.elements;
  71. // Convert [-1, 1] to [0, 1] projection matrix
  72. m[ 2 ] = 0.5 * m[ 2 ] + 0.5 * m[ 3 ];
  73. m[ 6 ] = 0.5 * m[ 6 ] + 0.5 * m[ 7 ];
  74. m[ 10 ] = 0.5 * m[ 10 ] + 0.5 * m[ 11 ];
  75. m[ 14 ] = 0.5 * m[ 14 ] + 0.5 * m[ 15 ];
  76. }
  77. function toReversedProjectionMatrix( projectionMatrix ) {
  78. const m = projectionMatrix.elements;
  79. const isPerspectiveMatrix = m[ 11 ] === - 1;
  80. // Reverse [0, 1] projection matrix
  81. if ( isPerspectiveMatrix ) {
  82. m[ 10 ] = - m[ 10 ] - 1;
  83. m[ 14 ] = - m[ 14 ];
  84. } else {
  85. m[ 10 ] = - m[ 10 ];
  86. m[ 14 ] = - m[ 14 ] + 1;
  87. }
  88. }
  89. export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS, createCanvasElement, warnOnce, probeAsync, toNormalizedProjectionMatrix, toReversedProjectionMatrix };
粤ICP备19079148号