engine.cubeTexture.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { ThinEngine } from "../../Engines/thinEngine.js";
  2. import { InternalTexture, InternalTextureSource } from "../../Materials/Textures/internalTexture.js";
  3. import { Logger } from "../../Misc/logger.js";
  4. import { GetExponentOfTwo } from "../../Misc/tools.functions.js";
  5. ThinEngine.prototype._createDepthStencilCubeTexture = function (size, options) {
  6. const internalTexture = new InternalTexture(this, InternalTextureSource.DepthStencil);
  7. internalTexture.isCube = true;
  8. if (this.webGLVersion === 1) {
  9. Logger.Error("Depth cube texture is not supported by WebGL 1.");
  10. return internalTexture;
  11. }
  12. const internalOptions = {
  13. bilinearFiltering: false,
  14. comparisonFunction: 0,
  15. generateStencil: false,
  16. ...options,
  17. };
  18. const gl = this._gl;
  19. this._bindTextureDirectly(gl.TEXTURE_CUBE_MAP, internalTexture, true);
  20. this._setupDepthStencilTexture(internalTexture, size, internalOptions.generateStencil, internalOptions.bilinearFiltering, internalOptions.comparisonFunction);
  21. // Create the depth/stencil buffer
  22. for (let face = 0; face < 6; face++) {
  23. if (internalOptions.generateStencil) {
  24. gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, gl.DEPTH24_STENCIL8, size, size, 0, gl.DEPTH_STENCIL, gl.UNSIGNED_INT_24_8, null);
  25. }
  26. else {
  27. gl.texImage2D(gl.TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, gl.DEPTH_COMPONENT24, size, size, 0, gl.DEPTH_COMPONENT, gl.UNSIGNED_INT, null);
  28. }
  29. }
  30. this._bindTextureDirectly(gl.TEXTURE_CUBE_MAP, null);
  31. this._internalTexturesCache.push(internalTexture);
  32. return internalTexture;
  33. };
  34. ThinEngine.prototype._setCubeMapTextureParams = function (texture, loadMipmap, maxLevel) {
  35. const gl = this._gl;
  36. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  37. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, loadMipmap ? gl.LINEAR_MIPMAP_LINEAR : gl.LINEAR);
  38. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
  39. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
  40. texture.samplingMode = loadMipmap ? 3 : 2;
  41. if (loadMipmap && this.getCaps().textureMaxLevel && maxLevel !== undefined && maxLevel > 0) {
  42. gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MAX_LEVEL, maxLevel);
  43. texture._maxLodLevel = maxLevel;
  44. }
  45. this._bindTextureDirectly(gl.TEXTURE_CUBE_MAP, null);
  46. };
  47. ThinEngine.prototype.createCubeTexture = function (rootUrl, scene, files, noMipmap, onLoad = null, onError = null, format, forcedExtension = null, createPolynomials = false, lodScale = 0, lodOffset = 0, fallback = null, loaderOptions, useSRGBBuffer = false) {
  48. const gl = this._gl;
  49. return this.createCubeTextureBase(rootUrl, scene, files, !!noMipmap, onLoad, onError, format, forcedExtension, createPolynomials, lodScale, lodOffset, fallback, (texture) => this._bindTextureDirectly(gl.TEXTURE_CUBE_MAP, texture, true), (texture, imgs) => {
  50. const width = this.needPOTTextures ? GetExponentOfTwo(imgs[0].width, this._caps.maxCubemapTextureSize) : imgs[0].width;
  51. const height = width;
  52. const faces = [
  53. gl.TEXTURE_CUBE_MAP_POSITIVE_X,
  54. gl.TEXTURE_CUBE_MAP_POSITIVE_Y,
  55. gl.TEXTURE_CUBE_MAP_POSITIVE_Z,
  56. gl.TEXTURE_CUBE_MAP_NEGATIVE_X,
  57. gl.TEXTURE_CUBE_MAP_NEGATIVE_Y,
  58. gl.TEXTURE_CUBE_MAP_NEGATIVE_Z,
  59. ];
  60. this._bindTextureDirectly(gl.TEXTURE_CUBE_MAP, texture, true);
  61. this._unpackFlipY(false);
  62. const internalFormat = format ? this._getInternalFormat(format, texture._useSRGBBuffer) : texture._useSRGBBuffer ? this._glSRGBExtensionValues.SRGB8_ALPHA8 : gl.RGBA;
  63. let texelFormat = format ? this._getInternalFormat(format) : gl.RGBA;
  64. if (texture._useSRGBBuffer && this.webGLVersion === 1) {
  65. texelFormat = internalFormat;
  66. }
  67. for (let index = 0; index < faces.length; index++) {
  68. if (imgs[index].width !== width || imgs[index].height !== height) {
  69. this._prepareWorkingCanvas();
  70. if (!this._workingCanvas || !this._workingContext) {
  71. Logger.Warn("Cannot create canvas to resize texture.");
  72. return;
  73. }
  74. this._workingCanvas.width = width;
  75. this._workingCanvas.height = height;
  76. this._workingContext.drawImage(imgs[index], 0, 0, imgs[index].width, imgs[index].height, 0, 0, width, height);
  77. gl.texImage2D(faces[index], 0, internalFormat, texelFormat, gl.UNSIGNED_BYTE, this._workingCanvas);
  78. }
  79. else {
  80. gl.texImage2D(faces[index], 0, internalFormat, texelFormat, gl.UNSIGNED_BYTE, imgs[index]);
  81. }
  82. }
  83. if (!noMipmap) {
  84. gl.generateMipmap(gl.TEXTURE_CUBE_MAP);
  85. }
  86. this._setCubeMapTextureParams(texture, !noMipmap);
  87. texture.width = width;
  88. texture.height = height;
  89. texture.isReady = true;
  90. if (format) {
  91. texture.format = format;
  92. }
  93. texture.onLoadedObservable.notifyObservers(texture);
  94. texture.onLoadedObservable.clear();
  95. if (onLoad) {
  96. onLoad();
  97. }
  98. }, !!useSRGBBuffer);
  99. };
  100. //# sourceMappingURL=engine.cubeTexture.js.map