engine.dynamicTexture.ts 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 canvas defines the canvas containing the source
  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>, canvas: HTMLCanvasElement | OffscreenCanvas, invertY: boolean, premulAlpha?: boolean, format?: number, forceBindTexture?: boolean): void;
  25. }
  26. }
  27. ThinEngine.prototype.createDynamicTexture = function(width: number, height: number, generateMipMaps: boolean, samplingMode: number): InternalTexture {
  28. var texture = new InternalTexture(this, InternalTextureSource.Dynamic);
  29. texture.baseWidth = width;
  30. texture.baseHeight = height;
  31. if (generateMipMaps) {
  32. width = this.needPOTTextures ? ThinEngine.GetExponentOfTwo(width, this._caps.maxTextureSize) : width;
  33. height = this.needPOTTextures ? ThinEngine.GetExponentOfTwo(height, this._caps.maxTextureSize) : height;
  34. }
  35. // this.resetTextureCache();
  36. texture.width = width;
  37. texture.height = height;
  38. texture.isReady = false;
  39. texture.generateMipMaps = generateMipMaps;
  40. texture.samplingMode = samplingMode;
  41. this.updateTextureSamplingMode(samplingMode, texture);
  42. this._internalTexturesCache.push(texture);
  43. return texture;
  44. };
  45. ThinEngine.prototype.updateDynamicTexture = function(texture: Nullable<InternalTexture>, canvas: HTMLCanvasElement, invertY: boolean, premulAlpha: boolean = false, format?: number, forceBindTexture: boolean = false): void {
  46. if (!texture) {
  47. return;
  48. }
  49. this._bindTextureDirectly(this._gl.TEXTURE_2D, texture, true, forceBindTexture);
  50. this._unpackFlipY(invertY);
  51. if (premulAlpha) {
  52. this._gl.pixelStorei(this._gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
  53. }
  54. let internalFormat = format ? this._getInternalFormat(format) : this._gl.RGBA;
  55. this._gl.texImage2D(this._gl.TEXTURE_2D, 0, internalFormat, internalFormat, this._gl.UNSIGNED_BYTE, canvas);
  56. if (texture.generateMipMaps) {
  57. this._gl.generateMipmap(this._gl.TEXTURE_2D);
  58. }
  59. this._bindTextureDirectly(this._gl.TEXTURE_2D, null);
  60. if (premulAlpha) {
  61. this._gl.pixelStorei(this._gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
  62. }
  63. texture.isReady = true;
  64. };