WebGLBufferRenderer.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. class WebGLBufferRenderer {
  2. constructor( backend ) {
  3. this.gl = backend.gl;
  4. this.extensions = backend.extensions;
  5. this.info = backend.renderer.info;
  6. this.mode = null;
  7. this.index = 0;
  8. this.type = null;
  9. this.object = null;
  10. }
  11. render( start, count ) {
  12. const { gl, mode, object, type, info, index } = this;
  13. if ( index !== 0 ) {
  14. gl.drawElements( mode, count, type, start );
  15. } else {
  16. gl.drawArrays( mode, start, count );
  17. }
  18. info.update( object, count, mode, 1 );
  19. }
  20. renderInstances( start, count, primcount ) {
  21. const { gl, mode, type, index, object, info } = this;
  22. if ( primcount === 0 ) return;
  23. if ( index !== 0 ) {
  24. gl.drawElementsInstanced( mode, count, type, start, primcount );
  25. } else {
  26. gl.drawArraysInstanced( mode, start, count, primcount );
  27. }
  28. info.update( object, count, mode, primcount );
  29. }
  30. renderMultiDraw( starts, counts, drawCount ) {
  31. const { extensions, mode, object, info } = this;
  32. if ( drawCount === 0 ) return;
  33. const extension = extensions.get( 'WEBGL_multi_draw' );
  34. if ( extension === null ) {
  35. for ( let i = 0; i < drawCount; i ++ ) {
  36. this.render( starts[ i ], counts[ i ] );
  37. }
  38. } else {
  39. if ( this.index !== 0 ) {
  40. extension.multiDrawElementsWEBGL( mode, counts, 0, this.type, starts, 0, drawCount );
  41. } else {
  42. extension.multiDrawArraysWEBGL( mode, starts, 0, counts, 0, drawCount );
  43. }
  44. let elementCount = 0;
  45. for ( let i = 0; i < drawCount; i ++ ) {
  46. elementCount += counts[ i ];
  47. }
  48. info.update( object, elementCount, mode, 1 );
  49. }
  50. }
  51. renderMultiDrawInstances( starts, counts, drawCount, primcount ) {
  52. const { extensions, mode, object, info } = this;
  53. if ( drawCount === 0 ) return;
  54. const extension = extensions.get( 'WEBGL_multi_draw' );
  55. if ( extension === null ) {
  56. for ( let i = 0; i < drawCount; i ++ ) {
  57. this.renderInstances( starts[ i ], counts[ i ], primcount[ i ] );
  58. }
  59. } else {
  60. if ( this.index !== 0 ) {
  61. extension.multiDrawElementsInstancedWEBGL( mode, counts, 0, this.type, starts, 0, primcount, 0, drawCount );
  62. } else {
  63. extension.multiDrawArraysInstancedWEBGL( mode, starts, 0, counts, 0, primcount, 0, drawCount );
  64. }
  65. let elementCount = 0;
  66. for ( let i = 0; i < drawCount; i ++ ) {
  67. elementCount += counts[ i ];
  68. }
  69. for ( let i = 0; i < primcount.length; i ++ ) {
  70. info.update( object, elementCount, mode, primcount[ i ] );
  71. }
  72. }
  73. }
  74. //
  75. }
  76. export { WebGLBufferRenderer };