Browse Source

better POT functions

David Catuhe 8 years ago
parent
commit
273078d6c1

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


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


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


File diff suppressed because it is too large
+ 5370 - 5343
dist/preview release/babylon.module.d.ts


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


+ 6 - 1
dist/preview release/canvas2D/babylon.canvas2d.js

@@ -9429,6 +9429,11 @@ var BABYLON;
                         setSize = true;
                     }
                 }
+                else {
+                    //this prevents the prim from flying off the screen when margin is not set (bug #1929)
+                    this._marginOffset.x = 0;
+                    this._marginOffset.y = 0;
+                }
                 if (!hasH) {
                     // If the Horizontal size is Auto, we have to compute it from its content and padding
                     if (isHSizeAuto) {
@@ -18222,7 +18227,7 @@ var BABYLON;
                 this._setLayoutDirty();
             }
             // If the canvas fit the rendering size and it changed, update
-            if (renderingSizeChanged && this._fitRenderingDevice) {
+            if (!this._designSize && renderingSizeChanged && this._fitRenderingDevice) {
                 this.size = this._renderingSize.clone();
                 if (this._background) {
                     this._background.size = this.size;

File diff suppressed because it is too large
+ 3 - 3
dist/preview release/canvas2D/babylon.canvas2d.min.js


File diff suppressed because it is too large
+ 6517 - 6491
dist/preview release/customConfigurations/minimalViewer/babylon.d.ts


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


+ 83 - 11
dist/preview release/customConfigurations/minimalViewer/babylon.max.js

@@ -5225,14 +5225,59 @@ var BABYLON;
             } while (count < value);
             return count === value;
         };
-        Tools.GetExponentOfTwo = function (value, max) {
-            var count = 1;
-            do {
-                count *= 2;
-            } while (count < value);
-            if (count > max)
-                count = max;
-            return count;
+        /**
+         * Find the next highest power of two.
+         * @param x Number to start search from.
+         * @return Next highest power of two.
+         */
+        Tools.CeilingPOT = function (x) {
+            x--;
+            x |= x >> 1;
+            x |= x >> 2;
+            x |= x >> 4;
+            x |= x >> 8;
+            x |= x >> 16;
+            x++;
+            return x;
+        };
+        /**
+         * Find the next lowest power of two.
+         * @param x Number to start search from.
+         * @return Next lowest power of two.
+         */
+        Tools.FloorPOT = function (x) {
+            x = x | (x >> 1);
+            x = x | (x >> 2);
+            x = x | (x >> 4);
+            x = x | (x >> 8);
+            x = x | (x >> 16);
+            return x - (x >> 1);
+        };
+        /**
+         * Find the nearest power of two.
+         * @param x Number to start search from.
+         * @return Next nearest power of two.
+         */
+        Tools.NearestPOT = function (x) {
+            var c = Tools.CeilingPOT(x);
+            var f = Tools.FloorPOT(x);
+            return (c - x) > (x - f) ? f : c;
+        };
+        Tools.GetExponentOfTwo = function (value, max, mode) {
+            if (mode === void 0) { mode = BABYLON.Engine.SCALEMODE_NEAREST; }
+            var pot;
+            switch (mode) {
+                case BABYLON.Engine.SCALEMODE_FLOOR:
+                    pot = Tools.FloorPOT(value);
+                    break;
+                case BABYLON.Engine.SCALEMODE_NEAREST:
+                    pot = Tools.NearestPOT(value);
+                    break;
+                case BABYLON.Engine.SCALEMODE_CEILING:
+                    pot = Tools.CeilingPOT(value);
+                    break;
+            }
+            return Math.min(pot, max);
         };
         Tools.GetFilename = function (path) {
             var index = path.lastIndexOf("/");
@@ -7694,6 +7739,27 @@ var BABYLON;
             enumerable: true,
             configurable: true
         });
+        Object.defineProperty(Engine, "SCALEMODE_FLOOR", {
+            get: function () {
+                return Engine._SCALEMODE_FLOOR;
+            },
+            enumerable: true,
+            configurable: true
+        });
+        Object.defineProperty(Engine, "SCALEMODE_NEAREST", {
+            get: function () {
+                return Engine._SCALEMODE_NEAREST;
+            },
+            enumerable: true,
+            configurable: true
+        });
+        Object.defineProperty(Engine, "SCALEMODE_CEILING", {
+            get: function () {
+                return Engine._SCALEMODE_CEILING;
+            },
+            enumerable: true,
+            configurable: true
+        });
         Object.defineProperty(Engine, "Version", {
             get: function () {
                 return "3.0-beta";
@@ -10415,6 +10481,10 @@ var BABYLON;
     Engine._INVERT = 0x150A;
     Engine._INCR_WRAP = 0x8507;
     Engine._DECR_WRAP = 0x8508;
+    // Texture rescaling mode
+    Engine._SCALEMODE_FLOOR = 1;
+    Engine._SCALEMODE_NEAREST = 2;
+    Engine._SCALEMODE_CEILING = 3;
     // Updatable statics so stick with vars here
     Engine.CollisionsEpsilon = 0.001;
     Engine.CodeRepository = "src/";
@@ -40808,6 +40878,8 @@ var BABYLON;
                 Can only be used on a single postprocess or on the last one of a chain.
             */
             this.enablePixelPerfectMode = false;
+            this.scaleMode = BABYLON.Engine.SCALEMODE_FLOOR;
+            this.alwaysForcePOT = false;
             this.samples = 1;
             this._reusable = false;
             this._textures = new BABYLON.SmartArray(2);
@@ -40962,12 +41034,12 @@ var BABYLON;
                 var requiredHeight = ((sourceTexture ? sourceTexture._height : this._engine.getRenderingCanvas().height) * this._options) | 0;
                 var desiredWidth = this._options.width || requiredWidth;
                 var desiredHeight = this._options.height || requiredHeight;
-                if (this.renderTargetSamplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE) {
+                if (this.renderTargetSamplingMode === BABYLON.Texture.TRILINEAR_SAMPLINGMODE || this.alwaysForcePOT) {
                     if (!this._options.width) {
-                        desiredWidth = BABYLON.Tools.GetExponentOfTwo(desiredWidth, maxSize);
+                        desiredWidth = BABYLON.Tools.GetExponentOfTwo(desiredWidth, maxSize, this.scaleMode);
                     }
                     if (!this._options.height) {
-                        desiredHeight = BABYLON.Tools.GetExponentOfTwo(desiredHeight, maxSize);
+                        desiredHeight = BABYLON.Tools.GetExponentOfTwo(desiredHeight, maxSize, this.scaleMode);
                     }
                 }
                 if (this.width !== desiredWidth || this.height !== desiredHeight) {

File diff suppressed because it is too large
+ 6517 - 6491
dist/preview release/customConfigurations/minimalViewer/babylon.module.d.ts


+ 7 - 5
src/PostProcess/babylon.postProcess.ts

@@ -17,6 +17,8 @@
         */ 
         public enablePixelPerfectMode = false;
 
+        public scaleMode = Engine.SCALEMODE_FLOOR;
+        public alwaysForcePOT = false;
         public samples = 1;
 
         private _camera: Camera;
@@ -204,13 +206,13 @@
                 var desiredWidth = (<PostProcessOptions>this._options).width || requiredWidth;
                 var desiredHeight = (<PostProcessOptions>this._options).height || requiredHeight;
 
-                if (this.renderTargetSamplingMode === Texture.TRILINEAR_SAMPLINGMODE) {
+                if (this.renderTargetSamplingMode === Texture.TRILINEAR_SAMPLINGMODE || this.alwaysForcePOT) {
                     if (!(<PostProcessOptions>this._options).width) {
-                        desiredWidth = Tools.GetExponentOfTwo(desiredWidth, maxSize);
+                        desiredWidth = Tools.GetExponentOfTwo(desiredWidth, maxSize, this.scaleMode);
                     }
 
                     if (!(<PostProcessOptions>this._options).height) {
-                        desiredHeight = Tools.GetExponentOfTwo(desiredHeight, maxSize);
+                        desiredHeight = Tools.GetExponentOfTwo(desiredHeight, maxSize, this.scaleMode);
                     }
                 }
 
@@ -351,6 +353,6 @@
             this.onApplyObservable.clear();
             this.onBeforeRenderObservable.clear();
             this.onSizeChangedObservable.clear();
-        }
+        }          
     }
-}
+}

+ 55 - 9
src/Tools/babylon.tools.ts

@@ -74,17 +74,63 @@
             return count === value;
         }
 
-        public static GetExponentOfTwo(value: number, max: number): number {
-            var count = 1;
-
-            do {
-                count *= 2;
-            } while (count < value);
+		/**
+		 * Find the next highest power of two.
+		 * @param x Number to start search from.
+		 * @return Next highest power of two.
+		 */
+		public static CeilingPOT(x: number): number {
+			x--;
+			x |= x >> 1;
+			x |= x >> 2;
+			x |= x >> 4;
+			x |= x >> 8;
+			x |= x >> 16;
+			x++;
+			return x;
+		}
 
-            if (count > max)
-                count = max;
+		/**
+		 * Find the next lowest power of two.
+		 * @param x Number to start search from.
+		 * @return Next lowest power of two.
+		 */
+		public static FloorPOT(x: number): number {
+			x = x | (x >> 1);
+			x = x | (x >> 2);
+			x = x | (x >> 4);
+			x = x | (x >> 8);
+			x = x | (x >> 16);
+			return x - (x >> 1);
+		}
 
-            return count;
+		/**
+		 * Find the nearest power of two.
+		 * @param x Number to start search from.
+		 * @return Next nearest power of two.
+		 */
+		public static NearestPOT(x: number): number {
+			var c = Tools.CeilingPOT(x);
+			var f = Tools.FloorPOT(x);
+			return (c - x) > (x - f) ? f : c;
+		}        
+
+        public static GetExponentOfTwo(value: number, max: number, mode = Engine.SCALEMODE_NEAREST): number {
+            let pot;
+
+            switch (mode) {
+                case Engine.SCALEMODE_FLOOR:
+                    pot = Tools.FloorPOT(value);
+                break;
+                case Engine.SCALEMODE_NEAREST:
+                    pot = Tools.NearestPOT(value);
+                break;
+                case Engine.SCALEMODE_CEILING:
+                    pot = Tools.CeilingPOT(value);
+                break;
+            }
+
+            return  Math.min(pot, max);
         }
 
         public static GetFilename(path: string): string {

+ 18 - 0
src/babylon.engine.ts

@@ -439,6 +439,24 @@
             return Engine._TEXTURETYPE_HALF_FLOAT;
         }
 
+
+        // Texture rescaling mode
+        private static _SCALEMODE_FLOOR = 1;
+        private static _SCALEMODE_NEAREST = 2;
+        private static _SCALEMODE_CEILING = 3;
+
+        public static get SCALEMODE_FLOOR(): number {
+            return Engine._SCALEMODE_FLOOR;
+        }
+
+        public static get SCALEMODE_NEAREST(): number {
+            return Engine._SCALEMODE_NEAREST;
+        }      
+
+        public static get SCALEMODE_CEILING(): number {
+            return Engine._SCALEMODE_CEILING;
+        }           
+
         public static get Version(): string {
             return "3.0-beta";
         }