engine.dynamicTexture.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { ThinEngine } from "../../Engines/thinEngine";
  2. import { InternalTexture, InternalTextureSource } from '../../Materials/Textures/internalTexture';
  3. import { Nullable } from '../../types';
  4. declare module "../../Engines/thinEngine" {
  5. export interface ThinEngine {
  6. /**
  7. * Creates a dynamic texture
  8. * @param width defines the width of the texture
  9. * @param height defines the height of the texture
  10. * @param generateMipMaps defines if the engine should generate the mip levels
  11. * @param samplingMode defines the required sampling mode (Texture.NEAREST_SAMPLINGMODE by default)
  12. * @returns the dynamic texture inside an InternalTexture
  13. */
  14. createDynamicTexture(width: number, height: number, generateMipMaps: boolean, samplingMode: number): InternalTexture;
  15. /**
  16. * Update the content of a dynamic texture
  17. * @param texture defines the texture to update
  18. * @param source defines the source containing the data
  19. * @param invertY defines if data must be stored with Y axis inverted
  20. * @param premulAlpha defines if alpha is stored as premultiplied
  21. * @param format defines the format of the data
  22. * @param forceBindTexture if the texture should be forced to be bound eg. after a graphics context loss (Default: false)
  23. */
  24. updateDynamicTexture(texture: Nullable<InternalTexture>,
  25. source: ImageBitmap | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | OffscreenCanvas,
  26. invertY?: boolean, premulAlpha?: boolean, format?: number, forceBindTexture?: boolean): void;
  27. }
  28. }
  29. ThinEngine.prototype.createDynamicTexture = function(width: number, height: number, generateMipMaps: boolean, samplingMode: number): InternalTexture {
  30. var texture = new InternalTexture(this, InternalTextureSource.Dynamic);
  31. texture.baseWidth = width;
  32. texture.baseHeight = height;
  33. if (generateMipMaps) {
  34. width = this.needPOTTextures ? ThinEngine.GetExponentOfTwo(width, this._caps.maxTextureSize) : width;
  35. height = this.needPOTTextures ? ThinEngine.GetExponentOfTwo(height, this._caps.maxTextureSize) : height;
  36. }
  37. // this.resetTextureCache();
  38. texture.width = width;
  39. texture.height = height;
  40. texture.isReady = false;
  41. texture.generateMipMaps = generateMipMaps;
  42. texture.samplingMode = samplingMode;
  43. this.updateTextureSamplingMode(samplingMode, texture);
  44. this._internalTexturesCache.push(texture);
  45. return texture;
  46. };
  47. ThinEngine.prototype.updateDynamicTexture = function(texture: Nullable<InternalTexture>,
  48. source: ImageBitmap | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | OffscreenCanvas,
  49. invertY?: boolean,
  50. premulAlpha: boolean = false,
  51. format?: number,
  52. forceBindTexture: boolean = false): void {
  53. if (!texture) {
  54. return;
  55. }
  56. const gl = this._gl;
  57. const target = gl.TEXTURE_2D;
  58. const wasPreviouslyBound = this._bindTextureDirectly(target, texture, true, forceBindTexture);
  59. this._unpackFlipY(invertY === undefined ? texture.invertY : invertY);
  60. if (premulAlpha) {
  61. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
  62. }
  63. const textureType = this._getWebGLTextureType(texture.type);
  64. const glformat = this._getInternalFormat(format ? format : texture.format);
  65. const internalFormat = this._getRGBABufferInternalSizedFormat(texture.type, glformat);
  66. gl.texImage2D(target, 0, internalFormat, glformat, textureType, source);
  67. if (texture.generateMipMaps) {
  68. gl.generateMipmap(target);
  69. }
  70. if (!wasPreviouslyBound) {
  71. this._bindTextureDirectly(target, null);
  72. }
  73. if (premulAlpha) {
  74. gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
  75. }
  76. texture.isReady = true;
  77. };