RenderObject.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.attributes = null;
  35. this.pipeline = null;
  36. this.vertexBuffers = null;
  37. this.updateClipping( renderContext.clippingContext );
  38. this.clippingContextVersion = this.clippingContext.version;
  39. this.initialNodesCacheKey = this.getNodesCacheKey();
  40. this.initialCacheKey = this.getCacheKey();
  41. this._nodeBuilderState = null;
  42. this._bindings = null;
  43. this.onDispose = null;
  44. this.isRenderObject = true;
  45. this.onMaterialDispose = () => {
  46. this.dispose();
  47. };
  48. this.material.addEventListener( 'dispose', this.onMaterialDispose );
  49. }
  50. updateClipping( parent ) {
  51. const material = this.material;
  52. let clippingContext = this.clippingContext;
  53. if ( Array.isArray( material.clippingPlanes ) ) {
  54. if ( clippingContext === parent || ! clippingContext ) {
  55. clippingContext = new ClippingContext();
  56. this.clippingContext = clippingContext;
  57. }
  58. clippingContext.update( parent, material );
  59. } else if ( this.clippingContext !== parent ) {
  60. this.clippingContext = parent;
  61. }
  62. }
  63. get clippingNeedsUpdate() {
  64. if ( this.clippingContext.version === this.clippingContextVersion ) return false;
  65. this.clippingContextVersion = this.clippingContext.version;
  66. return true;
  67. }
  68. getNodeBuilderState() {
  69. return this._nodeBuilderState || ( this._nodeBuilderState = this._nodes.getForRender( this ) );
  70. }
  71. getBindings() {
  72. return this._bindings || ( this._bindings = this.getNodeBuilderState().createBindings() );
  73. }
  74. getIndex() {
  75. return this._geometries.getIndex( this );
  76. }
  77. getChainArray() {
  78. return [ this.object, this.material, this.context, this.lightsNode ];
  79. }
  80. getAttributes() {
  81. if ( this.attributes !== null ) return this.attributes;
  82. const nodeAttributes = this.getNodeBuilderState().nodeAttributes;
  83. const geometry = this.geometry;
  84. const attributes = [];
  85. const vertexBuffers = new Set();
  86. for ( const nodeAttribute of nodeAttributes ) {
  87. const attribute = nodeAttribute.node && nodeAttribute.node.attribute ? nodeAttribute.node.attribute : geometry.getAttribute( nodeAttribute.name );
  88. if ( attribute === undefined ) continue;
  89. attributes.push( attribute );
  90. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  91. vertexBuffers.add( bufferAttribute );
  92. }
  93. this.attributes = attributes;
  94. this.vertexBuffers = Array.from( vertexBuffers.values() );
  95. return attributes;
  96. }
  97. getVertexBuffers() {
  98. if ( this.vertexBuffers === null ) this.getAttributes();
  99. return this.vertexBuffers;
  100. }
  101. getMaterialCacheKey() {
  102. const { object, material } = this;
  103. let cacheKey = material.customProgramCacheKey();
  104. for ( const property of getKeys( material ) ) {
  105. if ( /^(is[A-Z]|_)|^(visible|version|uuid|name|opacity|userData)$/.test( property ) ) continue;
  106. let value = material[ property ];
  107. if ( value !== null ) {
  108. const type = typeof value;
  109. if ( type === 'number' ) value = value !== 0 ? '1' : '0'; // Convert to on/off, important for clearcoat, transmission, etc
  110. else if ( type === 'object' ) value = '{}';
  111. }
  112. cacheKey += /*property + ':' +*/ value + ',';
  113. }
  114. cacheKey += this.clippingContextVersion + ',';
  115. if ( object.skeleton ) {
  116. cacheKey += object.skeleton.bones.length + ',';
  117. }
  118. if ( object.morphTargetInfluences ) {
  119. cacheKey += object.morphTargetInfluences.length + ',';
  120. }
  121. if ( object.isBatchedMesh ) {
  122. cacheKey += object._matricesTexture.uuid + ',';
  123. }
  124. return cacheKey;
  125. }
  126. get needsUpdate() {
  127. return this.initialNodesCacheKey !== this.getNodesCacheKey() || this.clippingNeedsUpdate;
  128. }
  129. getNodesCacheKey() {
  130. // Environment Nodes Cache Key
  131. return this._nodes.getCacheKey( this.scene, this.lightsNode );
  132. }
  133. getCacheKey() {
  134. return this.getMaterialCacheKey() + ',' + this.getNodesCacheKey();
  135. }
  136. dispose() {
  137. this.material.removeEventListener( 'dispose', this.onMaterialDispose );
  138. this.onDispose();
  139. }
  140. }