فهرست منبع

whats new and lint fix

Trevor Baron 6 سال پیش
والد
کامیت
8f56e443ce

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

@@ -4,7 +4,7 @@
 - WIP: Node Material (NEED DOC AND SAMPLES) ([Deltakosh](https://github.com/deltakosh/))
 - WIP: Node material editor (NEED DOC AND VIDEOS) ([Deltakosh](https://github.com/deltakosh/)/[TrevorDev](https://github.com/TrevorDev))
 - WIP: WebGPU support (NEED DOC AND SAMPLES) ([Sebavan](https://github.com/sebavan/)
-- WIP: .basis texture file format support [Demo](https://www.babylonjs-playground.com/#4RN0VF) ([TrevorDev](https://github.com/TrevorDev))
+- WIP: .basis texture file format support (mipmaps, web worker) [Demo](https://www.babylonjs-playground.com/#4RN0VF) ([TrevorDev](https://github.com/TrevorDev))
 
 ## Optimizations
 

+ 11 - 11
src/Materials/Textures/Loaders/basisTextureLoader.ts

@@ -80,11 +80,11 @@ export class _BasisTextureLoader implements IInternalTextureLoader {
                 etc2: caps.etc2 ? true : false
             }
         };
-        BasisTools.TranscodeAsync(data, transcodeConfig).then((result)=>{
+        BasisTools.TranscodeAsync(data, transcodeConfig).then((result) => {
             var rootImage = result.fileInfo.images[0].levels[0];
             callback(rootImage.width, rootImage.height, false, true, () => {
                 texture._invertVScale = true;
-                if(result.format === -1){
+                if (result.format === -1) {
                     // No compatable compressed format found, fallback to RGB
                     texture.type = Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5;
                     texture.format = Engine.TEXTUREFORMAT_RGB;
@@ -95,8 +95,8 @@ export class _BasisTextureLoader implements IInternalTextureLoader {
                     source.type = Engine.TEXTURETYPE_UNSIGNED_SHORT_5_6_5;
                     source.format = Engine.TEXTUREFORMAT_RGB;
                     // Fallback requires aligned width/height
-                    source.width = (rootImage.width + 3) & ~3;;
-                    source.height =(rootImage.height + 3) & ~3;
+                    source.width = (rootImage.width + 3) & ~3;
+                    source.height = (rootImage.height + 3) & ~3;
                     texture.getEngine()._bindTextureDirectly(source.getEngine()._gl.TEXTURE_2D, source, true);
                     texture.getEngine()._uploadDataToTextureDirectly(source, rootImage.transcodedPixels, 0, 0, Engine.TEXTUREFORMAT_RGB, true);
 
@@ -105,23 +105,23 @@ export class _BasisTextureLoader implements IInternalTextureLoader {
                         source.getEngine()._releaseTexture(source);
                         source.getEngine()._bindTextureDirectly(source.getEngine()._gl.TEXTURE_2D, texture, true);
                     });
-                }else{
+                }else {
                     texture.width = rootImage.width;
                     texture.height = rootImage.height;
 
                     // Upload all mip levels in the file
-                    result.fileInfo.images[0].levels.forEach((level, index)=>{
+                    result.fileInfo.images[0].levels.forEach((level, index) => {
                         texture.getEngine()._uploadCompressedDataToTextureDirectly(texture, BasisTools.GetInternalFormatFromBasisFormat(result.format!), level.width, level.height, level.transcodedPixels, 0, index);
-                    })
+                    });
 
-                    if(texture.getEngine().webGLVersion < 2 && (Scalar.Log2(texture.width) % 1 !== 0 || Scalar.Log2(texture.height) % 1 !== 0)){
+                    if (texture.getEngine().webGLVersion < 2 && (Scalar.Log2(texture.width) % 1 !== 0 || Scalar.Log2(texture.height) % 1 !== 0)) {
                         Tools.Warn("Loaded .basis texture width and height are not a power of two. Texture wrapping will be set to Texture.CLAMP_ADDRESSMODE as other modes are not supported with non power of two dimensions in webGL 1.");
                         texture._cachedWrapU = Texture.CLAMP_ADDRESSMODE;
                     }
-                    
+
                 }
-            })
-        })
+            });
+        });
     }
 }
 

+ 6 - 6
src/Materials/Textures/texture.ts

@@ -289,26 +289,26 @@ export class Texture extends BaseTexture {
         scene.getEngine().onBeforeTextureInitObservable.notifyObservers(this);
 
         let load = () => {
-            if(this._texture){
+            if (this._texture) {
                 if (this._texture._invertVScale) {
                     this.vScale = -1;
                 }
-                
+
                 // Update texutre to match internal texture's wrapping
-                if(this._texture._cachedWrapU !== null){
+                if (this._texture._cachedWrapU !== null) {
                     this.wrapU = this._texture._cachedWrapU;
                     this._texture._cachedWrapU = null;
                 }
-                if(this._texture._cachedWrapV !== null){
+                if (this._texture._cachedWrapV !== null) {
                     this.wrapV = this._texture._cachedWrapV;
                     this._texture._cachedWrapV = null;
                 }
-                if(this._texture._cachedWrapR !== null){
+                if (this._texture._cachedWrapR !== null) {
                     this.wrapR = this._texture._cachedWrapR;
                     this._texture._cachedWrapR = null;
                 }
             }
-            
+
             if (this.onLoadObservable.hasObservers()) {
                 this.onLoadObservable.notifyObservers(this);
             }

+ 63 - 65
src/Misc/basis.ts

@@ -12,7 +12,7 @@ class BasisFileInfo {
     /**
      * Info about each image of the basis file
      */
-    public images: Array<{levels: Array<{width: number, height: number, transcodedPixels: ArrayBufferView}>}>
+    public images: Array<{levels: Array<{width: number, height: number, transcodedPixels: ArrayBufferView}>}>;
 }
 
 /**
@@ -26,28 +26,28 @@ export class BasisTranscodeConfiguration {
         /**
          * etc1 compression format
          */
-        etc1?:boolean;
+        etc1?: boolean;
         /**
          * s3tc compression format
          */
-        s3tc?:boolean;
+        s3tc?: boolean;
         /**
          * pvrtc compression format
          */
-        pvrtc?:boolean;
+        pvrtc?: boolean;
         /**
          * etc2 compression format
          */
-        etc2?:boolean;
+        etc2?: boolean;
     };
     /**
      * If mipmap levels should be loaded for transcoded images (Default: true)
      */
-    loadMipmapLevels?:boolean
+    loadMipmapLevels?: boolean;
     /**
      * Index of a single image to load (Default: all images)
      */
-    loadSingleImage?:number
+    loadSingleImage?: number;
 }
 
 /**
@@ -86,7 +86,7 @@ export class BasisTools {
      * @returns internal format corresponding to the Basis format
      */
     public static GetInternalFormatFromBasisFormat(basisFormat: number) {
-        // Corrisponding internal formats 
+        // Corrisponding internal formats
         var COMPRESSED_RGB_S3TC_DXT1_EXT  = 0x83F0;
         var COMPRESSED_RGBA_S3TC_DXT5_EXT = 0x83F3;
         var RGB_ETC1_Format = 36196;
@@ -102,27 +102,27 @@ export class BasisTools {
         }
     }
 
-    private static _WorkerPromise:Nullable<Promise<Worker>> = null;
-    private static _Worker:Nullable<Worker> = null;
-    private static _CreateWorkerAsync(){
-        if(!this._WorkerPromise){
-            this._WorkerPromise = new Promise((res)=>{
-                if(this._Worker){
-                    res(this._Worker)
-                }else{
-                    Tools.loadFileAsync(BasisTools.WasmModuleURL).then((wasmBinary)=>{
+    private static _WorkerPromise: Nullable<Promise<Worker>> = null;
+    private static _Worker: Nullable<Worker> = null;
+    private static _CreateWorkerAsync() {
+        if (!this._WorkerPromise) {
+            this._WorkerPromise = new Promise((res) => {
+                if (this._Worker) {
+                    res(this._Worker);
+                }else {
+                    Tools.LoadFileAsync(BasisTools.WasmModuleURL).then((wasmBinary) => {
                         const workerBlobUrl = URL.createObjectURL(new Blob([`(${workerFunc})()`], { type: "application/javascript" }));
-                        this._Worker = new Worker(workerBlobUrl)
-                        
-                        var initHandler = (msg:any)=>{
-                            if(msg.data.action === "init"){
+                        this._Worker = new Worker(workerBlobUrl);
+
+                        var initHandler = (msg: any) => {
+                            if (msg.data.action === "init") {
                                 this._Worker!.removeEventListener("message", initHandler);
                                 res(this._Worker!);
                             }
                         };
                         this._Worker.addEventListener("message", initHandler);
                         this._Worker.postMessage({action: "init", url: BasisTools.JSModuleURL, wasmBinary: wasmBinary});
-                    })
+                    });
                 }
             });
         }
@@ -130,32 +130,31 @@ export class BasisTools {
     }
 
     /**
-     * Transcodes a loaded image file to compressed pixel data 
+     * Transcodes a loaded image file to compressed pixel data
      * @param imageData image data to transcode
      * @param config configuration options for the transcoding
      * @returns a promise resulting in the transcoded image
      */
-    public static TranscodeAsync(imageData: ArrayBuffer, config:BasisTranscodeConfiguration):Promise<{fileInfo:BasisFileInfo, format:number}>{
-        return new Promise((res)=>{
-            this._CreateWorkerAsync().then(()=>{
-                var messageHandler = (msg:any)=>{
-                    if(msg.data.action === "transcode"){
+    public static TranscodeAsync(imageData: ArrayBuffer, config: BasisTranscodeConfiguration): Promise<{fileInfo: BasisFileInfo, format: number}> {
+        return new Promise((res) => {
+            this._CreateWorkerAsync().then(() => {
+                var messageHandler = (msg: any) => {
+                    if (msg.data.action === "transcode") {
                         this._Worker!.removeEventListener("message", messageHandler);
                         res(msg.data);
                     }
                 };
                 this._Worker!.addEventListener("message", messageHandler);
-                this._Worker!.postMessage({action: "transcode", imageData: imageData, config:config, ignoreSupportedFormats: this._IgnoreSupportedFormats}, [imageData])
-            })
-        })
+                this._Worker!.postMessage({action: "transcode", imageData: imageData, config: config, ignoreSupportedFormats: this._IgnoreSupportedFormats}, [imageData]);
+            });
+        });
     }
 }
 
-
 // WorkerGlobalScope
 declare function importScripts(...urls: string[]): void;
 declare function postMessage(message: any, transfer?: any[]): void;
-declare var Module:any;
+declare var Module: any;
 function workerFunc(): void {
     var _BASIS_FORMAT = {
         cTFETC1: 0,
@@ -167,27 +166,27 @@ function workerFunc(): void {
         cTFBC3: 6,
         cTFBC5: 7,
     };
-    var transcoderModulePromise:Nullable<Promise<any>> = null;
+    var transcoderModulePromise: Nullable<Promise<any>> = null;
     onmessage = (event) => {
-        if(event.data.action === "init"){
+        if (event.data.action === "init") {
              // Load the transcoder if it hasn't been yet
-            if(!transcoderModulePromise){
+            if (!transcoderModulePromise) {
                 // Override wasm binary
                 Module = { wasmBinary: (event.data.wasmBinary) };
-                importScripts(event.data.url)
-                transcoderModulePromise = new Promise((res)=>{
+                importScripts(event.data.url);
+                transcoderModulePromise = new Promise((res) => {
                     Module.onRuntimeInitialized = () => {
                         Module.initializeBasis();
                         res();
                     };
                 });
             }
-            transcoderModulePromise.then(()=>{
-                postMessage({action: "init"})
-            })
-        }else if(event.data.action === "transcode"){
+            transcoderModulePromise.then(() => {
+                postMessage({action: "init"});
+            });
+        }else if (event.data.action === "transcode") {
             // Transcode the basis image and return the resulting pixels
-            var config:BasisTranscodeConfiguration = event.data.config;
+            var config: BasisTranscodeConfiguration = event.data.config;
             var imgData = event.data.imageData;
             var loadedFile = new Module.BasisFile(new Uint8Array(imgData));
             var fileInfo = GetFileInfo(loadedFile);
@@ -206,34 +205,33 @@ function workerFunc(): void {
                 throw "transcode failed";
             }
 
-            var buffers:Array<any> = [];
-            fileInfo.images.forEach((image, imageIndex)=>{
-                if(config.loadSingleImage === undefined || config.loadSingleImage === imageIndex){
-                    if(config.loadMipmapLevels === false){
+            var buffers: Array<any> = [];
+            fileInfo.images.forEach((image, imageIndex) => {
+                if (config.loadSingleImage === undefined || config.loadSingleImage === imageIndex) {
+                    if (config.loadMipmapLevels === false) {
                         var levelInfo = image.levels[0];
                         levelInfo.transcodedPixels = TranscodeLevel(loadedFile, imageIndex, 0, format!, needsConversion);
-                        buffers.push(levelInfo.transcodedPixels.buffer)
-                    }else{
-                        image.levels.forEach((levelInfo, levelIndex)=>{
+                        buffers.push(levelInfo.transcodedPixels.buffer);
+                    }else {
+                        image.levels.forEach((levelInfo, levelIndex) => {
                             levelInfo.transcodedPixels = TranscodeLevel(loadedFile, imageIndex, levelIndex, format!, needsConversion);
-                            buffers.push(levelInfo.transcodedPixels.buffer)
-                            
-                        })
+                            buffers.push(levelInfo.transcodedPixels.buffer);
+
+                        });
                     }
                 }
-            })
+            });
 
             // Close file
             loadedFile.close();
             loadedFile.delete();
 
-           
-            if(needsConversion){
+            if (needsConversion) {
                 format = -1;
             }
             postMessage({action: "transcode", fileInfo: fileInfo, format: format}, buffers);
         }
-       
+
     };
 
     /**
@@ -244,7 +242,7 @@ function workerFunc(): void {
      */
     function GetSupportedTranscodeFormat(config: BasisTranscodeConfiguration, fileInfo: BasisFileInfo): Nullable<number> {
         var format = null;
-        if(config.supportedCompressionFormats){
+        if (config.supportedCompressionFormats) {
             if (config.supportedCompressionFormats.etc1) {
                 format = _BASIS_FORMAT.cTFETC1;
             }else if (config.supportedCompressionFormats.s3tc) {
@@ -266,26 +264,26 @@ function workerFunc(): void {
     function GetFileInfo(basisFile: any): BasisFileInfo {
         var hasAlpha = basisFile.getHasAlpha();
         var imageCount = basisFile.getNumImages();
-        var images = []
-        for(var i = 0;i<imageCount;i++){
+        var images = [];
+        for (var i = 0; i < imageCount; i++) {
             var imageInfo = {
                 levels: ([] as Array<any>)
-            }
+            };
             var levelCount = basisFile.getNumLevels(i);
-            for(var level = 0;level < levelCount;level++){
+            for (var level = 0; level < levelCount; level++) {
                 var levelInfo = {
                     width: basisFile.getImageWidth(i, level),
                     height: basisFile.getImageHeight(i, level)
-                }
+                };
                 imageInfo.levels.push(levelInfo);
             }
-            images.push(imageInfo)
+            images.push(imageInfo);
         }
         var info = { hasAlpha, images };
         return info;
     }
 
-    function TranscodeLevel(loadedFile:any, imageIndex:number, levelIndex:number, format: number, convertToRgb565: boolean){
+    function TranscodeLevel(loadedFile: any, imageIndex: number, levelIndex: number, format: number, convertToRgb565: boolean) {
         var dstSize = loadedFile.getImageTranscodedSizeInBytes(imageIndex, levelIndex, format);
         var dst = new Uint8Array(dstSize);
         if (!loadedFile.transcodeImage(dst, imageIndex, levelIndex, format, 1, 0)) {

+ 1 - 1
src/Misc/tools.ts

@@ -1017,7 +1017,7 @@ export class Tools {
      * @param url the file url to load
      * @returns a promise containing an ArrayBuffer corrisponding to the loaded file
      */
-    public static loadFileAsync(url: string): Promise<ArrayBuffer> {
+    public static LoadFileAsync(url: string): Promise<ArrayBuffer> {
         return new Promise((resolve, reject) => {
             Tools.LoadFile(url, (data) => {
                 resolve(data as ArrayBuffer);