Textures.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import DataMap from './DataMap.js';
  2. import { Vector2, DepthTexture, DepthStencilFormat, UnsignedInt248Type } from 'three';
  3. const _size = new Vector2();
  4. class Textures extends DataMap {
  5. constructor( backend, info ) {
  6. super();
  7. this.backend = backend;
  8. this.info = info;
  9. }
  10. updateRenderTarget( renderTarget ) {
  11. const renderTargetData = this.get( renderTarget );
  12. const texture = renderTarget.texture;
  13. const size = this.getSize( texture );
  14. let depthTexture = renderTarget.depthTexture || renderTargetData.depthTexture;
  15. if ( depthTexture === undefined ) {
  16. depthTexture = new DepthTexture();
  17. depthTexture.format = DepthStencilFormat;
  18. depthTexture.type = UnsignedInt248Type;
  19. depthTexture.image.width = size.width;
  20. depthTexture.image.height = size.height;
  21. }
  22. if ( renderTargetData.width !== size.width || size.height !== renderTargetData.height ) {
  23. texture.needsUpdate = true;
  24. depthTexture.needsUpdate = true;
  25. depthTexture.image.width = size.width;
  26. depthTexture.image.height = size.height;
  27. }
  28. renderTargetData.width = size.width;
  29. renderTargetData.height = size.height;
  30. renderTargetData.texture = texture;
  31. renderTargetData.depthTexture = depthTexture;
  32. this.updateTexture( texture );
  33. this.updateTexture( depthTexture );
  34. // dispose handler
  35. if ( renderTargetData.initialized !== true ) {
  36. renderTargetData.initialized = true;
  37. // dispose
  38. const onDispose = () => {
  39. renderTarget.removeEventListener( 'dispose', onDispose );
  40. this._destroyTexture( texture );
  41. this._destroyTexture( depthTexture );
  42. };
  43. renderTarget.addEventListener( 'dispose', onDispose );
  44. }
  45. }
  46. updateTexture( texture ) {
  47. const textureData = this.get( texture );
  48. if ( textureData.initialized === true && textureData.version === texture.version ) return;
  49. const isRenderTexture = texture.isRenderTargetTexture || texture.isDepthTexture || texture.isFramebufferTexture;
  50. const backend = this.backend;
  51. if ( isRenderTexture && textureData.initialized === true ) {
  52. // it's an update
  53. backend.destroySampler( texture );
  54. backend.destroyTexture( texture );
  55. }
  56. //
  57. if ( isRenderTexture ) {
  58. backend.createSampler( texture );
  59. backend.createTexture( texture );
  60. } else {
  61. const needsCreate = textureData.initialized !== true;
  62. if ( needsCreate ) backend.createSampler( texture );
  63. if ( texture.version > 0 ) {
  64. const image = texture.image;
  65. if ( image === undefined ) {
  66. console.warn( 'THREE.Renderer: Texture marked for update but image is undefined.' );
  67. } else if ( image.complete === false ) {
  68. console.warn( 'THREE.Renderer: Texture marked for update but image is incomplete.' );
  69. } else {
  70. if ( textureData.isDefaultTexture === undefined || textureData.isDefaultTexture === true ) {
  71. backend.createTexture( texture );
  72. textureData.isDefaultTexture = false;
  73. }
  74. backend.updateTexture( texture );
  75. }
  76. } else {
  77. // async update
  78. backend.createDefaultTexture( texture );
  79. textureData.isDefaultTexture = true;
  80. }
  81. }
  82. // dispose handler
  83. if ( textureData.initialized !== true ) {
  84. textureData.initialized = true;
  85. //
  86. this.info.memory.textures ++;
  87. // dispose
  88. const onDispose = () => {
  89. texture.removeEventListener( 'dispose', onDispose );
  90. this._destroyTexture( texture );
  91. this.info.memory.textures --;
  92. };
  93. texture.addEventListener( 'dispose', onDispose );
  94. }
  95. //
  96. textureData.version = texture.version;
  97. }
  98. getSize( texture, target = _size ) {
  99. if ( texture.isCubeTexture ) {
  100. target.width = texture.image[ 0 ].width;
  101. target.height = texture.image[ 0 ].height;
  102. } else {
  103. target.width = texture.image.width;
  104. target.height = texture.image.height;
  105. }
  106. return target;
  107. }
  108. _destroyTexture( texture ) {
  109. this.backend.destroySampler( texture );
  110. this.backend.destroyTexture( texture );
  111. this.delete( texture );
  112. }
  113. }
  114. export default Textures;