RenderObject.js 4.2 KB

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