WebGLGeometries.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { Uint16BufferAttribute, Uint32BufferAttribute } from '../../core/BufferAttribute.js';
  2. import { arrayMax } from '../../utils.js';
  3. function WebGLGeometries( gl, attributes, info, bindingStates ) {
  4. const geometries = {};
  5. const wireframeAttributes = new WeakMap();
  6. function onGeometryDispose( event ) {
  7. const geometry = event.target;
  8. if ( geometry.index !== null ) {
  9. attributes.remove( geometry.index );
  10. }
  11. for ( const name in geometry.attributes ) {
  12. attributes.remove( geometry.attributes[ name ] );
  13. }
  14. geometry.removeEventListener( 'dispose', onGeometryDispose );
  15. delete geometries[ geometry.id ];
  16. const attribute = wireframeAttributes.get( geometry );
  17. if ( attribute ) {
  18. attributes.remove( attribute );
  19. wireframeAttributes.delete( geometry );
  20. }
  21. bindingStates.releaseStatesOfGeometry( geometry );
  22. if ( geometry.isInstancedBufferGeometry === true ) {
  23. delete geometry._maxInstanceCount;
  24. }
  25. //
  26. info.memory.geometries --;
  27. }
  28. function get( object, geometry ) {
  29. if ( geometries[ geometry.id ] === true ) return geometry;
  30. geometry.addEventListener( 'dispose', onGeometryDispose );
  31. geometries[ geometry.id ] = true;
  32. info.memory.geometries ++;
  33. return geometry;
  34. }
  35. function update( geometry ) {
  36. const geometryAttributes = geometry.attributes;
  37. // Updating index buffer in VAO now. See WebGLBindingStates.
  38. for ( const name in geometryAttributes ) {
  39. attributes.update( geometryAttributes[ name ], gl.ARRAY_BUFFER );
  40. }
  41. // morph targets
  42. const morphAttributes = geometry.morphAttributes;
  43. for ( const name in morphAttributes ) {
  44. const array = morphAttributes[ name ];
  45. for ( let i = 0, l = array.length; i < l; i ++ ) {
  46. attributes.update( array[ i ], gl.ARRAY_BUFFER );
  47. }
  48. }
  49. }
  50. function updateWireframeAttribute( geometry ) {
  51. const indices = [];
  52. const geometryIndex = geometry.index;
  53. const geometryPosition = geometry.attributes.position;
  54. let version = 0;
  55. if ( geometryIndex !== null ) {
  56. const array = geometryIndex.array;
  57. version = geometryIndex.version;
  58. for ( let i = 0, l = array.length; i < l; i += 3 ) {
  59. const a = array[ i + 0 ];
  60. const b = array[ i + 1 ];
  61. const c = array[ i + 2 ];
  62. indices.push( a, b, b, c, c, a );
  63. }
  64. } else {
  65. const array = geometryPosition.array;
  66. version = geometryPosition.version;
  67. for ( let i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {
  68. const a = i + 0;
  69. const b = i + 1;
  70. const c = i + 2;
  71. indices.push( a, b, b, c, c, a );
  72. }
  73. }
  74. const attribute = new ( arrayMax( indices ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
  75. attribute.version = version;
  76. // Updating index buffer in VAO now. See WebGLBindingStates
  77. //
  78. const previousAttribute = wireframeAttributes.get( geometry );
  79. if ( previousAttribute ) attributes.remove( previousAttribute );
  80. //
  81. wireframeAttributes.set( geometry, attribute );
  82. }
  83. function getWireframeAttribute( geometry ) {
  84. const currentAttribute = wireframeAttributes.get( geometry );
  85. if ( currentAttribute ) {
  86. const geometryIndex = geometry.index;
  87. if ( geometryIndex !== null ) {
  88. // if the attribute is obsolete, create a new one
  89. if ( currentAttribute.version < geometryIndex.version ) {
  90. updateWireframeAttribute( geometry );
  91. }
  92. }
  93. } else {
  94. updateWireframeAttribute( geometry );
  95. }
  96. return wireframeAttributes.get( geometry );
  97. }
  98. return {
  99. get: get,
  100. update: update,
  101. getWireframeAttribute: getWireframeAttribute
  102. };
  103. }
  104. export { WebGLGeometries };
粤ICP备19079148号