Преглед на файлове

generate sphericalPolynomial prior to converting to texture

Trevor Baron преди 7 години
родител
ревизия
65d8ab9c36
променени са 2 файла, в които са добавени 45 реда и са изтрити 5 реда
  1. 10 4
      src/Engine/babylon.engine.ts
  2. 35 1
      src/Tools/babylon.dds.ts

+ 10 - 4
src/Engine/babylon.engine.ts

@@ -5349,6 +5349,9 @@
                 }
 
                 let texture = loadData.texture as InternalTexture;
+                if(loadData.info.sphericalPolynomial){
+                    texture._sphericalPolynomial = loadData.info.sphericalPolynomial;
+                }
                 texture._dataSource = InternalTexture.DATASOURCE_CUBEPREFILTERED;
                 texture._lodGenerationScale = scale;
                 texture._lodGenerationOffset = offset;
@@ -5421,7 +5424,7 @@
                 }
             };
 
-            return this.createCubeTexture(rootUrl, scene, null, false, callback, onError, format, forcedExtension);
+            return this.createCubeTexture(rootUrl, scene, null, false, callback, onError, format, forcedExtension, true);
         }
 
         /**
@@ -5436,7 +5439,7 @@
          * @param forcedExtension defines the extension to use to pick the right loader
          * @returns the cube texture as an InternalTexture
          */
-        public createCubeTexture(rootUrl: string, scene: Nullable<Scene>, files: Nullable<string[]>, noMipmap?: boolean, onLoad: Nullable<(data?: any) => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null, format?: number, forcedExtension: any = null): InternalTexture {
+        public createCubeTexture(rootUrl: string, scene: Nullable<Scene>, files: Nullable<string[]>, noMipmap?: boolean, onLoad: Nullable<(data?: any) => void> = null, onError: Nullable<(message?: string, exception?: any) => void> = null, format?: number, forcedExtension: any = null, createPolynomials = false): InternalTexture {
             var gl = this._gl;
 
             var texture = new InternalTexture(this, InternalTexture.DATASOURCE_CUBE);
@@ -5527,7 +5530,10 @@
                     this._loadFile(rootUrl,
                         data => {
                             var info = DDSTools.GetDDSInfo(data);
-
+                            if(createPolynomials){
+                                info.sphericalPolynomial = new SphericalPolynomial();
+                            }
+                            
                             var loadMipmap = (info.isRGB || info.isLuminance || info.mipmapCount > 1) && !noMipmap;
 
                             this._bindTextureDirectly(gl.TEXTURE_CUBE_MAP, texture, true);
@@ -5545,7 +5551,7 @@
                             texture.height = info.height;
                             texture.isReady = true;
                             texture.type = info.textureType;
-
+                            
                             if (onLoad) {
                                 onLoad({ isDDS: true, width: info.width, info, data, texture });
                             }

+ 35 - 1
src/Tools/babylon.dds.ts

@@ -97,6 +97,7 @@
         isCompressed: boolean;
         dxgiFormat: number;
         textureType: number;
+        sphericalPolynomial?: SphericalPolynomial;
     };
 
     export class DDSTools {
@@ -371,7 +372,11 @@
             return byteArray;
         }
 
-        public static UploadDDSLevels(engine: Engine, gl: WebGLRenderingContext, arrayBuffer: any, info: DDSInfo, loadMipmaps: boolean, faces: number, lodIndex = -1, currentFace?: number): void {
+        public static UploadDDSLevels(engine: Engine, gl: WebGLRenderingContext, arrayBuffer: any, info: DDSInfo, loadMipmaps: boolean, faces: number, lodIndex = -1, currentFace?: number) {
+            var sphericalPolynomialFaces:Nullable<Array<ArrayBufferView>> = null;
+            if(info.sphericalPolynomial){
+                sphericalPolynomialFaces = new Array<ArrayBufferView>();
+            }
             var ext = engine.getCaps().s3tc;
 
             var header = new Int32Array(arrayBuffer, 0, headerLengthInt);
@@ -482,9 +487,15 @@
                             if (engine._badOS || engine._badDesktopOS || (!engine.getCaps().textureHalfFloat && !engine.getCaps().textureFloat)) { // Required because iOS has many issues with float and half float generation
                                 if (bpp === 128) {
                                     floatArray = DDSTools._GetFloatAsUIntRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer, i);
+                                    if(sphericalPolynomialFaces && i == 0){
+                                        sphericalPolynomialFaces.push(DDSTools._GetFloatRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer, i));
+                                    }
                                 }
                                 else if (bpp === 64) {
                                     floatArray = DDSTools._GetHalfFloatAsUIntRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer, i);
+                                    if(sphericalPolynomialFaces && i == 0){
+                                        sphericalPolynomialFaces.push(DDSTools._GetHalfFloatAsFloatRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer, i));
+                                    }
                                 }
 
                                 info.textureType = Engine.TEXTURETYPE_UNSIGNED_INT;
@@ -494,14 +505,23 @@
                             else {
                                 if (bpp === 128) {
                                     floatArray = DDSTools._GetFloatRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer, i);
+                                    if(sphericalPolynomialFaces && i == 0){
+                                        sphericalPolynomialFaces.push(floatArray);
+                                    }
                                 } else if (bpp === 64 && !engine.getCaps().textureHalfFloat) {
                                     floatArray = DDSTools._GetHalfFloatAsFloatRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer, i);
+                                    if(sphericalPolynomialFaces && i == 0){
+                                        sphericalPolynomialFaces.push(floatArray);
+                                    }
 
                                     info.textureType = Engine.TEXTURETYPE_FLOAT;
                                     format = engine._getWebGLTextureType(info.textureType);
                                     internalFormat = engine._getRGBABufferInternalSizedFormat(info.textureType);
                                 } else { // 64
                                     floatArray = DDSTools._GetHalfFloatRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer, i);
+                                    if(sphericalPolynomialFaces && i == 0){
+                                        sphericalPolynomialFaces.push(DDSTools._GetHalfFloatAsFloatRGBAArrayBuffer(width, height, dataOffset, dataLength, arrayBuffer, i));
+                                    }
                                 }
                             }
 
@@ -545,6 +565,20 @@
                     break;
                 }
             }
+            if(sphericalPolynomialFaces){
+                info.sphericalPolynomial = CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial({
+                    size: header[off_width],
+                    right: sphericalPolynomialFaces[0],
+                    left: sphericalPolynomialFaces[1],
+                    up: sphericalPolynomialFaces[2],
+                    down: sphericalPolynomialFaces[3],
+                    front: sphericalPolynomialFaces[4],
+                    back: sphericalPolynomialFaces[5],
+                    format: Engine.TEXTUREFORMAT_RGBA,
+                    type: Engine.TEXTURETYPE_FLOAT,
+                    gammaSpace: false,
+                });
+            }
         }
     }
 }