Parcourir la source

Merge pull request #1671 from Palmer-JC/master

Compressed textures cleanup
David Catuhe il y a 8 ans
Parent
commit
9e69c41f58

+ 69 - 0
Tools/CompressedTextured/ktx-files.bat

@@ -0,0 +1,69 @@
+echo off
+REM Create a series of compressed textures versions of the file name passed in. Skips files which already
+REM exist, since script can be very long running.
+REM arg 1: file
+REM arg 2: file without extension
+REM arg 3: Y when need alpha, else N
+REM arg 4: Q when best image required, else D for developer quality
+
+REM -i specifies the input file passed as the first arg (full path) dos use 1%
+REM -pot + indicates force power of 2
+REM -m indicates to generate mipmaps
+REM -f is the format, variable type (UBN unsigned byte normalized), colorspace
+REM -q indicates how much time to spend, varies by encoding type
+REM -o specifies output file name, uses arg without extension adds -family.ktx
+REM - - - - - - - - - - - - - - - ASTC  - - - - - - - - - - - - - - - 
+echo working with %1
+REM all ASTC formats have alpha
+IF EXIST %2-astc.ktx GOTO PVRTC
+
+SET quality=astcveryfast
+if %4 == 'Q' SET quality=astcexhaustive
+echo compressing...
+PVRTexToolCLI.exe -i %1 -flip y -pot + -m -f ASTC_8x8,UBN,lRGB -q %quality% -shh -o %2-astc.ktx >junk.txt
+echo Saved texture to %2-astc.ktx
+REM - - - - - - - - - - - - - - - PVRTC - - - - - - - - - - - - - - - 
+:PVRTC
+REM PVRTC must be square on iOS
+IF EXIST %2-pvrtc.ktx GOTO DXT
+
+SET format=PVRTC1_2_RGB
+if %3 == 'Y' SET format=PVRTC1_2
+
+SET quality=pvrtcfastest
+if %4 == 'Q' SET quality=pvrtcbest
+
+PVRTexToolCLI.exe -i %1 -flip y -pot + -square + -m -f %format%,UBN,lRGB -q %quality% -o %2-pvrtc.ktx
+
+REM - - - - - - - - - - - - - - -  DXT  - - - - - - - - - - - - - - -
+:DXT
+IF EXIST %2-dxt.ktx GOTO ETC1
+
+SET format=BC1
+if %3 == 'Y' SET format=BC2
+
+PVRTexToolCLI.exe -i %1 -flip y -pot + -m -f %format%,UBN,lRGB -o %2-dxt.ktx
+
+REM - - - - - - - - - - - - - - - ETC1  - - - - - - - - - - - - - - - 
+REM ETC1 does not have an alpha capable format
+:ETC1
+IF EXIST %2-etc1.ktx GOTO ETC2
+
+SET quality=etcfast
+if %4 == 'Q' SET quality=etcslowperceptual
+
+if %3 == 'N' PVRTexToolCLI.exe -i %1 -flip y -pot + -m -f ETC1,UBN,lRGB -q %quality% -o %2-etc1.ktx
+
+REM - - - - - - - - - - - - - - - ETC2  - - - - - - - - - - - - - - - 
+:ETC2
+IF EXIST %2-etc2.ktx GOTO END
+
+SET format=ETC2_RGB
+if %3 == 'Y' SET format=ETC2_RGBA
+
+SET quality=etcfast
+if %4 == 'Q' SET quality=etcslowperceptual
+
+PVRTexToolCLI.exe -i %1 -flip y -pot + -m -f %format%,UBN,lRGB -q %quality% -o %2-etc2.ktx
+REM - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
+:END

+ 9 - 0
Tools/CompressedTextured/make-ktx-batch.bat

@@ -0,0 +1,9 @@
+echo off
+REM create compressed textures for each .jpg & .png in the current directory
+REM arg 1: Q when best image required, else D for developer quality
+
+REM erasing of any previous file
+erase ktx-batch.bat
+
+for %%f in (*.jpg) do echo call ktx-files.bat %%f %%~nf 'N' '%1' >>ktx-batch.bat
+for %%f in (*.png) do echo call ktx-files.bat %%f %%~nf 'Y' '%1' >>ktx-batch.bat

+ 1 - 0
dist/preview release/what's new.md

@@ -9,6 +9,7 @@
  - New `HolographicCamera` to support rendering on Windows Holographic. - ([sebavan](https://github.com/sebavan))
  - New Facet Data feature ([jerome](https://github.com/jbousquie))
  - babylon.fontTexture.ts was moved from babylon.js to canvas2D ([nockawa](https://github.com/nockawa))
+ - Multi-platform Compressed Textures for Desktops & Mobile Devices with fall back.  Batch (dos) scripts to convert entire directories of .jpg's & .png's ([jcpalmer](https://github.com/Palmer-JC))
 
 ### Updates
  - Added addChild, removeChild, setParent to AbstractMesh.  ([abow](https://github.com/abow))

+ 11 - 7
src/babylon.engine.ts

@@ -2055,8 +2055,7 @@
          * for description see https://www.khronos.org/opengles/sdk/tools/KTX/
          * for file layout see https://www.khronos.org/opengles/sdk/tools/KTX/file_format_spec/
          * 
-         * Note: The result of this call is not taken into account when a texture is base64 or when
-         * using a database / manifest.
+         * Note: The result of this call is not taken into account when a texture is base64.
          * 
          * @param {Array<string>} formatsAvailable- The list of those format families you have created
          * on your server.  Syntax: '-' + format family + '.ktx'.  (Case and order do not matter.)
@@ -2077,12 +2076,13 @@
             return this._textureFormatInUse = null;
         }
 
-        public createTexture(url: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null, buffer: any = null): WebGLTexture {
-            var texture = this._gl.createTexture();
+        public createTexture(urlArg: string, noMipmap: boolean, invertY: boolean, scene: Scene, samplingMode: number = Texture.TRILINEAR_SAMPLINGMODE, onLoad: () => void = null, onError: () => void = null, buffer: any = null, fallBack?: WebGLTexture): WebGLTexture {
+            var texture = fallBack ? fallBack : this._gl.createTexture();
 
             var extension: string;
             var isKTX = false;
             var fromData: any = false;
+            var url = String(urlArg);
             if (url.substr(0, 5) === "data:") {
                 fromData = true;
             }
@@ -2090,10 +2090,11 @@
             if (!fromData) {
                 var lastDot = url.lastIndexOf('.')
                 extension = url.substring(lastDot).toLowerCase();
-                if (this._textureFormatInUse && !fromData && !scene.database) {
+                if (this._textureFormatInUse && !fromData && !fallBack) {
                     extension = this._textureFormatInUse;
                     url = url.substring(0, lastDot) + this._textureFormatInUse;
                     isKTX = true;
+                    
                 }
             } else {
                 var oldUrl = url;
@@ -2114,12 +2115,15 @@
             if (onLoad) {
                 texture.onLoadedCallbacks.push(onLoad);
             }
-            this._loadedTexturesCache.push(texture);
+            if (!fallBack) this._loadedTexturesCache.push(texture);
 
             var onerror = () => {
                 scene._removePendingData(texture);
 
-                if (onError) {
+                // fallback for when compressed file not found to try again.  For instance, etc1 does not have an alpha capable type
+                if (isKTX) {
+                    this.createTexture(urlArg, noMipmap, invertY, scene, samplingMode, onLoad, onError, buffer, texture);
+                } else if (onError) {
                     onError();
                 }
             };