WebGLObjects.js 5.7 KB

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