htmlElementTexture.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { BaseTexture } from "../../Materials/Textures/baseTexture.js";
  2. import { Matrix } from "../../Maths/math.vector.js";
  3. import { Observable } from "../../Misc/observable.js";
  4. import "../../Engines/Extensions/engine.dynamicTexture.js";
  5. import "../../Engines/Extensions/engine.videoTexture.js";
  6. import "../../Engines/Extensions/engine.externalTexture.js";
  7. /**
  8. * This represents the smallest workload to use an already existing element (Canvas or Video) as a texture.
  9. * To be as efficient as possible depending on your constraints nothing aside the first upload
  10. * is automatically managed.
  11. * It is a cheap VideoTexture or DynamicTexture if you prefer to keep full control of the elements
  12. * in your application.
  13. *
  14. * As the update is not automatic, you need to call them manually.
  15. */
  16. export class HtmlElementTexture extends BaseTexture {
  17. /**
  18. * Instantiates a HtmlElementTexture from the following parameters.
  19. *
  20. * @param name Defines the name of the texture
  21. * @param element Defines the video or canvas the texture is filled with
  22. * @param options Defines the other none mandatory texture creation options
  23. */
  24. constructor(name, element, options) {
  25. super(options.scene || options.engine);
  26. /**
  27. * Observable triggered once the texture has been loaded.
  28. */
  29. this.onLoadObservable = new Observable();
  30. if (!element || (!options.engine && !options.scene)) {
  31. return;
  32. }
  33. options = {
  34. ...HtmlElementTexture._DefaultOptions,
  35. ...options,
  36. };
  37. this._generateMipMaps = options.generateMipMaps;
  38. this._samplingMode = options.samplingMode;
  39. this._textureMatrix = Matrix.Identity();
  40. this._format = options.format;
  41. this.name = name;
  42. this.element = element;
  43. this._isVideo = !!element.getVideoPlaybackQuality;
  44. this._externalTexture = this._isVideo ? this._engine?.createExternalTexture(element) ?? null : null;
  45. this.anisotropicFilteringLevel = 1;
  46. this._createInternalTexture();
  47. }
  48. _createInternalTexture() {
  49. let width = 0;
  50. let height = 0;
  51. if (this._isVideo) {
  52. width = this.element.videoWidth;
  53. height = this.element.videoHeight;
  54. }
  55. else {
  56. width = this.element.width;
  57. height = this.element.height;
  58. }
  59. const engine = this._getEngine();
  60. if (engine) {
  61. this._texture = engine.createDynamicTexture(width, height, this._generateMipMaps, this._samplingMode);
  62. this._texture.format = this._format;
  63. }
  64. this.update();
  65. }
  66. /**
  67. * @returns the texture matrix used in most of the material.
  68. */
  69. getTextureMatrix() {
  70. return this._textureMatrix;
  71. }
  72. /**
  73. * Updates the content of the texture.
  74. * @param invertY Defines whether the texture should be inverted on Y (false by default on video and true on canvas)
  75. */
  76. update(invertY = null) {
  77. const engine = this._getEngine();
  78. if (this._texture == null || engine == null) {
  79. return;
  80. }
  81. const wasReady = this.isReady();
  82. if (this._isVideo) {
  83. const videoElement = this.element;
  84. if (videoElement.readyState < videoElement.HAVE_CURRENT_DATA) {
  85. return;
  86. }
  87. engine.updateVideoTexture(this._texture, this._externalTexture ? this._externalTexture : videoElement, invertY === null ? true : invertY);
  88. }
  89. else {
  90. const canvasElement = this.element;
  91. engine.updateDynamicTexture(this._texture, canvasElement, invertY === null ? true : invertY, false, this._format);
  92. }
  93. if (!wasReady && this.isReady()) {
  94. this.onLoadObservable.notifyObservers(this);
  95. }
  96. }
  97. /**
  98. * Dispose the texture and release its associated resources.
  99. */
  100. dispose() {
  101. this.onLoadObservable.clear();
  102. super.dispose();
  103. }
  104. }
  105. HtmlElementTexture._DefaultOptions = {
  106. generateMipMaps: false,
  107. samplingMode: 2,
  108. format: 5,
  109. engine: null,
  110. scene: null,
  111. };
  112. //# sourceMappingURL=htmlElementTexture.js.map