RenderObject.js 2.6 KB

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