RenderObject.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.vertexBuffers = null;
  18. this._materialVersion = - 1;
  19. this._materialCacheKey = '';
  20. this.onDispose = null;
  21. this.onMaterialDispose = () => {
  22. this.dispose();
  23. };
  24. this.material.addEventListener( 'dispose', this.onMaterialDispose );
  25. }
  26. getNodeBuilder() {
  27. return this._nodes.getForRender( this );
  28. }
  29. getBindings() {
  30. return this.getNodeBuilder().getBindings();
  31. }
  32. getIndex() {
  33. return this._geometries.getIndex( this );
  34. }
  35. getChainArray() {
  36. return [ this.object, this.material, this.scene, this.camera, this.lightsNode ];
  37. }
  38. getAttributes() {
  39. if ( this.attributes !== null ) return this.attributes;
  40. const nodeAttributes = this.getNodeBuilder().getAttributesArray();
  41. const geometry = this.geometry;
  42. const attributes = [];
  43. const vertexBuffers = new Set();
  44. for ( const nodeAttribute of nodeAttributes ) {
  45. const attribute = nodeAttribute.node && nodeAttribute.node.attribute ? nodeAttribute.node.attribute : geometry.getAttribute( nodeAttribute.name );
  46. attributes.push( attribute );
  47. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  48. vertexBuffers.add( bufferAttribute );
  49. }
  50. this.attributes = attributes;
  51. this.vertexBuffers = Array.from( vertexBuffers.values() );
  52. return attributes;
  53. }
  54. getVertexBuffers() {
  55. if ( this.vertexBuffers === null ) this.getAttributes();
  56. return this.vertexBuffers;
  57. }
  58. getCacheKey() {
  59. const { material, scene, lightsNode } = this;
  60. if ( material.version !== this._materialVersion ) {
  61. this._materialVersion = material.version;
  62. this._materialCacheKey = material.customProgramCacheKey();
  63. }
  64. const cacheKey = [];
  65. cacheKey.push( 'material:' + this._materialCacheKey );
  66. cacheKey.push( 'nodes:' + this._nodes.getCacheKey( scene, lightsNode ) );
  67. return '{' + cacheKey.join( ',' ) + '}';
  68. }
  69. dispose() {
  70. this.material.removeEventListener( 'dispose', this.onMaterialDispose );
  71. this.onDispose();
  72. }
  73. }