RenderObject.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. let id = 0;
  2. export default class RenderObject {
  3. constructor( nodes, geometries, renderer, object, material, scene, camera, lightsNode ) {
  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.geometry = object.geometry;
  14. this.attributes = null;
  15. this.context = null;
  16. this.pipeline = null;
  17. this._materialVersion = - 1;
  18. this._materialCacheKey = '';
  19. this.onDispose = null;
  20. this.onMaterialDispose = () => {
  21. this.dispose();
  22. };
  23. this.material.addEventListener( 'dispose', this.onMaterialDispose );
  24. }
  25. getNodeBuilder() {
  26. return this._nodes.getForRender( this );
  27. }
  28. getBindings() {
  29. return this.getNodeBuilder().getBindings();
  30. }
  31. getIndex() {
  32. return this._geometries.getIndex( this );
  33. }
  34. getChainArray() {
  35. return [ this.object, this.material, this.scene, this.camera, this.lightsNode ];
  36. }
  37. getAttributes() {
  38. if ( this.attributes !== null ) return this.attributes;
  39. const nodeAttributes = this.getNodeBuilder().getAttributesArray();
  40. const geometry = this.geometry;
  41. const attributes = [];
  42. for ( const nodeAttribute of nodeAttributes ) {
  43. attributes.push( nodeAttribute.node && nodeAttribute.node.attribute ? nodeAttribute.node.attribute : geometry.getAttribute( nodeAttribute.name ) );
  44. }
  45. this.attributes = attributes;
  46. return attributes;
  47. }
  48. getCacheKey() {
  49. const { material, scene, lightsNode } = this;
  50. if ( material.version !== this._materialVersion ) {
  51. this._materialVersion = material.version;
  52. this._materialCacheKey = material.customProgramCacheKey();
  53. }
  54. const cacheKey = [];
  55. cacheKey.push( 'material:' + this._materialCacheKey );
  56. cacheKey.push( 'nodes:' + this._nodes.getCacheKey( scene, lightsNode ) );
  57. return '{' + cacheKey.join( ',' ) + '}';
  58. }
  59. dispose() {
  60. this.material.removeEventListener( 'dispose', this.onMaterialDispose );
  61. this.onDispose();
  62. }
  63. }