WebGPURenderLists.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. function painterSortStable( a, b ) {
  2. if ( a.groupOrder !== b.groupOrder ) {
  3. return a.groupOrder - b.groupOrder;
  4. } else if ( a.renderOrder !== b.renderOrder ) {
  5. return a.renderOrder - b.renderOrder;
  6. } else if ( a.material.id !== b.material.id ) {
  7. return a.material.id - b.material.id;
  8. } else if ( a.z !== b.z ) {
  9. return a.z - b.z;
  10. } else {
  11. return a.id - b.id;
  12. }
  13. }
  14. function reversePainterSortStable( a, b ) {
  15. if ( a.groupOrder !== b.groupOrder ) {
  16. return a.groupOrder - b.groupOrder;
  17. } else if ( a.renderOrder !== b.renderOrder ) {
  18. return a.renderOrder - b.renderOrder;
  19. } else if ( a.z !== b.z ) {
  20. return b.z - a.z;
  21. } else {
  22. return a.id - b.id;
  23. }
  24. }
  25. class WebGPURenderList {
  26. constructor() {
  27. this.renderItems = [];
  28. this.renderItemsIndex = 0;
  29. this.opaque = [];
  30. this.transparent = [];
  31. }
  32. init() {
  33. this.renderItemsIndex = 0;
  34. this.opaque.length = 0;
  35. this.transparent.length = 0;
  36. }
  37. getNextRenderItem( object, geometry, material, groupOrder, z, group ) {
  38. let renderItem = this.renderItems[ this.renderItemsIndex ];
  39. if ( renderItem === undefined ) {
  40. renderItem = {
  41. id: object.id,
  42. object: object,
  43. geometry: geometry,
  44. material: material,
  45. groupOrder: groupOrder,
  46. renderOrder: object.renderOrder,
  47. z: z,
  48. group: group
  49. };
  50. this.renderItems[ this.renderItemsIndex ] = renderItem;
  51. } else {
  52. renderItem.id = object.id;
  53. renderItem.object = object;
  54. renderItem.geometry = geometry;
  55. renderItem.material = material;
  56. renderItem.groupOrder = groupOrder;
  57. renderItem.renderOrder = object.renderOrder;
  58. renderItem.z = z;
  59. renderItem.group = group;
  60. }
  61. this.renderItemsIndex ++;
  62. return renderItem;
  63. }
  64. push( object, geometry, material, groupOrder, z, group ) {
  65. const renderItem = this.getNextRenderItem( object, geometry, material, groupOrder, z, group );
  66. ( material.transparent === true ? this.transparent : this.opaque ).push( renderItem );
  67. }
  68. unshift( object, geometry, material, groupOrder, z, group ) {
  69. const renderItem = this.getNextRenderItem( object, geometry, material, groupOrder, z, group );
  70. ( material.transparent === true ? this.transparent : this.opaque ).unshift( renderItem );
  71. }
  72. sort( customOpaqueSort, customTransparentSort ) {
  73. if ( this.opaque.length > 1 ) this.opaque.sort( customOpaqueSort || painterSortStable );
  74. if ( this.transparent.length > 1 ) this.transparent.sort( customTransparentSort || reversePainterSortStable );
  75. }
  76. finish() {
  77. // Clear references from inactive renderItems in the list
  78. for ( let i = this.renderItemsIndex, il = this.renderItems.length; i < il; i ++ ) {
  79. const renderItem = this.renderItems[ i ];
  80. if ( renderItem.id === null ) break;
  81. renderItem.id = null;
  82. renderItem.object = null;
  83. renderItem.geometry = null;
  84. renderItem.material = null;
  85. renderItem.program = null;
  86. renderItem.group = null;
  87. }
  88. }
  89. }
  90. class WebGPURenderLists {
  91. constructor() {
  92. this.lists = new WeakMap();
  93. }
  94. get( scene, camera ) {
  95. const lists = this.lists;
  96. const cameras = lists.get( scene );
  97. let list;
  98. if ( cameras === undefined ) {
  99. list = new WebGPURenderList();
  100. lists.set( scene, new WeakMap() );
  101. lists.get( scene ).set( camera, list );
  102. } else {
  103. list = cameras.get( camera );
  104. if ( list === undefined ) {
  105. list = new WebGPURenderList();
  106. cameras.set( camera, list );
  107. }
  108. }
  109. return list;
  110. }
  111. dispose() {
  112. this.lists = new WeakMap();
  113. }
  114. }
  115. export default WebGPURenderLists;