WebGLObjects.js 5.6 KB

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