فهرست منبع

fix creating a non power of two texture

Trevor Baron 6 سال پیش
والد
کامیت
1231f5efe7
2فایلهای تغییر یافته به همراه34 افزوده شده و 10 حذف شده
  1. 31 1
      src/Engines/engine.ts
  2. 3 9
      src/Materials/Textures/Loaders/basisTextureLoader.ts

+ 31 - 1
src/Engines/engine.ts

@@ -4347,7 +4347,7 @@ export class Engine {
 
         return texture;
     }
-
+    
     private _rescaleTexture(source: InternalTexture, destination: InternalTexture, scene: Nullable<Scene>, internalFormat: number, onComplete: () => void): void {
         let rtt = this.createRenderTargetTexture({
             width: destination.width,
@@ -4976,6 +4976,30 @@ export class Engine {
     }
 
     /** @hidden */
+    public _uploadDataToTextureAndResize(texture: InternalTexture, imageData: ArrayBufferView, width:number, height:number, babylonInternalFormat?:number){
+        // Get gl type and formats
+        var textureType = this._getWebGLTextureType(texture.type);
+        var format = this._getInternalFormat(texture.format);
+        var internalFormat = babylonInternalFormat === undefined ? this._getRGBABufferInternalSizedFormat(texture.type,format) : this._getInternalFormat(babylonInternalFormat);
+        
+        // Create non power of two texture
+        var gl = this._gl;
+        let source = new InternalTexture(this, InternalTexture.DATASOURCE_TEMP);
+        this._bindTextureDirectly(gl.TEXTURE_2D, source, true);
+        gl.texImage2D(gl.TEXTURE_2D, 0, internalFormat, width, height, 0, format, textureType, imageData);
+
+        // rescale to a power of two
+        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
+        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
+        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
+        gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
+        this._rescaleTexture(source, texture, this.scenes[0], internalFormat, () => {
+            this._releaseTexture(source);
+            this._bindTextureDirectly(gl.TEXTURE_2D, texture, true);
+        });
+    }
+
+    /** @hidden */
     public _uploadDataToTextureDirectly(texture: InternalTexture, imageData: ArrayBufferView, faceIndex: number = 0, lod: number = 0): void {
         var gl = this._gl;
 
@@ -6089,6 +6113,12 @@ export class Engine {
                     return this._gl.HALF_FLOAT_OES;
                 case Engine.TEXTURETYPE_UNSIGNED_BYTE:
                     return this._gl.UNSIGNED_BYTE;
+                case Engine.TEXTURETYPE_UNSIGNED_SHORT_4_4_4_4:
+                    return this._gl.UNSIGNED_SHORT_4_4_4_4;
+                case Engine.TEXTURETYPE_UNSIGNED_SHORT_5_5_5_1:
+                    return this._gl.UNSIGNED_SHORT_5_5_5_1;
+                case Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5:
+                    return this._gl.UNSIGNED_SHORT_5_6_5;
             }
             return this._gl.UNSIGNED_BYTE;
         }

+ 3 - 9
src/Materials/Textures/Loaders/basisTextureLoader.ts

@@ -80,15 +80,9 @@ export class _BasisTextureLoader implements IInternalTextureLoader {
             // Upload data to texture
             callback(fileInfo.width, fileInfo.height, false, true, () => {
                 if (transcodeResult.fallbackToRgb565) {
-                    // Load rgb565Data to texture if conversion is needed
-                    texture.getEngine()._unpackFlipY(texture.invertY);
-                    var gl = texture.getEngine()._gl;
-                    gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, fileInfo.alignedWidth, fileInfo.alignedHeight, 0, gl.RGB, gl.UNSIGNED_SHORT_5_6_5, transcodeResult.pixels);
-
-                    // TODO this is not working because computed power of 2 image size is larger than rgb565Data causing an error, working around this with below code
-                    // texture.type = Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5;
-                    // texture.format = Engine.TEXTUREFORMAT_RGB;
-                    // texture.getEngine()._uploadDataToTextureDirectly(texture, rgb565Data, 0,0)
+                    texture.type = Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5;
+                    texture.format = Engine.TEXTUREFORMAT_RGB;
+                    texture.getEngine()._uploadDataToTextureAndResize(texture, transcodeResult.pixels, fileInfo.alignedWidth, fileInfo.alignedHeight, Engine.TEXTUREFORMAT_RGB);
                 }else {
                     // compress texture needs to be flipped
                     texture._invertVScale = true;