Browse Source

Merge pull request #967 from sebavan/PBRUpgrade

Pbr upgrade
David Catuhe 9 years ago
parent
commit
1447c0791a

File diff suppressed because it is too large
+ 32 - 22
dist/preview release/babylon.core.js


File diff suppressed because it is too large
+ 1380 - 1375
dist/preview release/babylon.d.ts


File diff suppressed because it is too large
+ 36 - 26
dist/preview release/babylon.js


File diff suppressed because it is too large
+ 5266 - 5234
dist/preview release/babylon.max.js


File diff suppressed because it is too large
+ 35 - 25
dist/preview release/babylon.noworker.js


File diff suppressed because it is too large
+ 1 - 1
materialsLibrary/dist/babylon.fireMaterial.js


File diff suppressed because it is too large
+ 1 - 1
materialsLibrary/dist/babylon.fireMaterial.min.js


File diff suppressed because it is too large
+ 1 - 1
materialsLibrary/dist/babylon.normalMaterial.js


File diff suppressed because it is too large
+ 1 - 1
materialsLibrary/dist/babylon.normalMaterial.min.js


File diff suppressed because it is too large
+ 33 - 15
materialsLibrary/dist/babylon.pbrMaterial.js


File diff suppressed because it is too large
+ 2 - 2
materialsLibrary/dist/babylon.pbrMaterial.min.js


File diff suppressed because it is too large
+ 1 - 1
materialsLibrary/dist/babylon.terrainMaterial.js


File diff suppressed because it is too large
+ 1 - 1
materialsLibrary/dist/babylon.terrainMaterial.min.js


+ 5 - 1
materialsLibrary/dist/dts/babylon.pbrMaterial.d.ts

@@ -55,6 +55,8 @@ declare module BABYLON {
         opacityFresnelParameters: FresnelParameters;
         emissiveFresnelParameters: FresnelParameters;
         useMicroSurfaceFromReflectivityMapAlpha: boolean;
+        useAutoMicroSurfaceFromReflectivityMap: boolean;
+        useScalarInLinearSpace: boolean;
         private _renderTargets;
         private _worldViewProjectionMatrix;
         private _globalAmbientColor;
@@ -70,12 +72,14 @@ declare module BABYLON {
         private _shouldUseAlphaFromAlbedoTexture();
         getAlphaTestTexture(): BaseTexture;
         private _checkCache(scene, mesh?, useInstances?);
+        private convertColorToLinearSpaceToRef(color, ref);
+        private static convertColorToLinearSpaceToRef(color, ref, useScalarInLinear);
         private static _scaledAlbedo;
         private static _scaledReflectivity;
         private static _scaledEmissive;
         private static _scaledReflection;
         private static _lightRadiuses;
-        static BindLights(scene: Scene, mesh: AbstractMesh, effect: Effect, defines: MaterialDefines): void;
+        static BindLights(scene: Scene, mesh: AbstractMesh, effect: Effect, defines: MaterialDefines, useScalarInLinearSpace: boolean): void;
         isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean;
         unbind(): void;
         bindOnlyWorldMatrix(world: Matrix): void;

+ 34 - 14
materialsLibrary/materials/pbr/babylon.pbrMaterial.ts

@@ -64,6 +64,7 @@ module BABYLON {
         public BonesPerMesh = 0;
         public INSTANCES = false;
         public MICROSURFACEFROMREFLECTIVITYMAP = false;
+        public MICROSURFACEAUTOMATIC = false;
         public EMISSIVEASILLUMINATION = false;
         public LINKEMISSIVEWITHALBEDO = false;
         public LIGHTMAP = false;
@@ -165,6 +166,8 @@ module BABYLON {
         public emissiveFresnelParameters: FresnelParameters;
 
         public useMicroSurfaceFromReflectivityMapAlpha = false;
+        public useAutoMicroSurfaceFromReflectivityMap = false;
+        public useScalarInLinearSpace = false;
 
         private _renderTargets = new SmartArray<RenderTargetTexture>(16);
         private _worldViewProjectionMatrix = Matrix.Zero();
@@ -243,13 +246,23 @@ module BABYLON {
             return false;
         }
       
+        private convertColorToLinearSpaceToRef (color: Color3, ref: Color3): void {
+            PBRMaterial.convertColorToLinearSpaceToRef(color, ref, this.useScalarInLinearSpace);
+        }
+        
+        private static convertColorToLinearSpaceToRef (color: Color3, ref: Color3, useScalarInLinear: boolean): void {
+            if (!useScalarInLinear) {
+                color.toLinearSpaceToRef(ref);
+            }
+        }
+        
         private static _scaledAlbedo = new Color3();
         private static _scaledReflectivity = new Color3();
         private static _scaledEmissive = new Color3();
         private static _scaledReflection = new Color3();
         private static _lightRadiuses = [1, 1, 1, 1];
 
-        public static BindLights(scene: Scene, mesh: AbstractMesh, effect: Effect, defines: MaterialDefines) {
+        public static BindLights(scene: Scene, mesh: AbstractMesh, effect: Effect, defines: MaterialDefines, useScalarInLinearSpace: boolean) {
             var lightIndex = 0;
             var depthValuesAlreadySet = false;
             for (var index = 0; index < scene.lights.length; index++) {
@@ -280,13 +293,13 @@ module BABYLON {
                 }
 
                 // GAMMA CORRECTION.
-                light.diffuse.toLinearSpaceToRef(PBRMaterial._scaledAlbedo);
+                this.convertColorToLinearSpaceToRef(light.diffuse, PBRMaterial._scaledAlbedo, useScalarInLinearSpace);
                 
                 PBRMaterial._scaledAlbedo.scaleToRef(light.intensity, PBRMaterial._scaledAlbedo);
                 effect.setColor4("vLightDiffuse" + lightIndex, PBRMaterial._scaledAlbedo, light.range);
                 
                 if (defines["SPECULARTERM"]) {
-                    light.specular.toLinearSpaceToRef(PBRMaterial._scaledReflectivity);
+                    this.convertColorToLinearSpaceToRef(light.specular, PBRMaterial._scaledReflectivity, useScalarInLinearSpace);
                     
                     PBRMaterial._scaledReflectivity.scaleToRef(light.intensity, PBRMaterial._scaledReflectivity);
                     effect.setColor3("vLightSpecular" + lightIndex, PBRMaterial._scaledReflectivity);
@@ -409,7 +422,7 @@ module BABYLON {
                                     break;
                             }
 
-                            if (this.reflectionTexture instanceof HDRCubeTexture) {
+                            if (this.reflectionTexture instanceof HDRCubeTexture && (<HDRCubeTexture>this.reflectionTexture)) {
                                 this._defines.USESPHERICALFROMREFLECTIONMAP = true;
                                 needNormals = true;
                             }
@@ -442,6 +455,7 @@ module BABYLON {
                             needUVs = true;
                             this._defines.REFLECTIVITY = true;
                             this._defines.MICROSURFACEFROMREFLECTIVITYMAP = this.useMicroSurfaceFromReflectivityMapAlpha;
+                            this._defines.MICROSURFACEAUTOMATIC = this.useAutoMicroSurfaceFromReflectivityMap;
                         }
                     }
                 }
@@ -892,29 +906,29 @@ module BABYLON {
                 this._myScene.ambientColor.multiplyToRef(this.ambientColor, this._globalAmbientColor);
                 
                 // GAMMA CORRECTION.
-                this.reflectivityColor.toLinearSpaceToRef(PBRMaterial._scaledReflectivity);
+                this.convertColorToLinearSpaceToRef(this.reflectivityColor, PBRMaterial._scaledReflectivity);
 
                 this._effect.setVector3("vEyePosition", this._myScene._mirroredCameraPosition ? this._myScene._mirroredCameraPosition : this._myScene.activeCamera.position);
                 this._effect.setColor3("vAmbientColor", this._globalAmbientColor);
                 this._effect.setColor4("vReflectivityColor", PBRMaterial._scaledReflectivity, this.microSurface);
 
                 // GAMMA CORRECTION.
-                this.emissiveColor.toLinearSpaceToRef(PBRMaterial._scaledEmissive);
+                this.convertColorToLinearSpaceToRef(this.emissiveColor,PBRMaterial._scaledEmissive);
                 this._effect.setColor3("vEmissiveColor", PBRMaterial._scaledEmissive);
 
                 // GAMMA CORRECTION.
-                this.reflectionColor.toLinearSpaceToRef(PBRMaterial._scaledReflection);
+                this.convertColorToLinearSpaceToRef(this.reflectionColor, PBRMaterial._scaledReflection);
                 this._effect.setColor3("vReflectionColor", PBRMaterial._scaledReflection);
             }
 
             if (this._myScene.getCachedMaterial() !== this || !this.isFrozen) {
                 // GAMMA CORRECTION.
-                this.albedoColor.toLinearSpaceToRef(PBRMaterial._scaledAlbedo);
+                this.convertColorToLinearSpaceToRef(this.albedoColor, PBRMaterial._scaledAlbedo);
                 this._effect.setColor4("vAlbedoColor", PBRMaterial._scaledAlbedo, this.alpha * mesh.visibility);
 
                 // Lights
                 if (this._myScene.lightsEnabled && !this.disableLighting) {
-                    PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines);
+                    PBRMaterial.BindLights(this._myScene, mesh, this._effect, this._defines, this.useScalarInLinearSpace);
                 }
 
                 // View
@@ -946,15 +960,15 @@ module BABYLON {
                 this._overloadedIntensity.w = this.overloadedEmissiveIntensity;
                 this._effect.setVector4("vOverloadedIntensity", this._overloadedIntensity);
 
-                this.overloadedAmbient.toLinearSpaceToRef(this._tempColor);
+                this.convertColorToLinearSpaceToRef(this.overloadedAmbient,this._tempColor);
                 this._effect.setColor3("vOverloadedAmbient", this._tempColor);
-                this.overloadedAlbedo.toLinearSpaceToRef(this._tempColor);
+                this.convertColorToLinearSpaceToRef(this.overloadedAlbedo, this._tempColor);
                 this._effect.setColor3("vOverloadedAlbedo", this._tempColor);
-                this.overloadedReflectivity.toLinearSpaceToRef(this._tempColor);
+                this.convertColorToLinearSpaceToRef(this.overloadedReflectivity, this._tempColor);
                 this._effect.setColor3("vOverloadedReflectivity", this._tempColor);
-                this.overloadedEmissive.toLinearSpaceToRef(this._tempColor);
+                this.convertColorToLinearSpaceToRef(this.overloadedEmissive, this._tempColor);
                 this._effect.setColor3("vOverloadedEmissive", this._tempColor);
-                this.overloadedReflection.toLinearSpaceToRef(this._tempColor);
+                this.convertColorToLinearSpaceToRef(this.overloadedReflection, this._tempColor);
                 this._effect.setColor3("vOverloadedReflection", this._tempColor);
 
                 this._overloadedMicroSurface.x = this.overloadedMicroSurface;
@@ -1127,6 +1141,8 @@ module BABYLON {
             newPBRMaterial.useAlphaFromAlbedoTexture = this.useAlphaFromAlbedoTexture;
             newPBRMaterial.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
             newPBRMaterial.useMicroSurfaceFromReflectivityMapAlpha = this.useMicroSurfaceFromReflectivityMapAlpha;
+            newPBRMaterial.useAutoMicroSurfaceFromReflectivityMap = this.useAutoMicroSurfaceFromReflectivityMap;
+            newPBRMaterial.useScalarInLinearSpace = this.useScalarInLinearSpace;
             newPBRMaterial.useSpecularOverAlpha = this.useSpecularOverAlpha;
             newPBRMaterial.indexOfRefraction = this.indexOfRefraction;
             newPBRMaterial.invertRefractionY = this.invertRefractionY;
@@ -1209,6 +1225,8 @@ module BABYLON {
             serializationObject.useAlphaFromAlbedoTexture = this.useAlphaFromAlbedoTexture;
             serializationObject.useEmissiveAsIllumination = this.useEmissiveAsIllumination;
             serializationObject.useMicroSurfaceFromReflectivityMapAlpha = this.useMicroSurfaceFromReflectivityMapAlpha;
+            serializationObject.useAutoMicroSurfaceFromReflectivityMap = this.useAutoMicroSurfaceFromReflectivityMap;
+            serializationObject.useScalarInLinear = this.useScalarInLinearSpace;
             serializationObject.useSpecularOverAlpha = this.useSpecularOverAlpha;
             serializationObject.indexOfRefraction = this.indexOfRefraction;
             serializationObject.invertRefractionY = this.invertRefractionY;
@@ -1304,6 +1322,8 @@ module BABYLON {
             material.useAlphaFromAlbedoTexture = source.useAlphaFromAlbedoTexture;
             material.useEmissiveAsIllumination = source.useEmissiveAsIllumination;
             material.useMicroSurfaceFromReflectivityMapAlpha = source.useMicroSurfaceFromReflectivityMapAlpha;
+            material.useAutoMicroSurfaceFromReflectivityMap = source.useAutoMicroSurfaceFromReflectivityMap;
+            material.useScalarInLinearSpace = source.useScalarInLinear;
             material.useSpecularOverAlpha = source.useSpecularOverAlpha;
             material.indexOfRefraction = source.indexOfRefraction;
             material.invertRefractionY = source.invertRefractionY;

+ 3 - 1
materialsLibrary/materials/pbr/pbr.fragment.fx

@@ -890,7 +890,9 @@ void main(void) {
         #ifdef MICROSURFACEFROMREFLECTIVITYMAP
             microSurface = surfaceReflectivityColorMap.a;
         #else
-            microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
+            #ifdef MICROSURFACEAUTOMATIC
+                microSurface = computeDefaultMicroSurface(microSurface, surfaceReflectivityColor);
+            #endif
         #endif
     #endif
 

+ 18 - 1
materialsLibrary/test/index.html

@@ -216,6 +216,15 @@
 					currentMesh.isVisible = true;
 					fur.resetFur();
 					
+                    options.lightRange = 1000;
+                    hemisphericLight.range = 1000;
+                    options.lightRange = 1000;
+                    directionalLight.range = 1000;
+                    options.lightRange = 1000;
+                    pointLight.range = 1000;
+                    options.lightRange = 1000;
+                    spotLight.range = 1000;
+                    
 					switch (options.material) {
 						case "simple":
 							currentMaterial = simple;
@@ -239,6 +248,14 @@
 							break;
 						case "pbr":
 							currentMaterial = pbr;
+                            options.lightRange = 1;
+                            hemisphericLight.range = 1;
+                            options.lightRange = 1;
+                            directionalLight.range = 1;
+                            options.lightRange = 1;
+                            pointLight.range = 1;
+                            options.lightRange = 1;
+                            spotLight.range = 1;
 							break;
 						case "fur":
 							currentMaterial = fur.material;
@@ -312,7 +329,7 @@
                     directionalLight.range = options.lightRange;
                     pointLight.range = options.lightRange;
                     spotLight.range = options.lightRange;
-                });
+                }).listen();
                 
                 f1.add(options, 'lightRadius').onChange(function() {
                     hemisphericLight.radius = options.lightRadius;

File diff suppressed because it is too large
+ 5266 - 5234
materialsLibrary/test/refs/babylon.max.js


+ 56 - 24
src/Materials/Textures/babylon.hdrCubeTexture.js

@@ -7,15 +7,21 @@ var BABYLON;
 (function (BABYLON) {
     var HDRCubeTexture = (function (_super) {
         __extends(HDRCubeTexture, _super);
-        function HDRCubeTexture(url, scene, size, noMipmap) {
+        function HDRCubeTexture(url, scene, size, noMipmap, generateHarmonics, useInGammaSpace) {
+            if (noMipmap === void 0) { noMipmap = false; }
+            if (generateHarmonics === void 0) { generateHarmonics = true; }
+            if (useInGammaSpace === void 0) { useInGammaSpace = false; }
             _super.call(this, scene);
             this.coordinatesMode = BABYLON.Texture.CUBIC_MODE;
+            this._useInGammaSpace = false;
+            this._generateHarmonics = true;
             this.sphericalPolynomial = null;
             this.name = url;
             this.url = url;
             this._noMipmap = noMipmap;
             this.hasAlpha = false;
             this._size = size;
+            this._useInGammaSpace = useInGammaSpace;
             if (!url) {
                 return;
             }
@@ -34,30 +40,46 @@ var BABYLON;
         HDRCubeTexture.prototype.loadTexture = function () {
             var _this = this;
             var callback = function (buffer) {
+                // Extract the raw linear data.
                 var data = BABYLON.Internals.HDRTools.GetCubeMapTextureData(buffer, _this._size);
-                _this.sphericalPolynomial = BABYLON.Internals.CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial(data);
-                var mapping = [
-                    "left",
-                    "down",
-                    "front",
-                    "right",
-                    "up",
-                    "back"
-                ];
+                // Generate harmonics if needed.
+                if (_this._generateHarmonics) {
+                    _this.sphericalPolynomial = BABYLON.Internals.CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial(data);
+                }
                 var results = [];
+                var byteArray = null;
+                // Create uintarray fallback.
+                if (!_this.getScene().getEngine().getCaps().textureFloat) {
+                    // 3 channels of 1 bytes per pixel in bytes.
+                    var byteBuffer = new ArrayBuffer(_this._size * _this._size * 3);
+                    byteArray = new Uint8Array(byteBuffer);
+                }
+                // Push each faces.
                 for (var j = 0; j < 6; j++) {
-                    var dataFace = data[mapping[j]];
-                    // TODO. Support Int Textures...
-                    //                     // 3 channels of 1 bytes per pixel in bytes.
-                    //                     var byteBuffer = new ArrayBuffer(this._size * this._size * 3);
-                    //                     var byteArray = new Uint8Array(byteBuffer);
-                    // 
-                    //                     /* now convert data from buffer into bytes */
-                    //                     for(var i = 0; i < this._size * this._size; i++) {
-                    //                         byteArray[(i * 3) + 0] = dataFace[(i * 3) + 0] * 255;
-                    //                         byteArray[(i * 3) + 1] = dataFace[(i * 3) + 1] * 255;
-                    //                         byteArray[(i * 3) + 2] = dataFace[(i * 3) + 2] * 255;
-                    //                     }
+                    var dataFace = data[HDRCubeTexture._facesMapping[j]];
+                    // If special cases.
+                    if (_this._useInGammaSpace || byteArray) {
+                        for (var i = 0; i < _this._size * _this._size; i++) {
+                            // Put in gamma space if requested.
+                            if (_this._useInGammaSpace) {
+                                dataFace[(i * 3) + 0] = Math.pow(dataFace[(i * 3) + 0], BABYLON.ToGammaSpace);
+                                dataFace[(i * 3) + 1] = Math.pow(dataFace[(i * 3) + 1], BABYLON.ToGammaSpace);
+                                dataFace[(i * 3) + 2] = Math.pow(dataFace[(i * 3) + 2], BABYLON.ToGammaSpace);
+                            }
+                            // Convert to int texture for fallback.
+                            if (byteArray) {
+                                // R
+                                byteArray[(i * 3) + 0] = dataFace[(i * 3) + 0] * 255;
+                                byteArray[(i * 3) + 0] = Math.min(255, byteArray[(i * 3) + 0]);
+                                // G
+                                byteArray[(i * 3) + 1] = dataFace[(i * 3) + 1] * 255;
+                                byteArray[(i * 3) + 1] = Math.min(255, byteArray[(i * 3) + 1]);
+                                // B
+                                byteArray[(i * 3) + 2] = dataFace[(i * 3) + 2] * 255;
+                                byteArray[(i * 3) + 2] = Math.min(255, byteArray[(i * 3) + 2]);
+                            }
+                        }
+                    }
                     results.push(dataFace);
                 }
                 return results;
@@ -65,7 +87,7 @@ var BABYLON;
             this._texture = this.getScene().getEngine().createRawCubeTexture(this.url, this.getScene(), this._size, BABYLON.Engine.TEXTUREFORMAT_RGB, BABYLON.Engine.TEXTURETYPE_FLOAT, this._noMipmap, callback);
         };
         HDRCubeTexture.prototype.clone = function () {
-            var newTexture = new HDRCubeTexture(this.url, this.getScene(), this._size, this._noMipmap);
+            var newTexture = new HDRCubeTexture(this.url, this.getScene(), this._size, this._noMipmap, this._generateHarmonics, this._useInGammaSpace);
             // Base texture
             newTexture.level = this.level;
             newTexture.wrapU = this.wrapU;
@@ -91,7 +113,7 @@ var BABYLON;
         HDRCubeTexture.Parse = function (parsedTexture, scene, rootUrl) {
             var texture = null;
             if (parsedTexture.name && !parsedTexture.isRenderTarget) {
-                texture = new BABYLON.HDRCubeTexture(rootUrl + parsedTexture.name, scene, parsedTexture.size);
+                texture = new BABYLON.HDRCubeTexture(rootUrl + parsedTexture.name, scene, parsedTexture.size, texture.generateHarmonics, texture.useInGammaSpace);
                 texture.name = parsedTexture.name;
                 texture.hasAlpha = parsedTexture.hasAlpha;
                 texture.level = parsedTexture.level;
@@ -110,8 +132,18 @@ var BABYLON;
             serializationObject.level = this.level;
             serializationObject.size = this._size;
             serializationObject.coordinatesMode = this.coordinatesMode;
+            serializationObject.useInGammaSpace = this._useInGammaSpace;
+            serializationObject.generateHarmonics = this._generateHarmonics;
             return serializationObject;
         };
+        HDRCubeTexture._facesMapping = [
+            "left",
+            "down",
+            "front",
+            "right",
+            "up",
+            "back"
+        ];
         return HDRCubeTexture;
     })(BABYLON.BaseTexture);
     BABYLON.HDRCubeTexture = HDRCubeTexture;

+ 66 - 29
src/Materials/Textures/babylon.hdrcubetexture.ts

@@ -2,15 +2,26 @@ module BABYLON {
     export class HDRCubeTexture extends BaseTexture {
         public url: string;
         public coordinatesMode = Texture.CUBIC_MODE;
-
+        
+        private _useInGammaSpace = false;
+        private _generateHarmonics = true;
         private _noMipmap: boolean;
         private _extensions: string[];
         private _textureMatrix: Matrix;
         private _size: number;
+        
+        private static _facesMapping = [
+            "left",
+            "down",
+            "front",
+            "right",
+            "up",
+            "back"
+        ]; 
 
         public sphericalPolynomial: SphericalPolynomial = null;
-
-        constructor(url: string, scene: Scene, size:number, noMipmap?: boolean) {
+        
+        constructor(url: string, scene: Scene, size:number, noMipmap = false, generateHarmonics = true, useInGammaSpace = false) {
             super(scene);
 
             this.name = url;
@@ -18,6 +29,7 @@ module BABYLON {
             this._noMipmap = noMipmap;
             this.hasAlpha = false;
             this._size = size;
+            this._useInGammaSpace = useInGammaSpace;
 
             if (!url) {
                 return;
@@ -40,32 +52,53 @@ module BABYLON {
         
         private loadTexture() {
             var callback = (buffer: ArrayBuffer) => {
+                // Extract the raw linear data.
                 var data = BABYLON.Internals.HDRTools.GetCubeMapTextureData(buffer, this._size);
-                this.sphericalPolynomial = BABYLON.Internals.CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial(data);
-
-                var mapping = [
-                    "left",
-                    "down",
-                    "front",
-                    "right",
-                    "up",
-                    "back"
-                ];
-
+                
+                // Generate harmonics if needed.
+                if (this._generateHarmonics) {
+                    this.sphericalPolynomial = BABYLON.Internals.CubeMapToSphericalPolynomialTools.ConvertCubeMapToSphericalPolynomial(data);
+                }
+                
                 var results = [];
+                var byteArray: Uint8Array = null;
+                
+                // Create uintarray fallback.
+                if (!this.getScene().getEngine().getCaps().textureFloat) {
+                    // 3 channels of 1 bytes per pixel in bytes.
+                    var byteBuffer = new ArrayBuffer(this._size * this._size * 3);
+                    byteArray = new Uint8Array(byteBuffer);
+                }
+                
+                // Push each faces.
                 for (var j = 0; j < 6; j++) {
-                    var dataFace = <Float32Array>data[mapping[j]];
-// TODO. Support Int Textures...
-//                     // 3 channels of 1 bytes per pixel in bytes.
-//                     var byteBuffer = new ArrayBuffer(this._size * this._size * 3);
-//                     var byteArray = new Uint8Array(byteBuffer);
-// 
-//                     /* now convert data from buffer into bytes */
-//                     for(var i = 0; i < this._size * this._size; i++) {
-//                         byteArray[(i * 3) + 0] = dataFace[(i * 3) + 0] * 255;
-//                         byteArray[(i * 3) + 1] = dataFace[(i * 3) + 1] * 255;
-//                         byteArray[(i * 3) + 2] = dataFace[(i * 3) + 2] * 255;
-//                     }
+                    var dataFace = <Float32Array>data[HDRCubeTexture._facesMapping[j]];
+                    
+                    // If special cases.
+                    if (this._useInGammaSpace || byteArray) {
+                        for(var i = 0; i < this._size * this._size; i++) {
+                             
+                             // Put in gamma space if requested.
+                             if (this._useInGammaSpace) {
+                                dataFace[(i * 3) + 0] = Math.pow(dataFace[(i * 3) + 0], BABYLON.ToGammaSpace);
+                                dataFace[(i * 3) + 1] = Math.pow(dataFace[(i * 3) + 1], BABYLON.ToGammaSpace);
+                                dataFace[(i * 3) + 2] = Math.pow(dataFace[(i * 3) + 2], BABYLON.ToGammaSpace);
+                             }
+                             
+                             // Convert to int texture for fallback.
+                             if (byteArray) {
+                                 // R
+                                 byteArray[(i * 3) + 0] = dataFace[(i * 3) + 0] * 255;
+                                 byteArray[(i * 3) + 0] = Math.min(255, byteArray[(i * 3) + 0]);
+                                 // G
+                                 byteArray[(i * 3) + 1] = dataFace[(i * 3) + 1] * 255;
+                                 byteArray[(i * 3) + 1] = Math.min(255, byteArray[(i * 3) + 1]);
+                                 // B
+                                 byteArray[(i * 3) + 2] = dataFace[(i * 3) + 2] * 255;
+                                 byteArray[(i * 3) + 2] = Math.min(255, byteArray[(i * 3) + 2]);
+                             }
+                         }
+                    }
 
                     results.push(dataFace);
                 }
@@ -76,7 +109,8 @@ module BABYLON {
         }
 
         public clone(): HDRCubeTexture {
-            var newTexture = new HDRCubeTexture(this.url, this.getScene(), this._size, this._noMipmap);
+            var newTexture = new HDRCubeTexture(this.url, this.getScene(), this._size, this._noMipmap,
+                this._generateHarmonics, this._useInGammaSpace);
 
             // Base texture
             newTexture.level = this.level;
@@ -84,7 +118,7 @@ module BABYLON {
             newTexture.wrapV = this.wrapV;
             newTexture.coordinatesIndex = this.coordinatesIndex;
             newTexture.coordinatesMode = this.coordinatesMode;
-
+            
             return newTexture;
         }
 
@@ -109,7 +143,8 @@ module BABYLON {
         public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): HDRCubeTexture {
             var texture = null;
             if (parsedTexture.name && !parsedTexture.isRenderTarget) {
-                texture = new BABYLON.HDRCubeTexture(rootUrl + parsedTexture.name, scene, parsedTexture.size);
+                texture = new BABYLON.HDRCubeTexture(rootUrl + parsedTexture.name, scene, parsedTexture.size,
+                    texture.generateHarmonics, texture.useInGammaSpace);
                 texture.name = parsedTexture.name;
                 texture.hasAlpha = parsedTexture.hasAlpha;
                 texture.level = parsedTexture.level;
@@ -130,6 +165,8 @@ module BABYLON {
             serializationObject.level = this.level;
             serializationObject.size = this._size;
             serializationObject.coordinatesMode = this.coordinatesMode;
+            serializationObject.useInGammaSpace = this._useInGammaSpace;
+            serializationObject.generateHarmonics = this._generateHarmonics;
 
             return serializationObject;
         }

+ 8 - 8
src/Math/babylon.math.js

@@ -1,7 +1,7 @@
 var BABYLON;
 (function (BABYLON) {
-    var ToGammaSpace = 1 / 2.2;
-    var ToLinearSpace = 2.2;
+    BABYLON.ToGammaSpace = 1 / 2.2;
+    BABYLON.ToLinearSpace = 2.2;
     var Color3 = (function () {
         function Color3(r, g, b) {
             if (r === void 0) { r = 0; }
@@ -105,9 +105,9 @@ var BABYLON;
             return convertedColor;
         };
         Color3.prototype.toLinearSpaceToRef = function (convertedColor) {
-            convertedColor.r = Math.pow(this.r, ToLinearSpace);
-            convertedColor.g = Math.pow(this.g, ToLinearSpace);
-            convertedColor.b = Math.pow(this.b, ToLinearSpace);
+            convertedColor.r = Math.pow(this.r, BABYLON.ToLinearSpace);
+            convertedColor.g = Math.pow(this.g, BABYLON.ToLinearSpace);
+            convertedColor.b = Math.pow(this.b, BABYLON.ToLinearSpace);
             return this;
         };
         Color3.prototype.toGammaSpace = function () {
@@ -116,9 +116,9 @@ var BABYLON;
             return convertedColor;
         };
         Color3.prototype.toGammaSpaceToRef = function (convertedColor) {
-            convertedColor.r = Math.pow(this.r, ToGammaSpace);
-            convertedColor.g = Math.pow(this.g, ToGammaSpace);
-            convertedColor.b = Math.pow(this.b, ToGammaSpace);
+            convertedColor.r = Math.pow(this.r, BABYLON.ToGammaSpace);
+            convertedColor.g = Math.pow(this.g, BABYLON.ToGammaSpace);
+            convertedColor.b = Math.pow(this.b, BABYLON.ToGammaSpace);
             return this;
         };
         // Statics

+ 2 - 2
src/Math/babylon.math.ts

@@ -2,8 +2,8 @@
 
     declare var SIMD;
 
-    const ToGammaSpace = 1 / 2.2;
-    const ToLinearSpace = 2.2;
+    export const ToGammaSpace = 1 / 2.2;
+    export const ToLinearSpace = 2.2;
 
     export class Color3 {
         constructor(public r: number = 0, public g: number = 0, public b: number = 0) {