RenderObject.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import ClippingContext from './ClippingContext.js';
  2. let id = 0;
  3. function getKeys( obj ) {
  4. const keys = Object.keys( obj );
  5. let proto = Object.getPrototypeOf( obj );
  6. while ( proto ) {
  7. const descriptors = Object.getOwnPropertyDescriptors( proto );
  8. for ( const key in descriptors ) {
  9. if ( descriptors[ key ] !== undefined ) {
  10. const descriptor = descriptors[ key ];
  11. if ( descriptor && typeof descriptor.get === 'function' ) {
  12. keys.push( key );
  13. }
  14. }
  15. }
  16. proto = Object.getPrototypeOf( proto );
  17. }
  18. return keys;
  19. }
  20. export default class RenderObject {
  21. constructor( nodes, geometries, renderer, object, material, scene, camera, lightsNode, renderContext ) {
  22. this._nodes = nodes;
  23. this._geometries = geometries;
  24. this.id = id ++;
  25. this.renderer = renderer;
  26. this.object = object;
  27. this.material = material;
  28. this.scene = scene;
  29. this.camera = camera;
  30. this.lightsNode = lightsNode;
  31. this.context = renderContext;
  32. this.geometry = object.geometry;
  33. this.version = material.version;
  34. this.drawRange = null;
  35. this.attributes = null;
  36. this.pipeline = null;
  37. this.vertexBuffers = null;
  38. this.updateClipping( renderContext.clippingContext );
  39. this.clippingContextVersion = this.clippingContext.version;
  40. this.initialNodesCacheKey = this.getNodesCacheKey();
  41. this.initialCacheKey = this.getCacheKey();
  42. this._nodeBuilderState = null;
  43. this._bindings = null;
  44. this.onDispose = null;
  45. this.isRenderObject = true;
  46. this.onMaterialDispose = () => {
  47. this.dispose();
  48. };
  49. this.material.addEventListener( 'dispose', this.onMaterialDispose );
  50. }
  51. updateClipping( parent ) {
  52. const material = this.material;
  53. let clippingContext = this.clippingContext;
  54. if ( Array.isArray( material.clippingPlanes ) ) {
  55. if ( clippingContext === parent || ! clippingContext ) {
  56. clippingContext = new ClippingContext();
  57. this.clippingContext = clippingContext;
  58. }
  59. clippingContext.update( parent, material );
  60. } else if ( this.clippingContext !== parent ) {
  61. this.clippingContext = parent;
  62. }
  63. }
  64. get clippingNeedsUpdate() {
  65. if ( this.clippingContext.version === this.clippingContextVersion ) return false;
  66. this.clippingContextVersion = this.clippingContext.version;
  67. return true;
  68. }
  69. getNodeBuilderState() {
  70. return this._nodeBuilderState || ( this._nodeBuilderState = this._nodes.getForRender( this ) );
  71. }
  72. getBindings() {
  73. return this._bindings || ( this._bindings = this.getNodeBuilderState().createBindings() );
  74. }
  75. getIndex() {
  76. return this._geometries.getIndex( this );
  77. }
  78. getChainArray() {
  79. return [ this.object, this.material, this.context, this.lightsNode ];
  80. }
  81. getAttributes() {
  82. if ( this.attributes !== null ) return this.attributes;
  83. const nodeAttributes = this.getNodeBuilderState().nodeAttributes;
  84. const geometry = this.geometry;
  85. const attributes = [];
  86. const vertexBuffers = new Set();
  87. for ( const nodeAttribute of nodeAttributes ) {
  88. const attribute = nodeAttribute.node && nodeAttribute.node.attribute ? nodeAttribute.node.attribute : geometry.getAttribute( nodeAttribute.name );
  89. if ( attribute === undefined ) continue;
  90. attributes.push( attribute );
  91. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  92. vertexBuffers.add( bufferAttribute );
  93. }
  94. this.attributes = attributes;
  95. this.vertexBuffers = Array.from( vertexBuffers.values() );
  96. return attributes;
  97. }
  98. getVertexBuffers() {
  99. if ( this.vertexBuffers === null ) this.getAttributes();
  100. return this.vertexBuffers;
  101. }
  102. getMaterialCacheKey() {
  103. const { object, material } = this;
  104. let cacheKey = material.customProgramCacheKey();
  105. for ( const property of getKeys( material ) ) {
  106. if ( /^(is[A-Z]|_)|^(visible|version|uuid|name|opacity|userData)$/.test( property ) ) continue;
  107. let value = material[ property ];
  108. if ( value !== null ) {
  109. const type = typeof value;
  110. if ( type === 'number' ) value = value !== 0 ? '1' : '0'; // Convert to on/off, important for clearcoat, transmission, etc
  111. else if ( type === 'object' ) value = '{}';
  112. }
  113. cacheKey += /*property + ':' +*/ value + ',';
  114. }
  115. cacheKey += this.clippingContextVersion + ',';
  116. if ( object.skeleton ) {
  117. cacheKey += object.skeleton.bones.length + ',';
  118. }
  119. if ( object.morphTargetInfluences ) {
  120. cacheKey += object.morphTargetInfluences.length + ',';
  121. }
  122. if ( object.isBatchedMesh ) {
  123. cacheKey += object._matricesTexture.uuid + ',';
  124. if ( object._colorsTexture !== null ) {
  125. cacheKey += object._colorsTexture.uuid + ',';
  126. }
  127. }
  128. if ( object.count > 1 ) {
  129. cacheKey += object.count + ',';
  130. }
  131. return cacheKey;
  132. }
  133. get needsUpdate() {
  134. return this.initialNodesCacheKey !== this.getNodesCacheKey() || this.clippingNeedsUpdate;
  135. }
  136. getNodesCacheKey() {
  137. // Environment Nodes Cache Key
  138. return this._nodes.getCacheKey( this.scene, this.lightsNode );
  139. }
  140. getCacheKey() {
  141. return this.getMaterialCacheKey() + ',' + this.getNodesCacheKey();
  142. }
  143. dispose() {
  144. this.material.removeEventListener( 'dispose', this.onMaterialDispose );
  145. this.onDispose();
  146. }
  147. }