WebGLBufferRenderer.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. function WebGLBufferRenderer( gl, extensions, info ) {
  2. let mode;
  3. function setMode( value ) {
  4. mode = value;
  5. }
  6. function render( start, count ) {
  7. gl.drawArrays( mode, start, count );
  8. info.update( count, mode, 1 );
  9. }
  10. function renderInstances( start, count, primcount ) {
  11. if ( primcount === 0 ) return;
  12. gl.drawArraysInstanced( mode, start, count, primcount );
  13. info.update( count, mode, primcount );
  14. }
  15. function renderMultiDraw( starts, counts, drawCount ) {
  16. if ( drawCount === 0 ) return;
  17. const extension = extensions.get( 'WEBGL_multi_draw' );
  18. if ( extension === null ) {
  19. for ( let i = 0; i < drawCount; i ++ ) {
  20. this.render( starts[ i ], counts[ i ] );
  21. }
  22. } else {
  23. extension.multiDrawArraysWEBGL( mode, starts, 0, counts, 0, drawCount );
  24. let elementCount = 0;
  25. for ( let i = 0; i < drawCount; i ++ ) {
  26. elementCount += counts[ i ];
  27. }
  28. info.update( elementCount, mode, 1 );
  29. }
  30. }
  31. function renderMultiDrawInstances( starts, counts, drawCount, primcount ) {
  32. if ( drawCount === 0 ) return;
  33. const extension = extensions.get( 'WEBGL_multi_draw' );
  34. if ( extension === null ) {
  35. for ( let i = 0; i < starts.length; i ++ ) {
  36. renderInstances( starts[ i ], counts[ i ], primcount[ i ] );
  37. }
  38. } else {
  39. extension.multiDrawArraysInstancedWEBGL( mode, starts, 0, counts, 0, primcount, 0, drawCount );
  40. let elementCount = 0;
  41. for ( let i = 0; i < drawCount; i ++ ) {
  42. elementCount += counts[ i ];
  43. }
  44. for ( let i = 0; i < primcount.length; i ++ ) {
  45. info.update( elementCount, mode, primcount[ i ] );
  46. }
  47. }
  48. }
  49. //
  50. this.setMode = setMode;
  51. this.render = render;
  52. this.renderInstances = renderInstances;
  53. this.renderMultiDraw = renderMultiDraw;
  54. this.renderMultiDrawInstances = renderMultiDrawInstances;
  55. }
  56. export { WebGLBufferRenderer };