engine.videoTexture.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { ThinEngine } from "../../Engines/thinEngine.js";
  2. ThinEngine.prototype.updateVideoTexture = function (texture, video, invertY) {
  3. if (!texture || texture._isDisabled) {
  4. return;
  5. }
  6. const glformat = this._getInternalFormat(texture.format);
  7. const internalFormat = this._getRGBABufferInternalSizedFormat(0, texture.format);
  8. const wasPreviouslyBound = this._bindTextureDirectly(this._gl.TEXTURE_2D, texture, true);
  9. this._unpackFlipY(!invertY); // Video are upside down by default
  10. try {
  11. // Testing video texture support
  12. if (this._videoTextureSupported === undefined) {
  13. // clear old errors just in case.
  14. this._gl.getError();
  15. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, internalFormat, glformat, this._gl.UNSIGNED_BYTE, video);
  16. if (this._gl.getError() !== 0) {
  17. this._videoTextureSupported = false;
  18. }
  19. else {
  20. this._videoTextureSupported = true;
  21. }
  22. }
  23. // Copy video through the current working canvas if video texture is not supported
  24. if (!this._videoTextureSupported) {
  25. if (!texture._workingCanvas) {
  26. texture._workingCanvas = this.createCanvas(texture.width, texture.height);
  27. const context = texture._workingCanvas.getContext("2d");
  28. if (!context) {
  29. throw new Error("Unable to get 2d context");
  30. }
  31. texture._workingContext = context;
  32. texture._workingCanvas.width = texture.width;
  33. texture._workingCanvas.height = texture.height;
  34. }
  35. texture._workingContext.clearRect(0, 0, texture.width, texture.height);
  36. texture._workingContext.drawImage(video, 0, 0, video.videoWidth, video.videoHeight, 0, 0, texture.width, texture.height);
  37. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, internalFormat, glformat, this._gl.UNSIGNED_BYTE, texture._workingCanvas);
  38. }
  39. else {
  40. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, internalFormat, glformat, this._gl.UNSIGNED_BYTE, video);
  41. }
  42. if (texture.generateMipMaps) {
  43. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  44. }
  45. if (!wasPreviouslyBound) {
  46. this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
  47. }
  48. // this.resetTextureCache();
  49. texture.isReady = true;
  50. }
  51. catch (ex) {
  52. // Something unexpected
  53. // Let's disable the texture
  54. texture._isDisabled = true;
  55. }
  56. };
  57. //# sourceMappingURL=engine.videoTexture.js.map