David Catuhe 8 years ago
parent
commit
5bdeb7a065

+ 9 - 0
Tools/Gulp/config.json

@@ -1273,6 +1273,15 @@
                     "../../proceduralTexturesLibrary/src/normalMap/normalMapProceduralTexture.fragment.fx"
                 ],
                 "output": "babylon.normalMapProceduralTexture.js"
+            },
+            {
+                "files": [
+                    "../../proceduralTexturesLibrary/src/perlinNoise/babylon.perlinNoiseProceduralTexture.ts"
+                ],
+                "shaderFiles":[
+                    "../../proceduralTexturesLibrary/src/perlinNoise/perlinNoiseProceduralTexture.fragment.fx"
+                ],
+                "output": "babylon.perlinNoiseProceduralTexture.js"
             }
         ],
         "build": {

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


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


+ 24 - 15
dist/preview release/babylon.max.js

@@ -47581,7 +47581,7 @@ var BABYLON;
 var BABYLON;
 (function (BABYLON) {
     var Gamepads = (function () {
-        function Gamepads(ongamedpadconnected) {
+        function Gamepads(ongamedpadconnected, ongamedpaddisconnected) {
             var _this = this;
             this.babylonGamepads = [];
             this.oneGamepadConnected = false;
@@ -47590,6 +47590,7 @@ var BABYLON;
             this.gamepadSupport = (navigator.getGamepads ||
                 navigator.webkitGetGamepads || navigator.msGetGamepads || navigator.webkitGamepads);
             this._callbackGamepadConnected = ongamedpadconnected;
+            this._callbackGamepadDisconnected = ongamedpaddisconnected;
             if (this.gamepadSupport) {
                 //first add already-connected gamepads
                 this._updateGamepadObjects();
@@ -47602,7 +47603,7 @@ var BABYLON;
                         _this._onGamepadConnected(evt.gamepad);
                     };
                     this._onGamepadDisonnectedEvent = function (evt) {
-                        _this._onGamepadDisconnected(evt);
+                        _this._onGamepadDisconnected(evt.gamepad);
                     };
                     window.addEventListener('gamepadconnected', this._onGamepadConnectedEvent, false);
                     window.addEventListener('gamepaddisconnected', this._onGamepadDisonnectedEvent, false);
@@ -47619,8 +47620,16 @@ var BABYLON;
                 this._onGamepadConnectedEvent = null;
                 this._onGamepadDisonnectedEvent = null;
             }
+            this.oneGamepadConnected = false;
+            this._stopMonitoringGamepads();
+            this.babylonGamepads = [];
         };
         Gamepads.prototype._onGamepadConnected = function (gamepad) {
+            // Protection code for Chrome which has a very buggy gamepad implementation...
+            // And raises a connected event on disconnection for instance
+            if (gamepad.index in this.babylonGamepads) {
+                return;
+            }
             var newGamepad = this._addNewGamepad(gamepad);
             if (this._callbackGamepadConnected)
                 this._callbackGamepadConnected(newGamepad);
@@ -47644,10 +47653,10 @@ var BABYLON;
             this.babylonGamepads.push(newGamepad);
             return newGamepad;
         };
-        Gamepads.prototype._onGamepadDisconnected = function (evt) {
+        Gamepads.prototype._onGamepadDisconnected = function (gamepad) {
             // Remove the gamepad from the list of gamepads to monitor.
             for (var i in this.babylonGamepads) {
-                if (this.babylonGamepads[i].index == evt.gamepad.index) {
+                if (this.babylonGamepads[i].index == gamepad.index) {
                     this.babylonGamepads.splice(+i, 1);
                     break;
                 }
@@ -47655,7 +47664,10 @@ var BABYLON;
             // If no gamepads are left, stop the polling loop.
             if (this.babylonGamepads.length == 0) {
                 this._stopMonitoringGamepads();
+                this.oneGamepadConnected = false;
             }
+            if (this._callbackGamepadDisconnected)
+                this._callbackGamepadDisconnected(gamepad);
         };
         Gamepads.prototype._startMonitoringGamepads = function () {
             if (!this.isMonitoring) {
@@ -47668,7 +47680,7 @@ var BABYLON;
         };
         Gamepads.prototype._checkGamepadsStatus = function () {
             var _this = this;
-            // updating gamepad objects
+            // Hack to be compatible Chrome
             this._updateGamepadObjects();
             for (var i in this.babylonGamepads) {
                 this.babylonGamepads[i].update();
@@ -47685,9 +47697,8 @@ var BABYLON;
                 }
             }
         };
-        // This function is called only on Chrome, which does not yet support
-        // connection/disconnection events, but requires you to monitor
-        // an array for changes.
+        // This function is called only on Chrome, which does not properly support
+        // connection/disconnection events and forces you to recopy again the gamepad object
         Gamepads.prototype._updateGamepadObjects = function () {
             var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
             for (var i = 0; i < gamepads.length; i++) {
@@ -47699,6 +47710,7 @@ var BABYLON;
                         }
                     }
                     else {
+                        // Forced to copy again this object for Chrome for unknown reason
                         this.babylonGamepads[i].browserGamepad = gamepads[i];
                     }
                 }
@@ -47785,13 +47797,10 @@ var BABYLON;
     BABYLON.Gamepad = Gamepad;
     var GenericPad = (function (_super) {
         __extends(GenericPad, _super);
-        function GenericPad(id, index, gamepad) {
-            var _this = _super.call(this, id, index, gamepad) || this;
-            _this.id = id;
-            _this.index = index;
-            _this.gamepad = gamepad;
+        function GenericPad(id, index, browserGamepad) {
+            var _this = _super.call(this, id, index, browserGamepad) || this;
             _this.type = Gamepad.GENERIC;
-            _this._buttons = new Array(gamepad.buttons.length);
+            _this._buttons = new Array(browserGamepad.buttons.length);
             return _this;
         }
         GenericPad.prototype.onbuttondown = function (callback) {
@@ -47814,7 +47823,7 @@ var BABYLON;
         GenericPad.prototype.update = function () {
             _super.prototype.update.call(this);
             for (var index = 0; index < this._buttons.length; index++) {
-                this._buttons[index] = this._setButtonValue(this.gamepad.buttons[index].value, this._buttons[index], index);
+                this._buttons[index] = this._setButtonValue(this.browserGamepad.buttons[index].value, this._buttons[index], index);
             }
         };
         return GenericPad;

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


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


+ 8 - 1
materialsLibrary/src/custom/babylon.customMaterial.ts

@@ -556,6 +556,8 @@ vColor=color;\n\
          public static ShaderIndexer = 1;
          public CustomParts :  ShaderSpecialParts;
          public ShaderVersion : CustomShaderStructure ;
+         _isCreatedShader : bool;
+         _createdShaderName : string;
          _customUniform : string[];
          _newUniforms : string[];
          _newUniformInstances : any[];
@@ -597,7 +599,9 @@ vColor=color;\n\
          }
          public Builder(shaderName: string, uniforms: string[], uniformBuffers: string[], samplers: string[], defines: StandardMaterialDefines) : string {
             
-           
+            if(this._isCreatedShader) return this._createdShaderName;
+              this._isCreatedShader  = false;
+            
             CustomMaterial.ShaderIndexer++;
             var name = name+"custom_"+CustomMaterial.ShaderIndexer;
 
@@ -626,6 +630,9 @@ vColor=color;\n\
             .replace('#[Fragment_Custom_Alpha]',(this.CustomParts.Fragment_Custom_Alpha ? this.CustomParts.Fragment_Custom_Alpha : ""))
             .replace('#[Fragment_Before_FragColor]',(this.CustomParts.Fragment_Before_FragColor ? this.CustomParts.Fragment_Before_FragColor : "")) ;
  
+             this._isCreatedShader  = true;
+              this._createdShaderName = name;
+           
              return name ;
          }
 

+ 8 - 1
proceduralTexturesLibrary/index.html

@@ -50,6 +50,7 @@
 	<script src="test/addMarblePT.js"></script>
 	<script src="test/addStarfieldPT.js"></script>
 	<script src="test/addNormalMapPT.js"></script>
+	<script src="test/addPerlinNoisePT.js"></script>
 	<script>
 		BABYLONDEVTOOLS.Loader.load(function() {
 		if (BABYLON.Engine.isSupported()) {
@@ -191,6 +192,9 @@
 				// Normal Map Procedural Texture
 				var normalMapPT = addNormalMapPT();
 				normalMapPT.baseTexture = diffuseTexture;
+
+				// Perlin Noise Procedural Texture
+				var perlinNoisePT = addPerlinNoisePT();
 								
 				// Default to std
 				var currentTexture = diffuseTexture;
@@ -307,7 +311,7 @@
 					}
 				}
 				
-				gui.add(options, 'texture', ['default', 'fire', 'wood', 'cloud', 'grass', 'road', 'brick', 'marble', 'starfield', 'normalMap']).onFinishChange(function () {
+				gui.add(options, 'texture', ['default', 'fire', 'wood', 'cloud', 'grass', 'road', 'brick', 'marble', 'starfield', 'normalMap', 'perlinNoise']).onFinishChange(function () {
 					resetPTOptions();
 					switch (options.texture) {
 						case "fire":
@@ -345,6 +349,9 @@
 						case "normalMap":
 							currentTexture = normalMapPT;
 							break;
+						case "perlinNoise":
+							currentTexture = perlinNoisePT;
+							break;
 						case "none":
 						default:
 							currentTexture = diffuseTexture;

+ 37 - 0
proceduralTexturesLibrary/src/perlinNoise/babylon.perlinNoiseProceduralTexture.ts

@@ -0,0 +1,37 @@
+/// <reference path="../../../dist/preview release/babylon.d.ts"/>
+
+module BABYLON {
+    export class PerlinNoiseProceduralTexture extends ProceduralTexture {
+        public time: number = 0.0;
+        public speed: number = 1.0;
+        public translationSpeed: number = 1.0;
+
+        private _currentTranslation: number = 0;
+
+        constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
+            super(name, size, "perlinNoiseProceduralTexture", scene, fallbackTexture, generateMipMaps);
+            this.updateShaderUniforms();
+        }
+
+        public updateShaderUniforms() {
+            this.setFloat("size", this.getRenderSize());
+
+            var deltaTime = this.getScene().getEngine().getDeltaTime();
+
+            this.time += deltaTime;
+            this.setFloat("time", this.time * this.speed / 1000);
+
+            this._currentTranslation += deltaTime * this.translationSpeed / 1000.0;
+            this.setFloat("translationSpeed", this._currentTranslation);
+        }
+
+        public render(useCameraPostProcess?: boolean) {
+            this.updateShaderUniforms();
+            super.render(useCameraPostProcess);
+        }
+
+        public resize(size: any, generateMipMaps: any): void {
+            super.resize(size, generateMipMaps);
+        }
+    }
+}

+ 97 - 0
proceduralTexturesLibrary/src/perlinNoise/perlinNoiseProceduralTexture.fragment.fx

@@ -0,0 +1,97 @@
+// Taken and improved from https://www.shadertoy.com/view/MdGSzt
+
+precision highp float;
+
+// Uniforms
+uniform float size;
+uniform float time;
+uniform float translationSpeed;
+
+// Varyings
+varying vec2 vUV;
+
+// Functions
+float r(float n)
+{
+ 	return fract(cos(n * 89.42) * 343.42);
+}
+
+vec2 r(vec2 n)
+{
+ 	return vec2(r(n.x * 23.62 - 300.0 + n.y * 34.35), r(n.x * 45.13 + 256.0 + n.y * 38.89)); 
+}
+
+float worley(vec2 n,float s)
+{
+    float dis = 1.0;
+    for(int x = -1; x <= 1; x++)
+    {
+        for(int y = -1; y <= 1; y++)
+        {
+            vec2 p = floor(n / s) + vec2(x, y);
+            float d = length(r(p) + vec2(x, y) - fract(n / s));
+
+            if (dis > d)
+             	dis = d;
+        }
+    }
+    return 1.0 - dis;
+}
+
+vec3 hash33(vec3 p3)
+{
+	p3 = fract(p3 * vec3(0.1031, 0.11369, 0.13787));
+    p3 += dot(p3, p3.yxz+19.19);
+    return -1.0 + 2.0 * fract(vec3((p3.x + p3.y) * p3.z, (p3.x + p3.z) * p3.y, (p3.y + p3.z) * p3.x));
+}
+
+float perlinNoise(vec3 p)
+{
+    vec3 pi = floor(p);
+    vec3 pf = p - pi;
+    
+    vec3 w = pf * pf * (3.0 - 2.0 * pf);
+    
+    return 	mix(
+        		mix(
+                	mix(
+                        dot(pf - vec3(0, 0, 0), hash33(pi + vec3(0, 0, 0))), 
+                        dot(pf - vec3(1, 0, 0), hash33(pi + vec3(1, 0, 0))),
+                       	w.x
+                    ),
+                	mix(
+                        dot(pf - vec3(0, 0, 1), hash33(pi + vec3(0, 0, 1))), 
+                        dot(pf - vec3(1, 0, 1), hash33(pi + vec3(1, 0, 1))),
+                       	w.x
+                    ),
+                    w.z
+                ),
+        		mix(
+                    mix(
+                        dot(pf - vec3(0, 1, 0), hash33(pi + vec3(0, 1, 0))), 
+                        dot(pf - vec3(1, 1, 0), hash33(pi + vec3(1, 1, 0))),
+                       	w.x
+                    ),
+                   	mix(
+                        dot(pf - vec3(0, 1, 1), hash33(pi + vec3(0, 1, 1))), 
+                        dot(pf - vec3(1, 1, 1), hash33(pi + vec3(1, 1, 1))),
+                       	w.x
+                    ),
+                    w.z
+                ),
+                w.y
+            );
+}
+
+// Main
+void main(void)
+{
+    vec2 uv = gl_FragCoord.xy + translationSpeed;
+
+    float dis = (
+        1.0 + perlinNoise(vec3(uv / vec2(size, size), time * 0.05) * 8.0))
+        * (1.0 + (worley(uv, 32.0)+ 0.5 * worley(2.0 * uv, 32.0) + 0.25 * worley(4.0 * uv, 32.0))
+    );
+
+	gl_FragColor = vec4(vec3(dis / 4.0), 1.0);
+}

+ 4 - 0
proceduralTexturesLibrary/test/addPerlinNoisePT.js

@@ -0,0 +1,4 @@
+window.addPerlinNoisePT = function() {
+    var pn = new BABYLON.PerlinNoiseProceduralTexture("perlinNoisePT", 512, scene);
+    return pn;
+};

+ 36 - 5
src/PostProcess/RenderPipeline/Pipelines/babylon.standardRenderingPipeline.ts

@@ -1,6 +1,4 @@
-/// <reference path="RenderPipeline\babylon.postProcessRenderPipeline.ts" />
-
-module BABYLON {
+module BABYLON {
     export class StandardRenderingPipeline extends PostProcessRenderPipeline implements IDisposable, IAnimatable {
         /**
         * Public members
@@ -95,11 +93,44 @@ module BABYLON {
         private _motionBlurSamples: number = 64;
 
         // Getters and setters
+        private _bloomEnabled: boolean = true;
         private _depthOfFieldEnabled: boolean = true;
         private _lensFlareEnabled: boolean = true;
         private _hdrEnabled: boolean = true;
         private _motionBlurEnabled: boolean = true;
 
+        public set BloomEnabled(enabled: boolean) {
+            if (enabled && !this._bloomEnabled) {
+                this._scene.postProcessRenderPipelineManager.enableEffectInPipeline(this._name, "HDRDownSampleX4", this._cameras);
+                this._scene.postProcessRenderPipelineManager.enableEffectInPipeline(this._name, "HDRBrightPass", this._cameras);
+
+                for (var i = 0; i < this.gaussianBlurHPostProcesses.length - 1; i++) {
+                    this._scene.postProcessRenderPipelineManager.enableEffectInPipeline(this._name, "HDRGaussianBlurH" + i, this._cameras);
+                    this._scene.postProcessRenderPipelineManager.enableEffectInPipeline(this._name, "HDRGaussianBlurV" + i, this._cameras);
+                }
+
+                this._scene.postProcessRenderPipelineManager.enableEffectInPipeline(this._name, "HDRTextureAdder", this._cameras);
+            }
+            else if (!enabled && this._bloomEnabled) {
+                this._scene.postProcessRenderPipelineManager.disableEffectInPipeline(this._name, "HDRDownSampleX4", this._cameras);
+                this._scene.postProcessRenderPipelineManager.disableEffectInPipeline(this._name, "HDRBrightPass", this._cameras);
+
+                for (var i = 0; i < this.gaussianBlurHPostProcesses.length - 1; i++) {
+                    this._scene.postProcessRenderPipelineManager.disableEffectInPipeline(this._name, "HDRGaussianBlurH" + i, this._cameras);
+                    this._scene.postProcessRenderPipelineManager.disableEffectInPipeline(this._name, "HDRGaussianBlurV" + i, this._cameras);
+                }
+
+                this._scene.postProcessRenderPipelineManager.disableEffectInPipeline(this._name, "HDRTextureAdder", this._cameras);
+            }
+
+            this._bloomEnabled = enabled;
+        }
+
+        @serialize()
+        public get BloomEnabled(): boolean {
+            return this._bloomEnabled;
+        }
+
         public set DepthOfFieldEnabled(enabled: boolean) {
             var blurIndex = this.gaussianBlurHPostProcesses.length - 1;
 
@@ -500,7 +531,7 @@ module BABYLON {
             var lastTime = 0;
 
             this.hdrPostProcess.onApply = (effect: Effect) => {
-                effect.setTextureFromPostProcess("textureAdderSampler", this._currentHDRSource);
+                effect.setTextureFromPostProcess("textureAdderSampler", this._bloomEnabled ? this._currentHDRSource : this.originalPostProcess);
 
                 time += scene.getEngine().getDeltaTime();
 
@@ -546,7 +577,7 @@ module BABYLON {
 
             // Lens flare
             this.lensFlarePostProcess.onApply = (effect: Effect) => {
-                effect.setTextureFromPostProcess("textureSampler", this.gaussianBlurHPostProcesses[0]);
+                effect.setTextureFromPostProcess("textureSampler", this._bloomEnabled ? this.gaussianBlurHPostProcesses[0] : this.originalPostProcess);
                 effect.setTexture("lensColorSampler", this.lensColorTexture);
                 effect.setFloat("strength", this.lensFlareStrength);
                 effect.setFloat("ghostDispersal", this.lensFlareGhostDispersal);

+ 28 - 14
src/Tools/babylon.gamepads.ts

@@ -9,12 +9,14 @@
             navigator.webkitGetGamepads || navigator.msGetGamepads || navigator.webkitGamepads);
 
         private _callbackGamepadConnected: (gamepad: T) => void;
+        private _callbackGamepadDisconnected: (gamepad: Gamepad) => void;
 
         private _onGamepadConnectedEvent: (evt) => void;
-        private _onGamepadDisonnectedEvent: (evt: Event) => void;
+        private _onGamepadDisonnectedEvent: (evt) => void;
 
-        constructor(ongamedpadconnected: (gamepad: T) => void) {
+        constructor(ongamedpadconnected: (gamepad: T) => void, ongamedpaddisconnected?: (gamepad: T) => void) {
             this._callbackGamepadConnected = ongamedpadconnected;
+            this._callbackGamepadDisconnected = ongamedpaddisconnected;
             if (this.gamepadSupport) {
                 //first add already-connected gamepads
                 this._updateGamepadObjects();
@@ -27,7 +29,7 @@
                         this._onGamepadConnected(evt.gamepad);
                     };
                     this._onGamepadDisonnectedEvent = (evt) => {
-                        this._onGamepadDisconnected(evt);
+                        this._onGamepadDisconnected(evt.gamepad);
                     };
                     window.addEventListener('gamepadconnected', this._onGamepadConnectedEvent, false);
                     window.addEventListener('gamepaddisconnected', this._onGamepadDisonnectedEvent, false);
@@ -45,9 +47,19 @@
                 this._onGamepadConnectedEvent = null;
                 this._onGamepadDisonnectedEvent = null;
             }
+            this.oneGamepadConnected = false;
+            this._stopMonitoringGamepads();
+            this.babylonGamepads = [];
+
         }
 
-        private _onGamepadConnected(gamepad) {
+        private _onGamepadConnected(gamepad: Gamepad) {
+            // Protection code for Chrome which has a very buggy gamepad implementation...
+            // And raises a connected event on disconnection for instance
+            if(gamepad.index in this.babylonGamepads) {
+                return;
+            }
+
             var newGamepad = this._addNewGamepad(gamepad);
             if (this._callbackGamepadConnected) this._callbackGamepadConnected(newGamepad);
             this._startMonitoringGamepads();
@@ -75,10 +87,10 @@
             return newGamepad;
         }
 
-        private _onGamepadDisconnected(evt) {
+        private _onGamepadDisconnected(gamepad: Gamepad) {
             // Remove the gamepad from the list of gamepads to monitor.
             for (var i in this.babylonGamepads) {
-                if (this.babylonGamepads[i].index == evt.gamepad.index) {
+                if (this.babylonGamepads[i].index == gamepad.index) {
                     this.babylonGamepads.splice(+i, 1);
                     break;
                 }
@@ -87,7 +99,9 @@
             // If no gamepads are left, stop the polling loop.
             if (this.babylonGamepads.length == 0) {
                 this._stopMonitoringGamepads();
+                this.oneGamepadConnected = false;
             }
+            if (this._callbackGamepadDisconnected) this._callbackGamepadDisconnected(gamepad);
         }
 
         private _startMonitoringGamepads() {
@@ -102,7 +116,7 @@
         }
 
         private _checkGamepadsStatus() {
-            // updating gamepad objects
+            // Hack to be compatible Chrome
             this._updateGamepadObjects();
 
             for (var i in this.babylonGamepads) {
@@ -120,9 +134,8 @@
             }
         }
 
-        // This function is called only on Chrome, which does not yet support
-        // connection/disconnection events, but requires you to monitor
-        // an array for changes.
+        // This function is called only on Chrome, which does not properly support
+        // connection/disconnection events and forces you to recopy again the gamepad object
         private _updateGamepadObjects() {
             var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
             for (var i = 0; i < gamepads.length; i++) {
@@ -134,6 +147,7 @@
                         }
                     }
                     else {
+                        // Forced to copy again this object for Chrome for unknown reason
                         this.babylonGamepads[i].browserGamepad = gamepads[i];
                     }
                 }
@@ -227,10 +241,10 @@
             this._onbuttonup = callback;
         }
 
-        constructor(public id: string, public index: number, public gamepad) {
-            super(id, index, gamepad);
+        constructor(id: string, index: number, browserGamepad) {
+            super(id, index, browserGamepad);
             this.type = Gamepad.GENERIC;
-            this._buttons = new Array(gamepad.buttons.length);
+            this._buttons = new Array(browserGamepad.buttons.length);
         }
 
         private _setButtonValue(newValue: number, currentValue: number, buttonIndex: number): number {
@@ -248,7 +262,7 @@
         public update() {
             super.update();
             for (var index = 0; index < this._buttons.length; index++) {
-                this._buttons[index] = this._setButtonValue(this.gamepad.buttons[index].value, this._buttons[index], index);
+                this._buttons[index] = this._setButtonValue(this.browserGamepad.buttons[index].value, this._buttons[index], index);
             }
         }
     }