WebGLObjects.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.WebGLObjects = function ( gl, info ) {
  5. var objects = {};
  6. var objectsImmediate = [];
  7. // stores attributes by uuid in order to decouple buffers from attribute objects
  8. var objectWebglProperties = {};
  9. var morphInfluences = new Float32Array( 8 );
  10. var geometries = new THREE.WebGLGeometries( gl, info );
  11. //
  12. function onObjectRemoved( event ) {
  13. var object = event.target;
  14. object.traverse( function ( child ) {
  15. child.removeEventListener( 'remove', onObjectRemoved );
  16. removeObject( child );
  17. } );
  18. }
  19. function removeObject( object ) {
  20. if ( object instanceof THREE.Mesh ||
  21. object instanceof THREE.PointCloud ||
  22. object instanceof THREE.Line ) {
  23. delete objects[ object.id ];
  24. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  25. removeInstances( objectsImmediate, object );
  26. }
  27. delete objectWebglProperties[object.uuid].__webglInit;
  28. delete object._modelViewMatrix;
  29. delete object._normalMatrix;
  30. delete objectWebglProperties[object.uuid].__webglActive;
  31. }
  32. function removeInstances( objlist, object ) {
  33. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  34. if ( objlist[ o ].object === object ) {
  35. objlist.splice( o, 1 );
  36. }
  37. }
  38. }
  39. //
  40. this.objects = objects;
  41. this.objectsImmediate = objectsImmediate;
  42. this.geometries = geometries;
  43. this.init = function ( object ) {
  44. if ( ! objectWebglProperties[object.uuid] ) {
  45. objectWebglProperties[object.uuid] = {};
  46. }
  47. if ( objectWebglProperties[object.uuid].__webglInit === undefined ) {
  48. objectWebglProperties[object.uuid].__webglInit = true;
  49. object._modelViewMatrix = new THREE.Matrix4();
  50. object._normalMatrix = new THREE.Matrix3();
  51. object.addEventListener( 'removed', onObjectRemoved );
  52. }
  53. if ( objectWebglProperties[object.uuid].__webglActive === undefined ) {
  54. objectWebglProperties[object.uuid].__webglActive = true;
  55. if ( object instanceof THREE.Mesh || object instanceof THREE.Line || object instanceof THREE.PointCloud ) {
  56. objects[ object.id ] = {
  57. id: object.id,
  58. object: object,
  59. z: 0
  60. };
  61. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  62. objectsImmediate.push( {
  63. id: null,
  64. object: object,
  65. opaque: null,
  66. transparent: null,
  67. z: 0
  68. } );
  69. }
  70. }
  71. };
  72. function numericalSort ( a, b ) {
  73. return b[ 0 ] - a[ 0 ];
  74. }
  75. function updateObject( object ) {
  76. var geometry = geometries.get( object );
  77. if ( object.geometry.dynamic === true ) {
  78. geometry.updateFromObject( object );
  79. }
  80. // morph targets
  81. if ( object.morphTargetInfluences !== undefined ) {
  82. var activeInfluences = [];
  83. var morphTargetInfluences = object.morphTargetInfluences;
  84. for ( var i = 0, l = morphTargetInfluences.length; i < l; i ++ ) {
  85. var influence = morphTargetInfluences[ i ];
  86. activeInfluences.push( [ influence, i ] );
  87. }
  88. activeInfluences.sort( numericalSort );
  89. if ( activeInfluences.length > 8 ) {
  90. activeInfluences.length = 8;
  91. }
  92. for ( var i = 0, l = activeInfluences.length; i < l; i ++ ) {
  93. morphInfluences[ i ] = activeInfluences[ i ][ 0 ];
  94. var attribute = geometry.morphAttributes[ activeInfluences[ i ][ 1 ] ];
  95. geometry.addAttribute( 'morphTarget' + i, attribute );
  96. }
  97. var material = object.material;
  98. if ( material.program !== undefined ) {
  99. var uniforms = material.program.getUniforms();
  100. if ( uniforms.morphTargetInfluences !== null ) {
  101. gl.uniform1fv( uniforms.morphTargetInfluences, morphInfluences );
  102. }
  103. } else {
  104. console.warn( 'TOFIX: material.program is undefined' );
  105. }
  106. }
  107. //
  108. var attributes = geometry.attributes;
  109. for ( var name in attributes ) {
  110. var attribute = attributes[ name ];
  111. if ( ! attribute.uuid ) {
  112. attribute.uuid = THREE.Math.generateUUID();
  113. }
  114. if ( ! objectWebglProperties[attribute.uuid] ) {
  115. objectWebglProperties[attribute.uuid] = {};
  116. }
  117. var bufferType = ( name === 'index' ) ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
  118. var data = ( attribute instanceof THREE.InterleavedBufferAttribute ) ? attribute.data : attribute;
  119. if ( objectWebglProperties[attribute.uuid].__webglBuffer === undefined ) {
  120. objectWebglProperties[attribute.uuid].__webglBuffer = gl.createBuffer();
  121. gl.bindBuffer( bufferType, objectWebglProperties[attribute.uuid].__webglBuffer );
  122. var usage = gl.STATIC_DRAW;
  123. if ( data instanceof THREE.DynamicBufferAttribute
  124. || ( data instanceof THREE.InstancedBufferAttribute && data.dynamic === true )
  125. || ( data instanceof THREE.InterleavedBuffer && data.dynamic === true ) ) {
  126. usage = gl.DYNAMIC_DRAW;
  127. }
  128. gl.bufferData( bufferType, data.array, usage );
  129. data.needsUpdate = false;
  130. } else if ( data.needsUpdate === true ) {
  131. gl.bindBuffer( bufferType, objectWebglProperties[attribute.uuid].__webglBuffer );
  132. if ( data.updateRange === undefined || data.updateRange.count === -1 ) { // Not using update ranges
  133. gl.bufferSubData( bufferType, 0, data.array );
  134. } else if ( data.updateRange.count === 0 ) {
  135. console.error( 'THREE.WebGLRenderer.updateObject: using updateRange for THREE.DynamicBufferAttribute and marked as needsUpdate but count is 0, ensure you are using set methods or updating manually.' );
  136. } else {
  137. gl.bufferSubData( bufferType, data.updateRange.offset * data.array.BYTES_PER_ELEMENT,
  138. data.array.subarray( data.updateRange.offset, data.updateRange.offset + data.updateRange.count ) );
  139. data.updateRange.count = 0; // reset range
  140. }
  141. data.needsUpdate = false;
  142. }
  143. }
  144. };
  145. // returns the webgl buffer for a specified attribute
  146. this.getAttributeBuffer = function (attribute) {
  147. return objectWebglProperties[attribute.uuid] ? objectWebglProperties[attribute.uuid].__webglBuffer : undefined;
  148. }
  149. this.update = function ( renderList ) {
  150. for ( var i = 0, ul = renderList.length; i < ul; i++ ) {
  151. var object = renderList[i].object;
  152. if ( object.material.visible !== false ) {
  153. updateObject( object );
  154. }
  155. }
  156. };
  157. };