RenderObject.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.initialCacheKey = this.getCacheKey();
  20. this._nodeBuilderState = null;
  21. this._bindings = null;
  22. this.onDispose = null;
  23. this.isRenderObject = true;
  24. this.onMaterialDispose = () => {
  25. this.dispose();
  26. };
  27. this.material.addEventListener( 'dispose', this.onMaterialDispose );
  28. }
  29. getNodeBuilderState() {
  30. return this._nodeBuilderState || ( this._nodeBuilderState = this._nodes.getForRender( this ) );
  31. }
  32. getBindings() {
  33. return this._bindings || ( this._bindings = this.getNodeBuilderState().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.getNodeBuilderState().nodeAttributes;
  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. getMaterialCacheKey() {
  62. const material = this.material;
  63. let cacheKey = material.customProgramCacheKey();
  64. for ( const property in material ) {
  65. if ( /^(is[A-Z])|^(visible|version|uuid|name|opacity|userData)$/.test( property ) ) continue;
  66. let value = material[ property ];
  67. if ( value !== null ) {
  68. const type = typeof value;
  69. if ( type === 'number' ) value = value !== 0 ? '1' : '0'; // Convert to on/off, important for clearcoat, transmission, etc
  70. else if ( type === 'object' ) value = '{}';
  71. }
  72. cacheKey += /*property + ':' +*/ value + ',';
  73. }
  74. return cacheKey;
  75. }
  76. getNodesCacheKey() {
  77. // Environment Nodes Cache Key
  78. return this._nodes.getCacheKey( this.scene, this.lightsNode );
  79. }
  80. getCacheKey() {
  81. return `{material:${ this.getMaterialCacheKey() },nodes:${ this.getNodesCacheKey()}`;
  82. }
  83. dispose() {
  84. this.material.removeEventListener( 'dispose', this.onMaterialDispose );
  85. this.onDispose();
  86. }
  87. }