RenderObject.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.initialNodesCacheKey = this.getNodesCacheKey();
  20. this.initialCacheKey = this.getCacheKey();
  21. this._nodeBuilderState = null;
  22. this._bindings = null;
  23. this.onDispose = null;
  24. this.isRenderObject = true;
  25. this.onMaterialDispose = () => {
  26. this.dispose();
  27. };
  28. this.material.addEventListener( 'dispose', this.onMaterialDispose );
  29. }
  30. getNodeBuilderState() {
  31. return this._nodeBuilderState || ( this._nodeBuilderState = this._nodes.getForRender( this ) );
  32. }
  33. getBindings() {
  34. return this._bindings || ( this._bindings = this.getNodeBuilderState().createBindings() );
  35. }
  36. getIndex() {
  37. return this._geometries.getIndex( this );
  38. }
  39. getChainArray() {
  40. return [ this.object, this.material, this.context, this.lightsNode ];
  41. }
  42. getAttributes() {
  43. if ( this.attributes !== null ) return this.attributes;
  44. const nodeAttributes = this.getNodeBuilderState().nodeAttributes;
  45. const geometry = this.geometry;
  46. const attributes = [];
  47. const vertexBuffers = new Set();
  48. for ( const nodeAttribute of nodeAttributes ) {
  49. const attribute = nodeAttribute.node && nodeAttribute.node.attribute ? nodeAttribute.node.attribute : geometry.getAttribute( nodeAttribute.name );
  50. attributes.push( attribute );
  51. const bufferAttribute = attribute.isInterleavedBufferAttribute ? attribute.data : attribute;
  52. vertexBuffers.add( bufferAttribute );
  53. }
  54. this.attributes = attributes;
  55. this.vertexBuffers = Array.from( vertexBuffers.values() );
  56. return attributes;
  57. }
  58. getVertexBuffers() {
  59. if ( this.vertexBuffers === null ) this.getAttributes();
  60. return this.vertexBuffers;
  61. }
  62. getMaterialCacheKey() {
  63. const { object, material } = this;
  64. let cacheKey = material.customProgramCacheKey();
  65. for ( const property in material ) {
  66. if ( /^(is[A-Z])|^(visible|version|uuid|name|opacity|userData)$/.test( property ) ) continue;
  67. let value = material[ property ];
  68. if ( value !== null ) {
  69. const type = typeof value;
  70. if ( type === 'number' ) value = value !== 0 ? '1' : '0'; // Convert to on/off, important for clearcoat, transmission, etc
  71. else if ( type === 'object' ) value = '{}';
  72. }
  73. cacheKey += /*property + ':' +*/ value + ',';
  74. }
  75. if ( object.morphTargetInfluences ) {
  76. cacheKey += object.morphTargetInfluences.length + ',';
  77. }
  78. return cacheKey;
  79. }
  80. get needsUpdate() {
  81. return this.initialNodesCacheKey !== this.getNodesCacheKey();
  82. }
  83. getNodesCacheKey() {
  84. // Environment Nodes Cache Key
  85. return this._nodes.getCacheKey( this.scene, this.lightsNode );
  86. }
  87. getCacheKey() {
  88. return this.getMaterialCacheKey() + ',' + this.getNodesCacheKey();
  89. }
  90. dispose() {
  91. this.material.removeEventListener( 'dispose', this.onMaterialDispose );
  92. this.onDispose();
  93. }
  94. }