Textures.js 3.6 KB

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