RenderObject.js 2.1 KB

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