WebGLObjects.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { BufferAttribute } from '../../core/BufferAttribute';
  5. import { WebGLGeometries } from './WebGLGeometries';
  6. function WebGLObjects( gl, properties, info ) {
  7. var geometries = new WebGLGeometries( gl, properties, info );
  8. //
  9. function update( object ) {
  10. // TODO: Avoid updating twice (when using shadowMap). Maybe add frame counter.
  11. var geometry = geometries.get( object );
  12. if ( object.geometry.isGeometry ) {
  13. geometry.updateFromObject( object );
  14. }
  15. var index = geometry.index;
  16. var attributes = geometry.attributes;
  17. if ( index !== null ) {
  18. updateAttribute( index, gl.ELEMENT_ARRAY_BUFFER );
  19. }
  20. for ( var name in attributes ) {
  21. updateAttribute( attributes[ name ], gl.ARRAY_BUFFER );
  22. }
  23. // morph targets
  24. var morphAttributes = geometry.morphAttributes;
  25. for ( var name in morphAttributes ) {
  26. var array = morphAttributes[ name ];
  27. for ( var i = 0, l = array.length; i < l; i ++ ) {
  28. updateAttribute( array[ i ], gl.ARRAY_BUFFER );
  29. }
  30. }
  31. return geometry;
  32. }
  33. function updateAttribute( attribute, bufferType ) {
  34. var data = ( attribute.isInterleavedBufferAttribute ) ? attribute.data : attribute;
  35. var attributeProperties = properties.get( data );
  36. if ( attributeProperties.__webglBuffer === undefined ) {
  37. createBuffer( attributeProperties, data, bufferType );
  38. } else if ( attributeProperties.version !== data.version ) {
  39. updateBuffer( attributeProperties, data, bufferType );
  40. }
  41. }
  42. function createBuffer( attributeProperties, data, bufferType ) {
  43. attributeProperties.__webglBuffer = gl.createBuffer();
  44. gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
  45. var usage = data.dynamic ? gl.DYNAMIC_DRAW : gl.STATIC_DRAW;
  46. gl.bufferData( bufferType, data.array, usage );
  47. var type = gl.FLOAT;
  48. var array = data.array;
  49. if ( array instanceof Float32Array ) {
  50. type = gl.FLOAT;
  51. } else if ( array instanceof Float64Array ) {
  52. console.warn( "Unsupported data buffer format: Float64Array" );
  53. } else if ( array instanceof Uint16Array ) {
  54. type = gl.UNSIGNED_SHORT;
  55. } else if ( array instanceof Int16Array ) {
  56. type = gl.SHORT;
  57. } else if ( array instanceof Uint32Array ) {
  58. type = gl.UNSIGNED_INT;
  59. } else if ( array instanceof Int32Array ) {
  60. type = gl.INT;
  61. } else if ( array instanceof Int8Array ) {
  62. type = gl.BYTE;
  63. } else if ( array instanceof Uint8Array ) {
  64. type = gl.UNSIGNED_BYTE;
  65. }
  66. attributeProperties.bytesPerElement = array.BYTES_PER_ELEMENT;
  67. attributeProperties.type = type;
  68. attributeProperties.version = data.version;
  69. }
  70. function updateBuffer( attributeProperties, data, bufferType ) {
  71. gl.bindBuffer( bufferType, attributeProperties.__webglBuffer );
  72. if ( data.dynamic === false ) {
  73. gl.bufferData( bufferType, data.array, gl.STATIC_DRAW );
  74. } else if ( data.updateRange.count === - 1 ) {
  75. // Not using update ranges
  76. gl.bufferSubData( bufferType, 0, data.array );
  77. } else if ( data.updateRange.count === 0 ) {
  78. console.error( 'THREE.WebGLObjects.updateBuffer: dynamic THREE.BufferAttribute marked as needsUpdate but updateRange.count is 0, ensure you are using set methods or updating manually.' );
  79. } else {
  80. gl.bufferSubData( bufferType, data.updateRange.offset * data.array.BYTES_PER_ELEMENT,
  81. data.array.subarray( data.updateRange.offset, data.updateRange.offset + data.updateRange.count ) );
  82. data.updateRange.count = 0; // reset range
  83. }
  84. attributeProperties.version = data.version;
  85. }
  86. function getAttributeBuffer( attribute ) {
  87. if ( attribute.isInterleavedBufferAttribute ) {
  88. return properties.get( attribute.data ).__webglBuffer;
  89. }
  90. return properties.get( attribute ).__webglBuffer;
  91. }
  92. function getAttributeProperties( attribute ) {
  93. if ( attribute.isInterleavedBufferAttribute ) {
  94. return properties.get( attribute.data );
  95. }
  96. return properties.get( attribute );
  97. }
  98. function getWireframeAttribute( geometry ) {
  99. var property = properties.get( geometry );
  100. if ( property.wireframe !== undefined ) {
  101. return property.wireframe;
  102. }
  103. var indices = [];
  104. var index = geometry.index;
  105. var attributes = geometry.attributes;
  106. var position = attributes.position;
  107. // console.time( 'wireframe' );
  108. if ( index !== null ) {
  109. var edges = {};
  110. var array = index.array;
  111. for ( var i = 0, l = array.length; i < l; i += 3 ) {
  112. var a = array[ i + 0 ];
  113. var b = array[ i + 1 ];
  114. var c = array[ i + 2 ];
  115. indices.push( a, b, b, c, c, a );
  116. }
  117. } else {
  118. var array = attributes.position.array;
  119. for ( var i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) {
  120. var a = i + 0;
  121. var b = i + 1;
  122. var c = i + 2;
  123. indices.push( a, b, b, c, c, a );
  124. }
  125. }
  126. // console.timeEnd( 'wireframe' );
  127. var TypeArray = position.count > 65535 ? Uint32Array : Uint16Array;
  128. var attribute = new BufferAttribute( new TypeArray( indices ), 1 );
  129. updateAttribute( attribute, gl.ELEMENT_ARRAY_BUFFER );
  130. property.wireframe = attribute;
  131. return attribute;
  132. }
  133. return {
  134. getAttributeBuffer: getAttributeBuffer,
  135. getAttributeProperties: getAttributeProperties,
  136. getWireframeAttribute: getWireframeAttribute,
  137. update: update
  138. };
  139. }
  140. export { WebGLObjects };