RenderObject.js 3.4 KB

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